Debian Codegraph @

dev/tools/merge-pr.sh

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 #!/usr/bin/bashscip-shell filesystem . . `/usr/bin/bash`.
2
3 set -e
4 set -o pipefail
5
6 ORG=rocq-prover
7 REPO=rocq
8
9 API=https://api.github.com/repos/$ORG/$REPO
10 OFFICIAL_REMOTE_GIT_URL="[email protected]:$ORG/$REPO"
11 OFFICIAL_REMOTE_HTTPS_URL="github.com/$ORG/$REPO"
12
13 # This script depends (at least) on git (>= 2.7) and jq.
14 # It should be used like this: dev/tools/merge-pr.sh /PR number/
15
16 # Set SLOW_CONF to have the confirmation output wait for a newline
17 # E.g. call $ SLOW_CONF= dev/tools/merge-pr.sh /PR number/
18 # emacs doesn't send characters until the RET so we can't quick_conf
19 if [scip-shell system . . `[`. -z ${SLOW_CONF+x} ] || [scip-shell system . . `[`. -n "$INSIDE_EMACS" ]; then
20 QUICK_CONF="-n 1"
21 else
22 QUICK_CONF=""
23 fi
24
25 RED="\033[31m"
26 RESET="\033[0m"
27 GREEN="\033[32m"
28 YELLOW="\033[33m"
29 infoscip-shell shell shell-project 0.0.0 info().() {
30 echoscip-shell system . . echo. -e "${GREEN}info:${RESET} $1 ${RESET}"
31 }
32 errorscip-shell shell shell-project 0.0.0 error().() {
33 echoscip-shell system . . echo. -e "${RED}error:${RESET} $1 ${RESET}"
34 }
35 warningscip-shell shell shell-project 0.0.0 warning().() {
36 echoscip-shell system . . echo. -e "${YELLOW}warning:${RESET} $1 ${RESET}"
37 }
38
39 check_utilscip-shell shell shell-project 0.0.0 check_util().() {
40 if ! command -v "$1" > /dev/nullscip-shell filesystem . . `/dev/null`. 2>&1; then
41 errorscip-shell shell shell-project 0.0.0 error(). "this script requires the $1 command line utility"
42 exit 1
43 fi
44 }
45
46 ask_confirmationscip-shell shell shell-project 0.0.0 ask_confirmation().() {
47 read -p "Continue anyway? [y/N] " $QUICK_CONF -r
48 echoscip-shell system . . echo.
49 if [[ ! $REPLY =~ ^[Yy]$ ]]
50 then
51 exit 1
52 fi
53 }
54
55 curl_paginate_arrayscip-shell shell shell-project 0.0.0 curl_paginate_array().() {
56 # as per https://developer.github.com/v3/guides/traversing-with-pagination/#changing-the-number-of-items-received, GitHub will never give us more than 100
57 url="$1?per_page=100"
58 # we keep fetching pages until the response is below the per-page limit (possibly 0 elements)
59 page=1
60 while truescip-shell system . . true.; do
61 response="$(curlscip-shell system . . curl. -s "${url}&page=${page}")"
62 echoscip-shell system . . echo. "${response}"
63 if [scip-shell system . . `[`. "$(jqscip-shell system . . jq. 'length' <<< "$response")" -lt 100 ]; then # done
64 break
65 fi
66 page=$(($page + 1))
67 done | jqscip-shell system . . jq. '[.[]]' # we concatenate the arrays
68 }
69
70 check_utilscip-shell shell shell-project 0.0.0 check_util(). jq
71 check_utilscip-shell shell shell-project 0.0.0 check_util(). curl
72 check_utilscip-shell shell shell-project 0.0.0 check_util(). git
73 check_utilscip-shell shell shell-project 0.0.0 check_util(). gpg
74 check_utilscip-shell shell shell-project 0.0.0 check_util(). grep
75
76 # command line parsing
77
78 if [scip-shell system . . `[`. $# != 1 ]; then
79 errorscip-shell shell shell-project 0.0.0 error(). "usage: $0 PR-number"
80 exit 1
81 fi
82
83 if [[ "$1" =~ ^[1-9][0-9]*$ ]]; then
84 PR=$1
85 else
86 errorscip-shell shell shell-project 0.0.0 error(). "$1 is not a number"
87 exit 1
88 fi
89
90 # Fetching PR metadata
91
92 # The main API call returns a dict/object, not an array, so we don't
93 # bother paginating
94 PRDATA=$(curlscip-shell system . . curl. -s "$API/pulls/$PR")
95
96 TITLE=$(echoscip-shell system . . echo. "$PRDATA" | jqscip-shell system . . jq. -r '.title')
97 infoscip-shell shell shell-project 0.0.0 info(). "title for PR $PR is $TITLE"
98
99 BASE_BRANCH=$(echoscip-shell system . . echo. "$PRDATA" | jqscip-shell system . . jq. -r '.base.ref')
100 infoscip-shell shell shell-project 0.0.0 info(). "PR $PR targets branch $BASE_BRANCH"
101
102 CURRENT_LOCAL_BRANCH=$(gitscip-shell system . . git. rev-parse --abbrev-ref HEAD)
103 infoscip-shell shell shell-project 0.0.0 info(). "you are merging in $CURRENT_LOCAL_BRANCH"
104
105 REMOTE=$(gitscip-shell system . . git. config --get "branch.$CURRENT_LOCAL_BRANCH.remote" || truescip-shell system . . true.)
106 if [scip-shell system . . `[`. -z "$REMOTE" ]; then
107 errorscip-shell shell shell-project 0.0.0 error(). "branch $CURRENT_LOCAL_BRANCH has not associated remote"
108 errorscip-shell shell shell-project 0.0.0 error(). "don't know where to fetch the PR from"
109 errorscip-shell shell shell-project 0.0.0 error(). "please run: git branch --set-upstream-to=THE_REMOTE/$CURRENT_LOCAL_BRANCH"
110 exit 1
111 fi
112 REMOTE_URL=$(gitscip-shell system . . git. remote get-url "$REMOTE" --all)
113 if [scip-shell system . . `[`. "$REMOTE_URL" != "${OFFICIAL_REMOTE_GIT_URL}" ] && \
114 [ "$REMOTE_URL" != "${OFFICIAL_REMOTE_GIT_URL}.git" ] && \
115 [ "$REMOTE_URL" != "https://${OFFICIAL_REMOTE_HTTPS_URL}" ] && \
116 [ "$REMOTE_URL" != "https://${OFFICIAL_REMOTE_HTTPS_URL}.git" ] && \
117 [[ "$REMOTE_URL" != "https://"*"@${OFFICIAL_REMOTE_HTTPS_URL}" ]] && \
118 [[ "$REMOTE_URL" != "https://"*"@${OFFICIAL_REMOTE_HTTPS_URL}.git" ]] ; then
119 errorscip-shell shell shell-project 0.0.0 error(). "remote $REMOTE does not point to the official Rocq repo"
120 errorscip-shell shell shell-project 0.0.0 error(). "that is $OFFICIAL_REMOTE_GIT_URL"
121 errorscip-shell shell shell-project 0.0.0 error(). "it points to $REMOTE_URL instead"
122 ask_confirmationscip-shell shell shell-project 0.0.0 ask_confirmation().
123 fi
124 infoscip-shell shell shell-project 0.0.0 info(). "remote for $CURRENT_LOCAL_BRANCH is $REMOTE"
125
126 infoscip-shell shell shell-project 0.0.0 info(). "fetching from $REMOTE the PR"
127 gitscip-shell system . . git. remote update "$REMOTE"
128 if ! gitscip-shell system . . git. ls-remote "$REMOTE" | grepscip-shell system . . grep. pull >/dev/nullscip-shell filesystem . . `/dev/null`.; then
129 errorscip-shell shell shell-project 0.0.0 error(). "remote $REMOTE is not configured to fetch pull requests"
130 errorscip-shell shell shell-project 0.0.0 error(). "run: git config remote.$REMOTE.fetch +refs/pull/*/head:refs/remotes/$REMOTE/pr/*"
131 exit 1
132 fi
133 gitscip-shell system . . git. fetch "$REMOTE" "refs/pull/$PR/head"
134 COMMIT=$(gitscip-shell system . . git. rev-parse FETCH_HEAD)
135 infoscip-shell shell shell-project 0.0.0 info(). "commit for PR $PR is $COMMIT"
136
137 # Sanity check: merge to a different branch
138
139 if [scip-shell system . . `[`. "$BASE_BRANCH" != "$CURRENT_LOCAL_BRANCH" ]; then
140 errorscip-shell shell shell-project 0.0.0 error(). "PR requests merge in $BASE_BRANCH but you are merging in $CURRENT_LOCAL_BRANCH"
141 ask_confirmationscip-shell shell shell-project 0.0.0 ask_confirmation().
142 fi;
143
144 # Sanity check: the local branch is up-to-date with upstream
145
146 LOCAL_BRANCH_COMMIT=$(gitscip-shell system . . git. rev-parse HEAD)
147 UPSTREAM_COMMIT=$(gitscip-shell system . . git. rev-parse @{u})
148 if [scip-shell system . . `[`. "$LOCAL_BRANCH_COMMIT" != "$UPSTREAM_COMMIT" ]; then
149
150 # Is it just that the upstream branch is behind?
151 # It could just be that we merged other PRs and we didn't push yet
152
153 if [scip-shell system . . `[`. "$BASE_BRANCH" = master ] \
154 && gitscip-shell system . . git. merge-base --is-ancestor -- "$UPSTREAM_COMMIT" "$LOCAL_BRANCH_COMMIT"; then
155 warningscip-shell shell shell-project 0.0.0 warning(). "Your branch is ahead of ${REMOTE}."
156 warningscip-shell shell shell-project 0.0.0 warning(). "On master, GitHub's branch protection rule prevents merging several PRs at once."
157 warningscip-shell shell shell-project 0.0.0 warning(). "You should run [git push ${REMOTE}] between each call to the merge script."
158 ask_confirmationscip-shell shell shell-project 0.0.0 ask_confirmation().
159 else
160 errorscip-shell shell shell-project 0.0.0 error(). "Local branch is not up-to-date with ${REMOTE}."
161 errorscip-shell shell shell-project 0.0.0 error(). "Pull before merging."
162 # This check should never be bypassed.
163 exit 1
164 fi
165 fi
166
167 # Sanity check: PR has an outdated version of CI
168
169 BASE_COMMIT=$(echoscip-shell system . . echo. "$PRDATA" | jqscip-shell system . . jq. -r '.base.sha')
170 CI_FILES=(".gitlab-ci.yml" ".github/workflows/ci.yml")
171
172 if ! gitscip-shell system . . git. diff --quiet "$BASE_COMMIT" "$LOCAL_BRANCH_COMMIT" -- "${CI_FILES[@]}"
173 then
174 warningscip-shell shell shell-project 0.0.0 warning(). "This PR didn't run with the latest version of CI."
175 warningscip-shell shell shell-project 0.0.0 warning(). "It is probably a good idea to ask for a rebase."
176 read -p "Do you want to see the diff? [Y/n] " $QUICK_CONF -r
177 echoscip-shell system . . echo.
178 if [[ ! $REPLY =~ ^[Nn]$ ]]
179 then
180 gitscip-shell system . . git. diff "$BASE_COMMIT" "$LOCAL_BRANCH_COMMIT" -- "${CI_FILES[@]}"
181 fi
182 ask_confirmationscip-shell shell shell-project 0.0.0 ask_confirmation().
183 fi
184
185 # Sanity check: CI failed
186
187 STATUS=$(curlscip-shell system . . curl. -s "$API/commits/$COMMIT/status")
188
189 if [scip-shell system . . `[`. "$(echoscip-shell system . . echo. "$STATUS" | jqscip-shell system . . jq. -r '.state')" != "success" ]; then
190 errorscip-shell shell shell-project 0.0.0 error(). "CI unsuccessful on $(echoscip-shell system . . echo. "$STATUS" |
191 jqscip-shell system . . jq. -r -c '.statuses|map(select(.state != "success"))|map(.context)')"
192 ask_confirmationscip-shell shell shell-project 0.0.0 ask_confirmation().
193 fi;
194
195 # Sanity check: has labels named "needs:"
196
197 NEEDS_LABELS=$(echoscip-shell system . . echo. "$PRDATA" | jqscip-shell system . . jq. -rc '.labels | map(select(.name | match("needs:"))) | map(.name)')
198 if [scip-shell system . . `[`. "$NEEDS_LABELS" != "[]" ]; then
199 errorscip-shell shell shell-project 0.0.0 error(). "needs:something labels still present: $NEEDS_LABELS"
200 ask_confirmationscip-shell shell shell-project 0.0.0 ask_confirmation().
201 fi
202
203 # Sanity check: has milestone
204
205 MILESTONE=$(echoscip-shell system . . echo. "$PRDATA" | jqscip-shell system . . jq. -rc '.milestone.title')
206 if [scip-shell system . . `[`. "$MILESTONE" = "null" ]; then
207 errorscip-shell shell shell-project 0.0.0 error(). "no milestone set, please set one"
208 ask_confirmationscip-shell shell shell-project 0.0.0 ask_confirmation().
209 fi
210
211 # Sanity check: has kind
212
213 KIND=$(echoscip-shell system . . echo. "$PRDATA" | jqscip-shell system . . jq. -rc '.labels | map(select(.name | match("kind:"))) | map(.name)')
214 if [scip-shell system . . `[`. "$KIND" = "[]" ]; then
215 errorscip-shell shell shell-project 0.0.0 error(). "no kind:something label set, please set one"
216 ask_confirmationscip-shell shell shell-project 0.0.0 ask_confirmation().
217 fi
218
219 # Sanity check: user.signingkey
220 if [scip-shell system . . `[`. -z "$(gitscip-shell system . . git. config user.signingkey)" ]; then
221 warningscip-shell shell shell-project 0.0.0 warning(). "git config user.signingkey is empty"
222 warningscip-shell shell shell-project 0.0.0 warning(). "gpg will guess a key out of your git config user.* data"
223 fi
224
225 # Generate commit message
226
227 infoscip-shell shell shell-project 0.0.0 info(). "Fetching review data"
228 reviews=$(curl_paginate_arrayscip-shell shell shell-project 0.0.0 curl_paginate_array(). "$API/pulls/$PR/reviews")
229 msg="Merge PR #$PR: $TITLE"
230
231 has_statescip-shell shell shell-project 0.0.0 has_state().() {
232 [scip-shell system . . `[`. "$(jqscip-shell system . . jq. -rc 'map(select(.user.login == "'"$1"'") | .state) | any(. == "'"$2"'")' <<< "$reviews")" = true ]
233 }
234
235 author=$(echoscip-shell system . . echo. "$PRDATA" | jqscip-shell system . . jq. -rc '.user.login')
236 for reviewer in $(jqscip-shell system . . jq. -rc 'map(.user.login | select(. != "'"$author"'")) | unique | join(" ")' <<< "$reviews" ); do
237 if has_statescip-shell shell shell-project 0.0.0 has_state(). "$reviewer" APPROVED; then
238 msg=$(printfscip-shell system . . printf. '%s\n' "$msg" | gitscip-shell system . . git. interpret-trailers --trailer Reviewed-by="$reviewer")
239 elif has_statescip-shell shell shell-project 0.0.0 has_state(). "$reviewer" COMMENTED; then
240 msg=$(printfscip-shell system . . printf. '%s\n' "$msg" | gitscip-shell system . . git. interpret-trailers --trailer Ack-by="$reviewer")
241 fi
242 done
243
244 infoscip-shell shell shell-project 0.0.0 info(). "merging"
245 gitscip-shell system . . git. merge -v -S --no-ff FETCH_HEAD -m "$msg" -e
246
247 # TODO: improve this check
248 if ! gitscip-shell system . . git. diff --quiet --diff-filter=A "$REMOTE/$CURRENT_LOCAL_BRANCH" -- dev/ci/user-overlays; then
249 warningscip-shell shell shell-project 0.0.0 warning(). "this PR has overlays, please check the following:"
250 warningscip-shell shell shell-project 0.0.0 warning(). "- each overlay has a corresponding open PR on the upstream repo"
251 warningscip-shell shell shell-project 0.0.0 warning(). "- after merging please notify the upstream they can merge the PR"
252 fi