Last active
February 16, 2021 16:55
-
-
Save danlamanna/c67ab0c8478c2efbd5136447486e7509 to your computer and use it in GitHub Desktop.
Create and merge a GH PR automatically
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -euo pipefail | |
MERGE_WHEN_CHECKS_PASS=0 | |
VIEW_IN_WEB=0 | |
if ! [ -x "$(command -v gh)" ]; then | |
echo 'Error: gh is not installed.' >&2 | |
exit 1 | |
fi | |
while test $# -gt 0; do | |
case "$1" in | |
-h | --help) | |
echo "create-gh-pr - Create a GitHub PR for the current branch, optionally merging when successful." | |
echo " " | |
echo "create-gh-pr.sh [options]" | |
echo " " | |
echo "options:" | |
echo "-h, --help show brief help" | |
echo "-m, --merge-when-checks-pass merge the PR when all checks pass" | |
echo "-w, --view open the PR in the web browser when created" | |
exit 0 | |
;; | |
-m | --merge-when-checks-pass) | |
MERGE_WHEN_CHECKS_PASS=1 | |
shift | |
;; | |
-w | --view) | |
VIEW_IN_WEB=1 | |
shift | |
;; | |
*) | |
break | |
;; | |
esac | |
done | |
pending_or_failed_checks() { | |
gh pr checks "$1" | awk -F'\t' '$2 != "pass"' | wc -l | |
} | |
# create PR automagically | |
# TODO: ignore if already exists, instead of failing | |
gh pr create --fill | |
PR_NUMBER=$(gh pr list --limit 1 | awk '{ print $1 }') | |
if [[ "$VIEW_IN_WEB" -eq 1 ]]; then | |
gh pr view --web "$PR_NUMBER" | |
fi | |
if [[ "$MERGE_WHEN_CHECKS_PASS" -eq 1 ]]; then | |
while pending_or_failed_checks "$PR_NUMBER"; do | |
sleep 10 | |
done | |
gh pr merge --merge "$PR_NUMBER" | |
echo "$PR_NUMBER merged." | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
One day, GitHub will support
git push -o merge_request.create -o merge_request.merge_when_pipeline_succeeds
as GitLab does.