Skip to content

Instantly share code, notes, and snippets.

@andrewrk
Created September 1, 2022 02:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewrk/bbc01fc2ebf2673b0c593cd67a1982c3 to your computer and use it in GitHub Desktop.
Save andrewrk/bbc01fc2ebf2673b0c593cd67a1982c3 to your computer and use it in GitHub Desktop.
rebase failing pull requests - useful when your contributors unfortunately open PRs against a broken master branch

Rebase Failing Pull Requests

Dependencies

  • gh with a logged in user
  • git clone zig repo in zig
  • jq, date, and git in PATH.

Instructions

Run ./collect and follow the printed instructions.

TODO

  • It would be nice to use --force-with-lease but when I try that it incorrectly fails with "stale info".

  • Possibly could automate making comments with something like one of these:

    • gh pr close "$pr" --comment "$CLOSE_MESSAGE"
    • gh pr comment -h
    • However, it's nice to type a more thoughtful message in some cases.
#!/bin/sh
set -e
LIMIT=250
CUTOFF="$(date --iso-8601 "--date=$(date --iso-8601) -2 day")"
cd zig
gh pr list \
--limit $LIMIT \
--search "status:failure draft:false updated:<$CUTOFF" \
--json number,author,title \
--jq 'map({key: .number|tostring, value: {login: .author.login, title: .title}})|from_entries' \
>../failed_prs.json
cd ..
jq <failed_prs.json
echo
echo "If this looks OK then run ./rebase-all"
#!/bin/sh
set -e
FAILED_PRS="$(jq -r <failed_prs.json 'keys|map(tonumber)|@sh')"
cd zig
git fetch origin --prune --tags
rm -f ../failed_rebases.txt
set +e
for pr in $FAILED_PRS
do
if [[ -n $(git status -s) ]]
then
echo "dirty git status"
exit 1
fi
USERNAME="$(jq -r <../failed_prs.json ".[$pr|tostring].login")"
TITLE="$(jq -r <../failed_prs.json ".[$pr|tostring].title")"
URL="https://github.com/ziglang/zig/pull/$pr"
if ! gh pr checkout -f "$pr"
then
echo "$pr checkout $URL $USERNAME $TITLE" >> ../failed_rebases.txt
echo "checkout failed: $URL $USERNAME $TITLE"
else
if ! git rebase origin/master
then
echo "$pr rebase $URL $USERNAME $TITLE" >> ../failed_rebases.txt
echo "rebase failed: $URL $USERNAME $TITLE"
git rebase --abort
elif ! git push -f "git@github.com:$USERNAME/zig.git"
then
echo "$pr push $URL $USERNAME $TITLE" >> ../failed_rebases.txt
echo "push failed: $URL $USERNAME $TITLE"
else
echo "success: $URL $USERNAME $TITLE"
fi
fi
done
cd ..
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment