Steve Davis
Automatic Code Reviews with Upsource and Git
Someone working at a desk writing code.

We're a small shop here at Bio::Neos and spend a lot of our time wearing different hats. We try to make switching hats as easy as possible by finding the right tooling and environment to make us as efficient as we can be. Code reviews are a crucial part of our development process that we use to (1) ensure code quality, (2) improve our skills and (3) share knowledge between our developers, at both a project level and a professional level. But because we spend a majority of our time designing and writing software, the code review can be at times an easy part to leave out of the process. We try to fight that by using Upsource and automating where we can. In this post we'll talk about some challenges we encountered and how we use a combination of Upsource and git hooks to make our review process as seamless as possible.

Upsource

Upsource is a nice system that makes it easy for us to see the commit history for a project and easily review source. We don't spend a lot of time configuring or managing the system and to be honest, most of the times Upsource itself gets lost in the background — which I think is exactly what a tool should do in our environment. Upsource offers post-commit review of code and is geared best towards reviewing branches as a whole (we'll address this later). I recommend checking it out if you are looking for a way to make code reviews easier than pull-ing code and using an IDE or your favorite text editor.

While our use-case of Upsource is pretty simple, it has evolved a bit over time. When we first adopted Upsource into our development process, we put the responsibility solely on the developer that was committing source to (1) open a review and (2) invite reviewers. You can probably guess where this is going. Yup, it wasn't too long before all of us were forgetting to create the code reviews in Upsource.

The good news is that Upsource allows you to create custom workflows that can address our problem. So off we went configuring automatic review creation and assigning users automatically.

Automatically create a review in Upsource

So, what's the problem

Unfortunately, having Upsource automatically create reviews for us wasn't the silver bullet. Yes, our reviews were always being created but the problem was with the commits that the reviews were including. I mentioned earlier that Upsource was best used when reviewing a branch but our problem is we don't ever push our branches...

We use Git here and in particular the VCS paradigm we follow is the Git Workflow

The Git Workflow

This workflow works really well for our team but a central part of the Git Workflow is that you do not push a feature branch, rather you merge a feature branch into one of your main branches (for us that is the devel branch).

Because of this, we don't have the option to review a feature branch in Upsource and have to be very careful about automatically creating a review. We found ourselves with two options:

  1. Create a review for every commit that is pushed
    • This can result in a completely unmanageable number of reviews
    or
  2. Append commits to an existing, open review
    • This works as long as the reviews are closed as quickly as possible, otherwise completely unrelated commits can appear in the same review

I did leave out a third option, which was to go back to requiring the developer to go into Upsource and create the review. But we know how that goes...

Git Hooks to the Rescue!

So we were faced with either forgetting to create a code review or having nonsense reviews. Or not. We thought, why not use a git hook to automatically create our reviews?

We use a lot of automation in our process. We kick off builds to our CI server when new code is pushed; we notify Slack when builds complete; we update Slack channels with important updates in Redmine; whatever will make our lives easier. And we use git hooks to do some of the automation. Git hooks allow you to react to different events in your repository, on both the client and the server. Our problem looked like a perfect one to be solved by the post-receive hook.

Piecing It All Together

Upsource has a great API that will allow you to programmatically interact with your projects. We were able to easily put together a post-receive git hook that would

  1. Notify Upsource of changes in our repository
  2. Create a review that includes all the revisions in our push
  3. Assign a default review group

All without the developer doing anything besides merging their branch into devel and pushing to the server.

Our git hook is available for use on Github. If you think this would help you in your process, give it a shot! The README on the Github repository details how to setup the hook.

This is a work in progress and was a quick and dirty implementation. Fork it and help us improve it!