treewide: add khaneliman maintainer (#350847)
[NixPkgs.git] / ci / get-merge-commit.sh
blobc62bb56dd993ec1e31d2758d26ff9e8e53863adb
1 #!/usr/bin/env bash
2 # See ./README.md for docs
4 set -euo pipefail
6 log() {
7 echo "$@" >&2
10 if (( $# < 2 )); then
11 log "Usage: $0 GITHUB_REPO PR_NUMBER"
12 exit 99
14 repo=$1
15 prNumber=$2
17 # Retry the API query this many times
18 retryCount=5
19 # Start with 5 seconds, but double every retry
20 retryInterval=5
22 while true; do
23 log "Checking whether the pull request can be merged"
24 prInfo=$(gh api \
25 -H "Accept: application/vnd.github+json" \
26 -H "X-GitHub-Api-Version: 2022-11-28" \
27 "/repos/$repo/pulls/$prNumber")
29 # Non-open PRs won't have their mergeability computed no matter what
30 state=$(jq -r .state <<< "$prInfo")
31 if [[ "$state" != open ]]; then
32 log "PR is not open anymore"
33 exit 1
36 mergeable=$(jq -r .mergeable <<< "$prInfo")
37 if [[ "$mergeable" == "null" ]]; then
38 if (( retryCount == 0 )); then
39 log "Not retrying anymore. It's likely that GitHub is having internal issues: check https://www.githubstatus.com/"
40 exit 3
41 else
42 (( retryCount -= 1 )) || true
44 # null indicates that GitHub is still computing whether it's mergeable
45 # Wait a couple seconds before trying again
46 log "GitHub is still computing whether this PR can be merged, waiting $retryInterval seconds before trying again ($retryCount retries left)"
47 sleep "$retryInterval"
49 (( retryInterval *= 2 )) || true
51 else
52 break
54 done
56 if [[ "$mergeable" == "true" ]]; then
57 log "The PR can be merged"
58 jq -r .merge_commit_sha <<< "$prInfo"
59 else
60 log "The PR has a merge conflict"
61 exit 2