Last active
January 12, 2023 16:49
-
-
Save baztian/467cc87fca8326faa46cc4a6d4e29d88 to your computer and use it in GitHub Desktop.
Command line tool to handle merging of GitHub pull requests
This file contains hidden or 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
#!/bin/bash | |
write_main_undo() { | |
echo git checkout $main_branch >> "$UNDO_FILE" | |
echo git reset --hard "${main_remote%%/*}" >> "$UNDO_FILE" | |
echo "Undo instructions written to $UNDO_FILE" | |
} | |
if [ -z "$1" ] | |
then | |
git_base=$(git config --get remote.upstream.url | sed -e 's/.*:\(.*\).git/\1/') | |
test -n "$git_base" || git_base=$(git config --get remote.origin.url | sed -e 's/.*:\(.*\).git/\1/') | |
echo "https://github.com/${git_base}/pulls" | |
gh pr list --json number,title,labels --template '{{range .}}{{tablerow (printf " #%v" .number | autocolor "green") .title (.labels | pluck "name" | join ", " |autocolor "cyan" ) }}{{end}}' | |
echo "PRs => $(gh pr list --json number --jq 'reverse|[.[].number]|join(" ")')" | |
echo "dependabot PRs only => $(gh pr list --json 'labels,number' --jq 'reverse|[.[]|select(.labels[].name|contains("dependencies"))|.number]|join(" ")')" | |
exit | |
else | |
prs=$* | |
fi | |
echo "Merging PRs $prs" | |
trap 'catch $? $LINENO' ERR | |
catch() { | |
set +x | |
echo "ERROR: Merge failed" > /dev/stderr | |
echo "branch=$branch_name" | |
echo "branch_names=$branch_names" | |
echo "pr=$i" | |
if [ -n "$UNDO_FILE" ]; then | |
write_main_undo | |
fi | |
} | |
set -ex | |
main_ref=$(git symbolic-ref refs/remotes/upstream/HEAD 2>/dev/null || git symbolic-ref refs/remotes/origin/HEAD) | |
main_remote=${main_ref##refs/remotes/} | |
main_branch=${main_remote##*/} | |
git checkout ${main_branch} | |
git pull | |
branch_names= | |
INSTRUCTIONS_FILE="/tmp/$(basename $PWD).$$" | |
UNDO_FILE="/tmp/$(basename $PWD)-undo.$$" | |
echo git rebase --abort >> "$UNDO_FILE" | |
for i in $prs | |
do | |
gh pr checkout $i | |
branch_name=$(git rev-parse --abbrev-ref HEAD) | |
echo git push -f ${main_remote%%/*} $branch_name >> "$INSTRUCTIONS_FILE" | |
echo git checkout $branch_name >> "$UNDO_FILE" | |
echo git reset --hard ${main_remote%%/*}/$branch_name >> "$UNDO_FILE" | |
branch_names="$branch_names $branch_name" | |
git rebase ${main_branch} | |
#git push -f | |
git checkout ${main_branch} | |
git merge --ff --ff-only --no-edit $branch_name | |
done | |
echo git push >> "$INSTRUCTIONS_FILE" | |
write_main_undo | |
git log --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%ci) %Cblue<%an>%Creset' ${main_remote}.. | |
set +x | |
cat "$INSTRUCTIONS_FILE" | |
echo "Commands written to $INSTRUCTIONS_FILE" | |
echo "Please consider running the tests before pushing this branch!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment