-
Notifications
You must be signed in to change notification settings - Fork 9
169 lines (153 loc) · 6.18 KB
/
commit_message.yml
File metadata and controls
169 lines (153 loc) · 6.18 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
name: Latest Commit Message Validation
on:
pull_request:
branches:
- develop
- main
- 'release/**'
types:
- opened
- edited
- reopened
- synchronize
pull_request_target:
branches:
- develop
- main
- 'release/**'
types:
- opened
- edited
- reopened
- synchronize
push:
branches:
- develop
- main
- 'release/**'
jobs:
validate_commit_messages:
name: Validate Latest Commit Message
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Debug commit information
run: |
echo "=== DEBUG: Commit info ==="
if [ "${{ github.event_name }}" = "pull_request" ]; then
echo "PR event - checking latest commit only"
echo "Latest commit: ${{ github.event.pull_request.head.sha }}"
echo "Latest commit message:"
git log --format="%B" -n 1 ${{ github.event.pull_request.head.sha }}
else
echo "Push event - checking pushed commit"
echo "HEAD commit: $(git rev-parse HEAD)"
echo "HEAD commit message:"
git log --format="%B" -n 1 HEAD
fi
echo "=== End Debug ==="
- name: Validate commit messages with custom script
run: |
#!/bin/bash
set -e
echo "=== Custom Commit Message Validation ==="
# Enhanced validation for conventional commits
# Allow detailed commit bodies while enforcing subject line format
validate_commit() {
local commit_sha="$1"
local subject_line="$2"
# Check if this is a merge commit - these have different rules
local MERGE_PATTERN='^Merge (branch|pull request|remote-tracking branch)'
if echo "$subject_line" | grep -qE "$MERGE_PATTERN"; then
return 0 # Pass silently for merge commits
fi
# Pattern for conventional commit subject line
# - Type is required
# - Scope is optional
# - Description must be at least 5 chars, max 100 for entire subject line
# - Use imperative mood (no period at end)
# - Allow upper or lowercase start (for acronyms like CLI, API, etc.)
local SUBJECT_PATTERN='^(feat|fix|docs|style|refactor|perf|test|chore|build|ci)(\(.+\))?: [a-zA-Z].{4,}[^.]$'
# Check subject line length (entire line should be <= 100 chars)
if [ ${#subject_line} -gt 100 ]; then
echo "❌ COMMIT $commit_sha FAILED:"
echo " Subject: '$subject_line'"
echo " Error: Subject line too long (${#subject_line} chars, max 100)"
echo ""
return 1
fi
# Check subject line format
if echo "$subject_line" | grep -qE "$SUBJECT_PATTERN"; then
return 0 # Pass silently
else
echo "❌ COMMIT $commit_sha FAILED:"
echo " Subject: '$subject_line'"
echo " Error: Subject line does not match conventional commit format"
echo ""
return 1
fi
}
# Get commits to validate
if [ "${{ github.event_name }}" = "pull_request" ]; then
echo "Validating latest commit in PR: ${{ github.event.pull_request.head.sha }}"
echo ""
# Only validate the latest commit (HEAD of the PR branch)
commit_sha="${{ github.event.pull_request.head.sha }}"
commit_msg=$(git log --format="%s" -n 1 "$commit_sha")
echo "Latest commit: $commit_sha"
echo "Message: '$commit_msg'"
echo ""
if validate_commit "$commit_sha" "$commit_msg"; then
echo "✅ Latest commit message is valid!"
else
echo ""
echo "📋 Required Format: <type>(scope): <description>"
echo ""
echo "Optional detailed body and footer are allowed below the subject line."
echo "Merge commits (starting with 'Merge branch/pull request') are automatically valid."
echo ""
echo "🏷️ Valid Types:"
echo "• feat - New feature"
echo "• fix - Bug fix"
echo "• docs - Documentation changes"
echo "• style - Code style changes (formatting, etc.)"
echo "• refactor - Code refactoring"
echo "• perf - Performance improvements"
echo "• test - Adding or updating tests"
echo "• chore - Maintenance tasks"
echo "• build - Build system changes"
echo "• ci - CI/CD changes"
echo ""
echo "📏 Subject Line Requirements:"
echo "• Total length: ≤100 characters"
echo "• Description: ≥5 characters"
echo "• Start with letter after colon (upper/lowercase both OK)"
echo "• No trailing period"
echo "• Be descriptive and clear"
echo ""
echo "✅ Good Examples:"
echo " feat(cli): add configuration validation"
echo " fix(routing): resolve path calculation bug"
echo " refactor(gui): CLI integration for entry point"
echo " docs: update installation guide with new requirements"
echo ""
echo "💡 Tip: Only the latest commit in your PR is checked."
echo " Use 'git commit --amend' to fix the message, or"
echo " 'git rebase -i' to edit older commits if needed."
echo ""
exit 1
fi
else
# Push event - validate the pushed commit
echo "Validating push commit"
commit_sha=$(git rev-parse HEAD)
commit_msg=$(git log --format="%s" -n 1 HEAD)
if validate_commit "$commit_sha" "$commit_msg"; then
echo "✅ Commit message is valid!"
else
exit 1
fi
fi