-
Notifications
You must be signed in to change notification settings - Fork 3
58 lines (48 loc) · 1.8 KB
/
Copy pathcommit-message-check.yml
File metadata and controls
58 lines (48 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
name: Validate Commit Messages
permissions:
contents: read
on:
pull_request:
jobs:
validate-commits:
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Check commit message format
run: |
BASE_SHA=${{ github.event.pull_request.base.sha }}
HEAD_SHA=${{ github.event.pull_request.head.sha }}
ALLOWED_TYPES="feat|fix|refs|doc|docs|perf|refactor|style|test|chore|ci|revert"
SCOPE_PATTERN="([a-z][a-z0-9-]*)"
TICKET_PATTERN="#[0-9]+(, #[0-9]+)*:"
FULL_PATTERN="^($ALLOWED_TYPES)(\($SCOPE_PATTERN\))? $TICKET_PATTERN.+$"
# Validate the format of all new submissions
for commit in $(git rev-list $BASE_SHA..$HEAD_SHA); do
msg=$(git show -s --format=%s $commit)
# skip merge
if [[ "$msg" =~ ^Merge ]]; then
continue
fi
# skip cargo release auto-generated commits
if [[ "$msg" =~ ^chore:\ Release ]]; then
continue
fi
# Basic format check regular expression
if ! echo "$msg" | grep -qE "$FULL_PATTERN"; then
echo "::error::Invalid commit format: $msg"
echo "Commit must follow: <type>(<scope>) #<ticket>: <description>"
echo "Allowed types: $ALLOWED_TYPES"
echo "Scope: lowercase with hyphen (optional)"
exit 1
fi
# Verify that non-chore/ci commits contain tickets
if ! echo "$msg" | grep -qE "$TICKET_PATTERN"; then
echo "::error::Ticket number missing: $msg"
echo "All non-merge commits must reference a ticket"
exit 1
fi
done