Skip to content

Commit

Permalink
Update validate.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
dpgiakatos authored Oct 11, 2024
1 parent 43eeeb6 commit 950eadd
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ jobs:
pr_number="${{ github.event.pull_request.number }}"
pr_url="${{ github.event.pull_request.html_url }}"
repo_full_name="${{ github.repository }}"
org_full_name="${{ github.organization }}"
message="Hello @${pr_author}, your PR is being processed. Here are the results:\n\n"
exit_code=0
echo "Starting PR validation for PR #${pr_number} by ${pr_author}"
# Function to make GitHub API calls
github_api_call() {
github_api_call_repos() {
local method=$1
local endpoint=$2
local data=$3
Expand All @@ -48,17 +49,28 @@ jobs:
echo "$response"
}
github_api_call_orgs() {
local method=$1
local endpoint=$2
local data=$3
local url="https://api.github.com/orgs/${org_full_name}/${endpoint}"
response=$(curl -s -X ${method} -H "Authorization: token ${GITHUB_TOKEN}" -H "Accept: application/vnd.github.v3+json" ${data:+-d "$data"} "$url")
echo "API call to ${endpoint}: $response" >&2
echo "$response"
}
# Check if PR author belongs to a specific team
org="InternetHealthReport"
team_slug="iij" # Replace with your actual team slug
team_membership=$(github_api_call GET "/orgs/{org}/teams/{team_slug}/memberships/{pr_author}")
team_membership=$(github_api_call_orgs GET "teams/{team_slug}/memberships/{pr_author}")
is_team_member=$(echo $team_membership | jq -r '.role')
echo "Author permissions: $is_team_member"
if [[ "$is_team_member" == "admin" || "$is_team_member" == "maintain" ]]; then
if [[ "$is_team_member" == "maintainer" ]]; then
echo "Author is a team member, bypassing checks"
message+="As a team member, your PR is automatically approved and will bypass further checks.\n"
github_api_call POST "issues/${pr_number}/labels" '{"labels":["PR:team-member-bypass"]}'
github_api_call_repos POST "issues/${pr_number}/labels" '{"labels":["PR:team-member-bypass"]}'
else
# echo "Proceeding with checks regardless of maintainer status for testing purposes"
# Check PR template
Expand All @@ -68,7 +80,7 @@ jobs:
reason="PR template is not respected. Please update your PR description to include the required sections."
message+="\n- $reason\n\nHere's a minimal example of a valid PR body:\n\n"
message+='```markdown\n### Description\n\n[Provide a brief description of your changes]\n\n### Related issue\n\nFixes #123\n```\n'
github_api_call POST "issues/${pr_number}/labels" '{"labels":["PR:fix-template"]}'
github_api_call_repos POST "issues/${pr_number}/labels" '{"labels":["PR:fix-template"]}'
else
echo "PR template respected, checking linked issues"
# Extract linked issues
Expand All @@ -84,20 +96,20 @@ jobs:
echo "No linked issue found. Exit code: $exit_code"
reason="No linked issue found. Please link an issue in your PR description (e.g., 'Fixes #123')."
message+="\n- $reason"
github_api_call POST "issues/${pr_number}/labels" '{"labels":["PR:no-linked-issue"]}'
github_api_call_repos POST "issues/${pr_number}/labels" '{"labels":["PR:no-linked-issue"]}'
else
echo "Linked issues found: $issue_numbers"
# Check if PR author is assigned to the linked issue(s)
if [[ "$issue_numbers" != "empty" ]]; then
for issue_number in $issue_numbers; do
issue_data=$(github_api_call GET "issues/${issue_number}")
issue_data=$(github_api_call_repos GET "issues/${issue_number}")
issue_assignees=$(echo "$issue_data" | jq -r '.assignees[].login')
if ! echo "$issue_assignees" | grep -q "$pr_author"; then
exit_code=4
echo "Author not assigned to issue #${issue_number}. Exit code: $exit_code"
reason="PR author is not assigned to the linked issue #$issue_number. Please assign yourself to the issue before submitting a PR."
message+="\n- $reason"
github_api_call POST "issues/${pr_number}/labels" '{"labels":["PR:author-not-assigned"]}'
github_api_call_repos POST "issues/${pr_number}/labels" '{"labels":["PR:author-not-assigned"]}'
else
echo "Author is assigned to issue #${issue_number}"
fi
Expand All @@ -111,16 +123,16 @@ jobs:
if [ "$exit_code" = 0 ]; then
echo "PR is valid"
message+="\n\nYour PR is valid and ready for review. Thank you for your contribution!"
github_api_call POST "issues/${pr_number}/labels" '{"labels":["PR:ready-for-review"]}'
github_api_call_repos POST "issues/${pr_number}/labels" '{"labels":["PR:ready-for-review"]}'
else
echo "PR is invalid. Closing PR."
message+="\n\nPlease address the above issues and update your PR. Once you've made the necessary changes, the PR will be automatically re-evaluated."
github_api_call PATCH "pulls/${pr_number}" '{"state":"closed"}'
github_api_call_repos PATCH "pulls/${pr_number}" '{"state":"closed"}'
fi
# Add comment to PR
echo "Adding comment to PR"
github_api_call POST "issues/${pr_number}/comments" "{\"body\":\"$message\"}"
github_api_call_repos POST "issues/${pr_number}/comments" "{\"body\":\"$message\"}"
echo "PR validation completed. Exit code: $exit_code"
exit $exit_code

0 comments on commit 950eadd

Please sign in to comment.