1import { danger, message, warn } from "danger";
2const { prHygiene } = require("danger-plugin-pr-hygiene");
3
4prHygiene({
5 rules: {
6 // Don't enable this rule just yet, as it can have false positives.
7 useImperativeMood: "off",
8 },
9});
10
11const RELEASE_NOTES_PATTERN = /Release Notes:\r?\n\s+-/gm;
12const body = danger.github.pr.body;
13
14const hasReleaseNotes = RELEASE_NOTES_PATTERN.test(body);
15
16if (!hasReleaseNotes) {
17 warn(
18 [
19 "This PR is missing release notes.",
20 "",
21 'Please add a "Release Notes" section that describes the change:',
22 "",
23 "```",
24 "Release Notes:",
25 "",
26 "- Added/Fixed/Improved ...",
27 "```",
28 "",
29 'If your change is not user-facing, you can use "N/A" for the entry:',
30 "```",
31 "Release Notes:",
32 "",
33 "- N/A",
34 "```",
35 ].join("\n"),
36 );
37}
38
39const ISSUE_LINK_PATTERN =
40 /(?<!(?:Close[sd]?|Fixe[sd]|Resolve[sd]|Implement[sed]|Follow-up of|Part of)\s+)https:\/\/github\.com\/[\w-]+\/[\w-]+\/issues\/\d+/gi;
41
42const bodyWithoutReleaseNotes = hasReleaseNotes ? body.split(/Release Notes:/)[0] : body;
43const includesIssueUrl = ISSUE_LINK_PATTERN.test(bodyWithoutReleaseNotes);
44
45if (includesIssueUrl) {
46 const matches = bodyWithoutReleaseNotes.match(ISSUE_LINK_PATTERN) ?? [];
47 const issues = matches
48 .map((match) => match.replace(/^#/, "").replace(/https:\/\/github\.com\/zed-industries\/zed\/issues\//, ""))
49 .filter((issue, index, self) => self.indexOf(issue) === index);
50
51 const issuesToReport = issues.map((issue) => `#${issue}`).join(", ");
52 message(
53 [
54 `This PR includes links to the following GitHub Issues: ${issuesToReport}`,
55 "If this PR aims to close an issue, please include a `Closes #ISSUE` line at the top of the PR body.",
56 ].join("\n"),
57 );
58}