🔥Let’s Do DevOps: Get All GitHub Repo Branches (Even if there’s Thousands)
This blog series focuses on presenting complex DevOps projects as simple and approachable via plain language and lots of pictures. You can do it!
I’ve been writing an ongoing project I’m calling our GitHubCop — it’s a tool that iterates over every repo we have (and we have thousands) and sets all the settings, branch policies, permissions, etc. on each of them every day.
I’ve been primarily relying on GitHub’s REST API + a lot of clever bash tricks. It isn’t the most modern language, but it makes sense to me, and heck, it works. As I’ve been developing the project I’ve found a few rough edges, and I wanted to write about one of them I stumbled on recently — grabbing a list of ALL the branches in a repo.
After all, we only want to set branch policies for branches that exist, right? So let’s talk about why this seemingly simple question isn’t simple, and how to solve it.
How Many Branches In This Repo?
Now, you wouldn’t be alone to think that it surely must be a single API call to find out how many branches you have. Or you could just get a single list of all the branches you have, right?
Unfortunately, it’s not as easy as you’d expect. The Get Repository REST call doesn’t contain it. Wanna know how many forks a repo has? That’s easy. Number of subscribers a repo has? Easy peasy. All of those are first class citizens in the Get Repository world. But not branch count.
I was sure I’d cracked it when I found the List Branches API call. In fact, I implemented this call, and was confused when it didn’t fetch all the branches
In fact, I implemented this call, and was confused when it didn’t fetch all the branches
In fact, the API call fetches the first 30 branches. Which, if you’ve worked with any enterprise (or open source!) software, is ridiculously low. We can bump up the number returned to 100 with