Validate format of GitHub "Squash and Merge" commit messages
Asked Answered
Q

1

11

I want to set up a GitHub project to use conventional-changelog standard version to generate its changelog. We use a squash and merge workflow to merge PRs into our main development branch.

Is there a way that I can enforce that the commit message of the squashed commit follows a specific format?

Ideally this would work nicely with the GitHub UI, such as showing as a failing check. I realize I could simply write a browser extension to enforce this, but then everyone working on the project would need to install the extension which is too high of a barrier to entry for an open source project.

Quintessence answered 2/2, 2017 at 20:38 Comment(1)
May be use some kind of git hook ?Inductee
P
7

It's not clear if you're intending for this to be on GitHub Public or Enterprise. I haven't tried to implement this on GitHub Public.

On Enterprise the process is to create a pre-receive hook for the specific repo you're interested in protecting. Keep in mind, unless you're willing to upload a new environment, you'll be limited in the tools that your pre-receive hook can use.

The best details can be found at: https://help.github.com/enterprise/2.11/admin/guides/developer-workflow/creating-a-pre-receive-hook-script/

Essentially the flow will be looking something like:

#!/usr/bin/env bash
#
DEFAULT_BRANCH=$(git symbolic-ref HEAD)

while read oldrev newrev refname; do
  # We only care about these changes to master
  if [[ "${refname}" != "${DEFAULT_BRANCH:=refs/heads/master}" ]]; then
    exit 0
  fi

  # branch or tag got deleted
  if [ "${newrev}" = "${zero_commit}" ]; then
    continue
  fi

  # branch or tag is being created
  if [ "${oldrev}" = "${zero_commit}" ]; then
    continue
  fi

  for COMMIT_HASH in `git rev-list ${oldrev}..${newrev}`;
  do
    # Add your COMMIT_HASH scrubbing process here
    # On failure exit 1
  done
done
exit 0
Palaeontology answered 8/12, 2017 at 1:52 Comment(2)
I also need this on the public GitHub, do you know if it is possible?Dorcasdorcea
unfortunately not :/ only github enterprise supports custom per-receive hooks https://mcmap.net/q/13146/-does-github-allow-pre-receive-hooksAllerus

© 2022 - 2024 — McMap. All rights reserved.