Skip to content

Commit

Permalink
Assign reviewers through API in ping code owners workflow
Browse files Browse the repository at this point in the history
Also loop through owners list in the "add code owners to PR"
workflow, as it is more comprehensible than the pipe.
  • Loading branch information
evan-bradley committed Nov 11, 2022
1 parent cdaed74 commit e570fd3
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 15 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/ping-codeowners-prs.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: 'Ping code owners on PRs'
on:
pull_request:
pull_request_target:
types: [labeled]

jobs:
Expand All @@ -13,7 +13,8 @@ jobs:
- name: Run ping-codeowners-prs.sh
run: ./.github/workflows/scripts/ping-codeowners-prs.sh
env:
REPO: ${{ github.repository }}
AUTHOR: ${{ github.event.pull_request.user.login }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR: ${{ github.event.number }}
COMPONENT: ${{ github.event.label.name }}
SENDER: ${{ github.event.sender.login }}
12 changes: 8 additions & 4 deletions .github/workflows/scripts/add-codeowners-to-pr.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fi
main () {
CUR_DIRECTORY=$(dirname "$0")
JSON=$(gh pr view "${PR}" --json "files,author")
AUTHOR=$(printf "${JSON}"| jq '.author.login')
AUTHOR=$(printf "${JSON}"| jq -r '.author.login')
FILES=$(printf "${JSON}"| jq -r '.files[].path')
COMPONENTS=$(grep -oE '^[a-z]+/[a-z/]+ ' < .github/CODEOWNERS)
REVIEWERS=""
Expand Down Expand Up @@ -62,12 +62,16 @@ main () {

OWNERS=$(COMPONENT="${COMPONENT}" bash "${CUR_DIRECTORY}/get-codeowners.sh")

if [[ -n "${OWNERS}" ]]; then
for OWNER in ${OWNERS}; do
if [[ "${OWNER}" = "@${AUTHOR}" ]]; then
continue
fi

if [[ -n "${REVIEWERS}" ]]; then
REVIEWERS+=","
fi
REVIEWERS+="$(echo "${OWNERS}" | sed -E 's/@([A-Za-z0-9_-]+)( |$)/"\1"\2/g' | sed -E "s/${AUTHOR} ?//g" | sed 's/ /,/g')"
fi
REVIEWERS+=$(echo "${OWNER}" | sed -E 's/@(.+)/"\1"/')
done

# Convert the CODEOWNERS entry to a label
COMPONENT_CLEAN=$(echo "${COMPONENT}" | sed -E 's%/$%%')
Expand Down
46 changes: 37 additions & 9 deletions .github/workflows/scripts/ping-codeowners-prs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,51 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
#

set -euo pipefail

if [[ -z "${COMPONENT:-}" || -z "${PR:-}" ]]; then
echo "At least one of COMPONENT or PR has not been set, please ensure each is set."
if [[ -z "${REPO:-}" || -z "${AUTHOR:-}" || -z "${COMPONENT:-}" || -z "${PR:-}" ]]; then
echo "At least one of REPO, AUTHOR, COMPONENT, or PR has not been set, please ensure each is set."
exit 0
fi

CUR_DIRECTORY=$(dirname "$0")

OWNERS=$(COMPONENT="${COMPONENT}" bash "${CUR_DIRECTORY}/get-codeowners.sh")
main() {
OWNERS=$(COMPONENT="${COMPONENT}" bash "${CUR_DIRECTORY}/get-codeowners.sh")
REVIEWERS=""

if [[ -z "${OWNERS}" ]]; then
exit 0
fi
if [[ -z "${OWNERS}" ]]; then
exit 0
fi

for OWNER in ${OWNERS}; do
if [[ "${OWNER}" = "@${AUTHOR}" ]]; then
continue
fi

if [[ -n "${REVIEWERS}" ]]; then
REVIEWERS+=","
fi
REVIEWERS+=$(echo "${OWNER}" | sed -E 's/@(.+)/"\1"/')
done

REVIEWERS=$(echo "${OWNERS}" | sed 's/@//g' | sed 's/ /,/g')
# We have to use the GitHub API directly due to an issue with how the CLI
# handles PR updates that causes it require access to organization teams,
# and the GitHub token doesn't provide that permission.
# For more: https://github.com/cli/cli/issues/4844
#
# 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 "Request failed to request review from code owners on #${PR}"
}

gh pr edit "${PR}" --add-reviewer "${REVIEWERS}"
main || echo "Failed to request review from code owners on PR #${PR}"

0 comments on commit e570fd3

Please sign in to comment.