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