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 },
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}
60
61const PROMPT_PATHS = [
62 "assets/prompts/content_prompt.hbs",
63 "assets/prompts/terminal_assistant_prompt.hbs",
64 "crates/agent/src/prompts/stale_files_prompt_header.txt",
65 "crates/agent/src/prompts/summarize_thread_detailed_prompt.txt",
66 "crates/agent/src/prompts/summarize_thread_prompt.txt",
67 "crates/assistant_tools/src/templates/create_file_prompt.hbs",
68 "crates/assistant_tools/src/templates/edit_file_prompt_xml.hbs",
69 "crates/assistant_tools/src/templates/edit_file_prompt_diff_fenced.hbs",
70 "crates/git_ui/src/commit_message_prompt.txt",
71];
72
73const PROMPT_CHANGE_ATTESTATION = "I have ensured the LLM Worker works with these prompt changes.";
74
75const modifiedPrompts = danger.git.modified_files.filter((file) =>
76 PROMPT_PATHS.some((promptPath) => file.includes(promptPath)),
77);
78
79for (const promptPath of modifiedPrompts) {
80 if (body.includes(PROMPT_CHANGE_ATTESTATION)) {
81 message(
82 [
83 `This PR contains changes to "${promptPath}".`,
84 "The author has attested the LLM Worker works with the changes to this prompt.",
85 ].join("\n"),
86 );
87 } else {
88 fail(
89 [
90 `Modifying the "${promptPath}" prompt may require corresponding changes in the LLM Worker.`,
91 "If you are ensure what this entails, talk to @maxdeviant or another AI team member.",
92 `Once you have made the changes—or determined that none are necessary—add "${PROMPT_CHANGE_ATTESTATION}" to the PR description.`,
93 ].join("\n"),
94 );
95 }
96}
97
98const FIXTURE_CHANGE_ATTESTATION = "Changes to test fixtures are intentional and necessary.";
99
100const FIXTURES_PATHS = ["crates/assistant_tools/src/edit_agent/evals/fixtures"];
101
102const modifiedFixtures = danger.git.modified_files.filter((file) =>
103 FIXTURES_PATHS.some((fixturePath) => file.includes(fixturePath)),
104);
105
106if (modifiedFixtures.length > 0) {
107 if (!body.includes(FIXTURE_CHANGE_ATTESTATION)) {
108 const modifiedFixturesStr = modifiedFixtures.map((path) => "`" + path + "`").join(", ");
109 fail(
110 [
111 `This PR modifies eval or test fixtures (${modifiedFixturesStr}), which are typically expected to remain unchanged.`,
112 "If these changes are intentional and required, please add the following attestation to your PR description: ",
113 `"${FIXTURE_CHANGE_ATTESTATION}"`,
114 ].join("\n\n"),
115 );
116 }
117}