github-merge-pr: be more conservative with git fetch
[maintainer-tools.git] / github-merge-pr.sh
1 #!/bin/bash
2
3 # Github repository, just the name/repo part, no .git suffix, no base url!
4 REPO="openwrt/openwrt"
5
6 # Your repository token, generate this token at your profile page:
7 # - Navigate to https://github.com/settings/tokens
8 # - Click on "Generate new token"
9 # - Enter a description, e.g. "pr.sh" and pick the "repo" scope
10 # - Hit "Generate token"
11 #TOKEN="d41d8cd98f00b204e9800998ecf8427e"
12
13 PRID="$1"
14 BRANCH="${2:-master}"
15 DRY_RUN="$3"
16 GIT=git
17
18 yesno() {
19 local prompt="$1"
20 local default="${2:-n}"
21 local input
22
23 while [ 1 ]; do
24 printf "%s y/n [%s] > " "$prompt" "$default"
25 read input
26 case "${input:-$default}" in
27 y*) return 0 ;;
28 n*) return 1 ;;
29 esac
30 done
31 }
32
33 if ! command -v jq &> /dev/null; then
34 echo "jq could not be found! This script require jq!"
35 exit 1
36 fi
37
38 if [ -z "$PRID" -o -n "${PRID//[0-9]*/}" ]; then
39 echo "Usage: $0 <PR-ID> [rebase-branch] [dry-run]" >&2
40 exit 1
41 fi
42
43 if [ -n "$DRY_RUN" ]; then
44 GIT="echo git"
45 fi
46
47 if [ -z "$(git branch --list "$BRANCH")" ]; then
48 echo "Given rebase branch '$BRANCH' does not exist!" >&2
49 exit 2
50 fi
51
52 if ! PR_INFO="$(curl -f -s "https://api.github.com/repos/$REPO/pulls/$PRID")"; then
53 echo "Failed fetch PR #$PRID info" >&2
54 exit 3
55 fi
56
57 if [ "$(echo "$PR_INFO" | jq -r ".maintainer_can_modify")" == "false" ]; then
58 echo "PR #$PRID can't be force pushed by maintainers. Trying to merge as is!" >&2
59 fi
60
61 if [ "$(echo "$PR_INFO" | jq -r ".mergeable")" == "false" ]; then
62 echo "PR #$PRID is not mergeable for Github.com. Check the PR!" >&2
63 exit 5
64 fi
65
66 echo "Pulling current $BRANCH from origin"
67 $GIT checkout $BRANCH
68 $GIT fetch origin $BRANCH
69
70 if ! $GIT rebase origin/$BRANCH; then
71 echo "Failed to rebase $BRANCH with origin/$BRANCH" >&2
72 exit 7
73 fi
74
75 PR_USER="$(echo "$PR_INFO" | jq -r ".head.user.login")"
76 PR_BRANCH="$(echo "$PR_INFO" | jq -r ".head.ref")"
77 LOCAL_PR_BRANCH="$PR_BRANCH"-"$PR_USER"
78 PR_REPO="$(echo "$PR_INFO" | jq -r ".head.repo.html_url")"
79
80 if ! $GIT remote get-url $PR_USER &> /dev/null || [ -n "$DRY_RUN" ]; then
81 echo "Adding $PR_USER with repo $PR_REPO to remote"
82 $GIT remote add $PR_USER $PR_REPO
83 fi
84
85 echo "Fetching remote $PR_USER"
86 $GIT fetch $PR_USER $PR_BRANCH
87
88 if [ "$(echo "$PR_INFO" | jq -r ".maintainer_can_modify")" == "true" ]; then
89 echo "Creating branch $LOCAL_PR_BRANCH for $PR_BRANCH"
90 if ! $GIT checkout -b $LOCAL_PR_BRANCH $PR_USER/$PR_BRANCH; then
91 echo "Failed to checkout new branch $PR_BRANCH from $PR_USER/$PR_BRANCH" >&2
92 exit 8
93 fi
94
95 echo "Rebasing $LOCAL_PR_BRANCH on top of $BRANCH"
96 if ! $GIT rebase origin/$BRANCH; then
97 echo "Failed to rebase $PR_BRANCH with origin/$BRANCH" >&2
98 exit 9
99 fi
100
101 echo "Force pushing $LOCAL_PR_BRANCH to HEAD:$PR_BRANCH for $PR_USER"
102 if ! $GIT push $PR_USER HEAD:$PR_BRANCH --force; then
103 echo "Failed to force push HEAD to $PR_USER" >&2
104 exit 10
105 fi
106
107 echo "Returning to $BRANCH"
108 $GIT checkout $BRANCH
109 fi
110
111 if [ -n "$($GIT log origin/$BRANCH..HEAD)" ]; then
112 echo "Working on dirty branch for $BRANCH! Please reset $BRANCH to origin/$BRANCH" >&2
113 exit 11
114 fi
115
116 echo "Actually merging the PR #$PRID from branch $PR_USER/$PR_BRANCH"
117 if ! $GIT merge --ff-only $PR_USER/$PR_BRANCH; then
118 echo "Failed to merge $PR_USER/$PR_BRANCH on $BRANCH" >&2
119 else
120 if yesno "Push to openwrt $BRANCH" "y"; then
121 echo "Pushing to openwrt git server"
122 if ! $GIT push; then
123 echo "Failed to push to $BRANCH but left branch as is." >&2
124 exit 12
125 fi
126
127 # Default close comment
128 COMMENT="Thanks! Rebased on top of $BRANCH and merged!"
129
130 if [ -n "$TOKEN" ] && [ -z "$DRY_RUN" ]; then
131 echo ""
132 echo "Enter a comment and hit <enter> to close the PR at Github automatically now."
133 echo "Hit <ctrl>-<c> to exit."
134 echo ""
135 echo "If you do not provide a comment, the default will be: "
136 echo "[$COMMENT]"
137
138 echo -n "Comment > "
139 read usercomment
140
141 echo "Sending message to PR..."
142
143 comment="${usercomment:-$COMMENT}"
144 comment="${comment//\\/\\\\}"
145 comment="${comment//\"/\\\"}"
146 comment="$(printf '{"body":"%s"}' "$comment")"
147
148 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" || \
149 ! 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"
150 then
151 echo "" >&2
152 echo "Something failed while sending comment to the PR via ">&2
153 echo "the Github API, please review the state manually at " >&2
154 echo "https://github.com/$REPO/pull/$PRID" >&2
155 exit 6
156 fi
157 fi
158
159 echo -e "\n"
160 echo "The PR has been merged!"
161 echo -e "\n"
162 fi
163 fi
164
165 if [ "$(echo "$PR_INFO" | jq -r ".maintainer_can_modify")" == "true" ]; then
166 echo "Deleting branch $LOCAL_PR_BRANCH"
167 $GIT branch -D $LOCAL_PR_BRANCH
168 fi
169
170 exit 0