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 =
9 "https://api.github.com/repos/zed-industries/zed/pulls";
10const DIVIDER = "-".repeat(80);
11
12main();
13
14async function main() {
15 const STAFF_MEMBERS = new Set(
16 (
17 await (
18 await fetch(
19 "https://api.github.com/orgs/zed-industries/teams/staff/members",
20 {
21 headers: {
22 Authorization: `token ${GITHUB_ACCESS_TOKEN}`,
23 Accept: "application/vnd.github+json",
24 },
25 },
26 )
27 ).json()
28 ).map(({ login }) => login.toLowerCase()),
29 );
30
31 const isStaffMember = (githubHandle) => {
32 githubHandle = githubHandle.toLowerCase();
33 return STAFF_MEMBERS.has(githubHandle);
34 };
35
36 // Get the last two preview tags
37 const [newTag, oldTag] = execFileSync(
38 "git",
39 ["tag", "--sort", "-committerdate"],
40 { encoding: "utf8" },
41 )
42 .split("\n")
43 .filter((t) => t.startsWith("v") && t.endsWith("-pre"));
44
45 // Print the previous release
46 console.log(`Changes from ${oldTag} to ${newTag}\n`);
47
48 if (!GITHUB_ACCESS_TOKEN) {
49 try {
50 GITHUB_ACCESS_TOKEN = execFileSync("gh", ["auth", "token"]).toString();
51 } catch (error) {
52 console.log(error);
53 console.log("No GITHUB_ACCESS_TOKEN, and no `gh auth token`");
54 process.exit(1);
55 }
56 }
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}