From 18ab6b721e288ca9b6f9c0000ea62bebd070d5a1 Mon Sep 17 00:00:00 2001 From: Wolfgang Walther Date: Sat, 1 Nov 2025 12:02:44 +0100 Subject: [PATCH] ci/request-reviews: move gh api calls out of get-code-owners All the github related logic is now bundled in `request-reviewers.sh`. This allows moving the `get-code-owners.sh` file into the eval/compare step in the next commit. --- .github/workflows/reviewers.yml | 14 -------- ci/request-reviews/get-code-owners.sh | 43 ++----------------------- ci/request-reviews/request-reviewers.sh | 34 ++++++++++++++++++- 3 files changed, 35 insertions(+), 56 deletions(-) diff --git a/.github/workflows/reviewers.yml b/.github/workflows/reviewers.yml index 4b49972e0732..a6822aa9af0e 100644 --- a/.github/workflows/reviewers.yml +++ b/.github/workflows/reviewers.yml @@ -63,27 +63,13 @@ jobs: permission-members: read permission-pull-requests: write - - name: Log current API rate limits (app-token) - if: ${{ steps.app-token.outputs.token }} - env: - GH_TOKEN: ${{ steps.app-token.outputs.token }} - run: gh api /rate_limit | jq - - name: Determining code owner reviews - if: steps.app-token.outputs.token env: - GH_TOKEN: ${{ steps.app-token.outputs.token }} REPOSITORY: ${{ github.repository }} NUMBER: ${{ github.event.number }} run: | result/bin/request-code-owner-reviews.sh "$REPOSITORY" "$NUMBER" ci/OWNERS > owners.txt - - name: Log current API rate limits (app-token) - if: ${{ steps.app-token.outputs.token }} - env: - GH_TOKEN: ${{ steps.app-token.outputs.token }} - run: gh api /rate_limit | jq - - name: Log current API rate limits (github.token) env: GH_TOKEN: ${{ github.token }} diff --git a/ci/request-reviews/get-code-owners.sh b/ci/request-reviews/get-code-owners.sh index 5d121f69a74c..33963bce7f30 100755 --- a/ci/request-reviews/get-code-owners.sh +++ b/ci/request-reviews/get-code-owners.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash -# Get the code owners of the files changed by a PR, returning one username per line +# Get the code owners of the files changed by a PR, returning one username or team per line set -euo pipefail @@ -16,16 +16,9 @@ fi touchedFilesFile=$1 ownersFile=$2 -tmp=$(mktemp -d) -trap 'rm -rf "$tmp"' exit - readarray -t touchedFiles < "$touchedFilesFile" log "This PR touches ${#touchedFiles[@]} files" -# Associative array with the user as the key for easy de-duplication -# Make sure to always lowercase keys to avoid duplicates with different casings -declare -A users=() - for file in "${touchedFiles[@]}"; do result=$(codeowners --file "$ownersFile" "$file") @@ -52,39 +45,7 @@ for file in "${touchedFiles[@]}"; do # The first regex match is everything after the @ entry=${BASH_REMATCH[1]} - if [[ "$entry" =~ (.*)/(.*) ]]; then - # Teams look like $org/$team - org=${BASH_REMATCH[1]} - team=${BASH_REMATCH[2]} - - # Instead of requesting a review from the team itself, - # we request reviews from the individual users. - # This is because once somebody from a team reviewed the PR, - # the API doesn't expose that the team was already requested for a review, - # so we wouldn't be able to avoid rerequesting reviews - # without saving some some extra state somewhere - - # We could also consider implementing a more advanced heuristic - # in the future that e.g. only pings one team member, - # but escalates to somebody else if that member doesn't respond in time. - gh api \ - --cache=1h \ - -H "Accept: application/vnd.github+json" \ - -H "X-GitHub-Api-Version: 2022-11-28" \ - "/orgs/$org/teams/$team/members" \ - --jq '.[].login' > "$tmp/team-members" - readarray -t members < "$tmp/team-members" - log "Team $entry has these members: ${members[*]}" - - for user in "${members[@]}"; do - users[${user,,}]= - done - else - # Everything else is a user - users[${entry,,}]= - fi + echo "$entry" done done - -printf "%s\n" "${!users[@]}" diff --git a/ci/request-reviews/request-reviewers.sh b/ci/request-reviews/request-reviewers.sh index 6782b621b70b..b4288669db93 100755 --- a/ci/request-reviews/request-reviewers.sh +++ b/ci/request-reviews/request-reviewers.sh @@ -33,9 +33,41 @@ prAuthor=$3 tmp=$(mktemp -d) trap 'rm -rf "$tmp"' exit +# Associative array with the user as the key for easy de-duplication +# Make sure to always lowercase keys to avoid duplicates with different casings declare -A users=() while read -r handle && [[ -n "$handle" ]]; do - users[${handle,,}]= + if [[ "$handle" =~ (.*)/(.*) ]]; then + # Teams look like $org/$team + org=${BASH_REMATCH[1]} + team=${BASH_REMATCH[2]} + + # Instead of requesting a review from the team itself, + # we request reviews from the individual users. + # This is because once somebody from a team reviewed the PR, + # the API doesn't expose that the team was already requested for a review, + # so we wouldn't be able to avoid rerequesting reviews + # without saving some some extra state somewhere + + # We could also consider implementing a more advanced heuristic + # in the future that e.g. only pings one team member, + # but escalates to somebody else if that member doesn't respond in time. + gh api \ + --cache=1h \ + -H "Accept: application/vnd.github+json" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + "/orgs/$org/teams/$team/members" \ + --jq '.[].login' > "$tmp/team-members" + readarray -t members < "$tmp/team-members" + log "Team $entry has these members: ${members[*]}" + + for user in "${members[@]}"; do + users[${user,,}]= + done + else + # Everything else is a user + users[${handle,,}]= + fi done # Cannot request a review from the author