Skip to content

Commit

Permalink
[chore] Fix "add codeowners to PR" workflow (#16249)
Browse files Browse the repository at this point in the history
Fix "add codeowners to PR" workflow

Co-authored-by: Evan Bradley <[email protected]>
  • Loading branch information
evan-bradley and evan-bradley authored Nov 11, 2022
1 parent 9518832 commit dcd3a94
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions .github/workflows/scripts/add-codeowners-to-pr.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fi

main () {
CUR_DIRECTORY=$(dirname "$0")
JSON=$(gh pr view "${PR}" --repo evan-bradley/opentelemetry-collector-contrib --json "files,author")
JSON=$(gh pr view "${PR}" --json "files,author")
AUTHOR=$(printf "${JSON}"| jq '.author.login')
FILES=$(printf "${JSON}"| jq -r '.files[].path')
COMPONENTS=$(grep -oE '^[a-z]+/[a-z/]+ ' < .github/CODEOWNERS)
Expand Down Expand Up @@ -81,7 +81,11 @@ main () {
done
done

gh pr edit "${PR}" --add-label "${LABELS}" || echo "Failed to add labels to #${PR}"
if [[ -n "${LABELS}" ]]; then
gh pr edit "${PR}" --add-label "${LABELS}" || echo "Failed to add labels to #${PR}"
else
echo "No labels found"
fi

# Note that adding the labels above will not trigger any other workflows to
# add code owners, so we have to do it here.
Expand All @@ -93,15 +97,19 @@ main () {
#
# The GitHub API validates that authors are not requested to review, but
# accepts duplicate logins and logins that are already reviewers.
curl \
--fail \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
"https://api.github.com/repos/${REPO}/pulls/${PR}/requested_reviewers" \
-d "{\"reviewers\":[${REVIEWERS}]}" \
| jq ".message" \
|| echo "Failed to add reviewers to #${PR}"
if [[ -n "${REVIEWERS}" ]]; then
curl \
--fail \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
"https://api.github.com/repos/${REPO}/pulls/${PR}/requested_reviewers" \
-d "{\"reviewers\":[${REVIEWERS}]}" \
| jq ".message" \
|| echo "Failed to add reviewers to #${PR}"
else
echo "No code owners found"
fi
}

main || echo "Failed to run $0"

0 comments on commit dcd3a94

Please sign in to comment.