b2c94b5d91947a8f75be9e0103d5563c765680bc
[maintainer-tools.git] / github-merge-pr.sh
1 #!/bin/bash
2
3 # How to setup the repository to make this correctly work
4 # 1. Clone repo and make sure you can correctly push to that
5 # (example with openwrt main repo you need to use ssh remote url)
6 # 2. Make sure you can correctly push and force push to the github
7 # repository
8 #
9 # Usage: github-merge-pr.sh PR_NUMBER BRANCH REPO_NAME
10 #
11 # BRANCH is optional and defaults to main.
12 # REPO_NAME is optional and defaults to openwrt. It does
13 # describe the repository name to use to pull PR info from.
14
15 # Github repository owner or organization name.
16 GITHUB_REPO_OWNER="openwrt"
17
18 # Your repository token, generate this token at your profile page:
19 # - Navigate to https://github.com/settings/tokens
20 # - Click on "Generate new token"
21 # - Enter a description, e.g. "pr.sh" and pick the "repo" scope
22 # - Hit "Generate token"
23 #TOKEN="d41d8cd98f00b204e9800998ecf8427e"
24 #
25 # Uncomment this line to use SSH key to rebase PR branch
26 # USE_SSH=1
27
28 PRID="$1"
29 BRANCH="${2:-main}"
30 GITHUB_REPO_NAME="${3:-openwrt}"
31 DRY_RUN="$4"
32
33 GIT=git
34 REPO="$GITHUB_REPO_OWNER"/"$GITHUB_REPO_NAME"
35
36 yesno() {
37 local prompt="$1"
38 local default="${2:-n}"
39 local input
40
41 while [ 1 ]; do
42 printf "%s y/n [%s] > " "$prompt" "$default"
43 read input
44 case "${input:-$default}" in
45 y*) return 0 ;;
46 n*) return 1 ;;
47 esac
48 done
49 }
50
51 if ! command -v jq &> /dev/null; then
52 echo "jq could not be found! This script require jq!"
53 exit 1
54 fi
55
56 if [ -z "$PRID" -o -n "${PRID//[0-9]*/}" ]; then
57 echo "Usage: $0 <PR-ID> [rebase-branch] [dry-run]" >&2
58 exit 1
59 fi
60
61 if [ -n "$DRY_RUN" ]; then
62 GIT="echo git"
63 fi
64
65 if [ -z "$(git branch --list "$BRANCH")" ]; then
66 echo "Given rebase branch '$BRANCH' does not exist!" >&2
67 exit 2
68 fi
69
70 if ! PR_INFO="$(curl -f -s "https://api.github.com/repos/$REPO/pulls/$PRID")"; then
71 echo "Failed fetch PR #$PRID info" >&2
72 exit 3
73 fi
74
75 if [ "$(echo "$PR_INFO" | jq -r ".maintainer_can_modify")" == "false" ]; then
76 echo "PR #$PRID can't be force pushed by maintainers. Trying to merge as is!" >&2
77 fi
78
79 if [ "$(echo "$PR_INFO" | jq -r ".mergeable")" == "false" ]; then
80 echo "PR #$PRID is not mergeable for Github.com. Check the PR!" >&2
81 exit 5
82 fi
83
84 echo "Pulling current $BRANCH from origin"
85 $GIT checkout $BRANCH
86 $GIT fetch origin $BRANCH
87
88 if ! $GIT rebase origin/$BRANCH; then
89 echo "Failed to rebase $BRANCH with origin/$BRANCH" >&2
90 exit 7
91 fi
92
93 PR_USER="$(echo "$PR_INFO" | jq -r ".head.user.login")"
94 PR_BRANCH="$(echo "$PR_INFO" | jq -r ".head.ref")"
95 LOCAL_PR_BRANCH="$PR_BRANCH"-"$PR_USER"
96 if [ "$USE_SSH" = "1" ]; then
97 PR_REPO="$(echo "$PR_INFO" | jq -r ".head.repo.ssh_url")"
98 else
99 PR_REPO="$(echo "$PR_INFO" | jq -r ".head.repo.html_url")"
100 fi
101
102 if ! $GIT remote get-url $PR_USER &> /dev/null || [ -n "$DRY_RUN" ]; then
103 echo "Adding $PR_USER with repo $PR_REPO to remote"
104 $GIT remote add $PR_USER $PR_REPO
105 fi
106
107 echo "Fetching remote $PR_USER"
108 $GIT fetch $PR_USER $PR_BRANCH
109
110 if [ "$(echo "$PR_INFO" | jq -r ".maintainer_can_modify")" == "true" ]; then
111 echo "Creating branch $LOCAL_PR_BRANCH for $PR_BRANCH"
112 if ! $GIT checkout -b $LOCAL_PR_BRANCH $PR_USER/$PR_BRANCH; then
113 echo "Failed to checkout new branch $PR_BRANCH from $PR_USER/$PR_BRANCH" >&2
114 exit 8
115 fi
116
117 echo "Rebasing $LOCAL_PR_BRANCH on top of $BRANCH"
118 if ! $GIT rebase origin/$BRANCH; then
119 echo "Failed to rebase $PR_BRANCH with origin/$BRANCH" >&2
120 exit 9
121 fi
122
123 # Add to each commit Link: https://github.com/$REPO/pull/$PRID
124 if ! $GIT filter-repo --message-callback "
125 return message + b\"Link: https://github.com/$REPO/pull/$PRID\"
126 " --refs $BRANCH..$LOCAL_PR_BRANCH; then
127 echo "Failed to add Link: Pull Request tag" >&2
128 exit 9
129 fi
130
131 # Remove any previous SoB tag if the Pull Request Author and Committer match
132 if ! $GIT filter-repo --message-callback "
133 return message.replace(b\"Signed-off-by: $(git config user.name) <$(git config user.email)>\",b\"\")
134 " --refs $BRANCH..$LOCAL_PR_BRANCH; then
135 echo "Failed to remove previous Committer SoB tag" >&2
136 exit 9
137 fi
138
139 # Add Committer SoB to better reference who merged the thing last
140 if ! $GIT rebase origin/$BRANCH $LOCAL_PR_BRANCH --exec "git commit -s --amend -C HEAD"; then
141 echo "Failed to add Committer SoB tag" >&2
142 exit 9
143 fi
144
145 echo "Force pushing $LOCAL_PR_BRANCH to HEAD:$PR_BRANCH for $PR_USER"
146 if ! $GIT push $PR_USER HEAD:$PR_BRANCH --force; then
147 echo "Failed to force push HEAD to $PR_USER" >&2
148 exit 10
149 fi
150
151 echo "Returning to $BRANCH"
152 $GIT checkout $BRANCH
153 fi
154
155 if [ -n "$($GIT log origin/$BRANCH..HEAD)" ]; then
156 echo "Working on dirty branch for $BRANCH! Please reset $BRANCH to origin/$BRANCH" >&2
157 exit 11
158 fi
159
160 echo "Actually merging the PR #$PRID from branch $PR_USER/$PR_BRANCH"
161 if ! $GIT merge --ff-only $PR_USER/$PR_BRANCH; then
162 echo "Failed to merge $PR_USER/$PR_BRANCH on $BRANCH" >&2
163 else
164 if yesno "Push to openwrt $BRANCH" "y"; then
165 echo "Pushing to openwrt git server"
166 if ! $GIT push; then
167 echo "Failed to push to $BRANCH but left branch as is." >&2
168 exit 12
169 fi
170
171 # Default close comment
172 COMMENT="Thanks! Rebased on top of $BRANCH and merged!"
173
174 if [ -n "$TOKEN" ] && [ -z "$DRY_RUN" ]; then
175 echo ""
176 echo "Enter a comment and hit <enter> to close the PR at Github automatically now."
177 echo "Hit <ctrl>-<c> to exit."
178 echo ""
179 echo "If you do not provide a comment, the default will be: "
180 echo "[$COMMENT]"
181
182 echo -n "Comment > "
183 read usercomment
184
185 echo "Sending message to PR..."
186
187 comment="${usercomment:-$COMMENT}"
188 comment="${comment//\\/\\\\}"
189 comment="${comment//\"/\\\"}"
190 comment="$(printf '{"body":"%s"}' "$comment")"
191
192 if ! curl -s -o /dev/null -w "%{http_code} %{url_effective}\\n" --user "$TOKEN:x-oauth-basic" --request POST --data "$comment" "https://api.github.com/repos/$REPO/issues/$PRID/comments" || \
193 ! curl -s -o /dev/null -w "%{http_code} %{url_effective}\\n" --user "$TOKEN:x-oauth-basic" --request PATCH --data '{"state":"closed"}' "https://api.github.com/repos/$REPO/pulls/$PRID"
194 then
195 echo "" >&2
196 echo "Something failed while sending comment to the PR via ">&2
197 echo "the Github API, please review the state manually at " >&2
198 echo "https://github.com/$REPO/pull/$PRID" >&2
199 exit 6
200 fi
201 fi
202
203 echo -e "\n"
204 echo "The PR has been merged!"
205 echo -e "\n"
206 fi
207 fi
208
209 if [ "$(echo "$PR_INFO" | jq -r ".maintainer_can_modify")" == "true" ]; then
210 echo "Deleting branch $LOCAL_PR_BRANCH"
211 $GIT branch -D $LOCAL_PR_BRANCH
212 fi
213
214 exit 0