-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathgenerate-changelog.ts
More file actions
303 lines (246 loc) · 8.17 KB
/
generate-changelog.ts
File metadata and controls
303 lines (246 loc) · 8.17 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import { readFileSync } from 'fs';
import { join } from 'path';
import { getNewGitCommits } from './get-commit-list';
type EntryType = 'important' | 'other' | 'internal';
interface ChangelogEntry {
type: EntryType;
content: string;
sortKey: string;
prNumber: string | null;
}
// ============================================================================
// Changelog Parsing
// ============================================================================
interface ParsedChangelog {
importantChanges: ChangelogEntry[];
otherChanges: ChangelogEntry[];
internalChanges: ChangelogEntry[];
changelogPRs: Set<string>;
contributorsLine: string;
}
function getUnreleasedSection(content: string): string[] {
const lines = content.split('\n');
const unreleasedIndex = lines.findIndex(line => line.trim() === '## Unreleased');
if (unreleasedIndex === -1) {
// eslint-disable-next-line no-console
console.error('Could not find "## Unreleased" section in CHANGELOG.md');
process.exit(1);
}
const nextVersionIndex = lines.findIndex((line, index) => index > unreleasedIndex && /^## \d+\.\d+\.\d+/.test(line));
if (nextVersionIndex === -1) {
// eslint-disable-next-line no-console
console.error('Could not find next version section after "## Unreleased"');
process.exit(1);
}
return lines.slice(unreleasedIndex + 1, nextVersionIndex);
}
function createEntry(content: string, type: EntryType): ChangelogEntry {
const firstLine = content.split('\n')[0] ?? content;
const prNumber = extractPRNumber(firstLine);
return {
type,
content,
sortKey: extractSortKey(firstLine),
prNumber,
};
}
function parseChangelog(unreleasedLines: string[]): ParsedChangelog {
const importantChanges: ChangelogEntry[] = [];
const otherChanges: ChangelogEntry[] = [];
const internalChanges: ChangelogEntry[] = [];
const changelogPRs = new Set<string>();
let contributorsLine = '';
let currentEntry: string[] = [];
let currentType: EntryType | null = null;
let inDetailsBlock = false;
let detailsContent: string[] = [];
const addEntry = (entry: ChangelogEntry): void => {
if (entry.prNumber) {
changelogPRs.add(entry.prNumber);
}
if (entry.type === 'important') {
importantChanges.push(entry);
} else if (entry.type === 'internal') {
internalChanges.push(entry);
} else {
otherChanges.push(entry);
}
};
const flushCurrentEntry = (): void => {
if (currentEntry.length === 0 || !currentType) return;
// Remove trailing empty lines from the entry
while (currentEntry.length > 0 && !currentEntry[currentEntry.length - 1]?.trim()) {
currentEntry.pop();
}
if (currentEntry.length === 0) return;
const entry = createEntry(currentEntry.join('\n'), currentType);
addEntry(entry);
currentEntry = [];
currentType = null;
};
const processDetailsContent = (): void => {
for (const line of detailsContent) {
const trimmed = line.trim();
if (trimmed.startsWith('-') && trimmed.includes('(#')) {
const entry = createEntry(trimmed, 'internal');
addEntry(entry);
}
}
detailsContent = [];
};
for (const line of unreleasedLines) {
// Skip undefined/null lines
if (line == null) continue;
// Skip empty lines at the start of an entry
if (!line.trim() && currentEntry.length === 0) continue;
// Skip quote lines
if (isQuoteLine(line)) continue;
// Capture contributors line
if (isContributorsLine(line)) {
contributorsLine = line;
continue;
}
// Skip section headings
if (isSectionHeading(line)) {
flushCurrentEntry();
continue;
}
// Handle details block
if (line.includes('<details>')) {
inDetailsBlock = true;
detailsContent = [];
continue;
}
if (line.includes('</details>')) {
inDetailsBlock = false;
processDetailsContent();
continue;
}
if (inDetailsBlock) {
if (!line.includes('<summary>')) {
detailsContent.push(line);
}
continue;
}
// Handle regular entries
if (line.trim().startsWith('- ')) {
flushCurrentEntry();
currentEntry = [line];
currentType = determineEntryType(line);
} else if (currentEntry.length > 0) {
currentEntry.push(line);
}
}
flushCurrentEntry();
return { importantChanges, otherChanges, internalChanges, changelogPRs, contributorsLine };
}
// ============================================================================
// Output Generation
// ============================================================================
export function sortEntries(entries: ChangelogEntry[]): void {
entries.sort((a, b) => a.sortKey.localeCompare(b.sortKey));
}
function generateOutput(
importantChanges: ChangelogEntry[],
otherChanges: ChangelogEntry[],
internalChanges: ChangelogEntry[],
contributorsLine: string,
): string {
const output: string[] = [];
if (importantChanges.length > 0) {
output.push('### Important Changes', '');
for (const entry of importantChanges) {
output.push(entry.content, '');
}
}
if (otherChanges.length > 0) {
output.push('### Other Changes', '');
for (const entry of otherChanges) {
output.push(entry.content);
}
output.push('');
}
if (internalChanges.length > 0) {
output.push('<details>', ' <summary><strong>Internal Changes</strong></summary>', '');
for (const entry of internalChanges) {
output.push(entry.content);
}
output.push('', '</details>', '');
}
if (contributorsLine) {
output.push(contributorsLine);
}
return output.join('\n');
}
// ============================================================================
// Main
// ============================================================================
function run(): void {
const changelogPath = join(__dirname, '..', 'CHANGELOG.md');
const changelogContent = readFileSync(changelogPath, 'utf-8');
const unreleasedLines = getUnreleasedSection(changelogContent);
// Parse existing changelog entries
const { importantChanges, otherChanges, internalChanges, changelogPRs, contributorsLine } =
parseChangelog(unreleasedLines);
// Add new git commits that aren't already in the changelog
for (const commit of getNewGitCommits()) {
const prNumber = extractPRNumber(commit);
// Skip duplicates
if (prNumber && changelogPRs.has(prNumber)) {
continue;
}
const entry = createEntry(commit, isInternalCommit(commit) ? 'internal' : 'other');
if (entry.type === 'internal') {
internalChanges.push(entry);
} else {
otherChanges.push(entry);
}
}
// Sort all categories
sortEntries(importantChanges);
sortEntries(otherChanges);
sortEntries(internalChanges);
// eslint-disable-next-line no-console
console.log(generateOutput(importantChanges, otherChanges, internalChanges, contributorsLine));
}
// ============================================================================
// Helper Functions
// ============================================================================
function extractPRNumber(line: string): string | null {
const match = line.match(/#(\d+)/);
return match?.[1] ?? null;
}
function extractSortKey(line: string): string {
return line
.trim()
.replace(/^- /, '')
.replace(/\*\*/g, '')
.replace(/\s*\(\[#\d+\].*?\)\s*$/, '')
.toLowerCase();
}
function isQuoteLine(line: string): boolean {
return line.includes('—') && (line.includes('Wayne Gretzky') || line.includes('Michael Scott'));
}
function isContributorsLine(line: string): boolean {
return line.includes('Work in this release was contributed by');
}
function isSectionHeading(line: string): boolean {
const trimmed = line.trim();
return trimmed === '### Important Changes' || trimmed === '### Other Changes';
}
function isInternalCommit(line: string): boolean {
return /^- (chore|ref|test|meta)/.test(line.trim());
}
function isImportantEntry(line: string): boolean {
return line.includes('**feat') || line.includes('**fix');
}
function determineEntryType(line: string): EntryType {
if (isImportantEntry(line)) {
return 'important';
}
if (isInternalCommit(line)) {
return 'internal';
}
return 'other';
}
run();