GsActions / commit-message-checker

GitHub Action that checks commit messages of pushes and pull request against a regex pattern
MIT License
99 stars 56 forks source link

Check only the most recent commit message #53

Closed bbchase closed 2 years ago

bbchase commented 2 years ago

I would like to be able to check for a pattern only in the most-recent commit message. Our use case is checking for the result of a test that I want to make sure has passed before a PR can be merged. e.g., Does the commit message contain "Test Successful"?

Some commits in a PR may not contain a test result or may contain an invalid test result, but I only care that the test was run and succeeds as of the latest commit.

I don't want to put the test result in the PR description because a commit made while the PR is open could cause my test to fail, but the GHA check would still pass because of the old message in the PR description.

gilbertsoft commented 2 years ago

In the first place I'd suggest to create a dedicated test action instead. For me it looks you mix up various jobs here which I really would not suggest to do.

Have a look at my workflows here, there is a clear separation of CI and pull request handling https://github.com/GsTYPO3/core-patches/tree/main/.github/workflows

PR related action: https://github.com/GsTYPO3/core-patches/actions/runs/1818090001 CI related action: https://github.com/GsTYPO3/core-patches/actions/runs/1818090153

bbchase commented 2 years ago

The test I'm referring to is run in local dev environment and devs paste the result into their commit. We want to use this check to make sure that the most-recent commit has passed this test before approving the PR.

gilbertsoft commented 2 years ago

@bbchase You should try it by set excludeDescription, excludeTitle and checkAllCommitMessages to true. This should work for your case I'd say. Then check the end of the message.

See also https://github.com/GsActions/commit-message-checker#configuration.

bbchase commented 2 years ago

I have tried that. In that case, all commits associated with a PR are checked and if any commit does not contain the test message for whatever reason, the check will fail. I really only care that the latest commit contains this message.

Here's the contents of the yml file if it helps:

name: 'Commit Message Check'
on:
  pull_request:
    types:
      - opened
      - edited
      - reopened
      - synchronize
  pull_request_target:
    types:
      - opened
      - edited
      - reopened
      - synchronize
  push:
    branches:
      - main

jobs:
  check-commit-message:
    name: Look for Test Suite message
    runs-on: ubuntu-latest
    steps:
      - name: Check Commit Type
        uses: gsactions/commit-message-checker@v1
        with:
          pattern: '^Successful Full Test Suite Run at \d\d\d\d+-\d\d-\d\d \d\d:\d\d:\d\d UTC$'
          error: 'The commit message must include a successful full test suite run.'
          excludeTitle: true
          excludeDescription: true
          checkAllCommitMessages: true
          accessToken: ${{ secrets.GITHUB_TOKEN }}
gilbertsoft commented 2 years ago

The problem here is your pattern, which tests for the start. Try this one Successful Full Test Suite Run at \d\d\d\d+-\d\d-\d\d \d\d:\d\d:\d\d UTC$ which will only take care about the end.

If this does still not work, enable debugging see https://docs.github.com/en/actions/monitoring-and-troubleshooting-workflows/enabling-debug-logging. Then you will see the message which is built and you can use tools like https://regexr.com/ afterwards to find a working pattern.

And if you enforce pull request, it does not make much sense to run the action for the main branch btw.