-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdangerfile.js
More file actions
219 lines (196 loc) · 7.06 KB
/
Copy pathdangerfile.js
File metadata and controls
219 lines (196 loc) · 7.06 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
/**
* dangerfile.js — automated PR governance for ChessVision.
*
* Runs in CI via `pnpm danger ci` on pull_request events. Enforces the
* engineering standards documented in CONTRIBUTING.md:
*
* 1. PR title follows Conventional Commits (types kept in sync with
* .commitlintrc.json).
* 2. Source changes ship with test changes (mandatory testing).
* 3. PR has a meaningful description.
* 4. PR stays reasonably scoped (atomic — warns on sprawling diffs).
* 5. lib/tooling invariants surfaced as reminders (lint zero-warning,
* lockfile sync, FEN parser tests).
*
* `fail()` blocks the PR; `warn()` / `message()` are advisory.
*/
import { danger, fail, warn, message, markdown } from 'danger';
const pr = danger.github.pr;
const modified = danger.git.modified_files;
const created = danger.git.created_files;
const allChanged = [...modified, ...created, ...danger.git.deleted_files];
const touched = [...modified, ...created];
// Conventional Commit types — MUST match .commitlintrc.json type-enum.
const CONVENTIONAL_TYPES = [
'feat',
'fix',
'docs',
'style',
'refactor',
'perf',
'test',
'chore',
'ci',
'build',
'revert'
];
// ---------------------------------------------------------------------------
// 1. Conventional Commit PR title
// ---------------------------------------------------------------------------
const titlePattern = new RegExp(
`^(${CONVENTIONAL_TYPES.join('|')})(\\([\\w$.\\-*/ ]+\\))?(!)?: .+`
);
if (!titlePattern.test(pr.title)) {
fail(
[
`**PR title is not a valid Conventional Commit.**`,
'',
`Received: \`${pr.title}\``,
'',
`Expected: \`<type>(optional-scope): <subject>\``,
`Allowed types: ${CONVENTIONAL_TYPES.map((t) => `\`${t}\``).join(', ')}`,
'',
'Examples: `feat(export): add SVG batch export`, `fix: correct FEN parsing on en passant`'
].join('\n')
);
} else if (/\.$/.test(pr.title)) {
fail('**PR title must not end with a period.**');
}
// Every commit subject (first line) must also be a valid Conventional Commit.
// Merge commits are exempt — they are generated, not authored.
const commits = danger.git.commits;
const nonConventionalCommits = commits.filter((c) => {
const subject = c.message.split('\n', 1)[0];
if (/^Merge /.test(subject)) return false;
return !titlePattern.test(subject);
});
if (nonConventionalCommits.length > 0) {
fail(
[
'**One or more commits are not valid Conventional Commits:**',
'',
...nonConventionalCommits.map(
(c) => `- \`${c.sha.slice(0, 7)}\` ${c.message.split('\n', 1)[0]}`
),
'',
'Rewrite the offending commits (`git rebase -i`) before merge.'
].join('\n')
);
}
// ---------------------------------------------------------------------------
// 1b. Sufficient commit context
// ---------------------------------------------------------------------------
const MIN_BODY_CHARS = 12;
const contextlessCommits = commits.filter((c) => {
const subject = c.message.split('\n', 1)[0];
if (/^Merge /.test(subject)) return false;
return (
subject.replace(/^[a-z]+(\([\w$.\-*/ ]+\))?(!)?:\s*/, '').trim().length <
MIN_BODY_CHARS
);
});
if (commits.length === 0) {
warn(
'**This PR has no commits Danger can inspect.** Verify the branch is pushed.'
);
} else if (contextlessCommits.length > 0) {
warn(
[
'**Some commits lack meaningful context** (subject too terse):',
'',
...contextlessCommits.map(
(c) => `- \`${c.sha.slice(0, 7)}\` ${c.message.split('\n', 1)[0]}`
),
'',
'Use a descriptive subject, and add a body explaining *why* for non-trivial changes.'
].join('\n')
);
}
// ---------------------------------------------------------------------------
// 2. Mandatory testing — source changes require test changes
// ---------------------------------------------------------------------------
const isSource = (f) =>
f.startsWith('src/') &&
/\.[jt]sx?$/.test(f) &&
!/\.(test|spec)\.[jt]sx?$/.test(f);
const isTest = (f) =>
/\.(test|spec)\.[jt]sx?$/.test(f) || /(^|\/)(__tests__|tests?)\//.test(f);
const sourceChanges = touched.filter(isSource);
const testChanges = touched.filter(isTest);
if (sourceChanges.length > 0 && testChanges.length === 0) {
warn(
[
'**Source changed but no tests were added or updated.**',
'',
'This project mandates tests for behavioral changes. If this PR changes',
'logic, add or update a co-located `*.test.ts`. If it is purely',
'cosmetic/refactor with no behavior change, say so in the description.'
].join('\n')
);
}
// fenParser is a hard invariant: any change requires its test to change too.
const touchedFenParser = touched.some((f) =>
f.endsWith('src/shared/utils/fenParser.ts')
);
const touchedFenParserTest = touched.some((f) =>
f.endsWith('src/shared/utils/fenParser.test.ts')
);
if (touchedFenParser && !touchedFenParserTest) {
fail(
'**`fenParser.ts` changed without updating `fenParser.test.ts`.** ' +
'Every change to the FEN parser requires a corresponding test (see CONTRIBUTING.md).'
);
}
// ---------------------------------------------------------------------------
// 3. PR description must exist
// ---------------------------------------------------------------------------
if (!pr.body || pr.body.trim().length < 20) {
fail(
'**PR description is missing or too short.** Describe what changed and why, ' +
'and link any related issue (e.g. `Fixes #123`).'
);
}
// ---------------------------------------------------------------------------
// 4. Atomic / scoped PR — advisory
// ---------------------------------------------------------------------------
const BIG_PR_FILE_COUNT = 40;
if (allChanged.length > BIG_PR_FILE_COUNT) {
warn(
`**Large PR:** ${allChanged.length} files changed. Consider splitting into ` +
'smaller, atomic PRs (one logical task each) for reviewability.'
);
}
// ---------------------------------------------------------------------------
// 5. Invariant reminders
// ---------------------------------------------------------------------------
const depsChanged = touched.some(
(f) => f === 'package.json' || f === 'pnpm-lock.yaml'
);
const onlyPackageJson =
touched.includes('package.json') && !touched.includes('pnpm-lock.yaml');
if (depsChanged && onlyPackageJson) {
warn(
'`package.json` changed but `pnpm-lock.yaml` did not. Run `pnpm install` and ' +
'commit the updated lockfile so CI `--frozen-lockfile` does not fail.'
);
}
const editedHooksOrCi = allChanged.some(
(f) => f.startsWith('.husky/') || f.startsWith('.github/workflows/')
);
if (editedHooksOrCi) {
message(
'This PR modifies git hooks or CI workflows — verify the pipeline still ' +
'blocks non-conforming commits/PRs.'
);
}
markdown(
[
'### Reviewer checklist',
'',
'- [ ] `pnpm test && npx tsc --noEmit && pnpm lint` all green (0 warnings).',
'- [ ] Commits are atomic and Conventional.',
'- [ ] No `any`, `@ts-ignore`, or non-null `!`.',
'- [ ] No hardcoded hex colors in JSX.',
'- [ ] Canvas blobs followed by `canvas.width = 0`.'
].join('\n')
);