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