vuls: init at 0.27.0
[NixPkgs.git] / ci / request-reviews / request-reviews.sh
blobb21354560242a5db09e21339ba94abbd16f8b285
1 #!/usr/bin/env bash
3 # Requests reviews for a PR after verifying that the base branch is correct
5 set -euo pipefail
6 tmp=$(mktemp -d)
7 trap 'rm -rf "$tmp"' exit
8 SCRIPT_DIR=$(dirname "$0")
10 log() {
11 echo "$@" >&2
14 effect() {
15 if [[ -n "${DRY_MODE:-}" ]]; then
16 log "Skipping in dry mode:" "${@@Q}"
17 else
18 "$@"
22 if (( $# < 3 )); then
23 log "Usage: $0 GITHUB_REPO PR_NUMBER OWNERS_FILE"
24 exit 1
26 baseRepo=$1
27 prNumber=$2
28 ownersFile=$3
30 log "Fetching PR info"
31 prInfo=$(gh api \
32 -H "Accept: application/vnd.github+json" \
33 -H "X-GitHub-Api-Version: 2022-11-28" \
34 "/repos/$baseRepo/pulls/$prNumber")
36 baseBranch=$(jq -r .base.ref <<< "$prInfo")
37 log "Base branch: $baseBranch"
38 prRepo=$(jq -r .head.repo.full_name <<< "$prInfo")
39 log "PR repo: $prRepo"
40 prBranch=$(jq -r .head.ref <<< "$prInfo")
41 log "PR branch: $prBranch"
42 prAuthor=$(jq -r .user.login <<< "$prInfo")
43 log "PR author: $prAuthor"
45 extraArgs=()
46 if pwdRepo=$(git rev-parse --show-toplevel 2>/dev/null); then
47 # Speedup for local runs
48 extraArgs+=(--reference-if-able "$pwdRepo")
51 log "Fetching Nixpkgs commit history"
52 # We only need the commit history, not the contents, so we can do a tree-less clone using tree:0
53 # https://github.blog/open-source/git/get-up-to-speed-with-partial-clone-and-shallow-clone/#user-content-quick-summary
54 git clone --bare --filter=tree:0 --no-tags --origin upstream "${extraArgs[@]}" https://github.com/"$baseRepo".git "$tmp"/nixpkgs.git
56 log "Fetching the PR commit history"
57 # Fetch the PR
58 git -C "$tmp/nixpkgs.git" remote add fork https://github.com/"$prRepo".git
59 # This remote config is the same as --filter=tree:0 when cloning
60 git -C "$tmp/nixpkgs.git" config remote.fork.partialclonefilter tree:0
61 git -C "$tmp/nixpkgs.git" config remote.fork.promisor true
63 git -C "$tmp/nixpkgs.git" fetch --no-tags fork "$prBranch"
64 headRef=$(git -C "$tmp/nixpkgs.git" rev-parse refs/remotes/fork/"$prBranch")
66 log "Checking correctness of the base branch"
67 if ! "$SCRIPT_DIR"/verify-base-branch.sh "$tmp/nixpkgs.git" "$headRef" "$baseRepo" "$baseBranch" "$prRepo" "$prBranch" | tee "$tmp/invalid-base-error" >&2; then
68 log "Posting error as comment"
69 if ! response=$(effect gh api \
70 --method POST \
71 -H "Accept: application/vnd.github+json" \
72 -H "X-GitHub-Api-Version: 2022-11-28" \
73 "/repos/$baseRepo/issues/$prNumber/comments" \
74 -F "body=@$tmp/invalid-base-error"); then
75 log "Failed to post the comment: $response"
77 exit 1
80 log "Getting code owners to request reviews from"
81 "$SCRIPT_DIR"/get-reviewers.sh "$tmp/nixpkgs.git" "$ownersFile" "$baseRepo" "$baseBranch" "$headRef" "$prNumber" "$prAuthor" > "$tmp/reviewers.json"
83 log "Requesting reviews from: $(<"$tmp/reviewers.json")"
85 if ! response=$(effect gh api \
86 --method POST \
87 -H "Accept: application/vnd.github+json" \
88 -H "X-GitHub-Api-Version: 2022-11-28" \
89 "/repos/$baseRepo/pulls/$prNumber/requested_reviewers" \
90 --input "$tmp/reviewers.json"); then
91 log "Failed to request reviews: $response"
92 exit 1
95 log "Successfully requested reviews"