Code reviews are part of the daily activities of software engineers and a key process in release management. With engineers spending 10% to 20% of their time on code reviews, automating code reviews (at least part of) allows them to focus on other tasks.

In addition, automating code reviews guarantee consistency across reviews and unblocks developers waiting for a review. It significantly increases developer velocity while reducing engineering costs. 

Automate Code Reviews

In this article, we will explain step by step how to automate code reviews on Github using Code Inspector, a code analysis platform that empowers developers to write better software. Code Inspector offers a function to automate code reviews that detect design, security, safety, good practice enforcement issues in code, as well as duplicates of complex functions.

Step 1: Install the GitHub App on your repository

Go on https://github.com/marketplace/code-inspector and install the application. Click on “Install for free” as shown below.

Add code inspector from Github apps


Then, click on “Complete order and begin installation” as shown below.

Add code inspector to Github

Finally, choose the repository you want to enable the automated code repository and select “Install & Authorize”, as shown below.

Add github repos for automated code reviews

Step 2: Push a Pull Request

To demonstrate the capabilities, we will start with a small Python project that has just a few lines of code. We will voluntarily put some errors.

In the terminal, go in an empty repository. We will assume you have a repository, all the commands below must be typed in the directory that contains the repository.

You Might Like: Jenkins Shared Library Tutorial For Beginners

Before we start to write any code, let’s switch to a new branch, called code-review-demo

git checkout -b code-review-demo

Let’s write a very small Python program that sums two numbers. We write the following code in the file main.py.


def sumTwoTerms(term1, term2):
   return term1 + term2

print("2 + 2 = {0}".format(sumTwoTerms(2, 2)))

Then commit and push our changes to our Github repository.

$ git add main.py
$ git commit -a
$ git push --set-upstream origin code-review-demo

Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 349 bytes | 349.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote:
remote: Create a pull request for 'code-review-demo' on GitHub by visiting:
remote:  	https://github.com/codeinspectordemo/demo/pull/new/code-review-demo
remote:
To https://github.com/codeinspectordemo/demo.git
 * [new branch]  	code-review-demo -> code-review-demo
Branch 'code-review-demo' set up to track remote branch 'code-review-demo' from 'origin'.

We pushed the branch to the remote repository on GitHub. Now, we need to create a pull request that will formally ask to push the branch on the master. The URL to create the pull request is provided when we pushed the branch and we just need to visit it:  https://github.com/codeinspectordemo/demo/pull/new/code-review-demo

When you open the link, you need to put a title and message for the Pull Request. Click on “Create pull request” below to create it.

Github Pull Request for automated code reviews

Step 3: Check the results in the Pull Request

The pull request will then be analyzed. Once the analysis is finished, you will see the summary of the analysis in the pull request. To see the result for each analyzed file, click on the File tab as shown below.

Code Inspector adds comments on each coding issue and explains what is wrong with the code.

Automated code review results in github pull requests

Step 4: Fixing and Validating Code does not have an issue

We can fix and address the issue reported in the automated review.

In the present case, according to the review, we need to:

  1. add documentation for the module
  2. Add documentation for the function
  3. make sure the function uses the snake_case rule naming
  4. add a final newline at the end of the file

In the present case, to fix the issues reported by the Code Inspector, we added documentation for the module to make sure the function uses the snake_case rule. We also added a final newline after the print statement.

This is how the correct code looks.

""" My first Python module """

def sum_two_terms(term1, term2):
   """
   Add two terms
   :param term1: the first term
   :param term2: the second term
   :return:
   """
   return term1 + term2

print("2 + 2 = {0}".format(sum_two_terms(2, 2)))

Once you modified the code, update it on the remote repository.


$ git commit -a
$ git push

Username for 'https://github.com': <username>
Password for 'https://<username>@github.com':
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 429 bytes | 429.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/codeinspectordemo/demo.git
   79594a2..c0bbd8a  code-review-demo -> code-review-demo

The pull request status will be automatically be updated and we have the guarantee that the updated code has been verified and is correct. Looking at the history of commits, we can see that the first commit did not pass the automated code review while the updated code passes all verification.

SHmbBx2valSWXZaP5yHgRrjNyM8vW3kzLSer s9MVZpUz U5742y3566b6ElaDUi3P3NivIFvOJGLp6YVG9n5cPeZSX9fbTUHAQRTpy9kbddiK2ZG

Wrapping Up

In this tutorial, we explained how to automate code reviews on GitHub with Code Inspector. While the example we took in this tutorial is basic, code Inspector supports more than ten languages and can be used on multiple platforms, including GitHub, Gitlab or Bitbucket. The Code Inspector engine includes rules for code duplicates, complexity or even readability. You can also integrate our analysis engine in your Continuous Integration pipeline in order to block merge or code that does not meet a given quality standard.

Links


Automating Code Reviews on GitHub