Skip to content

Commit

Permalink
Remove deriveApiUrl function only used by runner
Browse files Browse the repository at this point in the history
  • Loading branch information
henrymercer committed Nov 14, 2022
1 parent 8ecbaea commit dac8912
Show file tree
Hide file tree
Showing 6 changed files with 6 additions and 146 deletions.
16 changes: 1 addition & 15 deletions lib/api-client.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/api-client.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 0 additions & 42 deletions lib/api-client.test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/api-client.test.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 2 additions & 68 deletions src/api-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as githubUtils from "@actions/github/lib/utils";
import test, { ExecutionContext } from "ava";
import * as sinon from "sinon";

import { getApiClient } from "./api-client";
import { getApiClient, GitHubApiCombinedDetails } from "./api-client";
import { setupTests } from "./testing-utils";
import { initializeEnvironment } from "./util";

Expand All @@ -21,72 +21,6 @@ test.beforeEach(() => {
initializeEnvironment(pkg.version);
});

test("Get the client API", async (t) => {
doTest(
t,
{
auth: "xyz",
externalRepoAuth: "abc",
url: "http://hucairz",
},
undefined,
{
auth: "token xyz",
baseUrl: "http://hucairz/api/v3",
userAgent: `CodeQL-Action/${pkg.version}`,
}
);
});

test("Get the client API external", async (t) => {
doTest(
t,
{
auth: "xyz",
externalRepoAuth: "abc",
url: "http://hucairz",
},
{ allowExternal: true },
{
auth: "token abc",
baseUrl: "http://hucairz/api/v3",
userAgent: `CodeQL-Action/${pkg.version}`,
}
);
});

test("Get the client API external not present", async (t) => {
doTest(
t,
{
auth: "xyz",
url: "http://hucairz",
},
{ allowExternal: true },
{
auth: "token xyz",
baseUrl: "http://hucairz/api/v3",
userAgent: `CodeQL-Action/${pkg.version}`,
}
);
});

test("Get the client API with github url", async (t) => {
doTest(
t,
{
auth: "xyz",
url: "https://github.com/some/invalid/url",
},
undefined,
{
auth: "token xyz",
baseUrl: "https://api.github.com",
userAgent: `CodeQL-Action/${pkg.version}`,
}
);
});

test("Get the API with an API URL directly", async (t) => {
doTest(
t,
Expand All @@ -106,7 +40,7 @@ test("Get the API with an API URL directly", async (t) => {

function doTest(
t: ExecutionContext<unknown>,
clientArgs: any,
clientArgs: GitHubApiCombinedDetails,
clientOptions: any,
expected: any
) {
Expand Down
20 changes: 1 addition & 19 deletions src/api-client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import * as path from "path";

import * as githubUtils from "@actions/github/lib/utils";
import * as retry from "@octokit/plugin-retry";
import consoleLogLevel from "console-log-level";
Expand Down Expand Up @@ -38,31 +36,15 @@ export const getApiClient = function (
const auth =
(allowExternal && apiDetails.externalRepoAuth) || apiDetails.auth;
const retryingOctokit = githubUtils.GitHub.plugin(retry.retry);
const apiURL = apiDetails.apiURL || deriveApiUrl(apiDetails.url);
return new retryingOctokit(
githubUtils.getOctokitOptions(auth, {
baseUrl: apiURL,
baseUrl: apiDetails.apiURL,
userAgent: `CodeQL-Action/${pkg.version}`,
log: consoleLogLevel({ level: "debug" }),
})
);
};

// 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
// then return with a fixed canonical URL.
if (url.hostname === "github.com" || url.hostname === "api.github.com") {
return "https://api.github.com";
}

// Add the /api/v3 API prefix
url.pathname = path.join(url.pathname, "api", "v3");
return url.toString();
}

export function getApiDetails() {
return {
auth: getRequiredInput("token"),
Expand Down

0 comments on commit dac8912

Please sign in to comment.