-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdangerfile.js
More file actions
317 lines (273 loc) · 9.77 KB
/
dangerfile.js
File metadata and controls
317 lines (273 loc) · 9.77 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
// dangerfile.js - Evidence validation rules for PR enforcement
// Implements automated checks from issue-driven-delivery skill
// Issues: #300, #290, #291, #292
const { danger, warn, fail, message } = require("danger");
// Constants
const MAX_ITEM_PREVIEW_LENGTH = 80;
/**
* Check if text contains an evidence link (URL in parentheses or markdown link)
* Handles formats like:
* - ([link](https://...))
* - (https://...)
* - (see [link](https://...))
* - [link](https://...)
*/
function hasEvidenceLink(text) {
// Markdown link with URL: [text](https://...)
if (/\[[^\]]+\]\(https?:\/\/[^)]+\)/.test(text)) {
return true;
}
// URL anywhere in parentheses: (... https://... ...)
if (/\([^)]*https?:\/\/[^)]+\)/.test(text)) {
return true;
}
// Bare URL at end of line
if (/https?:\/\/\S+\s*$/.test(text)) {
return true;
}
return false;
}
/**
* Find checked items missing evidence links in given text
*/
function findCheckedItemsWithoutEvidence(text) {
const checkedItemsRegex = /- \[x\] (.+)/gi;
const itemsWithoutEvidence = [];
for (const match of text.matchAll(checkedItemsRegex)) {
const itemText = match[1];
if (!hasEvidenceLink(itemText)) {
itemsWithoutEvidence.push(itemText.substring(0, MAX_ITEM_PREVIEW_LENGTH));
}
}
return itemsWithoutEvidence;
}
// Helper to extract linked issue number from PR body
function getLinkedIssueNumber() {
const prBody = danger.github.pr.body || "";
const issueMatch = prBody.match(/(?:closes|fixes|resolves)\s+#(\d+)/i);
return issueMatch ? parseInt(issueMatch[1]) : null;
}
// Helper to safely get issue body (linked issue from PR)
async function getLinkedIssueBody() {
const issueNumber = getLinkedIssueNumber();
if (!issueNumber) {
return null;
}
try {
const issue = await danger.github.api.issues.get({
owner: danger.github.thisPR.owner,
repo: danger.github.thisPR.repo,
issue_number: issueNumber,
});
return issue.data.body || "";
} catch (error) {
// Log error details for debugging (visible in CI logs)
console.error(
`Failed to fetch issue #${issueNumber}: ${error.message || error}`,
);
if (error.status === 404) {
console.error(`Issue #${issueNumber} not found or inaccessible`);
} else if (error.status === 403) {
console.error(`Rate limited or insufficient permissions`);
}
return null;
}
}
// Helper to get comments on the linked issue
async function getLinkedIssueComments() {
const issueNumber = getLinkedIssueNumber();
if (!issueNumber) {
return null;
}
try {
const comments = await danger.github.api.issues.listComments({
owner: danger.github.thisPR.owner,
repo: danger.github.thisPR.repo,
issue_number: issueNumber,
});
return comments.data || [];
} catch (error) {
console.error(
`Failed to fetch comments for issue #${issueNumber}: ${error.message || error}`,
);
return null;
}
}
/**
* Check if a comment contains a plan (implementation plan for approval)
* Plan indicators:
* - Contains "## Plan" or "## Implementation Plan" or "## Refinement"
* - Links to docs/plans/ directory
* - Contains "awaiting approval" or "ready for approval"
*/
function isPlanComment(commentBody) {
if (!commentBody) return false;
const lowerBody = commentBody.toLowerCase();
// Check for plan headers
if (/##\s*(implementation\s+)?plan/i.test(commentBody)) return true;
if (/##\s*refinement/i.test(commentBody)) return true;
// Check for plan file links
if (/docs\/plans\//.test(commentBody)) return true;
// Check for approval request language
if (
lowerBody.includes("awaiting approval") ||
lowerBody.includes("ready for approval") ||
lowerBody.includes("plan ready for approval")
)
return true;
return false;
}
/**
* Check if a comment indicates plan approval
*/
function isApprovalComment(commentBody) {
if (!commentBody) return false;
const lowerBody = commentBody.toLowerCase();
return (
lowerBody.includes("approval acknowledged") ||
lowerBody.includes("approved to proceed") ||
lowerBody.includes("plan approved") ||
/proceed(ing)?\s+with/.test(lowerBody)
);
}
// Main validation logic
async function validate() {
const prBody = danger.github.pr.body || "";
// Rule 0: PR must reference an issue
const hasIssueReference = /(?:closes|fixes|resolves)\s+#\d+/i.test(prBody);
if (!hasIssueReference) {
fail("PR must reference an issue. Add 'Closes #N' to the PR description.");
return; // Can't validate further without issue reference
}
const issueBody = await getLinkedIssueBody();
if (!issueBody) {
warn("Could not fetch linked issue body. Manual verification required.");
return;
}
// Rule 1: All acceptance criteria must be checked
// Count all unchecked items, then subtract descoped ones
const allUncheckedMatches = issueBody.match(/- \[ \] /g) || [];
const descopedMatches = issueBody.match(/- \[ \] ~~[^~]+~~/g) || [];
const uncheckedCount = allUncheckedMatches.length - descopedMatches.length;
if (uncheckedCount > 0) {
fail(
`[Issue] ${uncheckedCount} acceptance criteria not checked. ` +
`Complete all items or mark as descoped (~~strikethrough~~) before PR.`,
);
}
// Rule 2: Checked items should have evidence links
const checkedWithoutEvidence = findCheckedItemsWithoutEvidence(issueBody);
if (checkedWithoutEvidence.length > 0) {
warn(
`[Issue] ${checkedWithoutEvidence.length} checked acceptance criteria may be missing evidence links. ` +
`Recommended format: - [x] Item ([evidence](link))`,
);
}
// Rule 3: Descoped items must have approval links
const descopedRegex = /- \[ \] ~~([^~]+)~~/g;
const descopedWithoutApproval = [];
for (const match of issueBody.matchAll(descopedRegex)) {
const lineEnd = issueBody.indexOf("\n", match.index);
const fullLine = issueBody.substring(
match.index,
lineEnd > 0 ? lineEnd : issueBody.length,
);
// Case-insensitive check for descoped approval
if (!/\(descoped:/i.test(fullLine) && !hasEvidenceLink(fullLine)) {
descopedWithoutApproval.push(
match[1].substring(0, MAX_ITEM_PREVIEW_LENGTH),
);
}
}
if (descopedWithoutApproval.length > 0) {
fail(
`[Issue] ${descopedWithoutApproval.length} descoped items missing approval links. ` +
`Format: - [ ] ~~Item~~ (descoped: [approval](link))`,
);
}
// Rule 4: Plan must be archived (check for plan file in archive)
const createdFiles = danger.git.created_files || [];
const modifiedFiles = danger.git.modified_files || [];
const planArchived = createdFiles.some((f) =>
f.includes("docs/plans/archive/"),
);
const planInProgress = modifiedFiles.some(
(f) => f.includes("docs/plans/") && !f.includes("archive"),
);
if (planInProgress && !planArchived) {
warn("Plan file modified but not archived. Archive plan before merge.");
}
// Rule 5: PR test plan validation
const testPlanSection = prBody.match(/## Test [Pp]lan[\s\S]*?(?=##|$)/);
if (testPlanSection) {
const testPlanContent = testPlanSection[0];
// Rule 5a: All test plan items must be checked before merge
const uncheckedTestPlan = (testPlanContent.match(/- \[ \] /g) || []).length;
if (uncheckedTestPlan > 0) {
fail(
`[PR] ${uncheckedTestPlan} test plan items not verified. ` +
`All test plan items must be checked before merge.`,
);
}
// Rule 5b: Checked test plan items should have evidence
const testPlanWithoutEvidence =
findCheckedItemsWithoutEvidence(testPlanContent);
if (testPlanWithoutEvidence.length > 0) {
fail(
`[PR] ${testPlanWithoutEvidence.length} test plan items missing evidence links. ` +
`Required format: - [x] Item ([evidence](link))`,
);
}
}
// Rule 6: PR must have a summary section
if (!/## Summary/i.test(prBody)) {
warn("PR should include a '## Summary' section describing the changes.");
}
// Rule 7: PR must have a test plan section
if (!/## Test [Pp]lan/i.test(prBody)) {
warn("PR should include a '## Test plan' section.");
}
// Rule 8: Review depth - warn on brief/empty approvals
const reviews = danger.github.reviews || [];
const approvedReviews = reviews.filter((r) => r.state === "APPROVED");
const MIN_REVIEW_BODY_LENGTH = 50;
if (approvedReviews.length > 0) {
const briefApprovals = approvedReviews.filter(
(r) => !r.body || r.body.trim().length < MIN_REVIEW_BODY_LENGTH,
);
if (briefApprovals.length === approvedReviews.length) {
warn(
`[Review] All ${approvedReviews.length} approval(s) have brief or empty review bodies. ` +
`Substantive reviews should include: files reviewed, potential issues checked, or specific feedback.`,
);
}
}
// Rule 9: Plan approval enforcement
// Issues must have a plan comment with approval before implementation
const issueComments = await getLinkedIssueComments();
if (issueComments) {
const hasPlanComment = issueComments.some((c) => isPlanComment(c.body));
const hasApprovalComment = issueComments.some((c) =>
isApprovalComment(c.body),
);
if (!hasPlanComment) {
warn(
`[Issue] No plan comment found on linked issue. ` +
`Post a plan comment with "## Plan" header or link to docs/plans/ before implementation. ` +
`See Issue #177 for exemplar.`,
);
} else if (!hasApprovalComment) {
warn(
`[Issue] Plan found but no approval comment detected. ` +
`Add approval comment (e.g., "Approval acknowledged" or "Plan approved") before implementation.`,
);
}
}
// Success message if all critical checks pass
const failures = danger.fails || [];
if (failures.length === 0) {
message("All PR validation checks passed.");
}
}
// Run validation
validate();