Skip to content

Commit f6c7f63

Browse files
committed
Ignore pre-release parts when comparing GHES versions
1 parent 6370c01 commit f6c7f63

File tree

9 files changed

+62
-35
lines changed

9 files changed

+62
-35
lines changed

lib/upload-lib.js

Lines changed: 2 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/upload-lib.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/upload-lib.test.js

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/upload-lib.test.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/util.js

Lines changed: 16 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/util.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/upload-lib.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,19 @@ test("throwIfCombineSarifFilesDisabled when on GHES 3.18", async (t) => {
548548
);
549549
});
550550

551+
test("throwIfCombineSarifFilesDisabled with an invalid GHES version", async (t) => {
552+
await t.notThrowsAsync(
553+
uploadLib.throwIfCombineSarifFilesDisabled(
554+
[createMockSarif("abc", "def"), createMockSarif("abc", "def")],
555+
createFeatures([]),
556+
{
557+
type: GitHubVariant.GHES,
558+
version: "foobar",
559+
},
560+
),
561+
);
562+
});
563+
551564
test("throwIfCombineSarifFilesDisabled with only 1 run", async (t) => {
552565
await t.notThrowsAsync(
553566
uploadLib.throwIfCombineSarifFilesDisabled(

src/upload-lib.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import * as core from "@actions/core";
66
import { OctokitResponse } from "@octokit/types";
77
import fileUrl from "file-url";
88
import * as jsonschema from "jsonschema";
9-
import * as semver from "semver";
109

1110
import * as actionsUtil from "./actions-util";
1211
import { getOptionalInput, getRequiredInput } from "./actions-util";
@@ -30,7 +29,7 @@ import {
3029
getRequiredEnvParam,
3130
GitHubVariant,
3231
GitHubVersion,
33-
parseGhesVersion,
32+
satisfiesGHESVersion,
3433
SarifFile,
3534
SarifRun,
3635
} from "./util";
@@ -133,7 +132,7 @@ export async function shouldShowCombineSarifFilesDeprecationWarning(
133132
// Do not show this warning on GHES versions before 3.14.0
134133
if (
135134
githubVersion.type === GitHubVariant.GHES &&
136-
semver.lt(parseGhesVersion(githubVersion.version), "3.14.0")
135+
satisfiesGHESVersion(githubVersion.version, "<3.14", true)
137136
) {
138137
return false;
139138
}
@@ -178,7 +177,7 @@ async function shouldDisableCombineSarifFiles(
178177
) {
179178
if (githubVersion.type === GitHubVariant.GHES) {
180179
// Never block on GHES versions before 3.18.
181-
if (semver.lt(parseGhesVersion(githubVersion.version), "3.18.0-0")) {
180+
if (satisfiesGHESVersion(githubVersion.version, "<3.18", true)) {
182181
return false;
183182
}
184183
} else {

src/util.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,20 +1133,28 @@ export function checkActionVersion(
11331133
}
11341134

11351135
/**
1136-
* This will parse a GitHub Enterprise Server version string into a SemVer object.
1136+
* This will check whether the given GitHub version satisfies the given range,
1137+
* taking into account that a range like >=3.18 will also match the GHES 3.18
1138+
* pre-release/RC versions.
11371139
*
1138-
* GHES versions are usually in a semver-compatible format, so usually this will
1139-
* just call the SemVer constructor. However, for GHES pre-release versions,
1140-
* the version string is in the format "3.18.0.pre1", which is not a valid semver
1141-
* version since the pre-release part of the version should be separated by a
1142-
* hyphen. This function will replace the ".pre" part of the version with "-pre"
1143-
* to make it a valid semver version.
1140+
* When the given `githubVersion` is not a GHES version, or if the version
1141+
* is invalid, this will return `defaultIfInvalid`.
11441142
*/
1145-
export function parseGhesVersion(version: string): semver.SemVer {
1146-
if (version.includes(".pre")) {
1147-
version = version.replace(".pre", "-pre");
1143+
export function satisfiesGHESVersion(
1144+
ghesVersion: string,
1145+
range: string,
1146+
defaultIfInvalid: boolean,
1147+
): boolean {
1148+
const semverVersion = semver.coerce(ghesVersion);
1149+
if (semverVersion === null) {
1150+
return defaultIfInvalid;
11481151
}
1149-
return new semver.SemVer(version);
1152+
1153+
// We always drop the pre-release part of the version, since anything that
1154+
// applies to GHES 3.18.0 should also apply to GHES 3.18.0.pre1.
1155+
semverVersion.prerelease = [];
1156+
1157+
return semver.satisfies(semverVersion, range);
11501158
}
11511159

11521160
/**

0 commit comments

Comments
 (0)