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
- I expected the workflow wouldn't run because I doesn't exist on the main branch yet.
- Actually it runs
- I exptected the workflow to check PR commits only
- The workflow fails because it starts validating EVERY commit in the main branch. And since I know older commits don't follow the rules, this will never pass.
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?
npx commitlint --from $commit --to HEAD --verbose || exit 1
– Severen|| exit 1
didn't help. the workflow still passes – Johnnienpx commitlint
exit with error codes at all? – Severen--from $commit --to HEAD
is wrong, it should be one commit, something like--from $commit~ --to $commit
. Or instead of the loop test all commits at once:--from ${{ github.base_ref }} --to ${{ github.head_ref }}
without a loop. – Severennpx commitlint --from ${{ github.base_ref }} --to ${{ github.head_ref }} --verbose
outside from Github – Johnniegit rev-list
documentation suggests to use the three dots...
notation to list merges. This seems more in line with the purpose or alternativelygit rev-list main test --not $(git merge-base --all main test)
– Seldafor commit in $(git rev-list ${{ github.base_ref }} ${{ github.head_ref }} --not $(git merge-base --all ${{ github.base_ref }} ${{ github.head_ref }} )); do
– Johnnie