<?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: Brian Vu</title>
    <description>The latest articles on DEV Community by Brian Vu (@brianvu).</description>
    <link>https://dev.to/brianvu</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%2F626311%2Ffcfc17f0-15b0-4415-91db-3d495bdbaecc.jpeg</url>
      <title>DEV Community: Brian Vu</title>
      <link>https://dev.to/brianvu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/brianvu"/>
    <language>en</language>
    <item>
      <title>Linting PR commit messages with Github Actions</title>
      <dc:creator>Brian Vu</dc:creator>
      <pubDate>Sat, 03 Sep 2022 06:39:43 +0000</pubDate>
      <link>https://dev.to/brianvu/linting-commit-messages-with-github-actions-388c</link>
      <guid>https://dev.to/brianvu/linting-commit-messages-with-github-actions-388c</guid>
      <description>&lt;p&gt;&lt;a href="https://www.conventionalcommits.org/en/v1.0.0/"&gt;Conventional Commits&lt;/a&gt; is a great new thing that helps to keep your commit messages clean and consistent. It also helps to automate the release process. But how do you enforce it? This is where Github Actions comes in. In this post, I will show you how to use Github Actions to lint your commit messages.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a conventional commit message?
&lt;/h2&gt;

&lt;p&gt;A commit message is a short description of the changes you made in a commit. It is usually written imperitively, present tense, and should be less than 50 characters long. For example, “Add a new blog post” is a good commit message, while “Added stuff" is not.&lt;/p&gt;

&lt;p&gt;A conventional commit takes things a step further by providing specific rules for writing commit messages. It also provides a list of commit types that you can use to categorize your commits. For example, you can use the &lt;code&gt;feat&lt;/code&gt; type to indicate that you added a new feature, or the &lt;code&gt;fix&lt;/code&gt; type to indicate that you fixed a bug.&lt;/p&gt;

&lt;p&gt;Some examples:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; feat: add a new blog post
 fix: fix a typo in the README file
 chore: update dependencies
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Why should I lint my commit messages?
&lt;/h2&gt;

&lt;p&gt;Linting your commit messages is a great way to enforce the Conventional Commits specification. It helps to keep your commit messages clean and consistent. It also helps to automate the release process. 😎&lt;/p&gt;

&lt;h2&gt;
  
  
  How does it help with the release process?
&lt;/h2&gt;

&lt;p&gt;When you use Conventional Commits, you can use tools like &lt;a href="https://github.com/semantic-release/semantic-release"&gt;semantic-release&lt;/a&gt;. This is a tool that automatically generates release notes and publishes a new version of your package to npm. It uses the commit messages to determine the type of changes that were made in the new version. For example, if the last commit message was &lt;code&gt;feat: add a new blog post&lt;/code&gt;, semantic-release will know that a new feature was added and will bump the minor version number.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a commit action to lint PRs
&lt;/h2&gt;

&lt;p&gt;Let's first create a Github action that automatically checks if PR messages adhere to the commit convention.&lt;br&gt;
First, create a new file called &lt;code&gt;commit-lint.yml&lt;/code&gt; in the &lt;code&gt;.github/workflows&lt;/code&gt; directory. Here's an example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;PR Lint&lt;/span&gt;

&lt;span class="na"&gt;on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;pull_request&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;types&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;opened&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;edited&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;reopened&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;synchronize&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;ready_for_review&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;
    &lt;span class="na"&gt;branches&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;main&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;

  &lt;span class="na"&gt;workflow_dispatch&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;

&lt;span class="na"&gt;jobs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;pr-lint&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Validate PR commit title meets commit convention&lt;/span&gt;
    &lt;span class="na"&gt;runs-on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ubuntu-latest&lt;/span&gt;
    &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;amannn/action-semantic-pull-request@v4.5.0&lt;/span&gt;
        &lt;span class="na"&gt;env&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;GITHUB_TOKEN&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ secrets.GITHUB_TOKEN }}&lt;/span&gt;
        &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;validateSingleCommit&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;
          &lt;span class="na"&gt;validateSingleCommitMatchesPrTitle&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are using the &lt;code&gt;amannn/action-semantic-pull-request&lt;/code&gt; action to lint our commit messages. It is a Github action that validates pull request titles against the Conventional Commits specification. It requires a Github token to be set up first. Also, there are a few options that we can set:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;validateSingleCommit&lt;/code&gt; option ensures that the PR contains only one commit.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;validateSingleCommitMatchesPrTitle&lt;/code&gt; option ensures that the commit message matches the PR title.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's it! Now, whenever you open a PR, the action will check if the commit message adheres to the commit convention. If it doesn't, the action will fail until the message is fixed.&lt;/p&gt;

&lt;p&gt;That's it!&lt;/p&gt;

</description>
      <category>devops</category>
      <category>workflows</category>
      <category>githubactions</category>
    </item>
  </channel>
</rss>
