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
9 # Usage: github-merge-pr.sh PR_NUMBER BRANCH
11 # Github repository, just the name/repo part, no .git suffix, no base url!
12 REPO
="openwrt/openwrt"
14 # Your repository token, generate this token at your profile page:
15 # - Navigate to https://github.com/settings/tokens
16 # - Click on "Generate new token"
17 # - Enter a description, e.g. "pr.sh" and pick the "repo" scope
18 # - Hit "Generate token"
19 #TOKEN="d41d8cd98f00b204e9800998ecf8427e"
21 # Uncomment this line to use SSH key to rebase PR branch
31 local default
="${2:-n}"
35 printf "%s y/n [%s] > " "$prompt" "$default"
37 case "${input:-$default}" in
44 if ! command -v jq
&> /dev
/null
; then
45 echo "jq could not be found! This script require jq!"
49 if [ -z "$PRID" -o -n "${PRID//[0-9]*/}" ]; then
50 echo "Usage: $0 <PR-ID> [rebase-branch] [dry-run]" >&2
54 if [ -n "$DRY_RUN" ]; then
58 if [ -z "$(git branch --list "$BRANCH")" ]; then
59 echo "Given rebase branch '$BRANCH' does not exist!" >&2
63 if ! PR_INFO
="$(curl -f -s "https
://api.github.com
/repos
/$REPO/pulls
/$PRID")"; then
64 echo "Failed fetch PR #$PRID info" >&2
68 if [ "$(echo "$PR_INFO" | jq -r ".maintainer_can_modify
")" == "false" ]; then
69 echo "PR #$PRID can't be force pushed by maintainers. Trying to merge as is!" >&2
72 if [ "$(echo "$PR_INFO" | jq -r ".mergeable
")" == "false" ]; then
73 echo "PR #$PRID is not mergeable for Github.com. Check the PR!" >&2
77 echo "Pulling current $BRANCH from origin"
79 $GIT fetch origin
$BRANCH
81 if ! $GIT rebase origin
/$BRANCH; then
82 echo "Failed to rebase $BRANCH with origin/$BRANCH" >&2
86 PR_USER
="$(echo "$PR_INFO" | jq -r ".
head.user.login
")"
87 PR_BRANCH
="$(echo "$PR_INFO" | jq -r ".
head.ref
")"
88 LOCAL_PR_BRANCH
="$PR_BRANCH"-"$PR_USER"
89 if [ "$USE_SSH" = "1" ]; then
90 PR_REPO
="$(echo "$PR_INFO" | jq -r ".
head.repo.ssh_url
")"
92 PR_REPO
="$(echo "$PR_INFO" | jq -r ".
head.repo.html_url
")"
95 if ! $GIT remote get-url
$PR_USER &> /dev
/null ||
[ -n "$DRY_RUN" ]; then
96 echo "Adding $PR_USER with repo $PR_REPO to remote"
97 $GIT remote add
$PR_USER $PR_REPO
100 echo "Fetching remote $PR_USER"
101 $GIT fetch
$PR_USER $PR_BRANCH
103 if [ "$(echo "$PR_INFO" | jq -r ".maintainer_can_modify
")" == "true" ]; then
104 echo "Creating branch $LOCAL_PR_BRANCH for $PR_BRANCH"
105 if ! $GIT checkout
-b $LOCAL_PR_BRANCH $PR_USER/$PR_BRANCH; then
106 echo "Failed to checkout new branch $PR_BRANCH from $PR_USER/$PR_BRANCH" >&2
110 echo "Rebasing $LOCAL_PR_BRANCH on top of $BRANCH"
111 if ! $GIT rebase origin
/$BRANCH; then
112 echo "Failed to rebase $PR_BRANCH with origin/$BRANCH" >&2
116 echo "Force pushing $LOCAL_PR_BRANCH to HEAD:$PR_BRANCH for $PR_USER"
117 if ! $GIT push
$PR_USER HEAD
:$PR_BRANCH --force; then
118 echo "Failed to force push HEAD to $PR_USER" >&2
122 echo "Returning to $BRANCH"
123 $GIT checkout
$BRANCH
126 if [ -n "$($GIT log origin/$BRANCH..HEAD)" ]; then
127 echo "Working on dirty branch for $BRANCH! Please reset $BRANCH to origin/$BRANCH" >&2
131 echo "Actually merging the PR #$PRID from branch $PR_USER/$PR_BRANCH"
132 if ! $GIT merge
--ff-only $PR_USER/$PR_BRANCH; then
133 echo "Failed to merge $PR_USER/$PR_BRANCH on $BRANCH" >&2
135 if yesno
"Push to openwrt $BRANCH" "y"; then
136 echo "Pushing to openwrt git server"
138 echo "Failed to push to $BRANCH but left branch as is." >&2
142 # Default close comment
143 COMMENT
="Thanks! Rebased on top of $BRANCH and merged!"
145 if [ -n "$TOKEN" ] && [ -z "$DRY_RUN" ]; then
147 echo "Enter a comment and hit <enter> to close the PR at Github automatically now."
148 echo "Hit <ctrl>-<c> to exit."
150 echo "If you do not provide a comment, the default will be: "
156 echo "Sending message to PR..."
158 comment
="${usercomment:-$COMMENT}"
159 comment
="${comment//\\/\\\\}"
160 comment
="${comment//\"/\\\"}"
161 comment
="$(printf '{"body
":"%s
"}' "$comment")"
163 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" || \
164 ! 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"
167 echo "Something failed while sending comment to the PR via ">&2
168 echo "the Github API, please review the state manually at " >&2
169 echo "https://github.com/$REPO/pull/$PRID" >&2
175 echo "The PR has been merged!"
180 if [ "$(echo "$PR_INFO" | jq -r ".maintainer_can_modify
")" == "true" ]; then
181 echo "Deleting branch $LOCAL_PR_BRANCH"
182 $GIT branch
-D $LOCAL_PR_BRANCH