-
Notifications
You must be signed in to change notification settings - Fork 0
351 lines (310 loc) · 14.6 KB
/
commitlint.yml
File metadata and controls
351 lines (310 loc) · 14.6 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
name: Conventional Commit Linter
on:
workflow_call:
inputs:
config-repo:
description: 'Repo containing commitlint.config.js'
required: false
type: string
default: 'ExtendRealityLtd/github-workflows.conventional-commit-linter'
config-ref:
description: 'Git ref of config repo (branch/tag/SHA)'
required: false
type: string
default: 'main'
permissions:
contents: read
pull-requests: write
jobs:
commitlint:
name: Validate Conventional Commit Messages
runs-on: ubuntu-latest
steps:
- name: Checkout target repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '18'
- name: Checkout config repository
uses: actions/checkout@v4
with:
repository: ${{ inputs.config-repo }}
ref: ${{ inputs.config-ref }}
path: .commitlint-config
sparse-checkout: |
commitlint.config.js
sparse-checkout-cone-mode: false
- name: Validate commits (build detailed PR comment)
id: validate
shell: bash
run: |
set -euo pipefail
export LANG=C.UTF-8
cp .commitlint-config/commitlint.config.js ./commitlint.config.js
# Generate a safely-quoted env.sh from config
cat > parse-config.js <<'EOF'
const fs = require('fs');
const cfg = require('./commitlint.config.js') || {};
const rules = cfg.rules || {};
const cr = cfg.customRules || {};
const m = cr.messages || {};
const typeEnum = (rules['type-enum'] && rules['type-enum'][2]) || ['feat','fix','chore','refactor'];
const maxHeader = (rules['header-max-length'] && rules['header-max-length'][2]) || 72;
const maxBodyLine = (rules['body-max-line-length'] && rules['body-max-line-length'][2]) || 72;
const maxFooterLine = (rules['footer-max-line-length'] && rules['footer-max-line-length'][2]) || 72;
const scopeRequired = (rules['scope-empty'] && rules['scope-empty'][1]) === 'never';
const subjectMin = (rules['subject-min-length'] && rules['subject-min-length'][2]) || 3;
const subjectCase = rules['subject-case'] && rules['subject-case'][2];
const subjectLower = subjectCase === 'lower-case';
const sf = rules['subject-full-stop'];
const noTrailing = Array.isArray(sf) && sf[1] === 'never' && sf[2] === '.';
const bodyLeadingBlank= (rules['body-leading-blank'] && rules['body-leading-blank'][1]) === 'always';
const bodyReqTypes = cr.bodyRequiredForTypes || [];
const enforceBreaking = cr.enforceBreakingChangeConsistency !== false;
const shQ = (s) => String(s ?? '').replace(/'/g, `'\"'\"'`);
const out = [];
out.push(`VALID_TYPES='${shQ(typeEnum.join('|'))}'`);
out.push(`MAX_HEADER_LENGTH='${shQ(maxHeader)}'`);
out.push(`MAX_BODY_LINE='${shQ(maxBodyLine)}'`);
out.push(`MAX_FOOTER_LINE='${shQ(maxFooterLine)}'`);
out.push(`SCOPE_REQUIRED='${shQ(scopeRequired)}'`);
out.push(`SUBJECT_MIN_LENGTH='${shQ(subjectMin)}'`);
out.push(`SUBJECT_LOWERCASE='${shQ(subjectLower)}'`);
out.push(`NO_TRAILING_PERIOD='${shQ(!!noTrailing)}'`);
out.push(`BODY_LEADING_BLANK='${shQ(bodyLeadingBlank)}'`);
out.push(`BODY_REQUIRED_TYPES='${shQ(bodyReqTypes.join('|'))}'`);
out.push(`ENFORCE_BREAKING_CHANGES='${shQ(enforceBreaking)}'`);
out.push(`FOOTER_COUNTS_AS_BODY='${shQ(!!cr.footerCountsAsBody)}'`);
// Messages (with fallbacks)
const msgs = {
MSG_HEADER_FORMAT: m.headerFormat || 'Header must follow format: type(scope): description',
MSG_INVALID_TYPE: m.invalidType || 'Invalid commit type. Valid types: feat, fix, chore, refactor',
MSG_SCOPE_REQUIRED: m.scopeRequired || 'Scope is required and cannot be empty. Use format: type(scope): description',
MSG_DESCRIPTION_REQUIRED: m.descriptionRequired || 'Description is required after ": "',
MSG_DESCRIPTION_TOO_SHORT: m.descriptionTooShort || 'Description too short (minimum 3 characters)',
MSG_LOWERCASE_REQUIRED: m.lowercaseRequired || 'Description should start with lowercase letter',
MSG_NO_TRAILING_PERIOD: m.noTrailingPeriod || 'Header should not end with a period',
MSG_HEADER_TOO_LONG: m.headerTooLong || 'Header too long ({length} chars, max 72)',
MSG_BODY_REQUIRED_TYPE: m.bodyRequiredForType || 'Body is required for {type} commits. Add a blank line after header, then describe what and why.',
MSG_BREAKING_NEEDS_FOOTER: (m.breakingNeedsFooter || m.breakingChangeInconsistent) || "Breaking change '!' in header requires 'BREAKING CHANGE:' in footer",
MSG_FOOTER_NEEDS_BANG: (m.footerNeedsBang || m.breakingChangeInconsistent) || "'BREAKING CHANGE:' in footer requires '!' in header",
MSG_EMPTY: m.cannotBeEmpty || 'Commit message cannot be empty',
MSG_NEED_BLANK_AFTER_HEADER: m.bodyLeadingBlank || 'Blank line required after header',
MSG_BODY_LINE_TOO_LONG: m.bodyLineTooLong || 'Body line too long ({length} chars, max 72)',
MSG_FOOTER_LINE_TOO_LONG: m.footerLineTooLong || 'Footer line too long ({length} chars, max 72)',
};
for (const [k, v] of Object.entries(msgs)) {
out.push(`${k}='${shQ(v)}'`);
}
fs.writeFileSync('env.sh', out.join('\n') + '\n');
EOF
node parse-config.js
# Load env safely
set -a
. ./env.sh
set +a
# Determine commit range
if [ "${{ github.event_name }}" = "pull_request" ]; then
BASE="${{ github.event.pull_request.base.sha }}"
HEAD="${{ github.event.pull_request.head.sha }}"
else
BASE="${{ github.event.before }}"
HEAD="${{ github.sha }}"
fi
git rev-list --no-merges "${BASE}..${HEAD}" > commits.txt || true
COUNT=$(wc -l < commits.txt | tr -d ' ')
if [ "$COUNT" -eq 0 ]; then
echo "failed=0" >> $GITHUB_OUTPUT
exit 0
fi
# Build PR body
{
echo "## 🚫 Commit Message Validation Failed"
echo
} > pr_comment.md
VALIDATION_FAILED=0
while read -r commit_sha; do
[ -z "$commit_sha" ] && continue
short_sha="${commit_sha:0:7}"
# Full message as-is (strip CRs)
commit_msg="$(git show -s --format=%B "$commit_sha" | sed 's/\r$//')"
# Split lines for analysis
mapfile -t LINES < <(printf "%s\n" "$commit_msg")
# Find header (first non-empty)
header=""
header_idx=-1
for i in "${!LINES[@]}"; do
if [[ -n "${LINES[$i]//[[:space:]]/}" ]]; then
header="${LINES[$i]}"
header_idx=$i
break
fi
done
# Next line after header (might not exist)
next_idx=$((header_idx+1))
next_line="${LINES[$next_idx]-}"
# Is there a blank line immediately after header?
blank_after_header=false
if [[ "$BODY_LEADING_BLANK" == "true" ]]; then
if [[ -z "${next_line//[[:space:]]/}" ]]; then
blank_after_header=true
fi
fi
# Collect body lines (non-footer, after header)
body_present=false
body_lines=()
footer_lines=()
seen_header=false
for i in "${!LINES[@]}"; do
line="${LINES[$i]}"
# Skip leading empties until header found
if ! $seen_header; then
if [[ $i -eq $header_idx ]]; then
seen_header=true
fi
continue
fi
# classify lines after header
if [[ "$line" =~ ^BREAKING[[:space:]-]CHANGE: ]]; then
footer_lines+=("$line")
continue
fi
# non-footer line
if [[ -n "${line//[[:space:]]/}" ]]; then
# count as body only if it's not the immediate line when a blank is required but missing
if [[ "$BODY_LEADING_BLANK" == "true" && $i -eq $next_idx && -n "${line//[[:space:]]/}" ]]; then
: # body will still be considered present (we enforce missing blank separately)
fi
body_present=true
body_lines+=("$line")
fi
done
# Start per-commit evaluation
commit_errors=""
has_errors=false
if [[ -z "$header" ]]; then
commit_errors+=" ❌ ${MSG_EMPTY}\n"
has_errors=true
else
# Format (scope required or optional)
if [[ "$SCOPE_REQUIRED" == "true" ]]; then
pattern='^[a-z]+\([^)]+\)(!)?: .+'
else
pattern='^[a-z]+(\([^)]+\))?(!)?: .+'
fi
if ! echo "$header" | grep -qE "$pattern"; then
commit_errors+=" ❌ ${MSG_HEADER_FORMAT}\n"
has_errors=true
else
commit_type="$(echo "$header" | sed -E 's/^([a-z]+).*/\1/')"
if ! echo "$commit_type" | grep -qE "^(${VALID_TYPES})$"; then
commit_errors+=" ❌ ${MSG_INVALID_TYPE}\n"
has_errors=true
fi
if [[ "$SCOPE_REQUIRED" == "true" ]] && ! echo "$header" | grep -qE '^[a-z]+\([^)]+\)'; then
commit_errors+=" ❌ ${MSG_SCOPE_REQUIRED}\n"
has_errors=true
fi
description="$(echo "$header" | sed -E 's/^[a-z]+(\([^)]+\))?(!)?: (.*)/\3/' 2>/dev/null || true)"
if [[ -z "$description" ]]; then
commit_errors+=" ❌ ${MSG_DESCRIPTION_REQUIRED}\n"
has_errors=true
elif (( ${#description} < SUBJECT_MIN_LENGTH )); then
commit_errors+=" ❌ ${MSG_DESCRIPTION_TOO_SHORT}\n"
has_errors=true
fi
if [[ "$SUBJECT_LOWERCASE" == "true" ]] && echo "$header" | grep -qE '^[a-z]+(\([^)]+\))?(!)?: [A-Z]'; then
commit_errors+=" ❌ ${MSG_LOWERCASE_REQUIRED}\n"
has_errors=true
fi
if [[ "$NO_TRAILING_PERIOD" == "true" ]] && echo "$header" | grep -qE '\.$'; then
commit_errors+=" ❌ ${MSG_NO_TRAILING_PERIOD}\n"
has_errors=true
fi
fi
if (( ${#header} > MAX_HEADER_LENGTH )); then
commit_errors+=" ❌ ${MSG_HEADER_TOO_LONG//\{length\}/${#header}}\n"
has_errors=true
fi
fi
# Enforce blank line after header
if [[ "$BODY_LEADING_BLANK" == "true" ]]; then
if [[ "$header_idx" -ge 0 ]]; then
if ! $blank_after_header; then
commit_errors+=" ❌ ${MSG_NEED_BLANK_AFTER_HEADER}\n"
has_errors=true
fi
fi
fi
# Body required for certain types
if [[ -n "$BODY_REQUIRED_TYPES" ]] && echo "$commit_type" | grep -qE "^(${BODY_REQUIRED_TYPES})$"; then
effective_body_present="$body_present"
# If allowed, treat presence of any footer as satisfying the body requirement
if [[ "${FOOTER_COUNTS_AS_BODY:-false}" == "true" && ${#footer_lines[@]} -gt 0 ]]; then
effective_body_present=true
fi
if [[ "$effective_body_present" == "false" ]]; then
commit_errors+=" ❌ ${MSG_BODY_REQUIRED_TYPE//\{type\}/$commit_type}\n"
has_errors=true
fi
fi
# Line length checks
if [[ ${#body_lines[@]} -gt 0 ]]; then
for line in "${body_lines[@]}"; do
len=${#line}
if (( len > MAX_BODY_LINE )); then
commit_errors+=" ❌ ${MSG_BODY_LINE_TOO_LONG//\{length\}/$len}\n"
has_errors=true
fi
done
fi
if [[ ${#footer_lines[@]} -gt 0 ]]; then
for line in "${footer_lines[@]}"; do
len=${#line}
if (( len > MAX_FOOTER_LINE )); then
commit_errors+=" ❌ ${MSG_FOOTER_LINE_TOO_LONG//\{length\}/$len}\n"
has_errors=true
fi
done
fi
# Breaking change consistency
if [[ "$ENFORCE_BREAKING_CHANGES" == "true" ]]; then
has_breaking_header=$(echo "$header" | grep -c "!" || true)
has_breaking_footer=$(printf "%s\n" "${footer_lines[@]}" | grep -cE "^BREAKING[ -]CHANGE:" || true)
if [[ "$has_breaking_header" -gt 0 && "$has_breaking_footer" -eq 0 ]]; then
commit_errors+=" ❌ ${MSG_BREAKING_NEEDS_FOOTER}\n"
has_errors=true
elif [[ "$has_breaking_header" -eq 0 && "$has_breaking_footer" -gt 0 ]]; then
commit_errors+=" ❌ ${MSG_FOOTER_NEEDS_BANG}\n"
has_errors=true
fi
fi
if [[ "$has_errors" == true ]]; then
{
echo "📝 Commit $short_sha"
echo "$header"
echo
printf "%b" "$commit_errors"
echo
} >> pr_comment.md
VALIDATION_FAILED=1
fi
done < commits.txt
echo "failed=$VALIDATION_FAILED" >> $GITHUB_OUTPUT
- name: Post PR comment
if: ${{ steps.validate.outputs.failed == '1' && github.event_name == 'pull_request' }}
uses: peter-evans/create-or-update-comment@v4
with:
issue-number: ${{ github.event.pull_request.number }}
body-path: pr_comment.md
edit-mode: replace
body-includes: "Commit Message Validation Failed"
- name: Fail workflow if validation failed
if: ${{ steps.validate.outputs.failed == '1' }}
run: |
echo "::error::Commit message validation failed. See PR comment for details."
cat pr_comment.md || true
exit 1