I have a Github repository, installed commitlint and husky locally and would like to setup a workflow running commitlint on every commit of a push when validating pull requests. On the main branch older commits are not following the conventional commit rules.
I created a separate branch, based on this comment
https://github.com/conventional-changelog/commitlint/issues/586#issuecomment-657226800
I started with this workflow
name: Run commitlint on pull request
on: pull_request
jobs:
run-commitlint-on-pull-request:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Setup Node
uses: actions/setup-node@v2
with:
node-version: 14.x
- name: Install dependencies
run: npm install
- name: Validate all commits from PR
run: npx commitlint --from HEAD~${{ github.event.pull_request.commits }} --to HEAD --verbose
I made two more commits following the conventional commit rules and started a pull request
The first solution coming to my mind would be to rebase everything and rename each commit to follow the rules but this would require a huge effort.
I'm not sure if I have to improve this line here
npx commitlint --from HEAD~${{ github.event.pull_request.commits }} --to HEAD --verbose
to check commits from the PR only (unfortunately I don't know what needs to get fixed there).
Do you have any ideas or is rebasing and renaming the only solution?
Typically pushing and pulling a few times a day is sufficient. Like @earlonrails said, more frequent pushes means less likelihood of conflicting changes but typically it isn't that big a deal. Think of it this way, by committing to your local repository you are basically saying "I trust this code. It is complete.
Commitlint is a simple tool that lints your commit messages and makes sure they follow a set of rules. It runs as a husky pre-commit hook, that is, it runs before the code is committed and blocks the commit in case it fails the lint checks.
The easiest way to create a Git commit with a message is to execute “git commit” with the “-m” option followed by your commit message. When using the Git CLI, note that you should restrict your commit message in order for it not to be wrapped.
Using git commit in GitHub Actions GitHub Actions provides full access to the runner at your disposal, and one thing you may want to do is make commits in a workflow run and push it back up to GitHub automatically.
We need to make a new folder called .github and then a new folder in it called workflows. Then we can add a file called commitlint.yml and add the workflow configuration.
It runs as a husky pre-commit hook, that is, it runs before the code is committed and blocks the commit in case it fails the lint checks. In this example, we are going to see how we can set up commitlint in a simple JavaScript project. To get started, let's create an empty project first:
Each workflow run will use the version of the workflow that is present in the associated commit SHA or Git ref of the event. When a workflow runs, GitHub sets the GITHUB_SHA (commit SHA) and GITHUB_REF (Git ref) environment variables in the runner environment.
The straight-forward solution is to use the --to
and --from
arguments of commitlint with the SHA-1 values instead of the branch names or relative references. On the one hand, this reliably solves the problem of unknown revisions or paths in the working tree. On the other hand, only commits in scope of the PR will be checked. As a sidenote: GitHub uses the same references (SHA-1) for the ad-hoc merge that is being checked-out.
We need the base-SHA as well as the head-SHA. In a GitHub action those values are available in the pull-request object of the event in the github
-context.
Therefore, you can use the following line which is tested and works as expected:
npx commitlint --from ${{ github.event.pull_request.base.sha }} --to ${{ github.event.pull_request.head.sha }} --verbose
Here is a POC repository on GitHub with 3 test cases (pull-requests).
name: Run Commitlint on PR
on:
pull_request:
jobs:
run-commitlint-on-pr:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Setup Node
uses: actions/setup-node@v2
with:
node-version: 14.x
- name: Install dependencies
run: npm install
- name: Validate all commits from PR
run: npx commitlint --from ${{ github.event.pull_request.base.sha }} --to ${{ github.event.pull_request.head.sha }} --verbose
git rev-list
is considered because the commit of the pull request (PR) seems invalid. No loop should be required.
This issue hints to checkout the PR branch which seems simpler than fetching the PR commits. From the question, testing on default branch does not seem required.
- uses: actions/checkout@v2
with:
fetch-depth: 0
ref: ${{ github.event.pull_request.base.sha }}
The lint instruction would be:
npx commitlint --from ${{ github.event.pull_request.base.sha }} --verbose
Documentation of pull-request payload does not offer the list of commits right away.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With