{"id":1037,"date":"2020-01-19T08:02:39","date_gmt":"2020-01-19T08:02:39","guid":{"rendered":"https:\/\/blog.ngocha.biz\/?p=1037"},"modified":"2020-01-19T08:02:39","modified_gmt":"2020-01-19T08:02:39","slug":"automating-code-reviews-github","status":"publish","type":"post","link":"https:\/\/blog.ngocha.biz\/?p=1037","title":{"rendered":"Automating Code Reviews on GitHub"},"content":{"rendered":"<p>Code reviews are part of the daily activities of software engineers and a key process in <a href=\"https:\/\/devopscube.com\/release-management-explained\/\" rel=\"noreferrer noopener\">release management<\/a>. 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.<\/p>\n<p>In addition, <a href=\"https:\/\/devopscube.com\/devops-tools-for-infrastructure-automation\/\" rel=\"noreferrer noopener\">automating<\/a> code reviews guarantee consistency across reviews and unblocks developers waiting for a review. It significantly increases developer velocity while reducing engineering costs.<\/p>\n<h2 id=\"automate-code-reviews\">Automate Code Reviews<\/h2>\n<p>In this article, we will explain step by step how to automate code reviews on <a href=\"https:\/\/devopscube.com\/jenkins-build-trigger-github-pull-request\/\" rel=\"noreferrer noopener\">Github<\/a> 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.<\/p>\n<h3 id=\"step-1-install-the-github-app-on-your-repository\">Step 1: Install the GitHub App on your repository<\/h3>\n<p>Go on <a href=\"https:\/\/github.com\/marketplace\/code-inspector?ref=devopscube.com\" rel=\"noreferrer noopener\">https:\/\/github.com\/marketplace\/code-inspector<\/a> and install the application. Click on \u201cInstall for free\u201d as shown below.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/27iebXWFE-Zte1bwINUdTlmw8_JJkNut-7Re_ImrBPRU7DD1tSwKZNox76Qw7FE8Bc-K5yx7IddfVJ5LFQu8PTq4M6kXdG60mep6tcPeMnTSP_UxaaLlkC3PO9hLBfusJzxzF3SE\" class=\"kg-image\" alt=\"Add code inspector from Github apps\" loading=\"lazy\"><\/figure>\n<p>Then, click on \u201cComplete order and begin installation\u201d as shown below.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/iBTjUbeE1XxXQmEaQIC5es59YfzguhWq1OT1irmdaQq0BcH009rQ3kTBlfTdWdOep42-4rG6vPgQJz9B-Z4dPbqkZi3J6CU7CfEg43WEk7crA7eHS0AoAoDo6HN0d71ZUVdfsQNl\" class=\"kg-image\" alt=\"Add code inspector to Github\" loading=\"lazy\"><\/figure>\n<p>Finally, choose the repository you want to enable the automated code repository and select \u201cInstall &amp; Authorize\u201d, as shown below.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/80erErMlHsGz5_cl2x89YyHFj6pBCqHuS_Ufg4THrV9xv3FvRJqSjc5SnXQ1iZ5UD_2wry4wf6VEf4aG8-7SLbTA8HQQztdDwhXvP8wjFAJwmvWyA4CSZI6JtyTfVtsBpx-LsUV-\" class=\"kg-image\" alt=\"Add github repos for automated code reviews\" loading=\"lazy\"><\/figure>\n<h3 id=\"step-2-push-a-pull-request\">Step 2: Push a Pull Request<\/h3>\n<p>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.<\/p>\n<p>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.<\/p>\n<blockquote><p>You Might Like: <a href=\"https:\/\/devopscube.com\/jenkins-shared-library-tutorial\/\" rel=\"noreferrer noopener\">Jenkins Shared Library Tutorial For Beginners<\/a><\/p><\/blockquote>\n<p>Before we start to write any code, let\u2019s switch to a new branch, called code-review-demo<\/p>\n<pre><code>git checkout -b code-review-demo<\/code><\/pre>\n<p>Let\u2019s write a very small Python program that sums two numbers. We write the following code in the file main.py.<\/p>\n<pre><code>\ndef sumTwoTerms(term1, term2):\n   return term1 + term2\n\nprint(\"2 + 2 = {0}\".format(sumTwoTerms(2, 2)))<\/code><\/pre>\n<p>Then commit and push our changes to our Github repository.<\/p>\n<pre><code>$ git add main.py\n$ git commit -a\n$ git push --set-upstream origin code-review-demo\n\nCounting objects: 3, done.\nDelta compression using up to 8 threads.\nCompressing objects: 100% (3\/3), done.\nWriting objects: 100% (3\/3), 349 bytes | 349.00 KiB\/s, done.\nTotal 3 (delta 0), reused 0 (delta 0)\nremote:\nremote: Create a pull request for 'code-review-demo' on GitHub by visiting:\nremote:  \thttps:\/\/github.com\/codeinspectordemo\/demo\/pull\/new\/code-review-demo\nremote:\nTo https:\/\/github.com\/codeinspectordemo\/demo.git\n * [new branch]  \tcode-review-demo -&gt; code-review-demo\nBranch 'code-review-demo' set up to track remote branch 'code-review-demo' from 'origin'.<\/code><\/pre>\n<p>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: \u00a0<a href=\"https:\/\/github.com\/codeinspectordemo\/demo\/pull\/new\/code-review-demo?ref=devopscube.com\" rel=\"noreferrer noopener\">https:\/\/github.com\/codeinspectordemo\/demo\/pull\/new\/code-review-demo<\/a><\/p>\n<p>When you open the link, you need to put a title and message for the Pull Request. Click on \u201cCreate pull request\u201d below to create it.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/s6gM0LTltj61xx_ArXFtigpHtG7SrvrxBFX-DrPRwgCLFMXkJ5DBKBAVV9gF0YTSP97HhbMZE3EixJDiPxTuLu4JUXp4QWL97xTwM0jgZI6VS4ev8tUmSf-1GCdbvHg_xDxSzpSf\" class=\"kg-image\" alt=\"Github Pull Request for automated code reviews\" loading=\"lazy\"><\/figure>\n<h3 id=\"step-3-check-the-results-in-the-pull-request\">Step 3: Check the results in the Pull Request<\/h3>\n<p>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.<\/p>\n<p>Code Inspector adds comments on each coding issue and explains what is wrong with the code.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/ZW_UT02XJdkjyGmIZJjaJQQZimzCi5e-wSXpOUWYy63L_hrEHohShSFsUfAfd5rK_vGK4D1vv27NQM24Q5SSGAHuFKwpuY_EkX0g6LWEvGbIu8ZlupO8FQr6SLj86XbVqTJMBluj\" class=\"kg-image\" alt=\"Automated code review results in github pull requests\" loading=\"lazy\"><\/figure>\n<h3 id=\"step-4-fixing-and-validating-code-does-not-have-an-issue\">Step 4: Fixing and Validating Code does not have an issue<\/h3>\n<p>We can fix and address the issue reported in the automated review.<\/p>\n<p>In the present case, according to the review, we need to:<\/p>\n<ol>\n<li>add documentation for the module<\/li>\n<li>Add documentation for the function<\/li>\n<li>make sure the function uses the snake_case rule naming<\/li>\n<li>add a final newline at the end of the file<\/li>\n<\/ol>\n<p>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.<\/p>\n<p>This is how the correct code looks.<\/p>\n<pre><code>\"\"\" My first Python module \"\"\"\n\ndef sum_two_terms(term1, term2):\n   \"\"\"\n   Add two terms\n   :param term1: the first term\n   :param term2: the second term\n   :return:\n   \"\"\"\n   return term1 + term2\n\nprint(\"2 + 2 = {0}\".format(sum_two_terms(2, 2)))<\/code><\/pre>\n<p>Once you modified the code, update it on the remote repository.<\/p>\n<pre><code>\n$ git commit -a\n$ git push\n\nUsername for 'https:\/\/github.com': &lt;username&gt;\nPassword for 'https:\/\/&lt;username&gt;@github.com':\nCounting objects: 3, done.\nDelta compression using up to 8 threads.\nCompressing objects: 100% (3\/3), done.\nWriting objects: 100% (3\/3), 429 bytes | 429.00 KiB\/s, done.\nTotal 3 (delta 0), reused 0 (delta 0)\nTo https:\/\/github.com\/codeinspectordemo\/demo.git\n   79594a2..c0bbd8a  code-review-demo -&gt; code-review-demo<\/code><\/pre>\n<p>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.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/SHmbBx2valSWXZaP5yHgRrjNyM8vW3kzLSer_s9MVZpUz_U5742y3566b6ElaDUi3P3NivIFvOJGLp6YVG9n5cPeZSX9fbTUHAQRTpy9kbddiK2ZG-xhWNvsdgRrRpo4On9PKqBg\" class=\"kg-image\" alt loading=\"lazy\"><\/figure>\n<h3 id=\"wrapping-up\">Wrapping Up<\/h3>\n<p>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.<\/p>\n<h3 id=\"links\">Links<\/h3>\n<ul>\n<li>Code Inspector: <a href=\"https:\/\/www.code-inspector.com\/?ref=devopscube.com\">https:\/\/www.code-inspector.com<\/a><\/li>\n<li>Example of code automated review: <a href=\"https:\/\/github.com\/codeinspectordemo\/demo\/pull\/2?ref=devopscube.com\">https:\/\/github.com\/codeinspectordemo\/demo\/pull\/2<\/a><\/li>\n<\/ul>\n<hr>\n<p><strong>Ngu\u1ed3n:<\/strong> <a href=\"https:\/\/devopscube.com\/automating-code-reviews-github\/\" target=\"_blank\" rel=\"noopener noreferrer\">Automating Code Reviews on GitHub \u2014 DevOpsCube<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Source: https:\/\/devopscube.com\/automating-code-reviews-github\/<\/p>\n","protected":false},"author":1,"featured_media":1038,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1037","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops"],"_links":{"self":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/1037","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1037"}],"version-history":[{"count":0,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/1037\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/media\/1038"}],"wp:attachment":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1037"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1037"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1037"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}