|
| 1 | +name: Conventional Commits |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - '**' |
| 7 | + pull_request: |
| 8 | + types: [opened, synchronize, reopened, edited] |
| 9 | + |
| 10 | +jobs: |
| 11 | + validate-commits: |
| 12 | + runs-on: ubuntu-latest |
| 13 | + steps: |
| 14 | + - name: Checkout |
| 15 | + uses: actions/checkout@v4 |
| 16 | + with: |
| 17 | + fetch-depth: 0 |
| 18 | + |
| 19 | + - name: Validate commit messages |
| 20 | + env: |
| 21 | + EVENT_NAME: ${{ github.event_name }} |
| 22 | + PR_BASE_SHA: ${{ github.event.pull_request.base.sha }} |
| 23 | + PR_HEAD_SHA: ${{ github.event.pull_request.head.sha }} |
| 24 | + PUSH_BEFORE_SHA: ${{ github.event.before }} |
| 25 | + PUSH_AFTER_SHA: ${{ github.sha }} |
| 26 | + run: | |
| 27 | + set -euo pipefail |
| 28 | +
|
| 29 | + pattern='^(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(\([a-z0-9._/-]+\))?!?: .+' |
| 30 | +
|
| 31 | + if [ "$EVENT_NAME" = "pull_request" ]; then |
| 32 | + BASE_SHA="$PR_BASE_SHA" |
| 33 | + HEAD_SHA="$PR_HEAD_SHA" |
| 34 | + else |
| 35 | + BASE_SHA="$PUSH_BEFORE_SHA" |
| 36 | + HEAD_SHA="$PUSH_AFTER_SHA" |
| 37 | + fi |
| 38 | +
|
| 39 | + # New branch first push has all-zero before SHA; validate only latest commit. |
| 40 | + if [ "$BASE_SHA" = "0000000000000000000000000000000000000000" ]; then |
| 41 | + RANGE="$HEAD_SHA^..$HEAD_SHA" |
| 42 | + else |
| 43 | + RANGE="$BASE_SHA..$HEAD_SHA" |
| 44 | + fi |
| 45 | +
|
| 46 | + invalid=0 |
| 47 | + while IFS= read -r subject; do |
| 48 | + [ -z "$subject" ] && continue |
| 49 | +
|
| 50 | + # Ignore merge commits in case the branch was merged/rebased with base. |
| 51 | + if [[ "$subject" =~ ^Merge\ ]]; then |
| 52 | + continue |
| 53 | + fi |
| 54 | +
|
| 55 | + if [[ ! "$subject" =~ $pattern ]]; then |
| 56 | + echo "Invalid commit message: $subject" |
| 57 | + invalid=1 |
| 58 | + fi |
| 59 | + done < <(git log --format=%s "$RANGE") |
| 60 | +
|
| 61 | + if [ "$invalid" -ne 0 ]; then |
| 62 | + echo "" |
| 63 | + echo "Expected Conventional Commits format, e.g.:" |
| 64 | + echo " feat(api): add public configs endpoint" |
| 65 | + echo " fix(ui): prevent fork button overflow" |
| 66 | + exit 1 |
| 67 | + fi |
| 68 | +
|
| 69 | + echo "All commit messages match Conventional Commits." |
0 commit comments