github-merge-pr: fix typo and correctly exit on error
[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 if ! command -v jq &> /dev/null; then
19 echo "jq could not be found! This script require jq!"
20 exit 1
21 fi
22
23 if [ -z "$PRID" -o -n "${PRID//[0-9]*/}" ]; then
24 echo "Usage: $0 <PR-ID> [rebase-branch] [dry-run]" >&2
25 exit 1
26 fi
27
28 if [ -n "$DRY_RUN" ]; then
29 GIT="echo git"
30 fi
31
32 if [ -z "$(git branch --list "$BRANCH")" ]; then
33 echo "Given rebase branch '$BRANCH' does not exist!" >&2
34 exit 2
35 fi
36
37 if ! PR_INFO="$(curl -f -s "https://api.github.com/repos/$REPO/pulls/$PRID")"; then
38 echo "Failed fetch PR #$PRID info" >&2
39 exit 3
40 fi
41
42 if [ "$(echo "$PR_INFO" | jq -r ".maintainer_can_modify")" == "false" ]; then
43 echo "PR #$PRID can't be force pushed by maintainers. Can't merge this PR!" >&2
44 exit 4
45 fi
46
47 if [ "$(echo "$PR_INFO" | jq -r ".mergeable")" == "false" ]; then
48 echo "PR #$PRID is not mergeable for Github.com. Check the PR!" >&2
49 exit 5
50 fi
51
52 echo "Pulling current $BRANCH from origin"
53 $GIT checkout $BRANCH
54 $GIT fetch origin
55
56 if ! $GIT rebase origin/$BRANCH; then
57 echo "Failed to rebase $BRANCH with origin/$BRANCH" >&2
58 exit 7
59 fi
60
61 PR_USER="$(echo "$PR_INFO" | jq -r ".head.user.login")"
62 PR_BRANCH="$(echo "$PR_INFO" | jq -r ".head.ref")"
63 PR_REPO="$(echo "$PR_INFO" | jq -r ".head.repo.html_url")"
64
65 if ! $GIT remote get-url $PR_USER &> /dev/null || [ -n "$DRY_RUN" ]; then
66 echo "Adding $PR_USER with repo $PR_REPO to remote"
67 $GIT remote add $PR_USER $PR_REPO
68 fi
69
70 echo "Fetching remote $PR_USER"
71 $GIT fetch $PR_USER
72
73 echo "Creating branch $PR_BRANCH"
74 if ! $GIT checkout -b $PR_BRANCH $PR_USER/$PR_BRANCH; then
75 echo "Failed to checkout new branch $PR_BRANCH from $PR_USER/$PR_BRANCH" >&2
76 exit 8
77 fi
78
79 echo "Rebasing $PR_BRANCH on top of $BRANCH"
80 if ! $GIT rebase origin/$BRANCH; then
81 echo "Failed to rebase $PR_BRANCH with origin/$BRANCH" >&2
82 exit 9
83 fi
84
85 echo "Force pushing $PR_BRANCH to $PR_USER"
86 if ! $GIT push $PR_USER HEAD --force; then
87 echo "Failed to force push HEAD to $PR_USER" >&2
88 exit 10
89 fi
90
91 echo "Returning to $BRANCH"
92 $GIT checkout $BRANCH
93
94 echo "Actually merging the PR #$PRID from branch $PR_USER/$PR_BRANCH"
95 if ! $GIT merge --ff-only $PR_USER/$PR_BRANCH; then
96 echo "Failed to merge $PR_USER/$PR_BRANCH on $BRANCH" >&2
97 exit 11
98 fi
99
100 echo "Pushing to openwrt git server"
101 if ! $GIT push; then
102 echo "Failed to push to $BRANCH but left branch as is." >&2
103 exit 12
104 fi
105
106 echo "Deleting branch $PR_BRANCH"
107 $GIT branch -D $PR_BRANCH
108
109 # Default close comment
110 COMMENT="Thanks! Rebased on top of $BRANCH and merged!"
111
112 if [ -n "$TOKEN" ] && [ -z "$DRY_RUN" ]; then
113 echo ""
114 echo "Enter a comment and hit <enter> to close the PR at Github automatically now."
115 echo "Hit <ctrl>-<c> to exit."
116 echo ""
117 echo "If you do not provide a comment, the default will be: "
118 echo "[$COMMENT]"
119
120 echo -n "Comment > "
121 read usercomment
122
123 echo "Sending message to PR..."
124
125 comment="${usercomment:-$COMMENT}"
126 comment="${comment//\\/\\\\}"
127 comment="${comment//\"/\\\"}"
128 comment="$(printf '{"body":"%s"}' "$comment")"
129
130 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" || \
131 ! 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"
132 then
133 echo "" >&2
134 echo "Something failed while sending comment to the PR via ">&2
135 echo "the Github API, please review the state manually at " >&2
136 echo "https://github.com/$REPO/pull/$PRID" >&2
137 exit 6
138 fi
139 fi
140
141 echo ""
142 echo "The PR has been merged!"
143
144 exit 0