<?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: Vishnu Thankappan</title>
    <description>The latest articles on DEV Community by Vishnu Thankappan (@vishnu_thankappan).</description>
    <link>https://dev.to/vishnu_thankappan</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%2F770562%2Fd0ba9d64-0386-4f39-b913-78dfbe30f6bb.jpeg</url>
      <title>DEV Community: Vishnu Thankappan</title>
      <link>https://dev.to/vishnu_thankappan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vishnu_thankappan"/>
    <language>en</language>
    <item>
      <title>The Right way to commit a code - Integrating Git hooks using husky(V7) with (ESLint + commitlint + SonarQube) in Angular(12+)</title>
      <dc:creator>Vishnu Thankappan</dc:creator>
      <pubDate>Wed, 08 Dec 2021 11:22:01 +0000</pubDate>
      <link>https://dev.to/vishnu_thankappan/integrating-git-hooks-using-huskyv7-with-eslint-commitlint-sonarqube-in-angular12-the-right-way-to-commit-a-code-24c9</link>
      <guid>https://dev.to/vishnu_thankappan/integrating-git-hooks-using-huskyv7-with-eslint-commitlint-sonarqube-in-angular12-the-right-way-to-commit-a-code-24c9</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Here we'll be automating our commit workflow i.e when a developer commits a code it will check for lint errors, do unit testing then inspect the code using SonarQube and check whether the commit message given by developer follows the commit convention or not. Only If all above conditions are satisfied the code will be commited.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In this article we'll cover the basic set-up required to integrate &lt;a href="https://typicode.github.io/husky/#/" rel="noopener noreferrer"&gt;husky&lt;/a&gt; into your Angular Application.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Husky improves your commits and more 🐶 woof!&lt;br&gt;
You can use it to lint your commit messages, run tests, lint code, etc... when you commit or push. Husky supports all Git hooks.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now, Let's get started&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First install husky&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;npm install husky --save-dev&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This command will add husky to your packahe.json file.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; enable Git Hook:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;npx husky-init&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;this will add following script to you package.json file&lt;br&gt;
 "prepare": "husky install"&lt;/p&gt;

&lt;p&gt;and it will also create a .husky folder in your root directory.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Next we'll be creating a file in the Husky directory for the purpose of linting commit messages&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;npx husky add .husky/commit-msg 'npx --no-install commitlint --edit'&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;this will create a file called commit-msg in .husky directory with the following data:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx --no-install commitlint --edit


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Now we'll install commit lint&lt;br&gt;
&lt;code&gt;npm install @commitlint/config-conventional @commitlint/cli --dev&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;now create a file commitlint.config.js in the root directory with the following data&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Now when you commit you changes using &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;git commit -m 'new fixes'&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;it will give the following error, if its not aligned with the git commit conventions (&lt;a href="https://www.conventionalcommits.org/en/v1.0.0-beta.2/#why-use-conventional-commits" rel="noopener noreferrer"&gt;git conventions&lt;/a&gt;)&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr7iioghiw7xrirob1d34.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr7iioghiw7xrirob1d34.png" alt="without convention commit"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;again perform the commit with 
&lt;code&gt;git commit -m 'style(package): package upgrade'&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6ikj1qppmqaoi4v8fwo5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6ikj1qppmqaoi4v8fwo5.png" alt="commit following git commit convention"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Linting before commit using Husky
&lt;/h2&gt;

&lt;p&gt;Install following package:&lt;br&gt;
&lt;code&gt;npm install lint-staged --save-dev&lt;br&gt;
npm install eslint&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add linting to the application
&lt;a href="https://github.com/angular-eslint/angular-eslint" rel="noopener noreferrer"&gt;https://github.com/angular-eslint/angular-eslint&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;ng add @angular-eslint/schematics&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ng g @angular-eslint/schematics:convert-tslint-to-eslint --remove-tslint-if-no-more-tslint-targets --ignore-existing-tslint-config&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;this will migrate the default tslist to esLint&lt;/p&gt;

&lt;p&gt;Now add the following content to the pre-commit file .husky/pre-commit&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx pretty-quick --staged
npx lint-staged
ng test --watch=false --browsers=ChromeHeadless



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;Now when you run the following git commit command it will run the &lt;strong&gt;pre-commit&lt;/strong&gt; script which you just defined.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;git commit -m 'style(package): package upgrade'&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It will run pretty-quick after that it will check lint and after that it will run test case for the project and if successful it will check for the commit message.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzxgmp5tp2q7lgj48gu4h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzxgmp5tp2q7lgj48gu4h.png" alt="running git commit"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Integrating SonarQube
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Install SonarQube in your system:&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;For Mac Users:&lt;br&gt;
&lt;code&gt;https://techblost.com/how-to-setup-sonarqube-locally-on-mac/&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;For Windows Users:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Install SonarQube form &lt;a href="https://www.sonarqube.org/downloads/" rel="noopener noreferrer"&gt;https://www.sonarqube.org/downloads/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Post download extracts the ZIP file to any folder and then run the StartSonar.bat from sonarqube\bin\windows-x86–64.
Now you can access the SonarQube from your browser using the default port &lt;a href="http://localhost:9000" rel="noopener noreferrer"&gt;http://localhost:9000&lt;/a&gt;. You can use the default credentials i.e admin and admin.&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvaii3ulb6d5rokzm2hck.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvaii3ulb6d5rokzm2hck.png" alt="SonarQube login window"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fntduvog71z47i9h19rhc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fntduvog71z47i9h19rhc.png" alt="SonarQube after login"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Configure Sonar with Angular
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;npm install sonar-scanner --save-dev&lt;/code&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a file name &lt;strong&gt;sonar-project.properties&lt;/strong&gt; in the root directory with the following data:&lt;/li&gt;
&lt;/ul&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Now, add following script in your package.json file:&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

"sonar": "sonar-scanner"


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;Now, run the following script using &lt;code&gt;npm run sonar&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If everything goes as planned SonarQube will start inspecting your angular project &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;On Successful completion you can navigate to &lt;code&gt;http://localhost:9000/projects&lt;/code&gt; and see the inspection result done on the project.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft3t9ct5rpl3swioncjib.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft3t9ct5rpl3swioncjib.png" alt="inspection result"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Final - Integrating Sonarqube with husky
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Add the following in your pre-commit file lazyloading/.husky/pre-commit&lt;/li&gt;
&lt;/ul&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Now run &lt;code&gt;git commit -m 'feat(sonar): sonarqube integrated'&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9xx0f66ifbrvqkvnbkr7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9xx0f66ifbrvqkvnbkr7.png" alt="SonarQube with husky"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's all, Congratulations🥳🥳  you have successfully integrated Husky with Es-Lint, commitlint and SonarQube with your commit workflow.&lt;/p&gt;

&lt;p&gt;You can check the sample code in &lt;a href="https://github.com/vishnuramero/lazyloading" rel="noopener noreferrer"&gt;https://github.com/vishnuramero/lazyloading&lt;/a&gt;&lt;/p&gt;

</description>
      <category>angular</category>
      <category>commit</category>
      <category>webdev</category>
      <category>productivity</category>
    </item>
  </channel>
</rss>
