-1

I want Github to reject all Git changes if any commit has an invalid message. Each message should follow the following pattern

<type> <issue>: <message>

e.g.

feat #28: Support multiple file upload

where

  • type should be an existing type from a predefined list (feat, fix, ci, ...)
  • issue should be an existing issue number (if that's even possible to check)
  • message should be between 1 and 100 characters long

I know about Git hooks running locally. So I could prevent invalid commits configuring a pre-commit hook. But one could simply delete that hook locally, force the commit and push that code to Github. Of course I already configured protected branches so the "attacker" is just able to corrupt his own branches. But I still have to check the messages in the pull requests on my own.

It would be nice to configure Github to reject every change with a smart error message

  • abc #28: message goes here gets rejected with the message

Rejected because 'abc' is not a valid type

  • feat #99999: message goes here gets rejected with the message

Rejected because the issue doesn't exist

  • feat #28: gets rejected with the message

Rejected because the message is empty

Does someone know how to configure such a "security" feature if that's even possible?

Imagine many people are working on the project and everyone should follow the rules. Commit messages should have a common style so it's easy to read the commit history. I know that only maintainers can directly push to the repo but one might "forget" the rules and create a push containing commits violating the rules.

Question3r
  • 2,166
  • 19
  • 100
  • 200
  • 2
    FWIW these kind of rules are SUPER annoying. E.g., think what happens when a commit titled `Merge remote-tracking branch 'origin/master' into my-feature-branch` gets rejected. What is the reason to want to implement such a rule? (please edit the question to address). – AD7six Mar 28 '21 at 20:27
  • You'd need Enterprise to run a pre-receive hook on GitHub: https://stackoverflow.com/q/10864903/3001761 – jonrsharpe Mar 28 '21 at 20:30
  • @AD7six that's a good question ... I didn't thought about it. I updated my question – Question3r Apr 03 '21 at 20:45
  • I suggest to abandon the specifics of the original question and instead use [squash and merge PR commits](https://docs.github.com/en/github/collaborating-with-issues-and-pull-requests/about-pull-request-merges#squash-and-merge-your-pull-request-commits) coupled with a PR title/description convention (such as: a PR MUST reference an issue to be approved and merged). The history of master will adhere to the intent of what is outlined in the question without the (significant, IMO) downsides of a pre-receive approach. – AD7six Apr 04 '21 at 16:28

3 Answers3

1

GitHub Apps are the recommended way by GitHub.

GitHub Apps are the first-class citizens in GitHUb ecosystem.

You can build a GitHub App to handle your specific use cases. GitHub provides a wealth of [APIs] (https://docs.github.com/en/developers/apps/using-the-github-api-in-your-app) to handle most of the scenarios. One of the easiest way to build a GitHUb App is using any existing well known frameworks like PROBOT, which is a nodejs based framework and is very easy to use.

For your case you would basically need to:

type: check it programmatically against already stored list of all possible valid types

issue : Get an issue If API returns response of 404 Not Found that definitely means issue does not exist.

message : Use the List commits on a pull request , get the commit messages and implement the commit message checking logic in your code.

Asif Kamran Malick
  • 2,409
  • 3
  • 25
  • 47
  • thank you very much for this! unfortunately I'm not even sure if I should validate those messages as mentioned in this comment https://stackoverflow.com/questions/66845857/how-to-configure-github-repository-to-reject-changes-if-any-commit-has-an-invali#comment118160763_66845857 – Question3r Apr 03 '21 at 20:41
0

You can do this in a CI job. You can automate this with a script like this:

#!/bin/sh

echo "Checking commit messages..."
if git log --format="%s" BASE...HEAD | grep -vE '^(feat|fix|ci) #[0-9]+: .{1,100}'
then
    exit 1
fi

You'll need to specify BASE and HEAD, and then the script will exit zero if all commits are okay and nonzero if they're not, printing the problematic ones. If you want to do more checking, such as for a valid issue, then you'll need to add such a thing, but this should get you started.

bk2204
  • 64,793
  • 6
  • 84
  • 100
0

If you are OK with adjusting your formatting requirements to follow the one in the conventionalcommits.org then you can add an existing check to your protected branches: enter image description here

Leo Y
  • 659
  • 7
  • 22