-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommitlint.config.js
More file actions
144 lines (128 loc) · 3.81 KB
/
Copy pathcommitlint.config.js
File metadata and controls
144 lines (128 loc) · 3.81 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
// $KYAULabs: commitlint.config.js kyau@akira.kyaulabs 2026/07/09 -0700 Exp $
const { spawnSync } = require('child_process');
const BANNED_CLOSING = new Set([
'close', 'closes', 'closed',
'resolve', 'resolves', 'resolved',
'fix', 'fixed',
]);
const CLOSING_RE = new RegExp(
'^\\s*(close|closes|closed|resolve|resolves|resolved|fix|fixes|fixed)\\b\\s*:?\\s*#\\d+\\s*$',
'i'
);
// ISSUE_REF_RE tracks Fixes:/Refs: trailers for placement enforcement.
// Colon is required (Fixes #42 or refs: #42 are rejected/mis-cased earlier by
// the CLOSING_RE checks). The /i flag catches lowercase `refs:` as well.
const ISSUE_REF_RE = /^\s*(Fixes|Refs):\s*#\d+\s*$/i;
const PLAN_BY_RE = /^\s*Plan-by:\s/;
const isMergeOrRevert = (parsed) => {
const isMerge =
(parsed.merge && parsed.merge.length > 0) ||
(parsed.header && /^Merge /.test(parsed.header));
const isRevert =
parsed.revert || (parsed.header && /^Revert /.test(parsed.header));
return isMerge || isRevert;
};
const trailersExist = (parsed, when, trailers) => {
// Exempt merge commits and reverts from trailer enforcement.
// `git merge --no-ff` and `git revert` produce auto-generated messages
// that cannot carry Plan-by/Acked-by/Signed-off-by trailers. CI applies
// the same exemption via this config, so merges/reverts pass everywhere.
if (isMergeOrRevert(parsed)) {
return [true, ''];
}
const output = spawnSync('git', ['interpret-trailers', '--parse'], {
input: parsed.raw || '',
}).stdout.toString();
const lines = output.split('\n');
const negated = when === 'never';
const missing = trailers.filter(
(t) => !lines.some((ln) => ln.startsWith(t))
);
const allPresent = missing.length === 0;
return [
negated ? !allPresent : allPresent,
'message must have ' +
trailers.map((t) => '`' + t + '`').join(', ') +
' trailer' + (trailers.length > 1 ? 's' : ''),
];
};
const issueRefConvention = (parsed, when) => {
// Exempt merge commits and reverts — same policy as trailersExist.
if (isMergeOrRevert(parsed)) {
return [true, ''];
}
const negated = when === 'never';
const lines = (parsed.raw || '').split('\n');
const violations = [];
const issueRefIdxs = [];
let planByIdx = -1;
lines.forEach((line, i) => {
const m = line.match(CLOSING_RE);
if (m) {
const kw = m[1];
const kwLow = kw.toLowerCase();
if (BANNED_CLOSING.has(kwLow)) {
violations.push(
'issue-closing keyword `' + kw + '` is not allowed — use `Fixes: #NN` to close an issue'
);
} else if (kwLow === 'fixes') {
if (kw !== 'Fixes') {
violations.push(
'issue-closing keyword `' + kw + '` must be Sentence-case `Fixes`'
);
}
if (!/^\s*Fixes:\s*#\d+/.test(line)) {
violations.push(
'issue reference must use the form `Fixes: #NN` (with colon) — found: `' + line.trim() + '`'
);
}
}
}
if (ISSUE_REF_RE.test(line)) {
issueRefIdxs.push(i);
}
if (PLAN_BY_RE.test(line) && planByIdx === -1) {
planByIdx = i;
}
});
if (planByIdx !== -1 && issueRefIdxs.some((idx) => idx > planByIdx)) {
violations.push(
'issue-reference trailers (`Fixes:`, `Refs:`) must appear before `Plan-by:`'
);
}
const ok = violations.length === 0;
return [negated ? !ok : ok, violations.join('; ')];
};
module.exports = {
extends: ['@commitlint/config-conventional'],
plugins: [
{
rules: {
'trailers-exist': trailersExist,
'issue-ref-convention': issueRefConvention,
},
},
],
rules: {
'header-max-length': [2, 'always', 100],
'type-enum': [2, 'always', [
'build',
'chore',
'ci',
'docs',
'feat',
'fix',
'patch',
'perf',
'refactor',
'revert',
'style',
'test',
'ignore',
]],
'trailers-exist': [2, 'always', ['Plan-by:', 'Acked-by:', 'Signed-off-by:']],
'issue-ref-convention': [2, 'always'],
'signed-off-by': [0],
},
};
// vim: ft=javascript sts=4 sw=4 ts=4 noet :