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 MIGRATION_SCHEMA_FILES = [
65 "crates/collab/migrations/20251208000000_test_schema.sql",
66 "crates/collab/migrations.sqlite/20221109000000_test_schema.sql",
67];
68
69const modifiedSchemaFiles = danger.git.modified_files.filter((file) =>
70 MIGRATION_SCHEMA_FILES.some((schemaFilePath) => file.endsWith(schemaFilePath)),
71);
72
73if (modifiedSchemaFiles.length > 0) {
74 warn(
75 [
76 "This PR modifies database schema files.",
77 "",
78 "If you are making database changes, a migration needs to be added in the Cloud repository.",
79 ].join("\n"),
80 );
81}
82
83const FIXTURE_CHANGE_ATTESTATION = "Changes to test fixtures are intentional and necessary.";
84
85const FIXTURES_PATHS = ["crates/assistant_tools/src/edit_agent/evals/fixtures"];
86
87const modifiedFixtures = danger.git.modified_files.filter((file) =>
88 FIXTURES_PATHS.some((fixturePath) => file.includes(fixturePath)),
89);
90
91if (modifiedFixtures.length > 0) {
92 if (!body.includes(FIXTURE_CHANGE_ATTESTATION)) {
93 const modifiedFixturesStr = modifiedFixtures.map((path) => "`" + path + "`").join(", ");
94 fail(
95 [
96 `This PR modifies eval or test fixtures (${modifiedFixturesStr}), which are typically expected to remain unchanged.`,
97 "If these changes are intentional and required, please add the following attestation to your PR description: ",
98 `"${FIXTURE_CHANGE_ATTESTATION}"`,
99 ].join("\n\n"),
100 );
101 }
102}