|
| 1 | +#!/usr/bin/env npx tsx |
| 2 | + |
| 3 | +import { parseArgs } from "node:util"; |
| 4 | + |
| 5 | +import * as core from "@actions/core"; |
| 6 | + |
| 7 | +import { OLDEST_SUPPORTED_MAJOR_VERSION } from "./config"; |
| 8 | + |
| 9 | +async function main() { |
| 10 | + const { values: options } = parseArgs({ |
| 11 | + options: { |
| 12 | + // The major version of the release |
| 13 | + "major-version": { |
| 14 | + type: "string", |
| 15 | + }, |
| 16 | + // The most recent tag published to the repository |
| 17 | + "latest-tag": { |
| 18 | + type: "string", |
| 19 | + }, |
| 20 | + }, |
| 21 | + strict: true, |
| 22 | + }); |
| 23 | + |
| 24 | + if (options["major-version"] === undefined) { |
| 25 | + throw Error("--major-version is required"); |
| 26 | + } |
| 27 | + if (options["latest-tag"] === undefined) { |
| 28 | + throw Error("--latest-tag is required"); |
| 29 | + } |
| 30 | + |
| 31 | + const majorVersion = Number.parseInt(options["major-version"].substring(1)); |
| 32 | + const latestTag = options["latest-tag"]; |
| 33 | + |
| 34 | + console.log(`major_version: v${majorVersion}`); |
| 35 | + console.log(`latest_tag: ${latestTag}`); |
| 36 | + |
| 37 | + // If this is a primary release, we backport to all supported branches, |
| 38 | + // so we check whether the major_version taken from the package.json |
| 39 | + // is greater than or equal to the latest tag pulled from the repo. |
| 40 | + // For example... |
| 41 | + // 'v1' >= 'v2' is False # we're operating from an older release branch and should not backport |
| 42 | + // 'v2' >= 'v2' is True # the normal case where we're updating the current version |
| 43 | + // 'v3' >= 'v2' is True # in this case we are making the first release of a new major version |
| 44 | + const latestTagMajor = Number.parseInt(latestTag.split(".")[0].substring(1)); |
| 45 | + const considerBackports = majorVersion >= latestTagMajor; |
| 46 | + |
| 47 | + core.setOutput("backport_source_branch", `releases/v${majorVersion}`); |
| 48 | + |
| 49 | + const backportTargetBranches: string[] = []; |
| 50 | + |
| 51 | + if (considerBackports) { |
| 52 | + for (let i = latestTagMajor - 1; i > 0; i--) { |
| 53 | + const branch_name = `releases/v${i}`; |
| 54 | + if (i >= OLDEST_SUPPORTED_MAJOR_VERSION) { |
| 55 | + backportTargetBranches.push(branch_name); |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + core.setOutput( |
| 61 | + "backport_target_branches", |
| 62 | + JSON.stringify(backportTargetBranches), |
| 63 | + ); |
| 64 | + |
| 65 | + process.exit(0); |
| 66 | +} |
| 67 | + |
| 68 | +// Only call `main` if this script was run directly. |
| 69 | +if (require.main === module) { |
| 70 | + void main(); |
| 71 | +} |
0 commit comments