compilerla / conventional-pre-commit

A pre-commit hook that checks commit messages for Conventional Commits formatting
Apache License 2.0
348 stars 59 forks source link

Support for CI integration #41

Closed johbo closed 1 year ago

johbo commented 1 year ago

I've just worked myself through a question how to best integrate the checks into a ci pipeline. Would like to share my experience from the approach and also trigger a question if it's possible to implement support for such use cases.

In our team the context is as follows:

While building this (in our case based on gitlab ci) I found it a particular challenge that I need to extract every commit message manually and then run pre-commit to have the message checked. I've reached a working state for us, and still I wonder if it wouldn't be a good improvement to be able to run a check by passing the hashes of a starting and ending commit.

As pseudo code it could look something like the following:

pre-commit --from-ref=XY --tro-ref=YZ --hook-stage=commit-msg

The below example shows how I've done it currently and probably helps to understand how I got to the question/suggestion:

      any_failure=0
      tmp_file="$(mktemp --tmpdir=.)"
      for x in $(git log --format="%H" "${CI_MERGE_REQUEST_DIFF_BASE_SHA}..${CI_COMMIT_SHA}")
      do
          echo "Checking commit message for $x"
          git log --format="%B" -n 1 "$x" > "$tmp_file"
          pre-commit run --hook-stage=commit-msg --commit-msg-filename="$tmp_file" \
              conventional-pre-commit || any_failure=1
      done
      rm "$tmp_file"
      if [ "$any_failure" -ne 0 ]
      then
          exit 1
      fi
thekaveman commented 1 year ago

@johbo Thank you for the question. Indeed, your use-case is one that has been discussed here before: https://github.com/compilerla/conventional-pre-commit/issues/9#issuecomment-966490052. Please take a look at that thread for more context.

If I am understanding this proposal / question correctly:

I wonder if it wouldn't be a good improvement to be able to run a check by passing the hashes of a starting and ending commit

(and based on the pseudocode), it seems like the change you request would actually be in pre-commit itself, to accept a range of commits to run (arbitrary hooks/stages) against?

The TLDR; answer for here is we won't be making any changes to conventional-pre-commit to support CI, because this is fundamentally incompatible with the git commit-msg hook/phase.

EDIT to add:

I'll also point out that conventional-pre-commit is a normal Python package that can be installed from pip and used in Python code. Perhaps this provides an easier way to accomplish some of what you are trying to do. See the example code in the README.