|
| 1 | +name: "Conventional Commit Message Check" |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - '**' |
| 7 | + pull_request: |
| 8 | + types: [opened, synchronize, reopened] |
| 9 | + |
| 10 | +jobs: |
| 11 | + commit-message-lint: |
| 12 | + runs-on: ubuntu-latest |
| 13 | + steps: |
| 14 | + - name: Checkout |
| 15 | + uses: actions/checkout@v4 |
| 16 | + with: |
| 17 | + fetch-depth: 0 |
| 18 | + |
| 19 | + - name: Lint commit messages |
| 20 | + run: | |
| 21 | + set -euo pipefail |
| 22 | + echo "Event: ${{ github.event_name }}" |
| 23 | +
|
| 24 | + if [ "${{ github.event_name }}" = "pull_request" ]; then |
| 25 | + range="${{ github.event.pull_request.base.sha }}..${{ github.event.pull_request.head.sha }}" |
| 26 | + else |
| 27 | + before="${{ github.event.before }}" |
| 28 | + sha="${{ github.sha }}" |
| 29 | + if [ "$before" = "0000000000000000000000000000000000000000" ]; then |
| 30 | + # new branch or initial push, check last commit only |
| 31 | + range="$sha^..$sha" |
| 32 | + else |
| 33 | + range="$before..$sha" |
| 34 | + fi |
| 35 | + fi |
| 36 | +
|
| 37 | + echo "Commit range: $range" |
| 38 | + # list commit subjects, skip merge commits |
| 39 | + commits=$(git log --no-merges --pretty=format:%s "$range" || true) |
| 40 | + if [ -z "$commits" ]; then |
| 41 | + echo "No commits found to lint." |
| 42 | + exit 0 |
| 43 | + fi |
| 44 | +
|
| 45 | + failed=0 |
| 46 | + while IFS= read -r subject; do |
| 47 | + [ -z "$subject" ] && continue |
| 48 | + # allow and skip merge commits if any slipped through |
| 49 | + if echo "$subject" | grep -q -i "^Merge "; then |
| 50 | + echo "Skipping merge commit: $subject" |
| 51 | + continue |
| 52 | + fi |
| 53 | +
|
| 54 | + if ! echo "$subject" | grep -E -q '^(revert: )?(feat|fix|docs|style|refactor|perf|test|build|ci|chore)(\([a-zA-Z0-9_.\/-]+\))?(!)?: .+'; then |
| 55 | + echo "::error ::Conventional commit validation failed for: $subject" |
| 56 | + failed=1 |
| 57 | + else |
| 58 | + echo "OK: $subject" |
| 59 | + fi |
| 60 | + done <<< "$commits" |
| 61 | +
|
| 62 | + if [ $failed -ne 0 ]; then |
| 63 | + echo "One or more commits do not follow Conventional Commits." |
| 64 | + exit 1 |
| 65 | + fi |
| 66 | +
|
0 commit comments