dangerfile.ts

 1import { danger, message, warn, fail } 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    noConventionalCommits: {
10      bannedTypes: ["feat", "fix", "style", "refactor", "perf", "test", "chore", "build", "revert"],
11    },
12  },
13});
14
15const RELEASE_NOTES_PATTERN = /Release Notes:\r?\n\s+-/gm;
16const body = danger.github.pr.body;
17
18const hasReleaseNotes = RELEASE_NOTES_PATTERN.test(body);
19
20if (!hasReleaseNotes) {
21  warn(
22    [
23      "This PR is missing release notes.",
24      "",
25      'Please add a "Release Notes" section that describes the change:',
26      "",
27      "```",
28      "Release Notes:",
29      "",
30      "- Added/Fixed/Improved ...",
31      "```",
32      "",
33      'If your change is not user-facing, you can use "N/A" for the entry:',
34      "```",
35      "Release Notes:",
36      "",
37      "- N/A",
38      "```",
39    ].join("\n"),
40  );
41}
42
43const ISSUE_LINK_PATTERN =
44  /(?:- )?(?<!(?:Close[sd]?|Fixe[sd]|Resolve[sd]|Implement[sed]|Follow-up of|Part of):?\s+)https:\/\/github\.com\/[\w-]+\/[\w-]+\/issues\/\d+/gi;
45
46const bodyWithoutReleaseNotes = hasReleaseNotes ? body.split(/Release Notes:/)[0] : body;
47const includesIssueUrl = ISSUE_LINK_PATTERN.test(bodyWithoutReleaseNotes);
48
49if (includesIssueUrl) {
50  const matches = bodyWithoutReleaseNotes.match(ISSUE_LINK_PATTERN) ?? [];
51  const issues = matches
52    .map((match) => match.replace(/^#/, "").replace(/https:\/\/github\.com\/zed-industries\/zed\/issues\//, ""))
53    .filter((issue, index, self) => self.indexOf(issue) === index);
54
55  const issuesToReport = issues.map((issue) => `#${issue}`).join(", ");
56  message(
57    [
58      `This PR includes links to the following GitHub Issues: ${issuesToReport}`,
59      "If this PR aims to close an issue, please include a `Closes #ISSUE` line at the top of the PR body.",
60    ].join("\n"),
61  );
62}
63
64const FIXTURE_CHANGE_ATTESTATION = "Changes to test fixtures are intentional and necessary.";
65
66const FIXTURES_PATHS = ["crates/assistant_tools/src/edit_agent/evals/fixtures"];
67
68const modifiedFixtures = danger.git.modified_files.filter((file) =>
69  FIXTURES_PATHS.some((fixturePath) => file.includes(fixturePath)),
70);
71
72if (modifiedFixtures.length > 0) {
73  if (!body.includes(FIXTURE_CHANGE_ATTESTATION)) {
74    const modifiedFixturesStr = modifiedFixtures.map((path) => "`" + path + "`").join(", ");
75    fail(
76      [
77        `This PR modifies eval or test fixtures (${modifiedFixturesStr}), which are typically expected to remain unchanged.`,
78        "If these changes are intentional and required, please add the following attestation to your PR description: ",
79        `"${FIXTURE_CHANGE_ATTESTATION}"`,
80      ].join("\n\n"),
81    );
82  }
83}