-
Notifications
You must be signed in to change notification settings - Fork 119
/
changelog.js
393 lines (343 loc) · 9.97 KB
/
changelog.js
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
const { spawn, exec, execSync } = require("child_process");
const { inspect } = require("util");
const { resolve } = require("path");
const { writeFile } = require("fs");
const { stripIndents } = require("common-tags");
const { format } = require("date-fns");
const parseChangelog = require("changelog-parser");
const parseCommit = require("conventional-commits-parser").sync;
const nunjucks = require("nunjucks");
const _ = require("lodash");
/**
* The purpose of this file is to parse all commits since the last release tag
* v*.*.* and update the contents of CHANGELOG.md to prepare for a new release.
*/
const repo = "https://github.com/Esri/arcgis-rest-js";
function getTags() {
return new Promise((resolve, reject) => {
let tags = [];
const log = spawn("git", ["tag", "-l", "v*", "--sort", "v:refname"]);
log.stdout.on("data", data => {
tags = tags.concat(data.toString().split("\n"));
});
log.on("close", code => {
resolve(_.compact(tags));
});
});
}
function getFirstCommit() {
return new Promise((resolve, reject) => {
let commits = [];
const log = spawn("git", [
"rev-list",
"--max-parents=0",
"HEAD",
"--reverse"
]);
log.stdout.on("data", data => {
commits = commits.concat(data.toString().split("\n"));
});
log.on("close", code => {
resolve(commits[0]);
});
});
}
function getCommitData(from, to) {
return new Promise((resolve, reject) => {
const hash = "%H";
const shortHash = "%h";
const authorName = "%an";
const authorEmail = "%ae";
const date = "%aI";
const subject = "%s";
const defaultFormat = {
hash,
shortHash,
date,
subject,
author: { name: authorName, email: authorEmail }
};
const cmd = `git log ${from}..${to} --pretty=format:'${JSON.stringify(
defaultFormat
)},'`;
exec(cmd, (err, stdout, stderr) => {
if (err) return reject(err);
if (stderr) return reject(stderr);
/*
NOTE: you will get parsing errors on the next step if your commit messages have special chars like "
so you may need to uncomment the call to .replace() in the next line w/ something like:
.replace(/"returns"/g, "returns")
.replace(/\\/g, "\\\\")
*/
// order commits from most recent to least recent
const commits = JSON.parse("[" + stdout.slice(0, -1).replace(/"duration"/g, "duration").replace(/"expiration"/g, "expiration") + "]").reverse();
const today = new Date();
resolve({
previousVersion: /v\d\.\d\.\d/.test(from)
? from.replace("v", "")
: from,
version: to === "HEAD" ? getPackageVersion() : to.replace("v", ""),
date: format(today, "MMMM Do YYYY"),
commits
});
});
});
}
function getPackageVersion() {
return require(resolve(process.cwd(), "./lerna.json")).version;
}
function pairReleases(releases) {
return Promise.all(
releases.reduce((pairs, release, idx, allReleases) => {
const nextRelease = allReleases[idx + 1];
if (nextRelease) {
pairs.push([release, nextRelease]);
}
return pairs;
}, [])
);
}
function processCommitMessages(releaseData) {
return releaseData.map(release => {
release.commits = _(release.commits)
.map(commit => {
const parsedCommit = parseCommit(
execSync(
`git log ${commit.hash} -n1 --pretty=format:'%B'`
).toString(),
{
mergePattern: /^Merge pull request #(\d+) from (.*)$/,
mergeCorrespondence: ["id", "source"],
noteKeywords: [
"BREAKING CHANGE",
"ISSUES CLOSED",
"AFFECTS PACKAGES"
]
}
);
if (!parsedCommit.type || !parsedCommit.scope) {
return;
}
Object.assign(commit, parsedCommit);
const breaking = commit.notes.findIndex(
n => n.title === "BREAKING CHANGE"
);
if (breaking >= 0) {
commit.breakingChanges = commit.notes[breaking].text;
}
const issues = commit.notes.findIndex(n => n.title === "ISSUES CLOSED");
if (issues >= 0) {
const issuesClosed = commit.notes[issues].text
.replace(/\n.*/gm, "")
.replace(/\s/g, "")
.split(",")
.map(i => i.replace("#", ""));
const issuesReferenced = commit.references.map(r => r.issue);
commit.relatedIssues = _.uniq(
issuesClosed.concat(issuesReferenced)
).map(i => {
return {
issue: i,
url: `${repo}/issues/${i}`
};
});
}
return commit;
})
.compact()
.sortBy("type")
.value();
return release;
});
}
function getPackagesForCommit(commit) {
const idx = commit.notes.findIndex(note => note.title === "AFFECTS PACKAGES");
if (idx >= 0) {
const affectedPackages = commit.notes[idx].text.split("\n");
return affectedPackages;
}
return ["Other Changes"];
}
function findReleasedPackages(releases) {
return releases.map(release => {
release.packages = release.commits.reduce((packages, commit) => {
packages = packages.concat(getPackagesForCommit(commit));
return _(packages).compact().uniq().sortBy(p => p).value();
}, []);
return release;
});
}
function hasBreakingChanges(commit) {
return commit.notes.find(n => n.title === "BREAKING CHANGE");
}
function groupCommitsByPackage(releases) {
return releases.map(release => {
release.groups = release.packages.reduce((groups, package) => {
const commitsForPackage = release.commits.filter(commit => {
return getPackagesForCommit(commit).some(
commitPackage => commitPackage === package
);
});
const [breaking, nonBreaking] = _.partition(
commitsForPackage,
hasBreakingChanges
);
if (breaking.length === 0) {
groups[package] = Object.assign(
{},
_.groupBy(_.sortBy(nonBreaking, "type"), "type")
);
} else {
groups[package] = Object.assign(
{ breaking },
_.groupBy(nonBreaking, "type")
);
}
return groups;
}, {});
delete release.commits;
return release;
});
}
function getTypeName(type) {
switch (type) {
case "feat":
return "New Features";
case "fix":
return "Bug Fixes";
case "docs":
return "Documentation";
case "style":
return "Code Style";
case "Refactor":
return "Refactoring";
case "perf":
return "Performance";
case "test":
return "Tests";
case "chore":
return "Chores";
case "revert":
return "Reverts";
case "breaking":
return "Breaking Changes";
case "WIP":
return "Work In Progress";
default:
return "Misc.";
}
}
const template = stripIndents`
{%- macro link(repo, shortHash, hash) -%}
[\`{{ shortHash }}\`]({{repo}}/commit/{{ hash }})
{%- endmacro %}
{%- macro relatedIssues(issues) -%}
{%- for i in issues -%}
{{" "}}[#{{i.issue}}]({{i.url}})
{%- endfor -%}
{%- endmacro %}
{%- for release in releases %}
## [{{ release.version }}] - {{ release.date }}
{% for package, groups in release.groups %}
### {{ package }}
{% for type, commits in groups -%}
* {{ getTypeName(type) }}
{% for c in commits -%}
{{" "}}* **{{ c.scope }}**: {{ c.subject }} {{link(repo, c.shortHash, c.hash)}}{{relatedIssues(c.relatedIssues)}}
{% endfor %}
{%- endfor %}
{%- endfor %}
{%- endfor %}
`;
function buildMarkdown(releases) {
return nunjucks.renderString(template, { releases, getTypeName, repo });
}
logStep = v => {
console.log(inspect(v, { depth: 7 }));
return v;
};
const changeLogTemplate = stripIndents`
# {{title}}
{{description}}
{{newVersion}}
{%- for version in oldVersions %}
## {{version.title}}
{{version.body}}
{% endfor %}
{% for link in links -%}
[{{link.ref}}]: {{link.href}} "{{link.title}}"
{% endfor -%}
`;
function getReleases() {
return Promise.all([
getTags(),
getFirstCommit()
]).then(([releases, firstCommit]) => {
if (releases.length === 0) {
return [];
}
releases.unshift(firstCommit);
releases.push("HEAD");
return releases;
});
}
function getChangelogData() {
return new Promise((resolve, reject) => {
parseChangelog("CHANGELOG.md", function(error, changelog) {
if (error) {
reject(error);
return;
}
resolve(changelog);
});
});
}
function filterReleases(releases) {
return releases.slice(-2);
}
getReleases()
.then(releases => filterReleases(releases))
.then(releases => pairReleases(releases))
.then(pairs =>
Promise.all(pairs.map(([from, to]) => getCommitData(from, to)))
)
.then(releaseData => processCommitMessages(releaseData))
.then(releaseData => findReleasedPackages(releaseData))
.then(releaseData => groupCommitsByPackage(releaseData))
.then(releaseData => buildMarkdown(releaseData))
.then(newVersion => {
return Promise.all([
getReleases().then(pairReleases),
getChangelogData(),
Promise.resolve(newVersion)
]);
})
.then(([pairs, changelog, newVersion]) => {
const links = pairs.map(([from, to]) => {
to = to === "HEAD" ? "v" + getPackageVersion() : to;
return {
ref: to.replace("v", ""),
title: to,
href: `${repo}/compare/${from}...${to}`
};
});
if (links.length) {
links.push({
ref: "HEAD",
title: "Unreleased Changes",
href: `${repo}/compare/${_.last(links).title}...HEAD`
});
}
const rendered = nunjucks.renderString(changeLogTemplate, {
title: changelog.title,
description: changelog.description,
oldVersions: changelog.versions,
newVersion: newVersion,
links,
repo
});
writeFile("CHANGELOG.md", rendered, function(e) {});
})
.catch(error => {
console.error(error.stack);
});