<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sofien</title>
    <description>The latest articles on DEV Community by Sofien (@sofien).</description>
    <link>https://dev.to/sofien</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F884402%2Fde818d9a-b203-46b8-8eba-e8e9f2075bb4.jpeg</url>
      <title>DEV Community: Sofien</title>
      <link>https://dev.to/sofien</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sofien"/>
    <language>en</language>
    <item>
      <title>Life is Too Short to Review Spaces</title>
      <dc:creator>Sofien</dc:creator>
      <pubDate>Mon, 01 Aug 2022 08:46:00 +0000</pubDate>
      <link>https://dev.to/gitguardian/life-is-too-short-to-review-spaces-5e8d</link>
      <guid>https://dev.to/gitguardian/life-is-too-short-to-review-spaces-5e8d</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Most developers hate doing things that could be automated.&lt;/p&gt;


&lt;blockquote class="ltag__twitter-tweet"&gt;

  &lt;div class="ltag__twitter-tweet__main"&gt;
    &lt;div class="ltag__twitter-tweet__header"&gt;
      &lt;img class="ltag__twitter-tweet__profile-image" src="https://res.cloudinary.com/practicaldev/image/fetch/s--MJz8RJ12--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://pbs.twimg.com/profile_images/1801896736/twitteravatar_normal.png" alt="Zhuowei Zhang profile image"&gt;
      &lt;div class="ltag__twitter-tweet__full-name"&gt;
        Zhuowei Zhang
      &lt;/div&gt;
      &lt;div class="ltag__twitter-tweet__username"&gt;
        @zhuowei
      &lt;/div&gt;
      &lt;div class="ltag__twitter-tweet__twitter-logo"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ir1kO05j--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/twitter-f95605061196010f91e64806688390eb1a4dbc9e913682e043eb8b1e06ca484f.svg" alt="twitter logo"&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="ltag__twitter-tweet__body"&gt;
      Never spend 6 minutes doing something by hand when you can spend 6 hours failing to automate it
    &lt;/div&gt;
    &lt;div class="ltag__twitter-tweet__date"&gt;
      04:28 AM - 26 Apr 2020
    &lt;/div&gt;


    &lt;div class="ltag__twitter-tweet__actions"&gt;
      &lt;a href="https://twitter.com/intent/tweet?in_reply_to=1254266079532154880" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fFnoeFxk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/twitter-reply-action-238fe0a37991706a6880ed13941c3efd6b371e4aefe288fe8e0db85250708bc4.svg" alt="Twitter reply action"&gt;
      &lt;/a&gt;
      &lt;a href="https://twitter.com/intent/retweet?tweet_id=1254266079532154880" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--k6dcrOn8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/twitter-retweet-action-632c83532a4e7de573c5c08dbb090ee18b348b13e2793175fea914827bc42046.svg" alt="Twitter retweet action"&gt;
      &lt;/a&gt;
      &lt;a href="https://twitter.com/intent/like?tweet_id=1254266079532154880" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SRQc9lOp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/twitter-like-action-1ea89f4b87c7d37465b0eb78d51fcb7fe6c03a089805d7ea014ba71365be5171.svg" alt="Twitter like action"&gt;
      &lt;/a&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/blockquote&gt;


&lt;p&gt;As emphasized in this tweet, we often have to accept that we cannot do it. Fortunately, in the case of &lt;strong&gt;code reviews&lt;/strong&gt;, a lot of things can indeed be automated. As my previous CTO told me once&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Life is too short to review spaces!&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Between the fingers of the developers and the eyes of the reviewers there are two main steps where this automated review process can be done:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  as pre-commit hooks&lt;/li&gt;
&lt;li&gt;  as CI jobs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this article, we’ll focus on the pre-commit step. We’ll see how to install and setup pre-commit hooks and we’ll list the top 8 hooks we use at GitGuardian&lt;/p&gt;

&lt;h2&gt;
  
  
  How to set up commit hooks
&lt;/h2&gt;

&lt;p&gt;From the &lt;a href="https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks"&gt;git book&lt;/a&gt;, git hooks are &lt;code&gt;a way to fire off custom scripts when certain important actions occur.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In the case of pre-commit hooks - as its name suggests - scripts are run just before the commit is created, allowing us to block it if it doesn’t meet our requirements. The main advantage of launching scripts at this step is that they can detect problems &lt;strong&gt;before they even enter the version control system&lt;/strong&gt;, letting us fix them easily, or even automatically fix them.&lt;/p&gt;

&lt;p&gt;At GitGuardian we use &lt;a href="https://pre-commit.com/"&gt;&lt;code&gt;pre-commit&lt;/code&gt;&lt;/a&gt; which is a multi-language package manager for pre-commit hooks written in Python. It makes it really easy to install and share the hooks across our organization. You’ll find good alternatives written in other languages like &lt;a href="https://github.com/typicode/husky"&gt;&lt;code&gt;husky&lt;/code&gt;&lt;/a&gt;  in javascript for example.&lt;/p&gt;

&lt;p&gt;To setup&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Add &lt;code&gt;pre-commit&lt;/code&gt; in your requirements.txt or in your Pipfile (in dev section).&lt;/li&gt;
&lt;li&gt; Add a pre-commit configuration file &lt;code&gt;.pre-commit-config.yaml&lt;/code&gt; with the list of hooks you want. Here is an example from the documentation:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You can find &lt;a href="https://pre-commit.com/hooks.html"&gt;here&lt;/a&gt; a list of common hooks.&lt;/p&gt;

&lt;p&gt;3. Run &lt;code&gt;pre-commit install&lt;/code&gt; in your Python env.&lt;/p&gt;

&lt;p&gt;Here is a video to install &lt;code&gt;pre-commit&lt;/code&gt; from &lt;code&gt;pip&lt;/code&gt; and install a GitGuardian hook for example.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/ySTG2NODQCg"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;That’s it! From now on, when you’ll run &lt;code&gt;git commit&lt;/code&gt; all hooks will be launched.&lt;/p&gt;

&lt;h2&gt;
  
  
  The pre-commit hooks we use at GitGuardian
&lt;/h2&gt;

&lt;p&gt;Let’s begin with the formatter hooks. As the title of this article suggests, the last thing we want when reviewing code is to fatigue ourselves by focusing on formatting. This is why we installed the following hooks:&lt;/p&gt;

&lt;h3&gt;
  
  
  flake8
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;flake8&lt;/code&gt; parses the modified python files to make sure that the PEP8 guidelines are followed and block the commit if it’s not the case. On top of it, we developed our own &lt;code&gt;flake8&lt;/code&gt; plugin that we named  &lt;code&gt;ggflake8&lt;/code&gt; to enforce a set of custom rules like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  all functions of 20 lines or more must have a docstring&lt;/li&gt;
&lt;li&gt;  function with 3 or more arguments must use named arguments&lt;/li&gt;
&lt;li&gt;  Tests docstrings must follow the &lt;code&gt;Gherkin&lt;/code&gt; ”GIVEN/WHEN/THEN” &lt;a href="https://cucumber.io/docs/gherkin/reference/"&gt;format&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  black
&lt;/h3&gt;

&lt;p&gt;We chose to add this strongly opinionated formatter on top of flake8 to remove all discussion about formating. As their documentation says:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Black is the uncompromising Python code formatter. By using it, you agree to cede control over minutiae of hand-formatting. In return, Black gives you speed, determinism, and freedom from &lt;code&gt;pycodestyle&lt;/code&gt; nagging about formatting. You will save time and mental energy for more important matters.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Other good alternatives include &lt;code&gt;pylint&lt;/code&gt; and &lt;code&gt;autopep8&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  isort
&lt;/h3&gt;

&lt;p&gt;As their &lt;a href="https://pycqa.github.io/isort/"&gt;documentation&lt;/a&gt;) says:  “isort your imports, so you don't have to”. It’s a handy Python utility that will take care of formatting the imports by sorting them alphabetically and separating them by sections and by type. One less thing to worry about!&lt;/p&gt;

&lt;h3&gt;
  
  
  prettier
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://github.com/prettier/prettier"&gt;&lt;code&gt;prettier&lt;/code&gt;&lt;/a&gt; and &lt;a href="https://github.com/eslint/eslint"&gt;&lt;code&gt;eslint&lt;/code&gt;&lt;/a&gt; are used to format our JSON, YAML, and markdown files.&lt;/p&gt;

&lt;h3&gt;
  
  
  check-*
&lt;/h3&gt;

&lt;p&gt;The first set of hooks checks the syntax of JSON and YAML files while the &lt;code&gt;check-added-large-files&lt;/code&gt; ensure that no one commits a huge file by mistake.&lt;/p&gt;

&lt;h3&gt;
  
  
  commitizen
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://github.com/commitizen-tools/commitizen"&gt;&lt;code&gt;commitizen&lt;/code&gt;&lt;/a&gt; makes sure our commit messages meet our company requirements, which is a format derived from &lt;code&gt;semantic-release&lt;/code&gt;  where we require to also put the related GitLab issue’s number. Here is an example of a valid GitGuardian commit message:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;chore(pre-commit): #2345 add commitizen hook&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  codespell
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://github.com/codespell-project/codespell"&gt;&lt;code&gt;codespell&lt;/code&gt;&lt;/a&gt; checks for typos. We chose this tool because it is based on a list of common typos, which reduces the number of false positives to a minimum.&lt;/p&gt;

&lt;p&gt;It turned out to be a very useful tool: what a relief not to have to reject your colleague's MR because of a minor typo!&lt;/p&gt;

&lt;h3&gt;
  
  
  ggshield
&lt;/h3&gt;

&lt;p&gt;How silly would it be to not use our own software?&lt;/p&gt;

&lt;p&gt;Pre-commit hooks are also a great place to run security tests. As with all tests, the sooner problems are detected the better. This is especially true for security issues, which can have disastrous impacts.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/GitGuardian/ggshield"&gt;&lt;code&gt;ggshield&lt;/code&gt;&lt;/a&gt; is one of the tools we develop at GitGuardian to help secure the codebase. Integrated as a hook it will scan the content of the git patch to make sure it does not contains any secret like an API token.&lt;/p&gt;

&lt;h2&gt;
  
  
  Usage
&lt;/h2&gt;

&lt;p&gt;Now that we have our pre-commit hooks installed and setup, they will be run every time we try to commit:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mTibJpxz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://blog.gitguardian.com/content/images/2022/06/Screenshot-from-2022-06-07-16-00-28.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mTibJpxz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://blog.gitguardian.com/content/images/2022/06/Screenshot-from-2022-06-07-16-00-28.png" alt="" width="839" height="327"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hooks run following a commit (skipped here because no files)&lt;/p&gt;

&lt;p&gt;But if for any reason you want to skip one or all hooks you can easily do so&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  simply add the &lt;code&gt;-n&lt;/code&gt; argument: &lt;code&gt;git commit -m "message" -n&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  to skip only one hook use: &lt;code&gt;SKIP=flake8 git commit -m "message"&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Pre-commit hooks are a must-have in any project because they are so easy to set up and offer a huge value. Having used them once, I would say - in my very personal opinion - that it would feel almost as crazy not using them as not using Git! (exaggerating it a bit, but you get the idea ;) )&lt;/p&gt;

&lt;p&gt;Nevertheless, this tool is not infallible as it can be skipped easily or not be installed at all. That is why it is important to maintain CI server-side tests and jobs, especially the security-related ones. &lt;strong&gt;Pre-commit hooks and CI jobs are complementary.&lt;/strong&gt; It also shows that for security tests, a complementary solution that would scan the VCS server-side is still necessary.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
