Skip to content

Commit f2547e0

Browse files
committed
Use instance in DownloadSource and pass it on to getApiClient
1 parent f728bb6 commit f2547e0

6 files changed

Lines changed: 125 additions & 72 deletions

File tree

‎lib/entry-points.js‎

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

‎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 & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
parseRepositoryNwo,
99
type RepositoryNwoStr,
1010
} from "../repository";
11-
import { type InstanceURL } from "../util";
1211

1312
/** The NWO of the standard CodeQL Action repository. */
1413
export const CODEQL_DEFAULT_ACTION_REPOSITORY = "github/codeql-action";
@@ -48,20 +47,14 @@ export function getCodeQLActionRepository(
4847
}
4948

5049
/**A download source is a pair of strings. */
51-
export type DownloadSource = [InstanceURL, RepositoryNwoStr];
52-
53-
/** GitHub.com, and the canonical Action. */
54-
export const DEFAULT_DOWNLOAD_SOURCE: DownloadSource = [
55-
GITHUB_DOTCOM_URL,
56-
CODEQL_DEFAULT_ACTION_REPOSITORY,
57-
];
50+
export type DownloadSource = [api.GitHubApiDetails, RepositoryNwoStr];
5851

5952
/** Decides if the two provided download sources are the same. */
6053
const isSameDownloadSource = (
61-
[srcUrl, srcRepo]: DownloadSource,
62-
[otherUrl, otherRepo]: DownloadSource,
54+
[srcInstance, srcRepo]: DownloadSource,
55+
[otherInstance, otherRepo]: DownloadSource,
6356
) => {
64-
return srcUrl === otherUrl && srcRepo === otherRepo;
57+
return srcInstance.url === otherInstance.url && srcRepo === otherRepo;
6558
};
6659

6760
/** A download URL is represented as a string. */
@@ -104,13 +97,21 @@ export async function getCodeQLAssetDownloadURL(
10497
): Promise<DownloadURL> {
10598
const codeQLActionRepository = getCodeQLActionRepository(action);
10699

100+
// We construct the default GitHub.com source here, because `getDotcomApiDetails`
101+
// may retrieve the `token` input if we are currently running on GitHub.com and
102+
// we want to avoid doing that statically.
103+
const defaultDownloadSource: DownloadSource = [
104+
api.getDotcomApiDetails(action.env),
105+
CODEQL_DEFAULT_ACTION_REPOSITORY,
106+
];
107+
107108
const potentialDownloadSources: DownloadSource[] = [
108109
// This GitHub instance, and this Action.
109-
[apiDetails.url, codeQLActionRepository],
110+
[apiDetails, codeQLActionRepository],
110111
// This GitHub instance, and the canonical Action.
111-
[apiDetails.url, CODEQL_DEFAULT_ACTION_REPOSITORY],
112+
[apiDetails, CODEQL_DEFAULT_ACTION_REPOSITORY],
112113
// GitHub.com, and the canonical Action.
113-
DEFAULT_DOWNLOAD_SOURCE,
114+
defaultDownloadSource,
114115
];
115116

116117
// We now filter out any duplicates.
@@ -123,34 +124,34 @@ export async function getCodeQLAssetDownloadURL(
123124
},
124125
);
125126

126-
for (const [instanceURL, repository] of uniqueDownloadSources) {
127+
for (const [instance, repository] of uniqueDownloadSources) {
127128
// If we've reached the final case, short-circuit the API check since we know the asset exists and is public.
128-
if (
129-
isSameDownloadSource(DEFAULT_DOWNLOAD_SOURCE, [instanceURL, repository])
130-
) {
129+
if (isSameDownloadSource(defaultDownloadSource, [instance, repository])) {
131130
break;
132131
}
133132

134133
const { owner: repositoryOwner, repo: repositoryName } =
135134
parseRepositoryNwo(repository);
136135
try {
137-
const release = await api.getApiClient().rest.repos.getReleaseByTag({
138-
owner: repositoryOwner,
139-
repo: repositoryName,
140-
tag: tagName,
141-
});
136+
const release = await api
137+
.getApiClient(action.env, instance)
138+
.rest.repos.getReleaseByTag({
139+
owner: repositoryOwner,
140+
repo: repositoryName,
141+
tag: tagName,
142+
});
142143

143144
for (const asset of release.data.assets) {
144145
if (asset.name === assetName) {
145146
action.logger.info(
146-
`Found ${assetKind} ${assetName} in ${repository} on ${instanceURL} with URL ${asset.url}.`,
147+
`Found ${assetKind} ${assetName} in ${repository} on ${instance.apiURL} with URL ${asset.url}.`,
147148
);
148149
return asset.url;
149150
}
150151
}
151152
} catch (e) {
152153
action.logger.info(
153-
`Looked for ${assetKind} ${assetName} in ${repository} on ${instanceURL} but got error ${e}.`,
154+
`Looked for ${assetKind} ${assetName} in ${repository} on ${instance.apiURL} but got error ${e}.`,
154155
);
155156
}
156157
}

0 commit comments

Comments
 (0)