From 62779fbfa4499ebe9545047980933022e8a11bb0 Mon Sep 17 00:00:00 2001 From: Wolfgang Walther Date: Sat, 4 Jan 2025 14:05:18 +0100 Subject: [PATCH] ci/request-reviews: share code to request reviewers from gh api This makes it easier to add ofborg's request-1-by-1 logic, where failed requests are OK for edge cases. --- .github/workflows/eval.yml | 9 +----- ci/request-reviews/default.nix | 2 +- .../request-code-owner-reviews.sh | 18 ++--------- ...cess-reviewers.sh => request-reviewers.sh} | 31 +++++++++++++++++-- 4 files changed, 32 insertions(+), 28 deletions(-) rename ci/request-reviews/{process-reviewers.sh => request-reviewers.sh} (69%) diff --git a/.github/workflows/eval.yml b/.github/workflows/eval.yml index 3c24ec77ae2fb..72b2103b87207 100644 --- a/.github/workflows/eval.yml +++ b/.github/workflows/eval.yml @@ -294,14 +294,7 @@ jobs: # There appears to be no API to request reviews based on GitHub IDs jq -r 'keys[]' comparison/maintainers.json \ | while read -r id; do gh api /user/"$id" --jq .login; done \ - | GH_TOKEN=${{ steps.app-token.outputs.token }} result/bin/process-reviewers.sh "$REPOSITORY" "$NUMBER" "$AUTHOR" \ - > reviewers.json - - # Request reviewers from maintainers of changed output paths - GH_TOKEN=${{ steps.app-token.outputs.token }} gh api \ - --method POST \ - /repos/"$REPOSITORY"/pulls/"$NUMBER"/requested_reviewers \ - --input reviewers.json + | GH_TOKEN=${{ steps.app-token.outputs.token }} result/bin/request-reviewers.sh "$REPOSITORY" "$NUMBER" "$AUTHOR" env: GH_TOKEN: ${{ github.token }} diff --git a/ci/request-reviews/default.nix b/ci/request-reviews/default.nix index e288fef4f9f61..b180d60be97c3 100644 --- a/ci/request-reviews/default.nix +++ b/ci/request-reviews/default.nix @@ -15,7 +15,7 @@ stdenvNoCC.mkDerivation { root = ./.; fileset = lib.fileset.unions [ ./get-code-owners.sh - ./process-reviewers.sh + ./request-reviewers.sh ./request-code-owner-reviews.sh ./verify-base-branch.sh ./dev-branches.txt diff --git a/ci/request-reviews/request-code-owner-reviews.sh b/ci/request-reviews/request-code-owner-reviews.sh index 27725bd50773f..fefc8c3be3fa2 100755 --- a/ci/request-reviews/request-code-owner-reviews.sh +++ b/ci/request-reviews/request-code-owner-reviews.sh @@ -77,20 +77,6 @@ if ! "$SCRIPT_DIR"/verify-base-branch.sh "$tmp/nixpkgs.git" "$headRef" "$baseRep exit 1 fi -log "Getting code owners to request reviews from" +log "Requesting reviews from code owners" "$SCRIPT_DIR"/get-code-owners.sh "$tmp/nixpkgs.git" "$ownersFile" "$baseBranch" "$headRef" | \ - "$SCRIPT_DIR"/process-reviewers.sh "$baseRepo" "$prNumber" "$prAuthor" > "$tmp/reviewers.json" - -log "Requesting reviews from: $(<"$tmp/reviewers.json")" - -if ! response=$(effect gh api \ - --method POST \ - -H "Accept: application/vnd.github+json" \ - -H "X-GitHub-Api-Version: 2022-11-28" \ - "/repos/$baseRepo/pulls/$prNumber/requested_reviewers" \ - --input "$tmp/reviewers.json"); then - log "Failed to request reviews: $response" - exit 1 -fi - -log "Successfully requested reviews" + "$SCRIPT_DIR"/request-reviewers.sh "$baseRepo" "$prNumber" "$prAuthor" diff --git a/ci/request-reviews/process-reviewers.sh b/ci/request-reviews/request-reviewers.sh similarity index 69% rename from ci/request-reviews/process-reviewers.sh rename to ci/request-reviews/request-reviewers.sh index 4ca5b8ace7aaa..1f6311e89c849 100755 --- a/ci/request-reviews/process-reviewers.sh +++ b/ci/request-reviews/request-reviewers.sh @@ -1,15 +1,26 @@ #!/usr/bin/env bash -# Process reviewers for a PR, reading line-separated usernames on stdin, -# returning a JSON suitable to be consumed by the API endpoint to request reviews: +# Request reviewers for a PR, reading line-separated usernames on stdin, +# filtering for valid reviewers before using the API endpoint to request reviews: # https://docs.github.com/en/rest/pulls/review-requests?apiVersion=2022-11-28#request-reviewers-for-a-pull-request set -euo pipefail +tmp=$(mktemp -d) +trap 'rm -rf "$tmp"' exit + log() { echo "$@" >&2 } +effect() { + if [[ -n "${DRY_MODE:-}" ]]; then + log "Skipping in dry mode:" "${@@Q}" + else + "$@" + fi +} + if (( "$#" < 3 )); then log "Usage: $0 BASE_REPO PR_NUMBER PR_AUTHOR" exit 1 @@ -62,4 +73,18 @@ jq -n \ --arg users "${!users[*]}" \ '{ reviewers: $users | split(" "), - }' + }' > "$tmp/reviewers.json" + +log "Requesting reviews from: $(<"$reviewersFile")" + +if ! response=$(effect gh api \ + --method POST \ + -H "Accept: application/vnd.github+json" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + "/repos/$baseRepo/pulls/$prNumber/requested_reviewers" \ + --input "$tmp/reviewers.json"); then + log "Failed to request reviews: $response" + exit 1 +fi + +log "Successfully requested reviews"