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