Skip to content

Commit

Permalink
ci: check that commit messages follow Conventional Commits format
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardosm committed Apr 26, 2024
1 parent dce389f commit b107762
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/convetional-commits.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: Conventional Commits

on:
pull_request:

jobs:
check-conventional-commits:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- run: ./ci/check-conventional-commits.sh "${{ github.event.pull_request.base.sha }}" "${{ github.event.pull_request.head.sha }}"
26 changes: 26 additions & 0 deletions ci/check-conventional-commits.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env bash
set -euo pipefail

if [ $# -ne 2 ]; then
echo "Usage: $0 <base_ref> <head_ref>"
exit 1
fi

commits="$(git rev-list --reverse "^$1" "$2")"
mapfile -t commits <<< "$commits"

lf=$'\n'
cr=$'\r'
exclam='!'
types="build|change|chore|ci|doc|feat|fix|perf|refactor|revert|style|test"
regex="^($types)(\([^$lf$cr]+\))?$exclam?: .+\$"

echo "Checking ${#commits[@]} commit(s)"
for commit in "${commits[@]}"; do
echo "Checking commit $commit"
commit_msg="$(git log --format=%B -n 1 "$commit")"
if [[ ! "$commit_msg" =~ $regex ]]; then
echo "Commit $commit does not follow Conventional Commits format"
exit 1
fi
done

0 comments on commit b107762

Please sign in to comment.