build: commit linting in ci (#21)

* build: commit-linting utility

* ci: add commit-lint to ci

* refactor: better messaging on failed linting

* wip: commit grabbing

* ci: fetch depth when linting commits

* ci: fetch depth when linting commits

* ci: checkout?

* ci: checkout?

* ci: checkout?

* fix: amend regexp

* fix: pattern matching in commit lint

* fix: pattern matching in commit lint

* docs: document lint-commit script
This commit is contained in:
Marc 2023-04-14 09:36:20 -04:00
parent 4343402a04
commit e7cc91c6ac
Signed by: marc
GPG key ID: 048E042F22B5DC79
2 changed files with 48 additions and 0 deletions

View file

@ -13,6 +13,16 @@ jobs:
- uses: actions/checkout@v3 - uses: actions/checkout@v3
- run: . ./script/check-deps - run: . ./script/check-deps
commit-lint:
name: Commit message hygiene
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- run: git fetch
- run: . ./script/lint-commits
dependencies: dependencies:
runs-on: ubuntu-latest runs-on: ubuntu-latest

38
script/lint-commits Normal file
View file

@ -0,0 +1,38 @@
#!/usr/bin/bash
#
# This loosely implements the commit message linting check described by
# Conventional Commit (https://www.conventionalcommits.org). Commits merged in
# branches on this repository are expected to meet that general format:
#
# <prefix>: <message>
#
# where <prefix> is one of the prefixes included in the filters below.
#
CURRENT_HEAD=$(git rev-parse HEAD)
BASE_HEAD=$(git log origin/main..$CURRENT_HEAD --oneline --format=%H | tail -1)
COMMITS=$(git log $BASE_HEAD..$CURRENT_HEAD --oneline --format=%s)
if [[ -z "$COMMITS" ]]; then
echo "No commits."
exit 0
fi
INVALID_COMMITS_FOUND=0
while IFS='\n' read -r line; do
if [[ $line =~ ^"Merge "[0-9a-z]+" into "[0-9a-z]+$ ]]; then
continue
elif [[ ($line =~ ^(feat|chore|fix|refactor|wip|docs|build|ci|perf):) ]]; then
echo "VALID - $line"
else
echo "INVALID - $line"
INVALID_COMMITS_FOUND=$((INVALID_COMMITS_FOUND+1))
fi;
done <<< "$COMMITS"
if [[ $INVALID_COMMITS_FOUND -gt 0 ]]; then
echo "$INVALID_COMMITS_FOUND bad commit messages found."
exit 1
fi