You can certainly achieve this using the GitHub API, but you will require a little bit of fiddling to do it.
First, use the list pull requests API to obtain a list of open pull requests. Each item in this list contains a ["head"]["ref"]
entry which will be the name of a branch.
Now, using the get all references API, list all of the references in your repository. Note that the syntax for branches in the Git Data API is slightly different than the one returned from the pull request API (e.g. refs/heads/topic
vs. topic
), so you'll have to compensate for this. The references API also returns tags unless you search just the refs/heads/
sub-namespace, as mentioned in the docs, so be aware of this.
Once you have these two lists of branch refs, it's simple enough to work out which branches have no open pull requests (don't forget to account for master
or any other branch you wish to keep!).
At this point, you can use the delete reference API to remove those branch refs from the repository.