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    let contributor = pullRequest.user.login || "";
 63    const captures = releaseNotesHeader.exec(releaseNotes);
 64    const notes = captures ? captures[1] : "MISSING";
 65    const skippableNoteRegex = /^\s*-?\s*n\/?a\s*/ims;
 66
 67    if (skippableNoteRegex.exec(notes) != null) {
 68      continue;
 69    }
 70    console.log("*", pullRequest.title);
 71    console.log("  PR URL:    ", webURL);
 72    console.log("  Author:    ", contributor);
 73
 74    // If the pull request contains a 'closes' line, print the closed issue.
 75    const fixesMatch = (pullRequest.body || "").match(FIXES_REGEX);
 76    if (fixesMatch) {
 77      const fixedIssueURL = fixesMatch[2];
 78      console.log("  Issue URL:    ", fixedIssueURL);
 79    }
 80
 81    releaseNotes = notes.trim().split("\n");
 82    console.log("  Release Notes:");
 83
 84    for (const line of releaseNotes) {
 85      console.log(`    ${line}`);
 86    }
 87
 88    console.log();
 89  }
 90}
 91
 92function getPullRequestNumbers(oldTag, newTag) {
 93  const pullRequestNumbers = execFileSync(
 94    "git",
 95    ["log", `${oldTag}..${newTag}`, "--oneline"],
 96    { encoding: "utf8" },
 97  )
 98    .split("\n")
 99    .filter((line) => line.length > 0)
100    .map((line) => {
101      const match = line.match(/#(\d+)/);
102      return match ? match[1] : null;
103    })
104    .filter((line) => line);
105
106  return pullRequestNumbers;
107}