Skip to content

Commit 250c858

Browse files
committed
Refactor backport computation into computeReleaseBranches
1 parent 5abdb52 commit 250c858

File tree

1 file changed

+61
-26
lines changed

1 file changed

+61
-26
lines changed

pr-checks/release-branches.ts

Lines changed: 61 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,57 @@ import * as core from "@actions/core";
66

77
import { OLDEST_SUPPORTED_MAJOR_VERSION } from "./config";
88

9+
/** The results of checking which release branches to backport to. */
10+
export interface BackportInfo {
11+
/** The source release branch. */
12+
backportSourceBranch: string;
13+
/**
14+
* The computed release branches we should backport to.
15+
* Will be empty if there are no branches we need to backport to.
16+
*/
17+
backportTargetBranches: string[];
18+
}
19+
20+
/**
21+
* Compute the backport source and target branches for a release.
22+
*
23+
* @param majorVersion - The major version string (e.g. "v4").
24+
* @param latestTag - The most recent tag published to the repository (e.g. "v4.32.6").
25+
* @param oldestSupportedMajorVersion - The oldest supported major version number.
26+
* @returns The names of the source branch and target branches.
27+
*/
28+
export function computeBackportBranches(
29+
majorVersion: string,
30+
latestTag: string,
31+
oldestSupportedMajorVersion: number,
32+
): BackportInfo {
33+
const majorVersionNumber = Number.parseInt(majorVersion.substring(1));
34+
const latestTagMajor = Number.parseInt(latestTag.split(".")[0].substring(1));
35+
36+
// If this is a primary release, we backport to all supported branches,
37+
// so we check whether the majorVersion taken from the package.json
38+
// is greater than or equal to the latest tag pulled from the repo.
39+
// For example...
40+
// 'v1' >= 'v2' is False # we're operating from an older release branch and should not backport
41+
// 'v2' >= 'v2' is True # the normal case where we're updating the current version
42+
// 'v3' >= 'v2' is True # in this case we are making the first release of a new major version
43+
const considerBackports = majorVersionNumber >= latestTagMajor;
44+
45+
const backportSourceBranch = `releases/v${majorVersionNumber}`;
46+
const backportTargetBranches: string[] = [];
47+
48+
if (considerBackports) {
49+
for (let i = majorVersionNumber - 1; i > 0; i--) {
50+
const branch_name = `releases/v${i}`;
51+
if (i >= oldestSupportedMajorVersion) {
52+
backportTargetBranches.push(branch_name);
53+
}
54+
}
55+
}
56+
57+
return { backportSourceBranch, backportTargetBranches };
58+
}
59+
960
async function main() {
1061
const { values: options } = parseArgs({
1162
options: {
@@ -28,38 +79,22 @@ async function main() {
2879
throw Error("--latest-tag is required");
2980
}
3081

31-
const majorVersion = Number.parseInt(options["major-version"].substring(1));
82+
const majorVersion = options["major-version"];
3283
const latestTag = options["latest-tag"];
3384

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[] = [];
85+
console.log(`Major version: ${majorVersion}`);
86+
console.log(`Latest tag: ${latestTag}`);
5087

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-
}
88+
const result = computeBackportBranches(
89+
majorVersion,
90+
latestTag,
91+
OLDEST_SUPPORTED_MAJOR_VERSION,
92+
);
5993

94+
core.setOutput("backport_source_branch", result.backportSourceBranch);
6095
core.setOutput(
6196
"backport_target_branches",
62-
JSON.stringify(backportTargetBranches),
97+
JSON.stringify(result.backportTargetBranches),
6398
);
6499

65100
process.exit(0);

0 commit comments

Comments
 (0)