š„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!
Hey all!
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 ?per_page=100
queryā¦