Skip to content

Instantly share code, notes, and snippets.

@cramforce
Created September 15, 2016 19:41
Show Gist options
  • Save cramforce/21fd5948183dc44f764ec8255ee0ae46 to your computer and use it in GitHub Desktop.
Save cramforce/21fd5948183dc44f764ec8255ee0ae46 to your computer and use it in GitHub Desktop.
Find local git branches with a closed GitHub PR
#!/bin/bash
# Outputs commands to delete local branches that have an
# associated merged (really closed) PR.
# In particular, this also works if the remote branch was
# before merging rebased remotely.
# BIG CAVEAT: This will report branches that have a closed,
# but not merged PR for deletion.
# Assumes two environment vars to exist:
# GITHUB_USERNAME
# GITHUB_TOKEN
for branch in $(git for-each-ref --format='%(refname)' refs/heads/); do
# Find closed PRs associated with the branch name.
# Note, that this ignores whether the PR was merged.
# TODO: Filter for only merged PRs.
json=$(curl --silent -X GET -H "User-Agent: amp-changelog-gulp-task" \
-u $GITHUB_USERNAME:$GITHUB_TOKEN \
-H "Accept: application/vnd.github.v3+json" -H "Content-Type: application/json" \
-H "Cache-Control: no-cache" \
"https://api.github.com/repos/ampproject/amphtml/pulls?state=closed&head=$GITHUB_USERNAME:$branch" 2>&1)
if [[ $json == {* ]];then
echo "ERROR $json"
exit 1
fi
if [ "$json" != "[]" ];then
echo "git update-ref -d $branch"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment