AUTHOR
Julien Delange, Founder and CEO
Julien is the CEO of Codiga. Before starting Codiga, Julien was a software engineer at Twitter and Amazon Web Services.
Julien has a PhD in computer science from Universite Pierre et Marie Curie in Paris, France.
In this article, we'll explain why code reviews are so important, the upsides of automating parts of the code review process, as well as how to do it. In less than ten minutes, you will learn how to have automated Code Reviews on your GitHub repositories using Codiga.
Why automate code reviews?
Code reviews are an important element of software development: they give you an opportunity to verify that your code is high-quality, accurate and does not contain any mistakes.
But code reviews are also time consuming, and in a world where we work in teams distributed in different timezones, it is difficult to complete reviews quickly. In addition, as code reviews are done by other developers and manual by nature, human error means that they can miss some important rules to check for and enforce in new code.
This is why automating your code reviews is extremely important, from both a quality and productivity perspective. First, Codiga's automated Code Reviews are performed in seconds and give real-time feedback to the submitter, allowing them to correct mistakes before submitting their code for manual review. Secondly, Automated Code Reviews ensure that all rules are being checked, ensuring better coverage of coding guidelines.
It is important to emphasize that Automated Code Reviews are not intended to replace manual code reviews; they're meant to complement them. The smart automated feature checks the coding guidelines, detects defective code and potential refactors. It can be done in seconds and ensure that the code being submitted follows coding guidelines and does not contain syntactic and semantic errors. Think of it as an additional yet necessary extra layer to ensure code quality. On the other hand, manual reviews are still required to verify that the code being submitted correctly meets the requirements of your system.
Automated Code Reviews should be used first to ensure that the code does not introduce any defect and enforce coding guidelines. Once the Automated Review passes, a manual review is requested. Following this process dratically reduces the time spent on manual reviews while improving the quality of each code submission.
In the latter half of this article, Code Reviews on GitHub using Codiga, a platform that helps developers reduce their technical debt and automates your code review. The platform is available for GitHub, Bitbucket and Gitlab. While this article focuses on GitHub, the same functionalities are available for Bitbucket and Gitlab.
Step 1: Install Codiga on your GitHub account
Important: This tutorial assumes you will make a pull request on an existing repository. If you do not have a repository yet, create one before (follow instructions from the GitHub documentation).
The first step is to install the Codiga GitHub application on your GitHub account to give Codiga access to your repositories. Once you installed the GitHub app, create a project on the Codiga App. Once projects are added, Codiga will compute the code quality of your repository and surface all errors on its application.
Step 2: Create your projects on Codiga
You need to create projects on the Codiga App. Go on
the app and select Create Project
. Select your repository
and click on Create Project
.
Step 3: Create a Pull Request
Now that you have installed the Codiga GitHub App, create a pull request and and put Codiga's smart tool to the test!
This is how to proceed.
First, create a new branch where you will add your change. We create a new branch called code-review-example. Use the following command to do so:
git checkout -b code-review-example
Then, add some code to your repository. This code is the one sent for review. For demonstrating our automated code review process, we add Python code that imports an unused module and is missing documentation on its classes and function. A copy of the code is shown at the end of this section.
Once the code is written, add it using git.
git add pythoncode.py
Then, commit the changes in the branch
git commit -a -m “Adding new code”
And finally, push the new branch to the origin of the repository (GitHub)
git push --set-upstream origin code-review-example
Until now, what you created is a new branch from the master branch. This new branch contains new code (the Python script pythoncode.py
). The pull request is asking to merge this new code into the master branch. What the automated code review will do is to check that the new code from the branch newbranch
is correct. Codiga will verify this new code and surface any issues in this new code.
When you push your changes, GItHub returns a URL you can use to generate a pull request, as shown below.
When you visit the URL shown when you push the changes, you have a form to create a pull request that will merge the branch with your changes into the main branch of your repository (main
or master
). Click on Compare & pull request
. On the next screen, add a title and click on Create pull request
.
Below is the code being added in pythoncode.py
import logging
class Person:
def __init__(self, firstname, lastname):
self.firstname = firstname
self.lastname = lastname
def print_person(person):
if not person:
return None
else:
return "{0} {1}".format(person.firstname, person.lastname)
p = Person("John", "Doe")
print("first name of the object just created is {0}".format(p.firstname))
print("using function: {0}".format(print_person(p)))
Step 4: See the Pull Request results
Once you create the pull request, Codiga's Code Reviews feature automatically inspects the new code and reports any issues with the changes directly on GitHub. In the main page of the Pull Request, Codiga shows the result of its analysis. If the Code REviews tool detects any issues, it will report the review as failed as shown below.
You can then visit the Files changed
tab to visualize the issues on each file. All issues reported by Codiga are reported as annotations, which makes it easy to visualize where issues are located in your changes.
You can then update your code and address all the violations. Each time you update your code, make sure you commit and push your changes using the following commands
git commit -a -m “updating code and fixing issues”
git push
Once Codiga validates the code and does not detect any issues, the check for the pull request will appear as passed with a green checkmark.
Step 5: Customize Code Reviews' results
You may want to customize Codiga's feature, either by adding more checks, making them more restrictive or in the opposite, ignoring some rules. This is really easy to do!
To ignore rules, visit the pull request view on Codiga. To do so, visit the tab Checks
on Github and click on the Resolve
link next to the Codiga entry (as shown below).
You will then visualize the pull request results on Codiga. In this view, you can click to ignore rules. You can disable rules that seem to be too restrictive or useless to you.
Finally, you can also change the preferences of the project on Codiga and adapt what rules are being run on your repository and change analysis parameters. The project preferences in Codiga contain an extensive list of analysis parameters to adapt.
Summary
In this article, we presented the importance of automated code reviews. We explained how to automate your code reviews on GitHub using Codiga. The same features are also available for Gitlab and Bitbucket.
It takes less than a few minutes to get your code changes analyzed and have feedback on your code within minutes. Automating code reviews provides quick feedback on the code, guarantees coverage of all rules that need to be verified and more importantly, reduces time spent by engineers on code reviews, increasing overall productivity.
Useful Links
- Codiga: https://www.codiga.io
- Codiga GitHub App: https://github.com/marketplace/codiga