-
-
Save npazo/2e13b864e11cf9597ef8bdf74fbb794c to your computer and use it in GitHub Desktop.
Find local git branches with a closed GitHub PR
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
#!/bin/bash | |
# Outputs commands to delete local branches that have an | |
# associated merged (really closed) PR. | |
# BIG CAVEAT: This will report branches that have a closed, | |
# but not merged PR for deletion. | |
# Your Github username and the name of the organization you are a part of | |
# If you are using forks, or aren't part of an org. then your GITHUB_ORG | |
# should just be your username | |
# Also make sure that these are all lowercase even if the actual names in | |
# Github appear otherwise | |
GITHUB_USERNAME=<username> | |
GITHUB_ORG=$GITHUB_USERNAME | |
GITHUB_REPO=<repo> | |
# You need to generate an API token with repo access https://github.com/settings/tokens | |
GITHUB_TOKEN=<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/$GITHUB_ORG/$GITHUB_REPO/pulls?state=closed&head=$GITHUB_ORG:$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