Skip to content

Commit 07455ed

Browse files
authored
Merge pull request #2972 from github/koesie10/ghes-satisfies
Ignore pre-release parts when comparing GHES versions
2 parents 0d17ea4 + e30db30 commit 07455ed

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
@@ -575,6 +575,19 @@ test("throwIfCombineSarifFilesDisabled when on GHES 3.18", async (t) => {
575575
);
576576
});
577577

578+
test("throwIfCombineSarifFilesDisabled with an invalid GHES version", async (t) => {
579+
await t.notThrowsAsync(
580+
uploadLib.throwIfCombineSarifFilesDisabled(
581+
[createMockSarif("abc", "def"), createMockSarif("abc", "def")],
582+
createFeatures([]),
583+
{
584+
type: GitHubVariant.GHES,
585+
version: "foobar",
586+
},
587+
),
588+
);
589+
});
590+
578591
test("throwIfCombineSarifFilesDisabled with only 1 run", async (t) => {
579592
await t.notThrowsAsync(
580593
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 * as api from "./api-client";
@@ -29,7 +28,7 @@ import {
2928
getRequiredEnvParam,
3029
GitHubVariant,
3130
GitHubVersion,
32-
parseGhesVersion,
31+
satisfiesGHESVersion,
3332
SarifFile,
3433
SarifRun,
3534
} from "./util";
@@ -132,7 +131,7 @@ export async function shouldShowCombineSarifFilesDeprecationWarning(
132131
// Do not show this warning on GHES versions before 3.14.0
133132
if (
134133
githubVersion.type === GitHubVariant.GHES &&
135-
semver.lt(parseGhesVersion(githubVersion.version), "3.14.0")
134+
satisfiesGHESVersion(githubVersion.version, "<3.14", true)
136135
) {
137136
return false;
138137
}
@@ -177,7 +176,7 @@ async function shouldDisableCombineSarifFiles(
177176
) {
178177
if (githubVersion.type === GitHubVariant.GHES) {
179178
// Never block on GHES versions before 3.18.
180-
if (semver.lt(parseGhesVersion(githubVersion.version), "3.18.0-0")) {
179+
if (satisfiesGHESVersion(githubVersion.version, "<3.18", true)) {
181180
return false;
182181
}
183182
} 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)