Add is_release_beta output in is_package_json_version_upgraded

This commit is contained in:
garronej 2021-12-01 14:40:14 +01:00
parent 5297ab00ab
commit e8c1eb1074
9 changed files with 324 additions and 84 deletions

View file

@ -22,7 +22,7 @@ type CoreLike = {
debug: (message: string) => void;
};
export const { setOutput } = setOutputFactory<"from_version" | "to_version" | "is_upgraded_version">();
export const { setOutput } = setOutputFactory<"from_version" | "to_version" | "is_upgraded_version" | "is_release_beta">();
export async function action(
_actionName: "is_package_json_version_upgraded",
@ -46,7 +46,7 @@ export async function action(
const { getLatestSemVersionedTag } = getLatestSemVersionedTagFactory({ octokit });
const { version: from_version } = await getLatestSemVersionedTag({ owner, repo })
const { version: from_version } = await getLatestSemVersionedTag({ owner, repo, "doIgnoreBeta": false })
.then(wrap => wrap === undefined ? { "version": NpmModuleVersion.parse("0.0.0") } : wrap);
core.debug(`Last version was ${NpmModuleVersion.stringify(from_version)}`);
@ -58,10 +58,13 @@ export async function action(
core.debug(`Is version upgraded: ${is_upgraded_version}`);
const is_release_beta= is_upgraded_version === "false" ? "false" : to_version.betaPreRelease !== undefined ? "true" : "false";
return {
"to_version": NpmModuleVersion.stringify(to_version),
"from_version": NpmModuleVersion.stringify(from_version),
is_upgraded_version
is_upgraded_version,
is_release_beta
};
}

View file

@ -15,7 +15,8 @@ export const outputNames = [
"npm_or_yarn",
"from_version",
"to_version",
"is_upgraded_version"
"is_upgraded_version",
"is_release_beta"
] as const;
@ -34,6 +35,7 @@ export function getOutputDescription(inputName: typeof outputNames[number]): str
case "from_version": return "Output of is_package_json_version_upgraded, string";
case "to_version": return "Output of is_package_json_version_upgraded, string";
case "is_upgraded_version": return "Output of is_package_json_version_upgraded, true|false";
case "is_release_beta": return "Output of is_package_json_version_upgraded, true|false";
}
}

View file

@ -4,6 +4,7 @@ export type NpmModuleVersion = {
major: number;
minor: number;
patch: number;
betaPreRelease?: number;
};
export namespace NpmModuleVersion {
@ -12,20 +13,28 @@ export namespace NpmModuleVersion {
const match = versionStr.match(/^([0-9]+)\.([0-9]+)\.([0-9]+)(?:-beta.([0-9]+))?/);
if( !match ){
if (!match) {
throw new Error(`${versionStr} is not a valid NPM version`);
}
return {
"major": parseInt(match[1]),
"minor": parseInt(match[2]),
"patch": parseInt(match[3])
"patch": parseInt(match[3]),
...(() => {
const str = match[4];
return str === undefined ?
{} :
{ "betaPreRelease": parseInt(str) };
})()
};
};
export function stringify(v: NpmModuleVersion) {
return `${v.major}.${v.minor}.${v.patch}`;
return `${v.major}.${v.minor}.${v.patch}${v.betaPreRelease === undefined ? "" : `-beta.${v.betaPreRelease}`}`;
}
/**
@ -37,18 +46,17 @@ export namespace NpmModuleVersion {
*/
export function compare(v1: NpmModuleVersion, v2: NpmModuleVersion): -1 | 0 | 1 {
const sign = (n: number): -1 | 0 | 1 => n === 0 ? 0 : (n < 0 ? -1 : 1);
const sign = (diff: number): -1 | 0 | 1 => diff === 0 ? 0 : (diff < 0 ? -1 : 1);
const noUndefined= (n: number | undefined)=> n ?? -1;
if (v1.major === v2.major) {
if (v1.minor === v2.minor) {
return sign(v1.patch - v2.patch);
} else {
return sign(v1.minor - v2.minor);
for (const level of ["major", "minor", "patch", "betaPreRelease"] as const) {
if (noUndefined(v1[level]) !== noUndefined(v2[level])) {
return sign(noUndefined(v1[level]) - noUndefined(v2[level]));
}
} else {
return sign(v1.major - v2.major);
}
return 0;
}
export function bumpType(
@ -56,34 +64,24 @@ export namespace NpmModuleVersion {
versionBehindStr: string;
versionAheadStr: string;
}
): "SAME" | "MAJOR" | "MINOR" | "PATCH" {
): "major" | "minor" | "patch" | "betaPreRelease" | "same" {
const versionAhead = parse(params.versionAheadStr);
const versionBehind = parse(params.versionBehindStr);
if( compare(versionBehind, versionAhead) === 1 ){
if (compare(versionBehind, versionAhead) === 1) {
throw new Error(`Version regression ${versionBehind} -> ${versionAhead}`);
}
if (versionBehind.major !== versionAhead.major) {
return "MAJOR";
} else if (versionBehind.minor !== versionAhead.minor) {
return "MINOR";
} else if (versionBehind.patch !== versionAhead.patch) {
return "PATCH";
} else {
return "SAME";
for (const level of ["major", "minor", "patch", "betaPreRelease"] as const) {
if (versionBehind[level] !== versionAhead[level]) {
return level;
}
}
return "same";
}
}

View file

@ -11,13 +11,14 @@ export function getLatestSemVersionedTagFactory(params: { octokit: Octokit; }) {
params: {
owner: string;
repo: string;
doIgnoreBeta: boolean;
}
): Promise<{
tag: string;
version: NpmModuleVersion;
} | undefined> {
const { owner, repo } = params;
const { owner, repo, doIgnoreBeta } = params;
const semVersionedTags: { tag: string; version: NpmModuleVersion; }[] = [];
@ -25,16 +26,21 @@ export function getLatestSemVersionedTagFactory(params: { octokit: Octokit; }) {
for await (const tag of listTags({ owner, repo })) {
const match = tag.match(/^v?([0-9]+\.[0-9]+\.[0-9]+)$/);
let version: NpmModuleVersion;
if (!match) {
try{
version = NpmModuleVersion.parse(tag.replace(/^[vV]?/, ""));
}catch{
continue;
}
semVersionedTags.push({
tag,
"version": NpmModuleVersion.parse( match[1])
});
if( doIgnoreBeta && version.betaPreRelease !== undefined ){
continue;
}
semVersionedTags.push({ tag, version });
}

View file

@ -8,6 +8,7 @@ import { NpmModuleVersion } from "./tools/NpmModuleVersion";
import { gitCommit } from "./tools/gitCommit";
import { getLatestSemVersionedTagFactory } from "./tools/octokit-addons/getLatestSemVersionedTag";
import { createOctokit } from "./tools/createOctokit";
import { assert } from "tsafe/assert";
export const { getActionParams } = getActionParamsFactory({
"inputNameSubset": [
@ -53,7 +54,7 @@ export async function action(
const { getLatestSemVersionedTag } = getLatestSemVersionedTagFactory({ octokit });
const { tag: branchBehind } = (await getLatestSemVersionedTag({ owner, repo })) ?? {};
const { tag: branchBehind } = (await getLatestSemVersionedTag({ owner, repo, "doIgnoreBeta": true })) ?? {};
if( branchBehind === undefined ){
@ -97,13 +98,22 @@ export async function action(
)
);
if( NpmModuleVersion.parse(branchAheadVersion).betaPreRelease !== undefined ){
core.warning(`Version on ${branch} is ${branchAheadVersion} it's a beta release, we do not update the CHANGELOG.md`);
return;
}
const bumpType = NpmModuleVersion.bumpType({
"versionAheadStr": branchAheadVersion,
"versionBehindStr": branchBehindVersion || "0.0.0"
});
if( bumpType === "SAME" ){
assert(bumpType !== "betaPreRelease");
if( bumpType === "same" ){
core.warning(`Version on ${branch} and ${branchBehind} are the same, not editing CHANGELOG.md`);
@ -162,7 +172,7 @@ function updateChangelog(
params: {
changelogRaw: string;
version: string;
bumpType: "MAJOR" | "MINOR" | "PATCH";
bumpType: "major" | "minor" | "patch";
body: string;
}
): { changelogRaw: string; } {
@ -180,7 +190,7 @@ function updateChangelog(
})();
const changelogRaw = [
`${bumpType === "MAJOR" ? "#" : (bumpType === "MINOR" ? "##" : "###")}`,
`${bumpType === "major" ? "#" : (bumpType === "minor" ? "##" : "###")}`,
` **${version}** (${dateString}) \n \n`,
`${body} \n \n`,
params.changelogRaw