get-preview-channel-changes

  1#!/usr/bin/env node --redirect-warnings=/dev/null
  2
  3const { execFileSync } = require("child_process");
  4let { GITHUB_ACCESS_TOKEN } = process.env;
  5const GITHUB_URL = "https://github.com";
  6const SKIPPABLE_NOTE_REGEX = /^\s*-?\s*n\/?a\s*/ims;
  7const PULL_REQUEST_WEB_URL = "https://github.com/zed-industries/zed/pull";
  8const PULL_REQUEST_API_URL =
  9  "https://api.github.com/repos/zed-industries/zed/pulls";
 10const DIVIDER = "-".repeat(80);
 11
 12main();
 13
 14async function main() {
 15  // Get the last two preview tags
 16  const [newTag, oldTag] = execFileSync(
 17    "git",
 18    ["tag", "--sort", "-committerdate"],
 19    { encoding: "utf8" },
 20  )
 21    .split("\n")
 22    .filter((t) => t.startsWith("v") && t.endsWith("-pre"));
 23
 24  // Print the previous release
 25  console.log(`Changes from ${oldTag} to ${newTag}\n`);
 26
 27  if (!GITHUB_ACCESS_TOKEN) {
 28    try {
 29      GITHUB_ACCESS_TOKEN = execFileSync("gh", ["auth", "token"]).toString();
 30    } catch (error) {
 31      console.log(error);
 32      console.log("No GITHUB_ACCESS_TOKEN, and no `gh auth token`");
 33      process.exit(1);
 34    }
 35  }
 36
 37  // Get the PRs merged between those two tags.
 38  const pullRequestNumbers = getPullRequestNumbers(oldTag, newTag);
 39
 40  // Get the PRs that were cherry-picked between main and the old tag.
 41  const existingPullRequestNumbers = new Set(
 42    getPullRequestNumbers("main", oldTag),
 43  );
 44
 45  // Filter out those existing PRs from the set of new PRs.
 46  const newPullRequestNumbers = pullRequestNumbers.filter(
 47    (number) => !existingPullRequestNumbers.has(number),
 48  );
 49
 50  // Fetch the pull requests from the GitHub API.
 51  console.log("Merged Pull requests:");
 52  console.log(DIVIDER);
 53  for (const pullRequestNumber of newPullRequestNumbers) {
 54    const pullRequestApiURL = `${PULL_REQUEST_API_URL}/${pullRequestNumber}`;
 55
 56    const response = await fetch(pullRequestApiURL, {
 57      headers: {
 58        Authorization: `token ${GITHUB_ACCESS_TOKEN}`,
 59      },
 60    });
 61
 62    const pullRequest = await response.json();
 63    const releaseNotesHeader = /^\s*Release Notes:(.+)/ims;
 64
 65    let releaseNotes = pullRequest.body || "";
 66    let contributor =
 67      pullRequest.user?.login ?? "Unable to identify contributor";
 68    const captures = releaseNotesHeader.exec(releaseNotes);
 69    let notes = captures ? captures[1] : "MISSING";
 70    notes = notes.trim();
 71
 72    if (SKIPPABLE_NOTE_REGEX.exec(notes) != null) {
 73      continue;
 74    }
 75
 76    let credit = getCreditString(pullRequestNumber, contributor);
 77
 78    console.log(`PR Title: ${pullRequest.title}`);
 79    console.log(`Credit: (${credit})`);
 80
 81    console.log("Release Notes:");
 82    console.log();
 83    console.log(notes);
 84
 85    console.log(DIVIDER);
 86  }
 87}
 88
 89function getCreditString(pullRequestNumber, contributor) {
 90  let credit = "";
 91
 92  if (pullRequestNumber) {
 93    let pullRequestMarkdownLink = `[#${pullRequestNumber}](${PULL_REQUEST_WEB_URL}/${pullRequestNumber})`;
 94    credit += pullRequestMarkdownLink;
 95  }
 96
 97  if (contributor) {
 98    const contributorMarkdownLink = `[${contributor}](${GITHUB_URL}/${contributor})`;
 99    credit += `; thanks ${contributorMarkdownLink}`;
100  }
101
102  return credit;
103}
104
105function getPullRequestNumbers(oldTag, newTag) {
106  const pullRequestNumbers = execFileSync(
107    "git",
108    ["log", `${oldTag}..${newTag}`, "--oneline"],
109    { encoding: "utf8" },
110  )
111    .split("\n")
112    .filter((line) => line.length > 0)
113    .map((line) => {
114      const match = line.match(/#(\d+)/);
115      return match ? match[1] : null;
116    })
117    .filter((line) => line);
118
119  return pullRequestNumbers;
120}