get-preview-channel-changes

  1#!/usr/bin/env node --redirect-warnings=/dev/null
  2
  3const { execFileSync } = require("child_process");
  4const { GITHUB_ACCESS_TOKEN } = process.env;
  5const PR_REGEX = /#\d+/ // Ex: matches on #4241
  6const FIXES_REGEX = /(fixes|closes|completes) (.+[/#]\d+.*)$/im;
  7
  8main();
  9
 10async function main() {
 11  // Get the last two preview tags
 12  const [newTag, oldTag] = execFileSync(
 13    "git",
 14    ["tag", "--sort", "-committerdate"],
 15    { encoding: "utf8" }
 16  )
 17    .split("\n")
 18    .filter((t) => t.startsWith("v") && t.endsWith("-pre"));
 19
 20  // Print the previous release
 21  console.log(`Changes from ${oldTag} to ${newTag}\n`);
 22
 23  let hasProtocolChanges = false;
 24  try {
 25    execFileSync("git", ["diff", oldTag, newTag, "--exit-code", "--", "crates/rpc"]).status != 0;
 26  } catch (error) {
 27    hasProtocolChanges = true;
 28  }
 29
 30  if (hasProtocolChanges) {
 31    console.warn("\033[31;1;4mRPC protocol changes, server should be re-deployed\033[0m\n");
 32  } else {
 33    console.log("No RPC protocol changes\n");
 34  }
 35
 36  // Get the PRs merged between those two tags.
 37  const pullRequestNumbers = getPullRequestNumbers(oldTag, newTag);
 38
 39  // Get the PRs that were cherry-picked between main and the old tag.
 40  const existingPullRequestNumbers = new Set(getPullRequestNumbers("main", oldTag));
 41
 42  // Filter out those existing PRs from the set of new PRs.
 43  const newPullRequestNumbers = pullRequestNumbers.filter(number => !existingPullRequestNumbers.has(number));
 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    console.log("*", pullRequest.title);
 60    console.log("  PR URL:    ", webURL);
 61
 62    // If the pull request contains a 'closes' line, print the closed issue.
 63    const fixesMatch = (pullRequest.body || "").match(FIXES_REGEX);
 64    if (fixesMatch) {
 65      const fixedIssueURL = fixesMatch[2];
 66      console.log("  Issue URL:    ", fixedIssueURL);
 67    }
 68
 69    let releaseNotes = (pullRequest.body || "").split("Release Notes:")[1];
 70
 71    if (releaseNotes) {
 72      releaseNotes = releaseNotes.trim().split("\n")
 73      console.log("  Release Notes:");
 74
 75      for (const line of releaseNotes) {
 76        console.log(`    ${line}`);
 77      }
 78    }
 79
 80    console.log()
 81  }
 82}
 83
 84function getPullRequestNumbers(oldTag, newTag) {
 85  const pullRequestNumbers = execFileSync(
 86    "git",
 87    [
 88      "log",
 89      `${oldTag}..${newTag}`,
 90      "--oneline"
 91    ],
 92    { encoding: "utf8" }
 93  )
 94    .split("\n")
 95    .filter(line => line.length > 0)
 96    .map(line => {
 97      const match = line.match(/#(\d+)/);
 98      return match ? match[1] : null;
 99    })
100    .filter(line => line);
101
102  return pullRequestNumbers;
103}