Getting Rusty: Branch Destroyer

2017-11-15 in github, rust

The hook handler ended up being a lot easier than I imagined, so I decided to tackle a more real-world problem (for me): cleaning up stale/fully-merged branches.

At my work we have many developers on many teams working on many branches, and while we have a policy of cleaning up fully-merged branches (such as when merging a pull request) it's easy to miss a few. These "few" then accumulate into "hundreds" over time, and it ends up hard to find the branch you want in the branch picker.

We used to email out every few months asking devs to clean up branches, but this is too manual and still relies on human intervention. I decided to script out the process in Rust, and ended up with branch-destroyer.

Here's a high-level summary of how branch-destroyer works:

  1. Authenticate with github using a personal access token and pull a list of branches
  2. Compare each branch against the default branch (master or develop usually) to get a list of changes
  3. Get the age of the latest commit (most recent commit on the branch diff from step #2)
  4. Delete each branch that is 0 ahead of the default branch (i.e. fully merged) and is older than the configured number of days (7 by default)

It defaults to doing a dry run and printing out each branch that will be destroyed, along with additional info ("how recent was the most recent commit?"). After inspecting that the list matches, passing --for-real will delete the branches listed (technically it runs the full process again and deletes the results).

Along the way, I learned:

  • How to parse CLI args using the clap crate
  • How to make API calls with the hyper crate
  • How to parse JSON with the serde crate
  • How to automate cross-platform builds with Travis CI

And a bunch more about general rust programming/debugging.