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