DEV Community

Brian Vu
Brian Vu

Posted on • Updated on

Linting PR commit messages with Github Actions

Conventional Commits 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.

What is a conventional commit message?

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.

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 feat type to indicate that you added a new feature, or the fix type to indicate that you fixed a bug.

Some examples:

 feat: add a new blog post
 fix: fix a typo in the README file
 chore: update dependencies
Enter fullscreen mode Exit fullscreen mode

Why should I lint my commit messages?

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. 😎

How does it help with the release process?

When you use Conventional Commits, you can use tools like semantic-release. 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 feat: add a new blog post, semantic-release will know that a new feature was added and will bump the minor version number.

Creating a commit action to lint PRs

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

name: PR Lint

on:
  pull_request:
    types: [opened, edited, reopened, synchronize, ready_for_review]
    branches: [main]

  workflow_dispatch:

jobs:
  pr-lint:
    name: Validate PR commit title meets commit convention
    runs-on: ubuntu-latest
    steps:
      - uses: amannn/action-semantic-pull-request@v4.5.0
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          validateSingleCommit: true
          validateSingleCommitMatchesPrTitle: true
Enter fullscreen mode Exit fullscreen mode

We are using the amannn/action-semantic-pull-request 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:

  • The validateSingleCommit option ensures that the PR contains only one commit.
  • The validateSingleCommitMatchesPrTitle option ensures that the commit message matches the PR title.

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.

That's it!

Top comments (0)