From e7cc91c6ac27eba47ca2113835eaed25f6e99f83 Mon Sep 17 00:00:00 2001 From: Marc Cataford Date: Fri, 14 Apr 2023 09:36:20 -0400 Subject: [PATCH] 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 --- .github/workflows/nodejs.yml | 10 ++++++++++ script/lint-commits | 38 ++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 script/lint-commits diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index 6478b8f..d6dbc17 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -13,6 +13,16 @@ jobs: - uses: actions/checkout@v3 - 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: runs-on: ubuntu-latest diff --git a/script/lint-commits b/script/lint-commits new file mode 100644 index 0000000..1d4679e --- /dev/null +++ b/script/lint-commits @@ -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: +# +# : +# +# where 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