Skip to content

Commit 0b0ac95

Browse files
committed
Move some definitions out of util.ts into a new github.ts module
1 parent 63387d1 commit 0b0ac95

9 files changed

Lines changed: 184 additions & 158 deletions

File tree

‎lib/entry-points.js‎

Lines changed: 46 additions & 44 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: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,15 @@ import {
1919
getEnv,
2020
exportEnvVar,
2121
} from "./environment";
22+
import { GITHUB_DOTCOM_URL, parseGitHubUrl } from "./github";
2223
import { Logger } from "./logging";
2324
import { getRepositoryNwo, RepositoryNwo } from "./repository";
2425
import {
2526
asHTTPError,
2627
ConfigurationError,
2728
getRequiredEnvParam,
28-
GITHUB_DOTCOM_URL,
2929
GitHubVariant,
3030
GitHubVersion,
31-
parseGitHubUrl,
3231
parseMatrixInput,
3332
} from "./util";
3433

‎src/database-upload.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ import { type CodeQL } from "./codeql";
1212
import { Config } from "./config-utils";
1313
import { Feature } from "./feature-flags";
1414
import * as gitUtils from "./git-utils";
15+
import { parseGitHubUrl } from "./github";
1516
import { Logger, withGroupAsync } from "./logging";
1617
import { OverlayDatabaseMode } from "./overlay/overlay-database-mode";
1718
import { RepositoryNwo } from "./repository";
1819
import * as util from "./util";
19-
import { asHTTPError, bundleDb, CleanupLevel, parseGitHubUrl } from "./util";
20+
import { asHTTPError, bundleDb, CleanupLevel } from "./util";
2021

2122
/** Information about a database upload. */
2223
export interface DatabaseUploadResult {

‎src/github.test.ts‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import test from "ava";
2+
3+
import * as github from "./github";
4+
import { setupTests } from "./testing-utils";
5+
6+
setupTests(test);
7+
8+
test("parseGitHubUrl", (t) => {
9+
t.deepEqual(github.parseGitHubUrl("github.com"), "https://github.com");
10+
t.deepEqual(
11+
github.parseGitHubUrl("https://github.com"),
12+
"https://github.com",
13+
);
14+
t.deepEqual(
15+
github.parseGitHubUrl("https://api.github.com"),
16+
"https://github.com",
17+
);
18+
t.deepEqual(
19+
github.parseGitHubUrl("https://github.com/foo/bar"),
20+
"https://github.com",
21+
);
22+
23+
t.deepEqual(
24+
github.parseGitHubUrl("github.example.com"),
25+
"https://github.example.com/",
26+
);
27+
t.deepEqual(
28+
github.parseGitHubUrl("https://github.example.com"),
29+
"https://github.example.com/",
30+
);
31+
t.deepEqual(
32+
github.parseGitHubUrl("https://api.github.example.com"),
33+
"https://github.example.com/",
34+
);
35+
t.deepEqual(
36+
github.parseGitHubUrl("https://github.example.com/api/v3"),
37+
"https://github.example.com/",
38+
);
39+
t.deepEqual(
40+
github.parseGitHubUrl("https://github.example.com:1234"),
41+
"https://github.example.com:1234/",
42+
);
43+
t.deepEqual(
44+
github.parseGitHubUrl("https://api.github.example.com:1234"),
45+
"https://github.example.com:1234/",
46+
);
47+
t.deepEqual(
48+
github.parseGitHubUrl("https://github.example.com:1234/api/v3"),
49+
"https://github.example.com:1234/",
50+
);
51+
t.deepEqual(
52+
github.parseGitHubUrl("https://github.example.com/base/path"),
53+
"https://github.example.com/base/path/",
54+
);
55+
t.deepEqual(
56+
github.parseGitHubUrl("https://github.example.com/base/path/api/v3"),
57+
"https://github.example.com/base/path/",
58+
);
59+
60+
t.throws(() => github.parseGitHubUrl(""), {
61+
message: '"" is not a valid URL',
62+
});
63+
t.throws(() => github.parseGitHubUrl("ssh://github.com"), {
64+
message: '"ssh://github.com" is not a http or https URL',
65+
});
66+
t.throws(() => github.parseGitHubUrl("http:///::::433"), {
67+
message: '"http:///::::433" is not a valid URL',
68+
});
69+
});

‎src/github.ts‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import type { GitHubInstance } from "./api-client";
2+
import { ActionsEnvVars, type ReadOnlyEnv } from "./environment";
3+
import { ConfigurationError } from "./util";
4+
5+
/** A string representing a GitHub instance address. */
6+
export type InstanceURL = string;
7+
8+
/**
9+
* The URL for github.com.
10+
*/
11+
export const GITHUB_DOTCOM_URL: InstanceURL = "https://github.com";
12+
13+
/** A string representing a GitHub API instance address. */
14+
export type InstanceApiURL = string;
15+
16+
/**
17+
* The URL for api.github.com.
18+
*/
19+
export const GITHUB_DOTCOM_API_URL: InstanceApiURL = "https://api.github.com";
20+
21+
/**
22+
* Parses user input of a github.com or GHES URL to a canonical form.
23+
* Removes any API prefix or suffix if one is present.
24+
*/
25+
export function parseGitHubUrl(inputUrl: string): string {
26+
const originalUrl = inputUrl;
27+
if (inputUrl.indexOf("://") === -1) {
28+
inputUrl = `https://${inputUrl}`;
29+
}
30+
if (!inputUrl.startsWith("http://") && !inputUrl.startsWith("https://")) {
31+
throw new ConfigurationError(`"${originalUrl}" is not a http or https URL`);
32+
}
33+
34+
let url: URL;
35+
try {
36+
url = new URL(inputUrl);
37+
} catch {
38+
throw new ConfigurationError(`"${originalUrl}" is not a valid URL`);
39+
}
40+
41+
// If we detect this is trying to be to github.com
42+
// then return with a fixed canonical URL.
43+
if (url.hostname === "github.com" || url.hostname === "api.github.com") {
44+
return GITHUB_DOTCOM_URL;
45+
}
46+
47+
// Remove the API prefix if it's present
48+
if (url.pathname.indexOf("/api/v3") !== -1) {
49+
url.pathname = url.pathname.substring(0, url.pathname.indexOf("/api/v3"));
50+
}
51+
// Also consider subdomain isolation on GHES
52+
if (url.hostname.startsWith("api.")) {
53+
url.hostname = url.hostname.substring(4);
54+
}
55+
56+
// Normalise path to having a trailing slash for consistency
57+
if (!url.pathname.endsWith("/")) {
58+
url.pathname = `${url.pathname}/`;
59+
}
60+
61+
return url.toString();
62+
}

‎src/setup-codeql.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {
3636
Feature,
3737
FeatureEnablement,
3838
} from "./feature-flags";
39+
import { GITHUB_DOTCOM_URL } from "./github";
3940
import { BuiltInLanguage } from "./languages";
4041
import { Logger } from "./logging";
4142
import { getCodeQlVersionsForOverlayBaseDatabases } from "./overlay/caching";
@@ -100,7 +101,7 @@ async function getCodeQLBundleDownloadURL(
100101
// This GitHub instance, and the canonical Action.
101102
[apiDetails.url, CODEQL_DEFAULT_ACTION_REPOSITORY],
102103
// GitHub.com, and the canonical Action.
103-
[util.GITHUB_DOTCOM_URL, CODEQL_DEFAULT_ACTION_REPOSITORY],
104+
[GITHUB_DOTCOM_URL, CODEQL_DEFAULT_ACTION_REPOSITORY],
104105
];
105106
// We now filter out any duplicates.
106107
// Duplicates will happen either because the GitHub instance is GitHub.com, or because the Action is not a fork.
@@ -113,7 +114,7 @@ async function getCodeQLBundleDownloadURL(
113114
const [apiURL, repository] = downloadSource;
114115
// If we've reached the final case, short-circuit the API check since we know the bundle exists and is public.
115116
if (
116-
apiURL === util.GITHUB_DOTCOM_URL &&
117+
apiURL === GITHUB_DOTCOM_URL &&
117118
repository === CODEQL_DEFAULT_ACTION_REPOSITORY
118119
) {
119120
break;

0 commit comments

Comments
 (0)