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 FIXES_REGEX = /(fixes|closes|completes) (.+[/#]\d+.*)$/im;
  6
  7main();
  8
  9async function main() {
 10  // Get the last two preview tags
 11  const [newTag, oldTag] = execFileSync(
 12    "git",
 13    ["tag", "--sort", "-committerdate"],
 14    { encoding: "utf8" },
 15  )
 16    .split("\n")
 17    .filter((t) => t.startsWith("v") && t.endsWith("-pre"));
 18
 19  // Print the previous release
 20  console.log(`Changes from ${oldTag} to ${newTag}\n`);
 21
 22  if (!GITHUB_ACCESS_TOKEN) {
 23    try {
 24      GITHUB_ACCESS_TOKEN = execFileSync("gh", ["auth", "token"]).toString();
 25    } catch (error) {
 26      console.log(error);
 27      console.log("No GITHUB_ACCESS_TOKEN, and no `gh auth token`");
 28      process.exit(1);
 29    }
 30  }
 31
 32  // Get the PRs merged between those two tags.
 33  const pullRequestNumbers = getPullRequestNumbers(oldTag, newTag);
 34
 35  // Get the PRs that were cherry-picked between main and the old tag.
 36  const existingPullRequestNumbers = new Set(
 37    getPullRequestNumbers("main", oldTag),
 38  );
 39
 40  // Filter out those existing PRs from the set of new PRs.
 41  const newPullRequestNumbers = pullRequestNumbers.filter(
 42    (number) => !existingPullRequestNumbers.has(number),
 43  );
 44
 45  // Fetch the pull requests from the GitHub API.
 46  console.log("Merged Pull requests:");
 47  for (const pullRequestNumber of newPullRequestNumbers) {
 48    const webURL = `https://github.com/zed-industries/zed/pull/${pullRequestNumber}`;
 49    const apiURL = `https://api.github.com/repos/zed-industries/zed/pulls/${pullRequestNumber}`;
 50
 51    const response = await fetch(apiURL, {
 52      headers: {
 53        Authorization: `token ${GITHUB_ACCESS_TOKEN}`,
 54      },
 55    });
 56
 57    // Print the pull request title and URL.
 58    const pullRequest = await response.json();
 59    const releaseNotesHeader = /^\s*Release Notes:(.+)/ims;
 60
 61    let releaseNotes = pullRequest.body || "";
 62    const captures = releaseNotesHeader.exec(releaseNotes);
 63    const notes = captures ? captures[1] : "MISSING";
 64    const skippableNoteRegex = /^\s*-?\s*n\/?a\s*/ims;
 65
 66    if (skippableNoteRegex.exec(notes) != null) {
 67      continue;
 68    }
 69    console.log("*", pullRequest.title);
 70    console.log("  PR URL:    ", webURL);
 71
 72    // If the pull request contains a 'closes' line, print the closed issue.
 73    const fixesMatch = (pullRequest.body || "").match(FIXES_REGEX);
 74    if (fixesMatch) {
 75      const fixedIssueURL = fixesMatch[2];
 76      console.log("  Issue URL:    ", fixedIssueURL);
 77    }
 78
 79    releaseNotes = notes.trim().split("\n");
 80    console.log("  Release Notes:");
 81
 82    for (const line of releaseNotes) {
 83      console.log(`    ${line}`);
 84    }
 85
 86    console.log();
 87  }
 88}
 89
 90function getPullRequestNumbers(oldTag, newTag) {
 91  const pullRequestNumbers = execFileSync(
 92    "git",
 93    ["log", `${oldTag}..${newTag}`, "--oneline"],
 94    { encoding: "utf8" },
 95  )
 96    .split("\n")
 97    .filter((line) => line.length > 0)
 98    .map((line) => {
 99      const match = line.match(/#(\d+)/);
100      return match ? match[1] : null;
101    })
102    .filter((line) => line);
103
104  return pullRequestNumbers;
105}