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