github-merge-pr: ask for confirmation before pushing to git
authorChristian Marangi <ansuelsmth@gmail.com>
Wed, 11 Jan 2023 15:01:50 +0000 (16:01 +0100)
committerChristian Marangi <ansuelsmth@gmail.com>
Wed, 11 Jan 2023 15:01:50 +0000 (16:01 +0100)
Ask for confirmation before pushing to git. This can be useful to give
an extra and last look at the merging pull request.

Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
github-merge-pr.sh

index bfd7e2a2fe68c6d498513d7d9b41f0ac067a79e7..f8758d3334abefc374dee24ef6ccdf925ca8bbc6 100755 (executable)
@@ -15,6 +15,21 @@ BRANCH="${2:-master}"
 DRY_RUN="$3"
 GIT=git
 
+yesno() {
+       local prompt="$1"
+       local default="${2:-n}"
+       local input
+
+       while [ 1 ]; do
+               printf "%s y/n [%s] > " "$prompt" "$default"
+               read input
+               case "${input:-$default}" in
+                       y*) return 0 ;;
+                       n*) return 1 ;;
+               esac
+       done
+}
+
 if ! command -v jq &> /dev/null; then
        echo "jq could not be found! This script require jq!"
        exit 1
@@ -98,48 +113,51 @@ if ! $GIT merge --ff-only $PR_USER/$PR_BRANCH; then
        exit 11
 fi
 
-echo "Pushing to openwrt git server"
-if ! $GIT push; then
-       echo "Failed to push to $BRANCH but left branch as is." >&2
-       exit 12
-fi
-
-echo "Deleting branch $LOCAL_PR_BRANCH"
-$GIT branch -D $LOCAL_PR_BRANCH
+if yesno "Push to openwrt $BRANCH" "y"; then
+       echo "Pushing to openwrt git server"
+       if ! $GIT push; then
+               echo "Failed to push to $BRANCH but left branch as is." >&2
+               exit 12
+       fi
 
-# Default close comment
-COMMENT="Thanks! Rebased on top of $BRANCH and merged!"
-
-if [ -n "$TOKEN" ] && [ -z "$DRY_RUN" ]; then
-       echo ""
-       echo "Enter a comment and hit <enter> to close the PR at Github automatically now."
-       echo "Hit <ctrl>-<c> to exit."
-       echo ""
-       echo "If you do not provide a comment, the default will be: "
-       echo "[$COMMENT]"
-
-       echo -n "Comment > "
-       read usercomment
-
-       echo "Sending message to PR..."
-
-       comment="${usercomment:-$COMMENT}"
-       comment="${comment//\\/\\\\}"
-       comment="${comment//\"/\\\"}"
-       comment="$(printf '{"body":"%s"}' "$comment")"
-
-       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" || \
-          ! 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"
-       then
-               echo ""                                                     >&2
-               echo "Something failed while sending comment to the PR via ">&2
-               echo "the Github API, please review the state manually at " >&2
-               echo "https://github.com/$REPO/pull/$PRID"                  >&2
-               exit 6
+       # Default close comment
+       COMMENT="Thanks! Rebased on top of $BRANCH and merged!"
+
+       if [ -n "$TOKEN" ] && [ -z "$DRY_RUN" ]; then
+               echo ""
+               echo "Enter a comment and hit <enter> to close the PR at Github automatically now."
+               echo "Hit <ctrl>-<c> to exit."
+               echo ""
+               echo "If you do not provide a comment, the default will be: "
+               echo "[$COMMENT]"
+
+               echo -n "Comment > "
+               read usercomment
+
+               echo "Sending message to PR..."
+
+               comment="${usercomment:-$COMMENT}"
+               comment="${comment//\\/\\\\}"
+               comment="${comment//\"/\\\"}"
+               comment="$(printf '{"body":"%s"}' "$comment")"
+
+               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" || \
+               ! 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"
+               then
+                       echo ""                                                     >&2
+                       echo "Something failed while sending comment to the PR via ">&2
+                       echo "the Github API, please review the state manually at " >&2
+                       echo "https://github.com/$REPO/pull/$PRID"                  >&2
+                       exit 6
+               fi
        fi
+
+       echo -e "\n"
+       echo "The PR has been merged!"
+       echo -e "\n"
 fi
 
-echo ""
-echo "The PR has been merged!"
+echo "Deleting branch $LOCAL_PR_BRANCH"
+$GIT branch -D $LOCAL_PR_BRANCH
 
 exit 0