-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathgenerate-release-notes.ts
More file actions
270 lines (234 loc) · 6.96 KB
/
generate-release-notes.ts
File metadata and controls
270 lines (234 loc) · 6.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#!/usr/bin/env bun
/**
* Appends contributor attribution to an existing GitHub release created by GoReleaser.
*
* Usage: bun scripts/generate-release-notes.ts <tag>
* Env: GITHUB_REPOSITORY (default: junhoyeo/contrabass)
*/
export {};
import { execFileSync } from "node:child_process";
import { writeFileSync, unlinkSync } from "node:fs";
const REPO = process.env.GITHUB_REPOSITORY || "junhoyeo/contrabass";
interface Commit {
hash: string;
message: string;
authorName: string;
authorEmail: string;
}
interface PRInfo {
number: number;
title: string;
authorLogin: string;
}
interface ContributorEntry {
author: string;
message: string;
prNumber?: number;
}
interface NewContributor {
username: string;
firstPrNumber: number;
}
function run(command: string, args: string[], allowFailure = false): string {
try {
return execFileSync(command, args, {
encoding: "utf8",
stdio: ["ignore", "pipe", "pipe"],
}).trim();
} catch (error) {
if (allowFailure) return "";
if (error instanceof Error) {
throw new Error(`${command} ${args.join(" ")} failed: ${error.message}`);
}
throw error;
}
}
function runJson<T>(command: string, args: string[], allowFailure = false): T | null {
const output = run(command, args, allowFailure);
if (!output) return null;
try {
return JSON.parse(output) as T;
} catch {
return null;
}
}
function getPreviousTag(currentTag: string): string | null {
const tag = run("git", ["describe", "--tags", "--abbrev=0", `${currentTag}^`], true);
return tag || null;
}
function getTagDate(tag: string): string {
return run("git", ["log", "-1", "--format=%cI", tag]);
}
function getCommitsBetween(fromTag: string, toTag: string): Commit[] {
const output = run("git", [
"log",
`${fromTag}..${toTag}`,
"--format=%H%x1f%s%x1f%an%x1f%ae",
"--no-merges",
]);
if (!output) return [];
return output
.split("\n")
.filter((line) => line.trim())
.map((line) => {
const [hash = "", message = "", authorName = "", authorEmail = ""] =
line.split("\x1f");
return { hash, message, authorName, authorEmail };
})
.filter(
(entry) =>
entry.hash &&
!entry.message.startsWith("chore: bump version") &&
!entry.message.startsWith("Merge"),
);
}
function resolveGitHubUsername(email: string, fallbackName: string): string {
if (email.includes("@users.noreply.github.com")) {
const match = email.match(
/(?:\d+\+)?([^@]+)@users\.noreply\.github\.com/,
);
if (match?.[1]) return `@${match[1]}`;
}
const search = runJson<{ items?: Array<{ login?: string }> }>(
"gh",
["api", `/search/users?q=${encodeURIComponent(email)}+in:email`],
true,
);
const login = search?.items?.[0]?.login;
return login ? `@${login}` : fallbackName;
}
function findAssociatedPR(commitHash: string): PRInfo | null {
const result = runJson<
Array<{
number: number;
title: string;
state: string;
merged_at?: string | null;
user?: { login?: string };
}>
>("gh", ["api", `repos/${REPO}/commits/${commitHash}/pulls`], true);
if (!result?.length) return null;
const pr =
result.find((p) => p.merged_at != null) ??
result.find((p) => p.state === "closed") ??
result[0];
if (!pr?.number || !pr.user?.login) return null;
return { number: pr.number, title: pr.title, authorLogin: pr.user.login };
}
function isFirstContributionAfter(
login: string,
thresholdDate: string,
): NewContributor | null {
const result = runJson<Array<{ number: number; mergedAt: string }>>(
"gh",
[
"pr",
"list",
"--repo",
REPO,
"--state",
"merged",
"--author",
login,
"--json",
"number,mergedAt",
"--limit",
"200",
],
true,
);
if (!result?.length) return null;
const oldest = [...result].sort(
(a, b) => new Date(a.mergedAt).getTime() - new Date(b.mergedAt).getTime(),
)[0];
return new Date(oldest.mergedAt) > new Date(thresholdDate)
? { username: `@${login}`, firstPrNumber: oldest.number }
: null;
}
function getExistingReleaseBody(tag: string): string {
return run(
"gh",
["release", "view", tag, "--repo", REPO, "--json", "body", "-q", ".body"],
);
}
function updateReleaseBody(tag: string, body: string): void {
const tmpFile = `/tmp/contrabass-release-${Date.now()}.md`;
writeFileSync(tmpFile, body);
try {
run("gh", ["release", "edit", tag, "--repo", REPO, "--notes-file", tmpFile]);
} finally {
unlinkSync(tmpFile);
}
}
function main(): void {
const tag = process.argv[2];
if (!tag) {
console.error("Usage: bun scripts/generate-release-notes.ts <tag>");
process.exit(1);
}
const prevTag = getPreviousTag(tag);
if (!prevTag) {
console.error("No previous tag found, skipping contributor attribution.");
process.exit(0);
}
const prevTagDate = getTagDate(prevTag);
const commits = getCommitsBetween(prevTag, tag);
const contributors: ContributorEntry[] = [];
const candidateLogins = new Set<string>();
const seenPRs = new Set<number>();
for (const commit of commits) {
const prInfo = findAssociatedPR(commit.hash);
if (prInfo?.number && seenPRs.has(prInfo.number)) {
continue;
}
if (prInfo?.number) {
seenPRs.add(prInfo.number);
}
const author = prInfo
? `@${prInfo.authorLogin}`
: resolveGitHubUsername(commit.authorEmail, commit.authorName);
contributors.push({
author,
message: prInfo?.title || commit.message,
prNumber: prInfo?.number,
});
if (prInfo?.authorLogin) {
candidateLogins.add(prInfo.authorLogin);
}
}
if (contributors.length === 0) {
console.log("No contributors to attribute.");
return;
}
const newContributors = Array.from(candidateLogins)
.map((login) => isFirstContributionAfter(login, prevTagDate))
.filter((item): item is NewContributor => Boolean(item));
const appendLines: string[] = ["", "---", "", "## Contributors"];
for (const entry of contributors) {
const prLink = entry.prNumber
? ` in https://github.com/${REPO}/pull/${entry.prNumber}`
: "";
appendLines.push(`* ${entry.message} by ${entry.author}${prLink}`);
}
if (newContributors.length > 0) {
appendLines.push("", "## New Contributors");
for (const c of newContributors) {
appendLines.push(
`* ${c.username} made their first contribution in https://github.com/${REPO}/pull/${c.firstPrNumber}`,
);
}
}
appendLines.push(
"",
`**Full Changelog**: https://github.com/${REPO}/compare/${prevTag}...${tag}`,
);
const existingBody = getExistingReleaseBody(tag);
if (existingBody.includes("## Contributors")) {
console.log(`Release ${tag} already contains contributor attribution, skipping.`);
return;
}
const updatedBody = existingBody + appendLines.join("\n") + "\n";
updateReleaseBody(tag, updatedBody);
console.log(`Updated release ${tag} with contributor attribution.`);
}
main();