Skip to content

Commit 362e7d4

Browse files
committed
Use getCodeQLAssetDownloadURL for start-proxy
1 parent 58de505 commit 362e7d4

4 files changed

Lines changed: 132 additions & 109 deletions

File tree

‎lib/entry-points.js‎

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

‎src/start-proxy-action.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as core from "@actions/core";
55

66
import { Action, ActionState, runInActions } from "./action-common";
77
import * as actionsUtil from "./actions-util";
8-
import { getGitHubVersion } from "./api-client";
8+
import { getApiDetails, getGitHubVersion } from "./api-client";
99
import { FeatureEnablement, initFeatures } from "./feature-flags";
1010
import { BuiltInLanguage, parseBuiltInLanguage } from "./languages";
1111
import { Logger } from "./logging";
@@ -98,7 +98,10 @@ async function run(action: ActionState<["Base", "Logger", "Env", "Actions"]>) {
9898
};
9999

100100
// Start the Proxy
101-
const proxyBin = await getProxyBinaryPath({ ...action, features });
101+
const proxyBin = await getProxyBinaryPath(
102+
{ ...action, features },
103+
getApiDetails(action.env),
104+
);
102105
const proxyInfo = await startProxy(
103106
proxyBin,
104107
proxyConfig,

‎src/start-proxy.test.ts‎

Lines changed: 81 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,29 @@ import test, { ExecutionContext } from "ava";
66
import sinon from "sinon";
77

88
import { ActionState } from "./action-common";
9+
import * as actionsUtil from "./actions-util";
910
import * as apiClient from "./api-client";
1011
import * as defaults from "./defaults.json";
12+
import { ActionsEnvVars } from "./environment";
1113
import { setUpFeatureFlagTests } from "./feature-flags/testing-util";
1214
import { UnvalidatedObject, validateSchema } from "./json";
1315
import { makeFromSchema } from "./json/testing-util";
1416
import { BuiltInLanguage } from "./languages";
1517
import { getRunnerLogger, Logger } from "./logging";
1618
import { BundlePlatform, getBundlePlatform } from "./platform";
19+
import * as repository from "./setup/repository";
1720
import * as startProxyExports from "./start-proxy";
1821
import * as statusReport from "./status-report";
1922
import {
23+
addSinonAssertions,
2024
assertNotLogged,
2125
checkExpectedLogMessages,
2226
createFeatures,
2327
initAllState,
2428
makeMacro,
2529
makeTestToken,
2630
RecordingLogger,
31+
SAMPLE_DOTCOM_API_DETAILS,
2732
setupTests,
2833
withRecordingLoggerAsync,
2934
} from "./testing-utils";
@@ -705,15 +710,23 @@ function mockGetApiClient(endpoints: any) {
705710

706711
type ReleaseAssets = Array<{ name: string; url?: string }>;
707712

708-
function mockGetReleaseByTag(assets?: ReleaseAssets) {
713+
function mockGetReleaseByTag(expectedTag?: string, assets?: ReleaseAssets) {
709714
const getReleaseByTag =
710715
assets === undefined
711716
? sinon.stub().rejects()
712-
: sinon.stub().resolves({
713-
status: 200,
714-
data: { assets },
715-
headers: {},
716-
url: "GET /repos/:owner/:repo/releases/tags/:tag",
717+
: sinon.stub().callsFake(({ tag }) => {
718+
if (tag === expectedTag) {
719+
return {
720+
status: 200,
721+
data: { assets },
722+
headers: {},
723+
url: "GET /repos/:owner/:repo/releases/tags/:tag",
724+
};
725+
} else {
726+
return {
727+
status: 404,
728+
};
729+
}
717730
});
718731

719732
return mockGetApiClient({ repos: { getReleaseByTag } });
@@ -743,6 +756,8 @@ test.serial(
743756
const logger = new RecordingLogger();
744757
mockGetReleaseByTag();
745758

759+
sinon.stub(repository, "getCodeQLAssetDownloadURL").rejects();
760+
746761
await withTmpDir(async (tempDir) => {
747762
const features = mockOfflineFeatures(tempDir, logger);
748763
const state = initAllState({
@@ -751,7 +766,14 @@ test.serial(
751766
logger,
752767
features,
753768
});
754-
const info = await startProxyExports.getDownloadUrl(state);
769+
const info = await startProxyExports.getDownloadUrl(
770+
state,
771+
SAMPLE_DOTCOM_API_DETAILS,
772+
);
773+
774+
t.true(
775+
logger.hasMessage(`Unsupported platform android on architecture ppc`),
776+
);
755777

756778
t.is(info.version, startProxyExports.UPDATEJOB_PROXY_VERSION);
757779
t.is(
@@ -760,10 +782,6 @@ test.serial(
760782
startProxyExports.getProxyPackage(BundlePlatform.Linux64),
761783
),
762784
);
763-
764-
t.true(
765-
logger.hasMessage(`Unsupported platform android on architecture ppc`),
766-
);
767785
});
768786
},
769787
);
@@ -777,7 +795,10 @@ test.serial(
777795
await withTmpDir(async (tempDir) => {
778796
const features = mockOfflineFeatures(tempDir, logger);
779797
const state = initAllState({ logger, features });
780-
const info = await startProxyExports.getDownloadUrl(state);
798+
const info = await startProxyExports.getDownloadUrl(
799+
state,
800+
SAMPLE_DOTCOM_API_DETAILS,
801+
);
781802

782803
t.is(info.version, startProxyExports.UPDATEJOB_PROXY_VERSION);
783804
t.is(
@@ -801,8 +822,11 @@ test.serial(
801822
const state = initAllState({ logger, features });
802823

803824
for (const assets of testAssets) {
804-
const stub = mockGetReleaseByTag(assets);
805-
const info = await startProxyExports.getDownloadUrl(state);
825+
const stub = mockGetReleaseByTag(defaults.bundleVersion, assets);
826+
const info = await startProxyExports.getDownloadUrl(
827+
state,
828+
SAMPLE_DOTCOM_API_DETAILS,
829+
);
806830

807831
t.is(info.version, startProxyExports.UPDATEJOB_PROXY_VERSION);
808832
t.is(
@@ -835,11 +859,17 @@ test.serial("getDownloadUrl returns matching release asset", async (t) => {
835859
url: "url-we-want",
836860
},
837861
];
838-
mockGetReleaseByTag(assets);
862+
const getReleaseByTag = mockGetReleaseByTag(defaults.bundleVersion, assets);
839863

840864
await withTmpDir(async (tempDir) => {
865+
state.env.set(ActionsEnvVars.RUNNER_TEMP, tempDir);
841866
const features = mockOfflineFeatures(tempDir, logger);
842-
const info = await startProxyExports.getDownloadUrl({ ...state, features });
867+
const info = await startProxyExports.getDownloadUrl(
868+
{ ...state, features },
869+
SAMPLE_DOTCOM_API_DETAILS,
870+
);
871+
872+
t.is(getReleaseByTag.callCount, 1);
843873

844874
t.is(info.version, defaults.cliVersion);
845875
t.is(info.url, "url-we-want");
@@ -975,6 +1005,7 @@ test.serial(
9751005
const features = mockOfflineFeatures(tempDir, logger);
9761006
const path = await startProxyExports.getProxyBinaryPath(
9771007
initAllState({ logger, features }),
1008+
SAMPLE_DOTCOM_API_DETAILS,
9781009
);
9791010

9801011
t.assert(path);
@@ -992,7 +1023,7 @@ test.serial(
9921023
const logger = new RecordingLogger();
9931024
const state = initAllState({ logger });
9941025
const downloadUrl = "url-we-want";
995-
mockGetReleaseByTag([
1026+
const getApiClient = mockGetReleaseByTag(defaults.bundleVersion, [
9961027
{
9971028
name: startProxyExports.getProxyPackage(getTestPlatform(state)),
9981029
url: downloadUrl,
@@ -1001,11 +1032,6 @@ test.serial(
10011032

10021033
const toolcachePath = "/path/to/proxy/dir";
10031034
const find = sinon.stub(toolcache, "find").returns("");
1004-
const getApiDetails = sinon.stub(apiClient, "getApiDetails").returns({
1005-
auth: "",
1006-
url: "",
1007-
apiURL: "",
1008-
});
10091035
const getAuthorizationHeaderFor = sinon
10101036
.stub(apiClient, "getAuthorizationHeaderFor")
10111037
.returns(undefined);
@@ -1019,15 +1045,26 @@ test.serial(
10191045
.resolves(extractedPath);
10201046
const cacheDir = sinon.stub(toolcache, "cacheDir").resolves(toolcachePath);
10211047

1022-
const path = await startProxyExports.getProxyBinaryPath({
1023-
...state,
1024-
features: createFeatures([]),
1025-
});
1048+
// Stub this to always return `true` so we get the default repo for the tests.
1049+
sinon.stub(actionsUtil, "isRunningLocalAction").returns(true);
10261050

1051+
const path = await startProxyExports.getProxyBinaryPath(
1052+
{
1053+
...state,
1054+
features: createFeatures([]),
1055+
},
1056+
SAMPLE_DOTCOM_API_DETAILS,
1057+
);
1058+
1059+
t.is(getApiClient.callCount, 1);
10271060
t.assert(find.calledOnce);
1028-
t.assert(getApiDetails.calledOnce);
10291061
t.assert(getAuthorizationHeaderFor.calledOnce);
1030-
t.assert(downloadTool.calledOnceWith(downloadUrl));
1062+
1063+
const st = addSinonAssertions(t);
1064+
st.onceWith(downloadTool, downloadUrl);
1065+
st.onceWith(extractTar, archivePath);
1066+
st.onceWith(cacheDir, extractedPath);
1067+
10311068
t.assert(extractTar.calledOnceWith(archivePath));
10321069
t.assert(cacheDir.calledOnceWith(extractedPath));
10331070
t.assert(path);
@@ -1037,7 +1074,7 @@ test.serial(
10371074
);
10381075

10391076
checkExpectedLogMessages(t, logger.messages, [
1040-
`Found '${startProxyExports.getProxyPackage(getTestPlatform(state))}' in release '${defaults.bundleVersion}' at '${downloadUrl}'`,
1077+
`Found private registry proxy ${startProxyExports.getProxyPackage(getTestPlatform(state))} in github/codeql-action on https://github.com with URL ${downloadUrl}`,
10411078
]);
10421079
},
10431080
);
@@ -1073,11 +1110,6 @@ test.serial(
10731110
await withTmpDir(async (tempDir) => {
10741111
const toolcachePath = "/path/to/proxy/dir";
10751112
const find = sinon.stub(toolcache, "find").returns("");
1076-
const getApiDetails = sinon.stub(apiClient, "getApiDetails").returns({
1077-
auth: "",
1078-
url: "",
1079-
apiURL: "",
1080-
});
10811113
const getAuthorizationHeaderFor = sinon
10821114
.stub(apiClient, "getAuthorizationHeaderFor")
10831115
.returns(undefined);
@@ -1107,18 +1139,23 @@ test.serial(
11071139
.resolves({
11081140
enabledVersions: [{ cliVersion: "2.20.1", tagName: expectedTag }],
11091141
});
1110-
const path = await startProxyExports.getProxyBinaryPath({
1111-
...state,
1112-
features,
1113-
});
11141142

1115-
t.assert(getDefaultCliVersion.calledOnce);
1116-
sinon.assert.calledOnceWithMatch(
1117-
getReleaseByTag,
1118-
sinon.match(expectedParams),
1143+
// Stub this to always return `true` so we get the default repo for the tests.
1144+
sinon.stub(actionsUtil, "isRunningLocalAction").returns(true);
1145+
1146+
const path = await startProxyExports.getProxyBinaryPath(
1147+
{
1148+
...state,
1149+
features,
1150+
},
1151+
SAMPLE_DOTCOM_API_DETAILS,
11191152
);
1153+
1154+
t.assert(getDefaultCliVersion.calledOnce);
1155+
1156+
const st = addSinonAssertions(t);
1157+
st.onceWith(getReleaseByTag, expectedParams);
11201158
t.assert(find.calledOnce);
1121-
t.assert(getApiDetails.calledOnce);
11221159
t.assert(getAuthorizationHeaderFor.calledOnce);
11231160
t.assert(downloadTool.calledOnceWith(downloadUrl));
11241161
t.assert(extractTar.calledOnceWith(archivePath));
@@ -1132,7 +1169,7 @@ test.serial(
11321169
});
11331170

11341171
checkExpectedLogMessages(t, logger.messages, [
1135-
`Found '${startProxyExports.getProxyPackage(getTestPlatform(state))}' in release '${expectedTag}' at '${downloadUrl}'`,
1172+
`Found private registry proxy ${startProxyExports.getProxyPackage(getTestPlatform(state))} in github/codeql-action on https://github.com with URL url-we-want.`,
11361173
]);
11371174
},
11381175
);

0 commit comments

Comments
 (0)