Skip to content

Commit 18ab6b7

Browse files
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.
1 parent d177d60 commit 18ab6b7

3 files changed

Lines changed: 35 additions & 56 deletions

File tree

.github/workflows/reviewers.yml

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,27 +63,13 @@ jobs:
6363
permission-members: read
6464
permission-pull-requests: write
6565

66-
- name: Log current API rate limits (app-token)
67-
if: ${{ steps.app-token.outputs.token }}
68-
env:
69-
GH_TOKEN: ${{ steps.app-token.outputs.token }}
70-
run: gh api /rate_limit | jq
71-
7266
- name: Determining code owner reviews
73-
if: steps.app-token.outputs.token
7467
env:
75-
GH_TOKEN: ${{ steps.app-token.outputs.token }}
7668
REPOSITORY: ${{ github.repository }}
7769
NUMBER: ${{ github.event.number }}
7870
run: |
7971
result/bin/request-code-owner-reviews.sh "$REPOSITORY" "$NUMBER" ci/OWNERS > owners.txt
8072
81-
- name: Log current API rate limits (app-token)
82-
if: ${{ steps.app-token.outputs.token }}
83-
env:
84-
GH_TOKEN: ${{ steps.app-token.outputs.token }}
85-
run: gh api /rate_limit | jq
86-
8773
- name: Log current API rate limits (github.token)
8874
env:
8975
GH_TOKEN: ${{ github.token }}
Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env bash
22

3-
# Get the code owners of the files changed by a PR, returning one username per line
3+
# Get the code owners of the files changed by a PR, returning one username or team per line
44

55
set -euo pipefail
66

@@ -16,16 +16,9 @@ fi
1616
touchedFilesFile=$1
1717
ownersFile=$2
1818

19-
tmp=$(mktemp -d)
20-
trap 'rm -rf "$tmp"' exit
21-
2219
readarray -t touchedFiles < "$touchedFilesFile"
2320
log "This PR touches ${#touchedFiles[@]} files"
2421

25-
# Associative array with the user as the key for easy de-duplication
26-
# Make sure to always lowercase keys to avoid duplicates with different casings
27-
declare -A users=()
28-
2922
for file in "${touchedFiles[@]}"; do
3023
result=$(codeowners --file "$ownersFile" "$file")
3124

@@ -52,39 +45,7 @@ for file in "${touchedFiles[@]}"; do
5245
# The first regex match is everything after the @
5346
entry=${BASH_REMATCH[1]}
5447

55-
if [[ "$entry" =~ (.*)/(.*) ]]; then
56-
# Teams look like $org/$team
57-
org=${BASH_REMATCH[1]}
58-
team=${BASH_REMATCH[2]}
59-
60-
# Instead of requesting a review from the team itself,
61-
# we request reviews from the individual users.
62-
# This is because once somebody from a team reviewed the PR,
63-
# the API doesn't expose that the team was already requested for a review,
64-
# so we wouldn't be able to avoid rerequesting reviews
65-
# without saving some some extra state somewhere
66-
67-
# We could also consider implementing a more advanced heuristic
68-
# in the future that e.g. only pings one team member,
69-
# but escalates to somebody else if that member doesn't respond in time.
70-
gh api \
71-
--cache=1h \
72-
-H "Accept: application/vnd.github+json" \
73-
-H "X-GitHub-Api-Version: 2022-11-28" \
74-
"/orgs/$org/teams/$team/members" \
75-
--jq '.[].login' > "$tmp/team-members"
76-
readarray -t members < "$tmp/team-members"
77-
log "Team $entry has these members: ${members[*]}"
78-
79-
for user in "${members[@]}"; do
80-
users[${user,,}]=
81-
done
82-
else
83-
# Everything else is a user
84-
users[${entry,,}]=
85-
fi
48+
echo "$entry"
8649
done
8750

8851
done
89-
90-
printf "%s\n" "${!users[@]}"

ci/request-reviews/request-reviewers.sh

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,41 @@ prAuthor=$3
3333
tmp=$(mktemp -d)
3434
trap 'rm -rf "$tmp"' exit
3535

36+
# Associative array with the user as the key for easy de-duplication
37+
# Make sure to always lowercase keys to avoid duplicates with different casings
3638
declare -A users=()
3739
while read -r handle && [[ -n "$handle" ]]; do
38-
users[${handle,,}]=
40+
if [[ "$handle" =~ (.*)/(.*) ]]; then
41+
# Teams look like $org/$team
42+
org=${BASH_REMATCH[1]}
43+
team=${BASH_REMATCH[2]}
44+
45+
# Instead of requesting a review from the team itself,
46+
# we request reviews from the individual users.
47+
# This is because once somebody from a team reviewed the PR,
48+
# the API doesn't expose that the team was already requested for a review,
49+
# so we wouldn't be able to avoid rerequesting reviews
50+
# without saving some some extra state somewhere
51+
52+
# We could also consider implementing a more advanced heuristic
53+
# in the future that e.g. only pings one team member,
54+
# but escalates to somebody else if that member doesn't respond in time.
55+
gh api \
56+
--cache=1h \
57+
-H "Accept: application/vnd.github+json" \
58+
-H "X-GitHub-Api-Version: 2022-11-28" \
59+
"/orgs/$org/teams/$team/members" \
60+
--jq '.[].login' > "$tmp/team-members"
61+
readarray -t members < "$tmp/team-members"
62+
log "Team $entry has these members: ${members[*]}"
63+
64+
for user in "${members[@]}"; do
65+
users[${user,,}]=
66+
done
67+
else
68+
# Everything else is a user
69+
users[${handle,,}]=
70+
fi
3971
done
4072

4173
# Cannot request a review from the author

0 commit comments

Comments
 (0)