github-merge-pr: ask for confirmation before pushing to git
[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. Can't merge this PR!" >&2
59 exit 4
60 fi
61
62 if [ "$(echo "$PR_INFO" | jq -r ".mergeable")" == "false" ]; then
63 echo "PR #$PRID is not mergeable for Github.com. Check the PR!" >&2
64 exit 5
65 fi
66
67 echo "Pulling current $BRANCH from origin"
68 $GIT checkout $BRANCH
69 $GIT fetch origin
70
71 if ! $GIT rebase origin/$BRANCH; then
72 echo "Failed to rebase $BRANCH with origin/$BRANCH" >&2
73 exit 7
74 fi
75
76 PR_USER="$(echo "$PR_INFO" | jq -r ".head.user.login")"
77 PR_BRANCH="$(echo "$PR_INFO" | jq -r ".head.ref")"
78 LOCAL_PR_BRANCH="$PR_BRANCH"-"$PR_USER"
79 PR_REPO="$(echo "$PR_INFO" | jq -r ".head.repo.html_url")"
80
81 if ! $GIT remote get-url $PR_USER &> /dev/null || [ -n "$DRY_RUN" ]; then
82 echo "Adding $PR_USER with repo $PR_REPO to remote"
83 $GIT remote add $PR_USER $PR_REPO
84 fi
85
86 echo "Fetching remote $PR_USER"
87 $GIT fetch $PR_USER
88
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
110 echo "Actually merging the PR #$PRID from branch $PR_USER/$PR_BRANCH"
111 if ! $GIT merge --ff-only $PR_USER/$PR_BRANCH; then
112 echo "Failed to merge $PR_USER/$PR_BRANCH on $BRANCH" >&2
113 exit 11
114 fi
115
116 if yesno "Push to openwrt $BRANCH" "y"; then
117 echo "Pushing to openwrt git server"
118 if ! $GIT push; then
119 echo "Failed to push to $BRANCH but left branch as is." >&2
120 exit 12
121 fi
122
123 # Default close comment
124 COMMENT="Thanks! Rebased on top of $BRANCH and merged!"
125
126 if [ -n "$TOKEN" ] && [ -z "$DRY_RUN" ]; then
127 echo ""
128 echo "Enter a comment and hit <enter> to close the PR at Github automatically now."
129 echo "Hit <ctrl>-<c> to exit."
130 echo ""
131 echo "If you do not provide a comment, the default will be: "
132 echo "[$COMMENT]"
133
134 echo -n "Comment > "
135 read usercomment
136
137 echo "Sending message to PR..."
138
139 comment="${usercomment:-$COMMENT}"
140 comment="${comment//\\/\\\\}"
141 comment="${comment//\"/\\\"}"
142 comment="$(printf '{"body":"%s"}' "$comment")"
143
144 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" || \
145 ! 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"
146 then
147 echo "" >&2
148 echo "Something failed while sending comment to the PR via ">&2
149 echo "the Github API, please review the state manually at " >&2
150 echo "https://github.com/$REPO/pull/$PRID" >&2
151 exit 6
152 fi
153 fi
154
155 echo -e "\n"
156 echo "The PR has been merged!"
157 echo -e "\n"
158 fi
159
160 echo "Deleting branch $LOCAL_PR_BRANCH"
161 $GIT branch -D $LOCAL_PR_BRANCH
162
163 exit 0