Videos
» pip install gitlint
» pip install commit-linter
Solution
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
Demo
Here is a POC repository on GitHub with 3 test cases (pull-requests).
Complete workflow
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.