github-apply.sh: add script to rebase, merge and close Github pull requests
[maintainer-tools.git] / github-apply.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 # Default close comment
14 COMMENT="Pulled into my staging tree at https://git.openwrt.org/openwrt/staging/$(whoami).git"
15
16 PRID="$1"
17 BRANCH="${2:-master}"
18
19 if [ -z "$PRID" -o -n "${PRID//[0-9]*/}" ]; then
20 echo "Usage: $0 <PR-ID> [rebase-branch]" >&2
21 exit 1
22 fi
23
24 if [ -z "$(git branch --list "$BRANCH")" ]; then
25 echo "Given rebase branch '$BRANCH' does not exist!" >&2
26 exit 2
27 fi
28
29 if ! git fetch "https://github.com/$REPO.git" "pull/$PRID/head:PR$PRID"; then
30 echo "Failed fetch PR #$PRID!" >&2
31 exit 3
32 fi
33
34 git checkout "PR$PRID"
35
36 if ! git rebase "$BRANCH"; then
37 echo "" >&2
38 echo "Cannot automatically rebase 'PR$PRID' onto '$BRANCH'!" >&2
39 echo "Fix conflicts manually and continue with:" >&2
40 echo "" >&2
41 echo " git checkout $BRANCH" >&2
42 echo " git merge --ff-only PR$PRID" >&2
43 echo " git branch -D PR$PRID" >&2
44 echo "" >&2
45 echo "Alternatively cancel the whole operation with:" >&2
46 echo "" >&2
47 echo " git rebase --abort" >&2
48 echo " git checkout $BRANCH" >&2
49 echo " git branch -D PR$PRID" >&2
50 echo "" >&2
51 fi
52
53 git checkout "$BRANCH"
54
55 oldhead="$(git log -1 --format="%H")"
56
57 if ! git merge --ff-only "PR$PRID"; then
58 echo "" >&2
59 echo "Failed to fast-forward merge 'PR$PRID' into '$BRANCH'!" >&2
60 echo "Aborting, but leaving branch 'PR$PRID' behind." >&2
61 exit 5
62 fi
63
64 git branch -D "PR$PRID"
65
66 if [ -n "$TOKEN" ]; then
67 echo ""
68 echo "Enter a comment and hit <enter> to close the PR at Github automatically now."
69 echo "Hit <ctrl>-<c> to exit."
70 echo ""
71 echo "If you do not provide a comment, the default will be: "
72 echo "[$COMMENT]"
73
74 echo -n "Comment > "
75 read usercomment
76
77 echo "Closing PR..."
78
79 comment="${usercomment:-$COMMENT}"
80 comment="${comment//\\/\\\\}"
81 comment="${comment//\"/\\\"}"
82 comment="$(printf '{"body":"%s"}' "$comment")"
83
84 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" || \
85 ! 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"
86 then
87 echo "" >&2
88 echo "Something failed while trying to close the PR via " >&2
89 echo "the Github API, please review the state manually at " >&2
90 echo "https://github.com/$REPO/pull/$PRID" >&2
91 exit 6
92 fi
93 fi
94
95 echo ""
96 echo "The PR has been merged!"
97 echo "Consider pushing your '$BRANCH' branch to its remote now."
98
99 exit 0