Debian Codegraph @

dev/tools/merge-pr.sh

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 #!/usr/bin/env 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 [ -z ${SLOW_CONF+x} ] || [ -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 info() {
30 echo -e "${GREEN}info:${RESET} $1 ${RESET}"
31 }
32 error() {
33 echo -e "${RED}error:${RESET} $1 ${RESET}"
34 }
35 warning() {
36 echo -e "${YELLOW}warning:${RESET} $1 ${RESET}"
37 }
38
39 check_util() {
40 if ! command -v "$1" > /dev/null 2>&1; then
41 error "this script requires the $1 command line utility"
42 exit 1
43 fi
44 }
45
46 ask_confirmation() {
47 read -p "Continue anyway? [y/N] " $QUICK_CONF -r
48 echo
49 if [[ ! $REPLY =~ ^[Yy]$ ]]
50 then
51 exit 1
52 fi
53 }
54
55 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 true; do
61 response="$(curl -s "${url}&page=${page}")"
62 echo "${response}"
63 if [ "$(jq 'length' <<< "$response")" -lt 100 ]; then # done
64 break
65 fi
66 page=$(($page + 1))
67 done | jq '[.[]]' # we concatenate the arrays
68 }
69
70 check_util jq
71 check_util curl
72 check_util git
73 check_util gpg
74 check_util grep
75
76 # command line parsing
77
78 if [ $# != 1 ]; then
79 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 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=$(curl -s "$API/pulls/$PR")
95
96 TITLE=$(echo "$PRDATA" | jq -r '.title')
97 info "title for PR $PR is $TITLE"
98
99 BASE_BRANCH=$(echo "$PRDATA" | jq -r '.base.ref')
100 info "PR $PR targets branch $BASE_BRANCH"
101
102 CURRENT_LOCAL_BRANCH=$(git rev-parse --abbrev-ref HEAD)
103 info "you are merging in $CURRENT_LOCAL_BRANCH"
104
105 REMOTE=$(git config --get "branch.$CURRENT_LOCAL_BRANCH.remote" || true)
106 if [ -z "$REMOTE" ]; then
107 error "branch $CURRENT_LOCAL_BRANCH has not associated remote"
108 error "don't know where to fetch the PR from"
109 error "please run: git branch --set-upstream-to=THE_REMOTE/$CURRENT_LOCAL_BRANCH"
110 exit 1
111 fi
112 REMOTE_URL=$(git remote get-url "$REMOTE" --all)
113 if [ "$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 error "remote $REMOTE does not point to the official Rocq repo"
120 error "that is $OFFICIAL_REMOTE_GIT_URL"
121 error "it points to $REMOTE_URL instead"
122 ask_confirmation
123 fi
124 info "remote for $CURRENT_LOCAL_BRANCH is $REMOTE"
125
126 info "fetching from $REMOTE the PR"
127 git remote update "$REMOTE"
128 if ! git ls-remote "$REMOTE" | grep pull >/dev/null; then
129 error "remote $REMOTE is not configured to fetch pull requests"
130 error "run: git config remote.$REMOTE.fetch +refs/pull/*/head:refs/remotes/$REMOTE/pr/*"
131 exit 1
132 fi
133 git fetch "$REMOTE" "refs/pull/$PR/head"
134 COMMIT=$(git rev-parse FETCH_HEAD)
135 info "commit for PR $PR is $COMMIT"
136
137 # Sanity check: merge to a different branch
138
139 if [ "$BASE_BRANCH" != "$CURRENT_LOCAL_BRANCH" ]; then
140 error "PR requests merge in $BASE_BRANCH but you are merging in $CURRENT_LOCAL_BRANCH"
141 ask_confirmation
142 fi;
143
144 # Sanity check: the local branch is up-to-date with upstream
145
146 LOCAL_BRANCH_COMMIT=$(git rev-parse HEAD)
147 UPSTREAM_COMMIT=$(git rev-parse @{u})
148 if [ "$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 [ "$BASE_BRANCH" = master ] \
154 && git merge-base --is-ancestor -- "$UPSTREAM_COMMIT" "$LOCAL_BRANCH_COMMIT"; then
155 warning "Your branch is ahead of ${REMOTE}."
156 warning "On master, GitHub's branch protection rule prevents merging several PRs at once."
157 warning "You should run [git push ${REMOTE}] between each call to the merge script."
158 ask_confirmation
159 else
160 error "Local branch is not up-to-date with ${REMOTE}."
161 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=$(echo "$PRDATA" | jq -r '.base.sha')
170 CI_FILES=(".gitlab-ci.yml" ".github/workflows/ci.yml")
171
172 if ! git diff --quiet "$BASE_COMMIT" "$LOCAL_BRANCH_COMMIT" -- "${CI_FILES[@]}"
173 then
174 warning "This PR didn't run with the latest version of CI."
175 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 echo
178 if [[ ! $REPLY =~ ^[Nn]$ ]]
179 then
180 git diff "$BASE_COMMIT" "$LOCAL_BRANCH_COMMIT" -- "${CI_FILES[@]}"
181 fi
182 ask_confirmation
183 fi
184
185 # Sanity check: CI failed
186
187 STATUS=$(curl -s "$API/commits/$COMMIT/status")
188
189 if [ "$(echo "$STATUS" | jq -r '.state')" != "success" ]; then
190 error "CI unsuccessful on $(echo "$STATUS" |
191 jq -r -c '.statuses|map(select(.state != "success"))|map(.context)')"
192 ask_confirmation
193 fi;
194
195 # Sanity check: has labels named "needs:"
196
197 NEEDS_LABELS=$(echo "$PRDATA" | jq -rc '.labels | map(select(.name | match("needs:"))) | map(.name)')
198 if [ "$NEEDS_LABELS" != "[]" ]; then
199 error "needs:something labels still present: $NEEDS_LABELS"
200 ask_confirmation
201 fi
202
203 # Sanity check: has milestone
204
205 MILESTONE=$(echo "$PRDATA" | jq -rc '.milestone.title')
206 if [ "$MILESTONE" = "null" ]; then
207 error "no milestone set, please set one"
208 ask_confirmation
209 fi
210
211 # Sanity check: has kind
212
213 KIND=$(echo "$PRDATA" | jq -rc '.labels | map(select(.name | match("kind:"))) | map(.name)')
214 if [ "$KIND" = "[]" ]; then
215 error "no kind:something label set, please set one"
216 ask_confirmation
217 fi
218
219 # Sanity check: user.signingkey
220 if [ -z "$(git config user.signingkey)" ]; then
221 warning "git config user.signingkey is empty"
222 warning "gpg will guess a key out of your git config user.* data"
223 fi
224
225 # Generate commit message
226
227 info "Fetching review data"
228 reviews=$(curl_paginate_array "$API/pulls/$PR/reviews")
229 msg="Merge PR #$PR: $TITLE"
230
231 has_state() {
232 [ "$(jq -rc 'map(select(.user.login == "'"$1"'") | .state) | any(. == "'"$2"'")' <<< "$reviews")" = true ]
233 }
234
235 author=$(echo "$PRDATA" | jq -rc '.user.login')
236 for reviewer in $(jq -rc 'map(.user.login | select(. != "'"$author"'")) | unique | join(" ")' <<< "$reviews" ); do
237 if has_state "$reviewer" APPROVED; then
238 msg=$(printf '%s\n' "$msg" | git interpret-trailers --trailer Reviewed-by="$reviewer")
239 elif has_state "$reviewer" COMMENTED; then
240 msg=$(printf '%s\n' "$msg" | git interpret-trailers --trailer Ack-by="$reviewer")
241 fi
242 done
243
244 info "merging"
245 git merge -v -S --no-ff FETCH_HEAD -m "$msg" -e
246
247 # TODO: improve this check
248 if ! git diff --quiet --diff-filter=A "$REMOTE/$CURRENT_LOCAL_BRANCH" -- dev/ci/user-overlays; then
249 warning "this PR has overlays, please check the following:"
250 warning "- each overlay has a corresponding open PR on the upstream repo"
251 warning "- after merging please notify the upstream they can merge the PR"
252 fi