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  if (!GITHUB_ACCESS_TOKEN) {
 16    try {
 17      GITHUB_ACCESS_TOKEN = execFileSync("gh", ["auth", "token"]).toString();
 18    } catch (error) {
 19      console.log(error);
 20      console.log("No GITHUB_ACCESS_TOKEN, and no `gh auth token`");
 21      process.exit(1);
 22    }
 23  }
 24
 25  const STAFF_MEMBERS = new Set(
 26    (
 27      await (
 28        await fetch(
 29          "https://api.github.com/orgs/zed-industries/teams/staff/members",
 30          {
 31            headers: {
 32              Authorization: `token ${GITHUB_ACCESS_TOKEN}`,
 33              Accept: "application/vnd.github+json",
 34            },
 35          },
 36        )
 37      ).json()
 38    ).map(({ login }) => login.toLowerCase()),
 39  );
 40
 41  const isStaffMember = (githubHandle) => {
 42    githubHandle = githubHandle.toLowerCase();
 43    return STAFF_MEMBERS.has(githubHandle);
 44  };
 45
 46  // Get the last two preview tags
 47  const [newTag, oldTag] = execFileSync(
 48    "git",
 49    ["tag", "--sort", "-committerdate"],
 50    { encoding: "utf8" },
 51  )
 52    .split("\n")
 53    .filter((t) => t.startsWith("v") && t.endsWith("-pre"));
 54
 55  // Print the previous release
 56  console.log(`Changes from ${oldTag} to ${newTag}\n`);
 57
 58  // Get the PRs merged between those two tags.
 59  const pullRequestNumbers = getPullRequestNumbers(oldTag, newTag);
 60
 61  // Get the PRs that were cherry-picked between main and the old tag.
 62  const existingPullRequestNumbers = new Set(
 63    getPullRequestNumbers("main", oldTag),
 64  );
 65
 66  // Filter out those existing PRs from the set of new PRs.
 67  const newPullRequestNumbers = pullRequestNumbers.filter(
 68    (number) => !existingPullRequestNumbers.has(number),
 69  );
 70
 71  // Fetch the pull requests from the GitHub API.
 72  console.log("Merged Pull requests:");
 73  console.log(DIVIDER);
 74  for (const pullRequestNumber of newPullRequestNumbers) {
 75    const pullRequestApiURL = `${PULL_REQUEST_API_URL}/${pullRequestNumber}`;
 76
 77    const response = await fetch(pullRequestApiURL, {
 78      headers: {
 79        Authorization: `token ${GITHUB_ACCESS_TOKEN}`,
 80      },
 81    });
 82
 83    const pullRequest = await response.json();
 84    const releaseNotesHeader = /^\s*Release Notes:(.+)/ims;
 85
 86    const releaseNotes = pullRequest.body || "";
 87    let contributor =
 88      pullRequest.user?.login ?? "Unable to identify contributor";
 89    const captures = releaseNotesHeader.exec(releaseNotes);
 90    let notes = captures ? captures[1] : "MISSING";
 91    notes = notes.trim();
 92    const isStaff = isStaffMember(contributor);
 93
 94    if (SKIPPABLE_NOTE_REGEX.exec(notes) != null) {
 95      continue;
 96    }
 97
 98    const credit = getCreditString(pullRequestNumber, contributor, isStaff);
 99    contributor = isStaff ? `${contributor} (staff)` : contributor;
100
101    console.log(`PR Title: ${pullRequest.title}`);
102    console.log(`Contributor: ${contributor}`);
103    console.log(`Credit: (${credit})`);
104
105    console.log("Release Notes:");
106    console.log();
107    console.log(notes);
108
109    console.log(DIVIDER);
110  }
111}
112
113function getCreditString(pullRequestNumber, contributor, isStaff) {
114  let credit = "";
115
116  if (pullRequestNumber) {
117    const pullRequestMarkdownLink = `[#${pullRequestNumber}](${PULL_REQUEST_WEB_URL}/${pullRequestNumber})`;
118    credit += pullRequestMarkdownLink;
119  }
120
121  if (contributor && !isStaff) {
122    const contributorMarkdownLink = `[${contributor}](${GITHUB_URL}/${contributor})`;
123    credit += `; thanks ${contributorMarkdownLink}`;
124  }
125
126  return credit;
127}
128
129function getPullRequestNumbers(oldTag, newTag) {
130  const pullRequestNumbers = execFileSync(
131    "git",
132    ["log", `${oldTag}..${newTag}`, "--oneline"],
133    { encoding: "utf8" },
134  )
135    .split("\n")
136    .filter((line) => line.length > 0)
137    .map((line) => {
138      const match = line.match(/#(\d+)/);
139      return match ? match[1] : null;
140    })
141    .filter((line) => line);
142
143  return pullRequestNumbers;
144}