863d3b753aa0d5bd0c15156ba4e8171f7b320143
3 # Github repository, just the name/repo part, no .git suffix, no base url!
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"
20 local default
="${2:-n}"
24 printf "%s y/n [%s] > " "$prompt" "$default"
26 case "${input:-$default}" in
33 if ! command -v jq
&> /dev
/null
; then
34 echo "jq could not be found! This script require jq!"
38 if [ -z "$PRID" -o -n "${PRID//[0-9]*/}" ]; then
39 echo "Usage: $0 <PR-ID> [rebase-branch] [dry-run]" >&2
43 if [ -n "$DRY_RUN" ]; then
47 if [ -z "$(git branch --list "$BRANCH")" ]; then
48 echo "Given rebase branch '$BRANCH' does not exist!" >&2
52 if ! PR_INFO
="$(curl -f -s "https
://api.github.com
/repos
/$REPO/pulls
/$PRID")"; then
53 echo "Failed fetch PR #$PRID info" >&2
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
61 if [ "$(echo "$PR_INFO" | jq -r ".mergeable
")" == "false" ]; then
62 echo "PR #$PRID is not mergeable for Github.com. Check the PR!" >&2
66 echo "Pulling current $BRANCH from origin"
68 $GIT fetch origin
$BRANCH
70 if ! $GIT rebase origin
/$BRANCH; then
71 echo "Failed to rebase $BRANCH with origin/$BRANCH" >&2
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
")"
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
85 echo "Fetching remote $PR_USER"
86 $GIT fetch
$PR_USER $PR_BRANCH
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
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
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
107 echo "Returning to $BRANCH"
108 $GIT checkout
$BRANCH
111 if [ -n "$($GIT log origin/$BRANCH..HEAD)" ]; then
112 echo "Working on dirty branch for $BRANCH! Please reset $BRANCH to origin/$BRANCH" >&2
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
120 if yesno
"Push to openwrt $BRANCH" "y"; then
121 echo "Pushing to openwrt git server"
123 echo "Failed to push to $BRANCH but left branch as is." >&2
127 # Default close comment
128 COMMENT
="Thanks! Rebased on top of $BRANCH and merged!"
130 if [ -n "$TOKEN" ] && [ -z "$DRY_RUN" ]; then
132 echo "Enter a comment and hit <enter> to close the PR at Github automatically now."
133 echo "Hit <ctrl>-<c> to exit."
135 echo "If you do not provide a comment, the default will be: "
141 echo "Sending message to PR..."
143 comment
="${usercomment:-$COMMENT}"
144 comment
="${comment//\\/\\\\}"
145 comment
="${comment//\"/\\\"}"
146 comment
="$(printf '{"body
":"%s
"}' "$comment")"
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"
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
160 echo "The PR has been merged!"
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