Use the API URL from the environment if it is present.

This commit is contained in:
Chris Gavin
2022-08-10 21:11:50 +01:00
parent a6d09016e7
commit bbdc9efa94
42 changed files with 122 additions and 22 deletions

View File

@@ -24,6 +24,7 @@ test("analyze action with RAM & threads from environment variables", async (t) =
await util.withTmpDir(async (tmpDir) => {
process.env["GITHUB_SERVER_URL"] = util.GITHUB_DOTCOM_URL;
process.env["GITHUB_REPOSITORY"] = "github/codeql-action-fake-repository";
process.env["GITHUB_API_URL"] = "https://api.github.com";
sinon
.stub(actionsUtil, "createStatusReportBase")
.resolves({} as actionsUtil.StatusReportBase);

View File

@@ -24,6 +24,7 @@ test("analyze action with RAM & threads from action inputs", async (t) => {
await util.withTmpDir(async (tmpDir) => {
process.env["GITHUB_SERVER_URL"] = util.GITHUB_DOTCOM_URL;
process.env["GITHUB_REPOSITORY"] = "github/codeql-action-fake-repository";
process.env["GITHUB_API_URL"] = "https://api.github.com";
sinon
.stub(actionsUtil, "createStatusReportBase")
.resolves({} as actionsUtil.StatusReportBase);

View File

@@ -101,6 +101,7 @@ async function run() {
const apiDetails = {
auth: actionsUtil.getRequiredInput("token"),
url: util.getRequiredEnvParam("GITHUB_SERVER_URL"),
apiURL: util.getRequiredEnvParam("GITHUB_API_URL"),
};
const outputDir = actionsUtil.getRequiredInput("output");
const threads = util.getThreadsFlag(

View File

@@ -87,6 +87,23 @@ test("Get the client API with github url", async (t) => {
);
});
test("Get the API with an API URL directly", async (t) => {
doTest(
t,
{
auth: "xyz",
url: "http://github.localhost",
apiURL: "http://api.github.localhost",
},
undefined,
{
auth: "token xyz",
baseUrl: "http://api.github.localhost",
userAgent: `CodeQL-Action/${pkg.version}`,
}
);
});
function doTest(
t: ExecutionContext<unknown>,
clientArgs: any,

View File

@@ -22,11 +22,13 @@ export type GitHubApiCombinedDetails = GitHubApiDetails &
export interface GitHubApiDetails {
auth: string;
url: string;
apiURL: string | undefined;
}
export interface GitHubApiExternalRepoDetails {
externalRepoAuth?: string;
url: string;
apiURL: string | undefined;
}
export const getApiClient = function (
@@ -36,16 +38,21 @@ export const getApiClient = function (
const auth =
(allowExternal && apiDetails.externalRepoAuth) || apiDetails.auth;
const retryingOctokit = githubUtils.GitHub.plugin(retry.retry);
let apiURL = apiDetails.apiURL;
if (!apiURL) {
apiURL = deriveApiUrl(apiDetails.url);
}
return new retryingOctokit(
githubUtils.getOctokitOptions(auth, {
baseUrl: getApiUrl(apiDetails.url),
baseUrl: apiURL,
userAgent: `CodeQL-${getMode()}/${pkg.version}`,
log: consoleLogLevel({ level: "debug" }),
})
);
};
function getApiUrl(githubUrl: string): string {
// Once the runner is deleted, this can also be removed since the GitHub API URL is always available in an environment variable on Actions.
function deriveApiUrl(githubUrl: string): string {
const url = new URL(githubUrl);
// If we detect this is trying to connect to github.com
@@ -63,6 +70,7 @@ function getApiDetails() {
return {
auth: getRequiredInput("token"),
url: getRequiredEnvParam("GITHUB_SERVER_URL"),
apiURL: getRequiredEnvParam("GITHUB_API_URL"),
};
}

View File

@@ -21,11 +21,13 @@ setupTests(test);
const sampleApiDetails = {
auth: "token",
url: "https://github.com",
apiURL: undefined,
};
const sampleGHAEApiDetails = {
auth: "token",
url: "https://example.githubenterprise.com",
apiURL: undefined,
};
test.beforeEach(() => {

View File

@@ -20,6 +20,7 @@ const sampleApiDetails = {
auth: "token",
externalRepoAuth: "token",
url: "https://github.example.com",
apiURL: undefined,
};
const gitHubVersion = { type: util.GitHubVariant.DOTCOM } as util.GitHubVersion;

View File

@@ -38,6 +38,7 @@ const testRepoName: RepositoryNwo = { owner: "github", repo: "example" };
const testApiDetails: GitHubApiDetails = {
auth: "1234",
url: "https://github.com",
apiURL: undefined,
};
function getTestConfig(tmpDir: string): Config {

View File

@@ -85,7 +85,11 @@ test("checkoutExternalQueries", async (t) => {
await externalQueries.checkoutExternalRepository(
repoName,
commit1Sha,
{ url: `file://${testRepoBaseDir}`, externalRepoAuth: "" },
{
url: `file://${testRepoBaseDir}`,
externalRepoAuth: "",
apiURL: undefined,
},
tmpDir,
getRunnerLogger(true)
);
@@ -99,7 +103,11 @@ test("checkoutExternalQueries", async (t) => {
await externalQueries.checkoutExternalRepository(
repoName,
commit2Sha,
{ url: `file://${testRepoBaseDir}`, externalRepoAuth: "" },
{
url: `file://${testRepoBaseDir}`,
externalRepoAuth: "",
apiURL: undefined,
},
tmpDir,
getRunnerLogger(true)
);
@@ -114,6 +122,7 @@ test("buildCheckoutURL", (t) => {
externalQueries.buildCheckoutURL("foo/bar", {
url: "https://github.com",
externalRepoAuth: undefined,
apiURL: undefined,
}),
"https://github.com/foo/bar"
);
@@ -121,6 +130,7 @@ test("buildCheckoutURL", (t) => {
externalQueries.buildCheckoutURL("foo/bar", {
url: "https://github.example.com/",
externalRepoAuth: undefined,
apiURL: undefined,
}),
"https://github.example.com/foo/bar"
);
@@ -129,6 +139,7 @@ test("buildCheckoutURL", (t) => {
externalQueries.buildCheckoutURL("foo/bar", {
url: "https://github.com",
externalRepoAuth: "abc",
apiURL: undefined,
}),
"https://x-access-token:abc@github.com/foo/bar"
);
@@ -136,6 +147,7 @@ test("buildCheckoutURL", (t) => {
externalQueries.buildCheckoutURL("foo/bar", {
url: "https://github.example.com/",
externalRepoAuth: "abc",
apiURL: undefined,
}),
"https://x-access-token:abc@github.example.com/foo/bar"
);

View File

@@ -23,6 +23,7 @@ test.beforeEach(() => {
const testApiDetails: GitHubApiDetails = {
auth: "1234",
url: "https://github.com",
apiURL: undefined,
};
const testRepositoryNwo = parseRepositoryNwo("github/example");

View File

@@ -134,6 +134,7 @@ async function run() {
auth: getRequiredInput("token"),
externalRepoAuth: getOptionalInput("external-repository-token"),
url: getRequiredEnvParam("GITHUB_SERVER_URL"),
apiURL: getRequiredEnvParam("GITHUB_API_URL"),
};
const gitHubVersion = await getGitHubVersionActionsOnly();

View File

@@ -202,6 +202,7 @@ program
auth,
externalRepoAuth: auth,
url: parseGitHubUrl(cmd.githubUrl),
apiURL: undefined,
};
const gitHubVersion = await getGitHubVersion(apiDetails);
@@ -475,6 +476,7 @@ program
const apiDetails = {
auth,
url: parseGitHubUrl(cmd.githubUrl),
apiURL: undefined,
};
const outputDir =
@@ -587,6 +589,7 @@ program
const apiDetails = {
auth,
url: parseGitHubUrl(cmd.githubUrl),
apiURL: undefined,
};
try {
const gitHubVersion = await getGitHubVersion(apiDetails);

View File

@@ -56,6 +56,7 @@ async function run() {
const apiDetails = {
auth: actionsUtil.getRequiredInput("token"),
url: getRequiredEnvParam("GITHUB_SERVER_URL"),
apiURL: getRequiredEnvParam("GITHUB_API_URL"),
};
const gitHubVersion = await getGitHubVersionActionsOnly();

View File

@@ -207,6 +207,7 @@ test("getGitHubVersion", async (t) => {
const v = await util.getGitHubVersion({
auth: "",
url: "https://github.com",
apiURL: undefined,
});
t.deepEqual(util.GitHubVariant.DOTCOM, v.type);
@@ -214,6 +215,7 @@ test("getGitHubVersion", async (t) => {
const v2 = await util.getGitHubVersion({
auth: "",
url: "https://ghe.example.com",
apiURL: undefined,
});
t.deepEqual(
{ type: util.GitHubVariant.GHES, version: "2.0" } as util.GitHubVersion,
@@ -224,6 +226,7 @@ test("getGitHubVersion", async (t) => {
const ghae = await util.getGitHubVersion({
auth: "",
url: "https://example.githubenterprise.com",
apiURL: undefined,
});
t.deepEqual({ type: util.GitHubVariant.GHAE }, ghae);
@@ -231,6 +234,7 @@ test("getGitHubVersion", async (t) => {
const v3 = await util.getGitHubVersion({
auth: "",
url: "https://ghe.example.com",
apiURL: undefined,
});
t.deepEqual({ type: util.GitHubVariant.DOTCOM }, v3);
});