-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdangerfile.js
More file actions
207 lines (177 loc) · 6.39 KB
/
dangerfile.js
File metadata and controls
207 lines (177 loc) · 6.39 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
// dangerfile.js - Evidence validation rules for PR enforcement
// Implements automated checks from issue-driven-delivery skill
// Issue: #300
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 safely get issue body (linked issue from PR)
async function getLinkedIssueBody() {
const prBody = danger.github.pr.body || "";
// Extract issue number from "Closes #N" or "Fixes #N" patterns
const issueMatch = prBody.match(/(?:closes|fixes|resolves)\s+#(\d+)/i);
if (!issueMatch) {
return null;
}
const issueNumber = issueMatch[1];
try {
const issue = await danger.github.api.issues.get({
owner: danger.github.thisPR.owner,
repo: danger.github.thisPR.repo,
issue_number: parseInt(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;
}
}
// 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.");
}
// Success message if all critical checks pass
const failures = danger.fails || [];
if (failures.length === 0) {
message("All PR validation checks passed.");
}
}
// Run validation
validate();