Skip to content

Commit af641b2

Browse files
committed
Use externalRepoAuth when getting a remote config
This allows users to specify a different token for retrieving the codeql config from a different repository. Fixes github/advanced-security-field#185
1 parent 8a00ebe commit af641b2

File tree

9 files changed

+194
-19
lines changed

9 files changed

+194
-19
lines changed

lib/api-client.js

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

lib/api-client.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/api-client.test.js

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

lib/api-client.test.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/config-utils.js

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

lib/config-utils.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/api-client.test.ts

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import * as githubUtils from "@actions/github/lib/utils";
2+
import test, { ExecutionContext } from "ava";
3+
import sinon from "sinon";
4+
5+
import { getApiClient } from "./api-client";
6+
import { setupTests } from "./testing-utils";
7+
8+
setupTests(test);
9+
10+
let githubStub: sinon.SinonStub;
11+
12+
test.beforeEach(() => {
13+
githubStub = sinon.stub(githubUtils, "GitHub");
14+
});
15+
16+
test("Get the client API", async (t) => {
17+
doTest(
18+
t,
19+
{
20+
auth: "xyz",
21+
externalRepoAuth: "abc",
22+
url: "http://hucairz",
23+
},
24+
undefined,
25+
{
26+
auth: "token xyz",
27+
baseUrl: "http://hucairz/api/v3",
28+
userAgent: "CodeQL Action",
29+
}
30+
);
31+
});
32+
33+
test("Get the client API external", async (t) => {
34+
doTest(
35+
t,
36+
{
37+
auth: "xyz",
38+
externalRepoAuth: "abc",
39+
url: "http://hucairz",
40+
},
41+
{ allowExternal: true },
42+
{
43+
auth: "token abc",
44+
baseUrl: "http://hucairz/api/v3",
45+
userAgent: "CodeQL Action",
46+
}
47+
);
48+
});
49+
50+
test("Get the client API external not present", async (t) => {
51+
doTest(
52+
t,
53+
{
54+
auth: "xyz",
55+
url: "http://hucairz",
56+
},
57+
{ allowExternal: true },
58+
{
59+
auth: "token xyz",
60+
baseUrl: "http://hucairz/api/v3",
61+
userAgent: "CodeQL Action",
62+
}
63+
);
64+
});
65+
66+
test("Get the client API with github url", async (t) => {
67+
doTest(
68+
t,
69+
{
70+
auth: "xyz",
71+
url: "https://github.com/some/invalid/url",
72+
},
73+
undefined,
74+
{
75+
auth: "token xyz",
76+
baseUrl: "https://api.github.com",
77+
userAgent: "CodeQL Action",
78+
}
79+
);
80+
});
81+
82+
function doTest(
83+
t: ExecutionContext<unknown>,
84+
clientArgs: any,
85+
clientOptions: any,
86+
expected: any
87+
) {
88+
getApiClient(clientArgs, clientOptions);
89+
90+
const firstCallArgs = githubStub.args[0];
91+
// log is a function, so we don't need to test for equality of it
92+
delete firstCallArgs[0].log;
93+
t.deepEqual(firstCallArgs, [expected]);
94+
}

src/api-client.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,17 @@ export interface GitHubApiExternalRepoDetails {
2525
}
2626

2727
export const getApiClient = function (
28-
apiDetails: GitHubApiDetails,
29-
allowLocalRun = false
28+
apiDetails: GitHubApiCombinedDetails,
29+
{ allowLocalRun = false, allowExternal = false } = {}
3030
) {
3131
if (isLocalRun() && !allowLocalRun) {
3232
throw new Error("Invalid API call in local run");
3333
}
34+
35+
const auth =
36+
(allowExternal && apiDetails.externalRepoAuth) || apiDetails.auth;
3437
return new githubUtils.GitHub(
35-
githubUtils.getOctokitOptions(apiDetails.auth, {
38+
githubUtils.getOctokitOptions(auth, {
3639
baseUrl: getApiUrl(apiDetails.url),
3740
userAgent: "CodeQL Action",
3841
log: consoleLogLevel({ level: "debug" }),
@@ -63,5 +66,5 @@ export function getActionsApiClient(allowLocalRun = false) {
6366
url: getRequiredEnvParam("GITHUB_SERVER_URL"),
6467
};
6568

66-
return getApiClient(apiDetails, allowLocalRun);
69+
return getApiClient(apiDetails, { allowLocalRun });
6770
}

src/config-utils.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ async function getLanguagesInRepo(
601601
): Promise<Language[]> {
602602
logger.debug(`GitHub repo ${repository.owner} ${repository.repo}`);
603603
const response = await api
604-
.getApiClient(apiDetails, true)
604+
.getApiClient(apiDetails, { allowLocalRun: true })
605605
.repos.listLanguages({
606606
owner: repository.owner,
607607
repo: repository.repo,
@@ -1013,7 +1013,7 @@ function getLocalConfig(configFile: string, checkoutPath: string): UserConfig {
10131013

10141014
async function getRemoteConfig(
10151015
configFile: string,
1016-
apiDetails: api.GitHubApiDetails
1016+
apiDetails: api.GitHubApiCombinedDetails
10171017
): Promise<UserConfig> {
10181018
// retrieve the various parts of the config location, and ensure they're present
10191019
const format = new RegExp(
@@ -1025,12 +1025,14 @@ async function getRemoteConfig(
10251025
throw new Error(getConfigFileRepoFormatInvalidMessage(configFile));
10261026
}
10271027

1028-
const response = await api.getApiClient(apiDetails, true).repos.getContent({
1029-
owner: pieces.groups.owner,
1030-
repo: pieces.groups.repo,
1031-
path: pieces.groups.path,
1032-
ref: pieces.groups.ref,
1033-
});
1028+
const response = await api
1029+
.getApiClient(apiDetails, { allowLocalRun: true, allowExternal: true })
1030+
.repos.getContent({
1031+
owner: pieces.groups.owner,
1032+
repo: pieces.groups.repo,
1033+
path: pieces.groups.path,
1034+
ref: pieces.groups.ref,
1035+
});
10341036

10351037
let fileContents: string;
10361038
if ("content" in response.data && response.data.content !== undefined) {

0 commit comments

Comments
 (0)