Skip to content

Commit 1d39187

Browse files
committed
Use instance in DownloadSource and pass it on to getApiClient
1 parent 49ddacd commit 1d39187

6 files changed

Lines changed: 95 additions & 54 deletions

File tree

‎lib/entry-points.js‎

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

‎src/api-client.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,11 @@ export function getDotcomApiDetails(env: ReadOnlyEnv): GitHubApiDetails {
193193
}
194194
}
195195

196-
export function getApiClient(env: ReadOnlyEnv = getEnv()) {
197-
return createApiClientWithDetails(getApiDetails(env));
196+
export function getApiClient(
197+
env: ReadOnlyEnv = getEnv(),
198+
apiDetails?: GitHubApiDetails,
199+
) {
200+
return createApiClientWithDetails(apiDetails ?? getApiDetails(env));
198201
}
199202

200203
export function getApiClientWithExternalAuth(

‎src/codeql.test.ts‎

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import {
3434
makeVersionInfo,
3535
createTestConfig,
3636
makeMacro,
37+
mockApiDetails,
3738
} from "./testing-utils";
3839
import { ToolsDownloadStatusReport } from "./tools-download";
3940
import * as util from "./util";
@@ -126,19 +127,6 @@ function mockReleaseApi({
126127
});
127128
}
128129

129-
function mockApiDetails(apiDetails: Required<GitHubApiDetails>) {
130-
// This is a workaround to mock `api.getApiDetails()` since it doesn't seem to be possible to
131-
// mock this directly. The difficulty is that `getApiDetails()` is called locally in
132-
// `api-client.ts`, but `sinon.stub(api, "getApiDetails")` only affects calls to
133-
// `getApiDetails()` via an imported `api` module.
134-
sinon
135-
.stub(actionsUtil, "getRequiredInput")
136-
.withArgs("token")
137-
.returns(apiDetails.auth);
138-
process.env["GITHUB_SERVER_URL"] = apiDetails.url;
139-
process.env["GITHUB_API_URL"] = apiDetails.apiURL || "";
140-
}
141-
142130
async function stubCodeql(): Promise<codeql.CodeQL> {
143131
const codeqlObject = await codeql.getCodeQLForTesting();
144132
sinon.stub(codeqlObject, "getVersion").resolves(makeVersionInfo("2.17.6"));
@@ -290,6 +278,7 @@ for (const { cliVersion, tagName, expectedToolcacheVersion } of [
290278
async (t) => {
291279
await util.withTmpDir(async (tmpDir) => {
292280
setupActionsVars(tmpDir, tmpDir);
281+
mockApiDetails(SAMPLE_DOTCOM_API_DETAILS);
293282

294283
const result = await installIntoToolcache({
295284
cliVersion,
@@ -470,6 +459,7 @@ test.serial(
470459

471460
await util.withTmpDir(async (tmpDir) => {
472461
setupActionsVars(tmpDir, tmpDir);
462+
mockApiDetails(SAMPLE_DOTCOM_API_DETAILS);
473463

474464
await installIntoToolcache({
475465
tagName: "codeql-bundle-20200601",
@@ -517,6 +507,7 @@ test.serial(
517507

518508
await util.withTmpDir(async (tmpDir) => {
519509
setupActionsVars(tmpDir, tmpDir);
510+
mockApiDetails(SAMPLE_DOTCOM_API_DETAILS);
520511

521512
await installIntoToolcache({
522513
tagName: "codeql-bundle-20200601",

‎src/setup-codeql.test.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
getTestEnv,
3333
initAllState,
3434
makeMacro,
35+
mockApiDetails,
3536
mockBundleDownloadApi,
3637
setupActionsVars,
3738
setupTests,
@@ -221,6 +222,7 @@ for (const {
221222

222223
await withTmpDir(async (tmpDir) => {
223224
setupActionsVars(tmpDir, tmpDir);
225+
mockApiDetails(SAMPLE_DOTCOM_API_DETAILS);
224226
const source = await setupCodeql.getCodeQLSource(
225227
"linked",
226228
SAMPLE_DEFAULT_CLI_VERSION,
@@ -255,6 +257,7 @@ test.serial(
255257

256258
await withTmpDir(async (tmpDir) => {
257259
setupActionsVars(tmpDir, tmpDir);
260+
mockApiDetails(SAMPLE_DOTCOM_API_DETAILS);
258261
const source = await setupCodeql.getCodeQLSource(
259262
"latest",
260263
SAMPLE_DEFAULT_CLI_VERSION,
@@ -298,6 +301,7 @@ test.serial(
298301

299302
await withTmpDir(async (tmpDir) => {
300303
setupActionsVars(tmpDir, tmpDir);
304+
mockApiDetails(SAMPLE_DOTCOM_API_DETAILS);
301305
const result = await setupCodeql.setupCodeQLBundle(
302306
"linked",
303307
SAMPLE_DOTCOM_API_DETAILS,
@@ -479,6 +483,7 @@ test.serial(
479483

480484
await withTmpDir(async (tmpDir) => {
481485
setupActionsVars(tmpDir, tmpDir, { GITHUB_EVENT_NAME: "dynamic" });
486+
mockApiDetails(SAMPLE_DOTCOM_API_DETAILS);
482487

483488
const source = await setupCodeql.getCodeQLSource(
484489
undefined,
@@ -624,6 +629,7 @@ for (const toolsInput of ["nightly", "nightly-latest"]) {
624629

625630
await withTmpDir(async (tmpDir) => {
626631
setupActionsVars(tmpDir, tmpDir);
632+
mockApiDetails(SAMPLE_DOTCOM_API_DETAILS);
627633
const source = await setupCodeql.getCodeQLSource(
628634
toolsInput,
629635
SAMPLE_DEFAULT_CLI_VERSION,
@@ -945,6 +951,7 @@ const toolcacheInputFallbackMacro = makeMacro({
945951

946952
await withTmpDir(async (tmpDir) => {
947953
setupActionsVars(tmpDir, tmpDir);
954+
mockApiDetails(SAMPLE_DOTCOM_API_DETAILS);
948955

949956
for (const [k, v] of Object.entries(environment)) {
950957
process.env[k] = v;
@@ -1165,6 +1172,7 @@ test.serial(
11651172

11661173
await withTmpDir(async (tmpDir) => {
11671174
setupActionsVars(tmpDir, tmpDir);
1175+
mockApiDetails(SAMPLE_DOTCOM_API_DETAILS);
11681176
const source = await setupCodeql.getCodeQLSource(
11691177
undefined,
11701178
PER_LANGUAGE_CLI_VERSION,
@@ -1207,6 +1215,7 @@ test.serial(
12071215

12081216
await withTmpDir(async (tmpDir) => {
12091217
setupActionsVars(tmpDir, tmpDir);
1218+
mockApiDetails(SAMPLE_DOTCOM_API_DETAILS);
12101219
for (const { otherLanguagePacksReason, features } of [
12111220
// The per-language feature is disabled.
12121221
{ otherLanguagePacksReason: undefined, features: createFeatures([]) },
@@ -1286,6 +1295,7 @@ for (const fallback of [false, true]) {
12861295
setupActionsVars(tmpDir, tmpDir, {
12871296
GITHUB_ACTION_REPOSITORY: "codeql-testing/action-fork",
12881297
});
1298+
mockApiDetails(SAMPLE_DOTCOM_API_DETAILS);
12891299
const result = await setupCodeql.setupCodeQLBundle(
12901300
undefined,
12911301
SAMPLE_DOTCOM_API_DETAILS,
@@ -1553,6 +1563,7 @@ for (const error of [
15531563

15541564
await withTmpDir(async (tmpDir) => {
15551565
setupActionsVars(tmpDir, tmpDir);
1566+
mockApiDetails(SAMPLE_DOTCOM_API_DETAILS);
15561567
await t.throwsAsync(
15571568
setupCodeql.setupCodeQLBundle(
15581569
undefined,
@@ -2259,6 +2270,7 @@ test.serial(
22592270
async (t) => {
22602271
await withTmpDir(async (tmpDir) => {
22612272
setupActionsVars(tmpDir, tmpDir);
2273+
mockApiDetails(SAMPLE_DOTCOM_API_DETAILS);
22622274
delete process.env[EnvVar.HAS_SET_UP_CODEQL];
22632275

22642276
stubDownloadAndExtract();
@@ -2294,6 +2306,7 @@ test.serial(
22942306

22952307
await withTmpDir(async (tmpDir) => {
22962308
setupActionsVars(tmpDir, tmpDir);
2309+
mockApiDetails(SAMPLE_DOTCOM_API_DETAILS);
22972310
process.env[ActionsEnvVars.RUNNER_ENVIRONMENT] = "github-hosted";
22982311
delete process.env[EnvVar.HAS_SET_UP_CODEQL];
22992312
const staleDirectory = createToolcacheEntry(

‎src/setup/repository.ts‎

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { ActionState } from "../action-common";
22
import { isRunningLocalAction } from "../actions-util";
33
import * as api from "../api-client";
44
import { ActionsEnvVars } from "../environment";
5-
import { GITHUB_DOTCOM_URL } from "../github";
65
import { isRepositoryNwoStr, type RepositoryNwoStr } from "../repository";
76

87
/** The NWO of the standard CodeQL Action repository. */
@@ -43,20 +42,14 @@ export function getCodeQLActionRepository(
4342
}
4443

4544
/**A download source is a pair of strings. */
46-
export type DownloadSource = [string, RepositoryNwoStr];
47-
48-
/** GitHub.com, and the canonical Action. */
49-
export const DEFAULT_DOWNLOAD_SOURCE: DownloadSource = [
50-
GITHUB_DOTCOM_URL,
51-
CODEQL_DEFAULT_ACTION_REPOSITORY,
52-
];
45+
export type DownloadSource = [api.GitHubApiDetails, RepositoryNwoStr];
5346

5447
/** Decides if the two provided download sources are the same. */
5548
const isSameDownloadSource = (
56-
[srcUrl, srcRepo]: DownloadSource,
57-
[otherUrl, otherRepo]: DownloadSource,
49+
[srcInstance, srcRepo]: DownloadSource,
50+
[otherInstance, otherRepo]: DownloadSource,
5851
) => {
59-
return srcUrl === otherUrl && srcRepo === otherRepo;
52+
return srcInstance.url === otherInstance.url && srcRepo === otherRepo;
6053
};
6154

6255
/** A download URL is represented as a string. */
@@ -88,13 +81,21 @@ export async function getCodeQLAssetDownloadURL(
8881
): Promise<DownloadURL> {
8982
const codeQLActionRepository = getCodeQLActionRepository(action);
9083

84+
// We construct the default GitHub.com source here, because `getDotcomApiDetails`
85+
// may retrieve the `token` input if we are currently running on GitHub.com and
86+
// we want to avoid doing that statically.
87+
const defaultDownloadSource: DownloadSource = [
88+
api.getDotcomApiDetails(action.env),
89+
CODEQL_DEFAULT_ACTION_REPOSITORY,
90+
];
91+
9192
const potentialDownloadSources: DownloadSource[] = [
9293
// This GitHub instance, and this Action.
93-
[apiDetails.url, codeQLActionRepository],
94+
[apiDetails, codeQLActionRepository],
9495
// This GitHub instance, and the canonical Action.
95-
[apiDetails.url, CODEQL_DEFAULT_ACTION_REPOSITORY],
96+
[apiDetails, CODEQL_DEFAULT_ACTION_REPOSITORY],
9697
// GitHub.com, and the canonical Action.
97-
DEFAULT_DOWNLOAD_SOURCE,
98+
defaultDownloadSource,
9899
];
99100

100101
// We now filter out any duplicates.
@@ -107,31 +108,33 @@ export async function getCodeQLAssetDownloadURL(
107108
},
108109
);
109110

110-
for (const [apiURL, repository] of uniqueDownloadSources) {
111+
for (const [instance, repository] of uniqueDownloadSources) {
111112
// If we've reached the final case, short-circuit the API check since we know the asset exists and is public.
112-
if (isSameDownloadSource(DEFAULT_DOWNLOAD_SOURCE, [apiURL, repository])) {
113+
if (isSameDownloadSource(defaultDownloadSource, [instance, repository])) {
113114
break;
114115
}
115116

116117
const [repositoryOwner, repositoryName] = repository.split("/");
117118
try {
118-
const release = await api.getApiClient().rest.repos.getReleaseByTag({
119-
owner: repositoryOwner,
120-
repo: repositoryName,
121-
tag: tagName,
122-
});
119+
const release = await api
120+
.getApiClient(action.env, instance)
121+
.rest.repos.getReleaseByTag({
122+
owner: repositoryOwner,
123+
repo: repositoryName,
124+
tag: tagName,
125+
});
123126

124127
for (const asset of release.data.assets) {
125128
if (asset.name === assetName) {
126129
action.logger.info(
127-
`Found ${assetKind} ${assetName} in ${repository} on ${apiURL} with URL ${asset.url}.`,
130+
`Found ${assetKind} ${assetName} in ${repository} on ${instance.apiURL} with URL ${asset.url}.`,
128131
);
129132
return asset.url;
130133
}
131134
}
132135
} catch (e) {
133136
action.logger.info(
134-
`Looked for ${assetKind} ${assetName} in ${repository} on ${apiURL} but got error ${e}.`,
137+
`Looked for ${assetKind} ${assetName} in ${repository} on ${instance.apiURL} but got error ${e}.`,
135138
);
136139
}
137140
}

0 commit comments

Comments
 (0)