forked from PaloAltoNetworks/docusaurus-openapi-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
changelog.ts
executable file
·121 lines (97 loc) · 3.01 KB
/
changelog.ts
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
#!/usr/bin/env ts-node
/* ============================================================================
* Copyright (c) Palo Alto Networks
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
* ========================================================================== */
import pkg from "../lerna.json";
import { getOutput } from "./utils/get-output";
import { printBanner, printSpacer } from "./utils/print-utils";
const ORG = "PaloAltoNetworks";
const REPO = "docusaurus-openapi-docs";
const BRANCH = "main";
const COMMIT_FILTERS = [/\(release\) v.*/];
// Makes the script crash on unhandled rejections instead of silently
// ignoring them. In the future, promise rejections that are not handled will
// terminate the Node.js process with a non-zero exit code.
process.on("unhandledRejection", (err) => {
throw err;
});
function findUpstreamMaster() {
const remotes = getOutput("git remote -v").split(/\r?\n/);
for (const remote of remotes) {
const [name, url, method] = remote.split(/\s/);
const r = new RegExp(`${ORG}\\/${REPO}(\\.git)?$`);
if (r.test(url) && method === "(push)") {
return `${name}/${BRANCH}`;
}
}
return undefined;
}
function findLatestTag() {
return getOutput(`git describe --tags --abbrev=0 --match "v*"`);
}
function getCommits(commitRange: string) {
return getOutput(`git log --pretty="%s" ${commitRange}`).split(/\r?\n/);
}
function formatCommits(commits: string[]) {
return commits
.filter((c) => {
for (const filter of COMMIT_FILTERS) {
if (filter.test(c)) {
return false;
}
}
return true;
})
.map((c) => {
const r = /\(#(\d+)\)$/;
return `- ${c.replace(
r,
`([#$1](https://github.com/${ORG}/${REPO}/pull/$1))`
)}`;
});
}
function main() {
const args = process.argv.slice(2);
let commitRange;
if (args.length > 0) {
commitRange = args.join(" ");
} else {
const latestTag = findLatestTag();
if (latestTag === undefined) {
console.error("Error: Unable to find the latest tag.");
process.exit(1);
}
const upstream = findUpstreamMaster();
if (upstream === undefined) {
console.error("Error: Unable to find the upstream.");
process.exit(1);
}
commitRange = `${latestTag}...${upstream}`;
}
console.log(`Comparing ${commitRange}`);
const commits = getCommits(commitRange);
const formattedCommits = formatCommits(commits);
if (formattedCommits.length === 0) {
console.error("Error: There has been no changes since last release.");
process.exit(1);
}
const date = new Date().toLocaleDateString("en-US", {
month: "short",
day: "numeric",
year: "numeric",
});
const changelog = `
## ${pkg.version} (${date})
High level enhancements
- TODO HIGHLIGHTS
Other enhancements and bug fixes
${formattedCommits.join("\n")}
`;
printBanner("Prepend the following to CHANGELOG.md");
console.log(changelog);
printSpacer();
}
main();