-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDangerfile.ts
108 lines (101 loc) · 3.27 KB
/
Dangerfile.ts
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
import { message, danger, fail, warn } from "danger";
const issueKeyRe = /(BDGR-\d+)/g;
async function findAddedAndRemovedTodoIssues() {
const removed = new Set<string>();
const added = new Set<string>();
const linesWithoutKey = new Set<string>();
const fixmes = new Set<string>();
for (const file of danger.git.modified_files.concat(
...danger.git.created_files,
)) {
if (file === "Dangerfile.ts") {
continue;
}
const delta = await danger.git.structuredDiffForFile(file);
if (!delta) {
continue;
}
for (const chunk of delta.chunks) {
for (const line of chunk.changes) {
if (line.content.includes("TODO")) {
const matches = line.content.match(issueKeyRe);
if (matches) {
for (const match of matches) {
if (line.type === "del") {
removed.add(match);
} else if (line.type === "add") {
added.add(match);
}
}
} else {
// Don't warn if we're removing it
if (line.type === "add") {
linesWithoutKey.add(line.content);
}
}
} else if (line.content.includes("FIXME") && line.type !== "del") {
fixmes.add(line.content);
}
}
}
}
return { removed, added, linesWithoutKey, fixmes };
}
export default async () => {
const { removed, added, linesWithoutKey, fixmes } =
await findAddedAndRemovedTodoIssues();
if (removed.size > 0) {
message(`Removed TODOs: ${Array.from(removed)
.map((key) => `[${key}](https://linear.app/ystv/issue/${key})`)
.join(", ")}
Once merged, please close the corresponding Linear tickets.
You can also include \`Closes ${Array.from(removed).join(
", ",
)}\` in the PR description to close the tickets automatically.`);
}
if (added.size > 0) {
message(
`Added TODOs: ${Array.from(added)
.map((key) => `[${key}](https://linear.app/ystv/issue/${key})`)
.join(", ")}`,
);
}
if (linesWithoutKey.size > 0) {
fail(
`TODOs without issue key. Please file a Linear ticket and add the number to the comment: \n${Array.from(
linesWithoutKey,
)
.map((l) => " * `" + l + "`")
.join("\n")}`,
);
}
if (fixmes.size > 0) {
fail(
`Found ${fixmes.size} FIXME comments. Please either remove them or convert them to TODOs (with an associated Linear ticket).`,
);
}
};
const ticketBranchPrefixRe = /^bow-\d+/i;
if (danger.github.pr) {
if (
!(
issueKeyRe.test(danger.github.pr.title) ||
issueKeyRe.test(danger.github.pr.body) ||
ticketBranchPrefixRe.test(danger.github.pr.head.ref)
)
) {
if (danger.github.pr.user.type !== "Bot") {
warn(
"No Linear ticket found. Please include one in either the pull request title (e.g. `[BDGR-123] Fix something`), the description (`Fixes BDGR-123.`), or the branch name (`bow-123-fix-something`).",
);
}
}
const touchesMigrations = danger.git.modified_files.some((f) =>
f.startsWith("utility/prisma/migrations"),
);
if (touchesMigrations && !danger.github.pr.title.includes("[DB]")) {
fail(
"This PR adds a database migration. Please add [DB] to the title, as a reminder to run them when deploying.",
);
}
}