Commit 60773fe3 authored by Silvan Mosberger's avatar Silvan Mosberger
Browse files

ci/request-reviews/get-code-owners.sh: First-class code owner team review requests

parent e6c822f5
Loading
Loading
Loading
Loading
+5 −35
Original line number Diff line number Diff line
#!/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 GitHub user/team handle per line

set -euo pipefail

@@ -29,9 +29,9 @@ log "This PR touches ${#touchedFiles[@]} files"
# remove code owners to avoid pinging them
git -C "$gitRepo" show "$baseRef":"$ownersFile" > "$tmp"/codeowners

# Associative array with the user as the key for easy de-duplication
# Associative array with the user/team as the key for easy de-duplication
# Make sure to always lowercase keys to avoid duplicates with different casings
declare -A users=()
declare -A finalOwners=()

for file in "${touchedFiles[@]}"; do
    result=$(codeowners --file "$tmp"/codeowners "$file")
@@ -59,39 +59,9 @@ 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
        finalOwners[${entry,,}]=
    done

done

printf "%s\n" "${!users[@]}"
printf "%s\n" "${!finalOwners[@]}"