Skip to content

Commit 9892b52

Browse files
Copilotfelickz
andauthored
Add tests for nightly-until-default overlay-aware and GHES override scenarios
Co-authored-by: felickz <1760475+felickz@users.noreply.github.com>
1 parent 71844e5 commit 9892b52

3 files changed

Lines changed: 311 additions & 19 deletions

File tree

‎lib/entry-points.js‎

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

‎src/setup-codeql.test.ts‎

Lines changed: 261 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as fs from "fs";
12
import * as path from "path";
23

34
import * as github from "@actions/github";
@@ -835,10 +836,12 @@ for (const {
835836
}
836837

837838
/**
838-
* `nightly-until-default-<version>` compares the given version threshold directly against
839-
* `SAMPLE_DEFAULT_CLI_VERSION`'s CLI version, `2.20.0`, without ever listing CodeQL bundle
840-
* releases. When the default version is at or above the threshold, resolution should proceed
841-
* exactly as if `tools` were not specified at all.
839+
* `nightly-until-default-<version>` compares the given version threshold against the CLI version
840+
* that would actually be used if `tools` had not been specified at all, i.e. after normal
841+
* (possibly overlay-aware) default CLI version selection and any applicable overrides, without
842+
* ever listing CodeQL bundle releases. In these test cases, that resolved version is
843+
* `SAMPLE_DEFAULT_CLI_VERSION`'s CLI version, `2.20.0`. When the resolved version is at or above
844+
* the threshold, resolution should proceed exactly as if `tools` were not specified at all.
842845
*/
843846
const NIGHTLY_UNTIL_DEFAULT_RESUMES_DEFAULT_TOOLS_INPUT_TEST_CASES = [
844847
{
@@ -965,6 +968,260 @@ test.serial(
965968
},
966969
);
967970

971+
const nightlyUntilDefaultOverlayEnabledVersions = {
972+
enabledVersions: [
973+
{ cliVersion: "2.20.2", tagName: "codeql-bundle-v2.20.2" },
974+
{ cliVersion: "2.20.1", tagName: "codeql-bundle-v2.20.1" },
975+
{ cliVersion: "2.20.0", tagName: "codeql-bundle-v2.20.0" },
976+
],
977+
toolsFeatureFlagsValid: true,
978+
};
979+
980+
async function stubOverlayBaseCacheForNightlyUntilDefaultTests(
981+
cliVersion: string,
982+
) {
983+
sinon.stub(api, "getAutomationID").resolves("test/");
984+
sinon.stub(api, "listActionsCaches").resolves([
985+
{
986+
key: await fakeOverlayBaseCacheKey("javascript", cliVersion, "abc-1-1"),
987+
},
988+
]);
989+
process.env[EnvVar.CODE_SCANNING_REF] = "refs/heads/feature-branch";
990+
process.env[EnvVar.CODE_SCANNING_BASE_BRANCH] = "main";
991+
}
992+
993+
test.serial(
994+
"getCodeQLSource stays on nightly for 'tools: nightly-until-default-<version>' when the " +
995+
"newest enabled default satisfies the threshold, but overlay-aware selection resolves to " +
996+
"an older version that does not",
997+
async (t) => {
998+
const loggedMessages: LoggedMessage[] = [];
999+
const logger = getRecordingLogger(loggedMessages);
1000+
const features = createFeatures([
1001+
Feature.OverlayAnalysisMatchCodeqlVersion,
1002+
]);
1003+
1004+
const expectedDate = "30260213";
1005+
const expectedTag = `codeql-bundle-${expectedDate}`;
1006+
1007+
sinon.stub(process, "platform").value("linux");
1008+
sinon.stub(tar, "isZstdAvailable").resolves({
1009+
available: true,
1010+
foundZstdBinary: true,
1011+
});
1012+
1013+
const client = github.getOctokit("123");
1014+
const listReleases = sinon.stub(client.rest.repos, "listReleases");
1015+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
1016+
listReleases.resolves({
1017+
data: [{ tag_name: expectedTag }],
1018+
} as any);
1019+
sinon.stub(api, "getApiClient").value(() => client);
1020+
1021+
// The overlay-base cache only has an entry for 2.20.1, which is below the 2.20.2 threshold,
1022+
// even though the newest enabled default version, 2.20.2, satisfies it.
1023+
await stubOverlayBaseCacheForNightlyUntilDefaultTests("2.20.1");
1024+
sinon
1025+
.stub(toolcache, "find")
1026+
.withArgs("CodeQL", "2.20.1")
1027+
.returns("/path/to/codeql-2.20.1");
1028+
1029+
const toolsInput = "nightly-until-default-2.20.2";
1030+
1031+
await withTmpDir(async (tmpDir) => {
1032+
setupActionsVars(tmpDir, tmpDir);
1033+
const source = await setupCodeql.getCodeQLSource(
1034+
toolsInput,
1035+
nightlyUntilDefaultOverlayEnabledVersions,
1036+
["javascript"],
1037+
true, // useOverlayAwareDefaultCliVersion
1038+
SAMPLE_DOTCOM_API_DETAILS,
1039+
GitHubVariant.DOTCOM,
1040+
false,
1041+
features,
1042+
logger,
1043+
);
1044+
1045+
t.is(source.sourceType, "download");
1046+
t.true(
1047+
listReleases.neverCalledWith(
1048+
sinon.match({ owner: "github", repo: "codeql-action" }),
1049+
),
1050+
);
1051+
checkExpectedLogMessages(t, loggedMessages, [
1052+
`Using the latest CodeQL CLI nightly, as requested by 'tools: ${toolsInput}', since the ` +
1053+
"resolved default CodeQL version 2.20.1 does not satisfy the version threshold",
1054+
]);
1055+
});
1056+
},
1057+
);
1058+
1059+
test.serial(
1060+
"getCodeQLSource resumes normal default CLI version selection for 'tools: " +
1061+
"nightly-until-default-<version>' when overlay-aware selection resolves to a version that " +
1062+
"satisfies the threshold",
1063+
async (t) => {
1064+
const loggedMessages: LoggedMessage[] = [];
1065+
const logger = getRecordingLogger(loggedMessages);
1066+
const features = createFeatures([
1067+
Feature.OverlayAnalysisMatchCodeqlVersion,
1068+
]);
1069+
1070+
const client = github.getOctokit("123");
1071+
const listReleases = sinon.stub(client.rest.repos, "listReleases");
1072+
sinon.stub(api, "getApiClient").value(() => client);
1073+
1074+
await stubOverlayBaseCacheForNightlyUntilDefaultTests("2.20.1");
1075+
sinon
1076+
.stub(toolcache, "find")
1077+
.withArgs("CodeQL", "2.20.1")
1078+
.returns("/path/to/codeql-2.20.1");
1079+
1080+
const toolsInput = "nightly-until-default-2.20.1";
1081+
1082+
await withTmpDir(async (tmpDir) => {
1083+
setupActionsVars(tmpDir, tmpDir);
1084+
const source = await setupCodeql.getCodeQLSource(
1085+
toolsInput,
1086+
nightlyUntilDefaultOverlayEnabledVersions,
1087+
["javascript"],
1088+
true, // useOverlayAwareDefaultCliVersion
1089+
SAMPLE_DOTCOM_API_DETAILS,
1090+
GitHubVariant.DOTCOM,
1091+
false,
1092+
features,
1093+
logger,
1094+
);
1095+
1096+
t.is(source.sourceType, "toolcache");
1097+
t.is(source.toolsVersion, "2.20.1");
1098+
t.true(listReleases.notCalled);
1099+
checkExpectedLogMessages(t, loggedMessages, [
1100+
`'tools: ${toolsInput}' was requested, so using the default CodeQL version, since the ` +
1101+
"resolved default CodeQL version 2.20.1 satisfies the version threshold",
1102+
]);
1103+
});
1104+
},
1105+
);
1106+
1107+
test.serial(
1108+
"getCodeQLSource stays on nightly for 'tools: nightly-until-default-<version>' on GHES when " +
1109+
"the newest enabled default satisfies the threshold, but a pinned toolcache override " +
1110+
"resolves to an older version that does not",
1111+
async (t) => {
1112+
const loggedMessages: LoggedMessage[] = [];
1113+
const logger = getRecordingLogger(loggedMessages);
1114+
const features = createFeatures([]);
1115+
1116+
const expectedDate = "30260213";
1117+
const expectedTag = `codeql-bundle-${expectedDate}`;
1118+
1119+
sinon.stub(process, "platform").value("linux");
1120+
sinon.stub(tar, "isZstdAvailable").resolves({
1121+
available: true,
1122+
foundZstdBinary: true,
1123+
});
1124+
1125+
const client = github.getOctokit("123");
1126+
const listReleases = sinon.stub(client.rest.repos, "listReleases");
1127+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
1128+
listReleases.resolves({
1129+
data: [{ tag_name: expectedTag }],
1130+
} as any);
1131+
sinon.stub(api, "getApiClient").value(() => client);
1132+
1133+
const toolsInput = "nightly-until-default-2.20.2";
1134+
1135+
await withTmpDir(async (tmpDir) => {
1136+
setupActionsVars(tmpDir, tmpDir);
1137+
1138+
// Nothing in the toolcache matches the resolved default version 2.20.2, but a pinned
1139+
// (overriding) version 2.20.1 is present, which is below the threshold.
1140+
const pinnedFolder = path.join(tmpDir, "pinned-codeql-2.20.1");
1141+
fs.mkdirSync(pinnedFolder, { recursive: true });
1142+
fs.writeFileSync(path.join(pinnedFolder, "pinned-version"), "");
1143+
sinon.stub(toolcache, "findAllVersions").returns(["2.20.1"]);
1144+
sinon
1145+
.stub(toolcache, "find")
1146+
.withArgs("CodeQL", "2.20.1")
1147+
.returns(pinnedFolder);
1148+
1149+
const source = await setupCodeql.getCodeQLSource(
1150+
toolsInput,
1151+
nightlyUntilDefaultOverlayEnabledVersions,
1152+
undefined, // rawLanguages
1153+
false, // useOverlayAwareDefaultCliVersion
1154+
SAMPLE_GHES_API_DETAILS,
1155+
GitHubVariant.GHES,
1156+
false,
1157+
features,
1158+
logger,
1159+
);
1160+
1161+
t.is(source.sourceType, "download");
1162+
t.true(
1163+
listReleases.neverCalledWith(
1164+
sinon.match({ owner: "github", repo: "codeql-action" }),
1165+
),
1166+
);
1167+
checkExpectedLogMessages(t, loggedMessages, [
1168+
`Using the latest CodeQL CLI nightly, as requested by 'tools: ${toolsInput}', since the ` +
1169+
"resolved default CodeQL version 2.20.1 does not satisfy the version threshold",
1170+
]);
1171+
});
1172+
},
1173+
);
1174+
1175+
test.serial(
1176+
"getCodeQLSource resumes normal default CLI version selection for 'tools: " +
1177+
"nightly-until-default-<version>' on GHES when a pinned toolcache override resolves to a " +
1178+
"version that satisfies the threshold",
1179+
async (t) => {
1180+
const loggedMessages: LoggedMessage[] = [];
1181+
const logger = getRecordingLogger(loggedMessages);
1182+
const features = createFeatures([]);
1183+
1184+
const client = github.getOctokit("123");
1185+
const listReleases = sinon.stub(client.rest.repos, "listReleases");
1186+
sinon.stub(api, "getApiClient").value(() => client);
1187+
1188+
const toolsInput = "nightly-until-default-2.20.1";
1189+
1190+
await withTmpDir(async (tmpDir) => {
1191+
setupActionsVars(tmpDir, tmpDir);
1192+
1193+
const pinnedFolder = path.join(tmpDir, "pinned-codeql-2.20.1");
1194+
fs.mkdirSync(pinnedFolder, { recursive: true });
1195+
fs.writeFileSync(path.join(pinnedFolder, "pinned-version"), "");
1196+
sinon.stub(toolcache, "findAllVersions").returns(["2.20.1"]);
1197+
sinon
1198+
.stub(toolcache, "find")
1199+
.withArgs("CodeQL", "2.20.1")
1200+
.returns(pinnedFolder);
1201+
1202+
const source = await setupCodeql.getCodeQLSource(
1203+
toolsInput,
1204+
nightlyUntilDefaultOverlayEnabledVersions,
1205+
undefined, // rawLanguages
1206+
false, // useOverlayAwareDefaultCliVersion
1207+
SAMPLE_GHES_API_DETAILS,
1208+
GitHubVariant.GHES,
1209+
false,
1210+
features,
1211+
logger,
1212+
);
1213+
1214+
t.is(source.sourceType, "toolcache");
1215+
t.is(source.toolsVersion, "2.20.1");
1216+
t.true(listReleases.notCalled);
1217+
checkExpectedLogMessages(t, loggedMessages, [
1218+
`'tools: ${toolsInput}' was requested, so using the default CodeQL version, since the ` +
1219+
"resolved default CodeQL version 2.20.1 satisfies the version threshold",
1220+
]);
1221+
});
1222+
},
1223+
);
1224+
9681225
const NIGHTLY_UNTIL_FALLBACK_TOOLS_INPUT_TEST_CASES = [
9691226
{
9701227
name: "no release, stable or prerelease, satisfies the threshold",

‎src/setup-codeql.ts‎

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -828,25 +828,49 @@ export async function getCodeQLSource(
828828
tryGetNightlyUntilDefaultToolsInput(toolsInput) !== undefined
829829
) {
830830
// The `nightly-until-default-<version>` syntax was used: compare the given version threshold
831-
// directly against this CodeQL Action's already-known default CLI version, without querying
832-
// the CodeQL bundle release history. If the default CLI version is at or above the
833-
// threshold, resume normal default CLI version selection, exactly as if `tools` had not been
834-
// specified at all. Otherwise, fall back to the latest nightly bundle.
831+
// against the CLI version that would actually be used if `tools` had not been specified at
832+
// all, i.e. after normal default CLI version selection (which may be overlay-aware) and any
833+
// applicable overrides (such as a pinned version found in the toolcache on Enterprise
834+
// Server), without ever querying the CodeQL bundle release history. If that resolved version
835+
// satisfies the threshold, we resume with that already-resolved source directly, so we never
836+
// duplicate the default-selection or override logic below. Otherwise, we fall back to the
837+
// latest nightly bundle.
835838
const rawThreshold = tryGetNightlyUntilDefaultToolsInput(toolsInput)!;
836839
const threshold = parseNightlyUntilThreshold(toolsInput, rawThreshold);
837-
const defaultVersion = defaultCliVersion.enabledVersions[0].cliVersion;
838840

839-
if (semver.gte(defaultVersion, threshold)) {
841+
const defaultSource = await getCodeQLSource(
842+
undefined,
843+
defaultCliVersion,
844+
rawLanguages,
845+
useOverlayAwareDefaultCliVersion,
846+
apiDetails,
847+
variant,
848+
tarSupportsZstd,
849+
features,
850+
logger,
851+
);
852+
const resolvedVersion =
853+
"cliVersion" in defaultSource
854+
? defaultSource.cliVersion
855+
: defaultSource.toolsVersion;
856+
857+
if (
858+
resolvedVersion !== undefined &&
859+
semver.valid(resolvedVersion) !== null &&
860+
semver.gte(resolvedVersion, threshold)
861+
) {
840862
logger.info(
841863
`'tools: ${toolsInput}' was requested, so using the default CodeQL version, since the ` +
842-
`default CodeQL version ${defaultVersion} satisfies the version threshold of ${threshold}.`,
864+
`resolved default CodeQL version ${resolvedVersion} satisfies the version threshold ` +
865+
`of ${threshold}.`,
843866
);
844-
toolsInput = undefined;
867+
return defaultSource;
845868
} else {
846869
logger.info(
847870
`Using the latest CodeQL CLI nightly, as requested by 'tools: ${toolsInput}', since the ` +
848-
`default CodeQL version ${defaultVersion} does not satisfy the version threshold of ` +
849-
`${threshold}.`,
871+
`resolved default CodeQL version${
872+
resolvedVersion !== undefined ? ` ${resolvedVersion}` : ""
873+
} does not satisfy the version threshold of ${threshold}.`,
850874
);
851875
toolsInput = await getNightlyToolsUrl(logger);
852876
}

0 commit comments

Comments
 (0)