|
| 1 | +import * as fs from "fs"; |
1 | 2 | import * as path from "path"; |
2 | 3 |
|
3 | 4 | import * as github from "@actions/github"; |
@@ -835,10 +836,12 @@ for (const { |
835 | 836 | } |
836 | 837 |
|
837 | 838 | /** |
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. |
842 | 845 | */ |
843 | 846 | const NIGHTLY_UNTIL_DEFAULT_RESUMES_DEFAULT_TOOLS_INPUT_TEST_CASES = [ |
844 | 847 | { |
@@ -965,6 +968,260 @@ test.serial( |
965 | 968 | }, |
966 | 969 | ); |
967 | 970 |
|
| 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 | + |
968 | 1225 | const NIGHTLY_UNTIL_FALLBACK_TOOLS_INPUT_TEST_CASES = [ |
969 | 1226 | { |
970 | 1227 | name: "no release, stable or prerelease, satisfies the threshold", |
|
0 commit comments