|
| 1 | +#!/usr/bin/env ts-node |
| 2 | +/* ============================================================================ |
| 3 | + * Copyright (c) Palo Alto Networks |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + * ========================================================================== */ |
| 8 | + |
| 9 | +import pkg from "../lerna.json"; |
| 10 | +import { getOutput } from "./utils/get-output"; |
| 11 | +import { printBanner, printSpacer } from "./utils/print-utils"; |
| 12 | + |
| 13 | +const ORG = "PaloAltoNetworks"; |
| 14 | +const REPO = "docusaurus-openapi-docs"; |
| 15 | +const BRANCH = "v2.0.0"; |
| 16 | + |
| 17 | +const COMMIT_FILTERS = [/\(release\) v.*/]; |
| 18 | + |
| 19 | +// Makes the script crash on unhandled rejections instead of silently |
| 20 | +// ignoring them. In the future, promise rejections that are not handled will |
| 21 | +// terminate the Node.js process with a non-zero exit code. |
| 22 | +process.on("unhandledRejection", (err) => { |
| 23 | + throw err; |
| 24 | +}); |
| 25 | + |
| 26 | +function findUpstreamMaster() { |
| 27 | + const remotes = getOutput("git remote -v").split(/\r?\n/); |
| 28 | + |
| 29 | + for (const remote of remotes) { |
| 30 | + const [name, url, method] = remote.split(/\s/); |
| 31 | + |
| 32 | + const r = new RegExp(`${ORG}\\/${REPO}(\\.git)?$`); |
| 33 | + |
| 34 | + if (r.test(url) && method === "(push)") { |
| 35 | + return `${name}/${BRANCH}`; |
| 36 | + } |
| 37 | + } |
| 38 | + return undefined; |
| 39 | +} |
| 40 | + |
| 41 | +function findLatestTag() { |
| 42 | + return getOutput(`git describe --tags --abbrev=0 --match "v*"`); |
| 43 | +} |
| 44 | + |
| 45 | +function getCommits(commitRange: string) { |
| 46 | + return getOutput(`git log --pretty="%s" ${commitRange}`).split(/\r?\n/); |
| 47 | +} |
| 48 | + |
| 49 | +function formatCommits(commits: string[]) { |
| 50 | + return commits |
| 51 | + .filter((c) => { |
| 52 | + for (const filter of COMMIT_FILTERS) { |
| 53 | + if (filter.test(c)) { |
| 54 | + return false; |
| 55 | + } |
| 56 | + } |
| 57 | + return true; |
| 58 | + }) |
| 59 | + .map((c) => { |
| 60 | + const r = /\(#(\d+)\)$/; |
| 61 | + return `- ${c.replace( |
| 62 | + r, |
| 63 | + `([#$1](https://github.com/${ORG}/${REPO}/pull/$1))` |
| 64 | + )}`; |
| 65 | + }); |
| 66 | +} |
| 67 | + |
| 68 | +function main() { |
| 69 | + const args = process.argv.slice(2); |
| 70 | + let commitRange; |
| 71 | + if (args.length > 0) { |
| 72 | + commitRange = args.join(" "); |
| 73 | + } else { |
| 74 | + const latestTag = findLatestTag(); |
| 75 | + if (latestTag === undefined) { |
| 76 | + console.error("Error: Unable to find the latest tag."); |
| 77 | + process.exit(1); |
| 78 | + } |
| 79 | + |
| 80 | + const upstream = findUpstreamMaster(); |
| 81 | + if (upstream === undefined) { |
| 82 | + console.error("Error: Unable to find the upstream."); |
| 83 | + process.exit(1); |
| 84 | + } |
| 85 | + commitRange = `${latestTag}...${upstream}`; |
| 86 | + } |
| 87 | + |
| 88 | + console.log(`Comparing ${commitRange}`); |
| 89 | + |
| 90 | + const commits = getCommits(commitRange); |
| 91 | + const formattedCommits = formatCommits(commits); |
| 92 | + |
| 93 | + if (formattedCommits.length === 0) { |
| 94 | + console.error("Error: There has been no changes since last release."); |
| 95 | + process.exit(1); |
| 96 | + } |
| 97 | + |
| 98 | + const date = new Date().toLocaleDateString("en-US", { |
| 99 | + month: "short", |
| 100 | + day: "numeric", |
| 101 | + year: "numeric", |
| 102 | + }); |
| 103 | + |
| 104 | + const changelog = ` |
| 105 | +## ${pkg.version} (${date}) |
| 106 | +
|
| 107 | +High level enhancements |
| 108 | +
|
| 109 | +- TODO HIGHLIGHTS |
| 110 | +
|
| 111 | +Other enhancements and bug fixes |
| 112 | +
|
| 113 | +${formattedCommits.join("\n")} |
| 114 | + `; |
| 115 | + |
| 116 | + printBanner("Prepend the following to CHANGELOG.md"); |
| 117 | + console.log(changelog); |
| 118 | + printSpacer(); |
| 119 | +} |
| 120 | + |
| 121 | +main(); |
0 commit comments