Skip to content

Commit 529bbe3

Browse files
committed
Handle GHEC-DR repository URLs
1 parent 31b2d24 commit 529bbe3

File tree

5 files changed

+111
-47
lines changed

5 files changed

+111
-47
lines changed

extensions/ql-vscode/src/common/github-url-identifier-helper.ts

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,37 +29,45 @@ function validGitHubNwoOrOwner(
2929

3030
/**
3131
* Extracts an NWO from a GitHub URL.
32-
* @param githubUrl The GitHub repository URL
32+
* @param repositoryUrl The GitHub repository URL
33+
* @param githubUrl The URL of the GitHub instance
3334
* @return The corresponding NWO, or undefined if the URL is not valid
3435
*/
35-
export function getNwoFromGitHubUrl(githubUrl: string): string | undefined {
36-
return getNwoOrOwnerFromGitHubUrl(githubUrl, "nwo");
36+
export function getNwoFromGitHubUrl(
37+
repositoryUrl: string,
38+
githubUrl: URL,
39+
): string | undefined {
40+
return getNwoOrOwnerFromGitHubUrl(repositoryUrl, githubUrl, "nwo");
3741
}
3842

3943
/**
4044
* Extracts an owner from a GitHub URL.
41-
* @param githubUrl The GitHub repository URL
45+
* @param repositoryUrl The GitHub repository URL
46+
* @param githubUrl The URL of the GitHub instance
4247
* @return The corresponding Owner, or undefined if the URL is not valid
4348
*/
44-
export function getOwnerFromGitHubUrl(githubUrl: string): string | undefined {
45-
return getNwoOrOwnerFromGitHubUrl(githubUrl, "owner");
49+
export function getOwnerFromGitHubUrl(
50+
repositoryUrl: string,
51+
githubUrl: URL,
52+
): string | undefined {
53+
return getNwoOrOwnerFromGitHubUrl(repositoryUrl, githubUrl, "owner");
4654
}
4755

4856
function getNwoOrOwnerFromGitHubUrl(
49-
githubUrl: string,
57+
repositoryUrl: string,
58+
githubUrl: URL,
5059
kind: "owner" | "nwo",
5160
): string | undefined {
61+
const validHostnames = [githubUrl.hostname, `www.${githubUrl.hostname}`];
62+
5263
try {
5364
let paths: string[];
54-
const urlElements = githubUrl.split("/");
55-
if (
56-
urlElements[0] === "github.com" ||
57-
urlElements[0] === "www.github.com"
58-
) {
59-
paths = githubUrl.split("/").slice(1);
65+
const urlElements = repositoryUrl.split("/");
66+
if (validHostnames.includes(urlElements[0])) {
67+
paths = repositoryUrl.split("/").slice(1);
6068
} else {
61-
const uri = new URL(githubUrl);
62-
if (uri.hostname !== "github.com" && uri.hostname !== "www.github.com") {
69+
const uri = new URL(repositoryUrl);
70+
if (!validHostnames.includes(uri.hostname)) {
6371
return;
6472
}
6573
paths = uri.pathname.split("/").filter((segment: string) => segment);

extensions/ql-vscode/src/config.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,14 @@ export function getGhecDrUri(): Uri | undefined {
140140
}
141141
}
142142

143+
export function getGitHubInstanceUrl(): URL {
144+
const ghecDrUri = getGhecDrUri();
145+
if (ghecDrUri) {
146+
return new URL(ghecDrUri.toString());
147+
}
148+
return GITHUB_URL;
149+
}
150+
143151
const ROOT_SETTING = new Setting("codeQL");
144152

145153
// Telemetry configuration
@@ -622,11 +630,7 @@ export class VariantAnalysisConfigListener
622630
}
623631

624632
public get githubUrl(): URL {
625-
const ghecDrUri = getGhecDrUri();
626-
if (ghecDrUri) {
627-
return new URL(ghecDrUri.toString());
628-
}
629-
return GITHUB_URL;
633+
return getGitHubInstanceUrl();
630634
}
631635
}
632636

extensions/ql-vscode/src/databases/database-fetcher.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
addDatabaseSourceToWorkspace,
3030
allowHttp,
3131
downloadTimeout,
32+
getGitHubInstanceUrl,
3233
isCanary,
3334
} from "../config";
3435
import { showAndLogInformationMessage } from "../common/logging";
@@ -180,7 +181,8 @@ export class DatabaseFetcher {
180181
makeSelected = true,
181182
addSourceArchiveFolder = addDatabaseSourceToWorkspace(),
182183
): Promise<DatabaseItem | undefined> {
183-
const nwo = getNwoFromGitHubUrl(githubRepo) || githubRepo;
184+
const nwo =
185+
getNwoFromGitHubUrl(githubRepo, getGitHubInstanceUrl()) || githubRepo;
184186
if (!isValidGitHubNwo(nwo)) {
185187
throw new Error(`Invalid GitHub repository: ${githubRepo}`);
186188
}

extensions/ql-vscode/src/databases/ui/db-panel.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import type { App } from "../../common/app";
2525
import { QueryLanguage } from "../../common/query-language";
2626
import { getCodeSearchRepositories } from "../code-search-api";
2727
import { showAndLogErrorMessage } from "../../common/logging";
28+
import { getGitHubInstanceUrl } from "../../config";
2829

2930
export interface RemoteDatabaseQuickPickItem extends QuickPickItem {
3031
remoteDatabaseKind: string;
@@ -155,7 +156,8 @@ export class DbPanel extends DisposableObject {
155156
return;
156157
}
157158

158-
const nwo = getNwoFromGitHubUrl(repoName) || repoName;
159+
const nwo =
160+
getNwoFromGitHubUrl(repoName, getGitHubInstanceUrl()) || repoName;
159161
if (!isValidGitHubNwo(nwo)) {
160162
void showAndLogErrorMessage(
161163
this.app.logger,
@@ -186,7 +188,8 @@ export class DbPanel extends DisposableObject {
186188
return;
187189
}
188190

189-
const owner = getOwnerFromGitHubUrl(ownerName) || ownerName;
191+
const owner =
192+
getOwnerFromGitHubUrl(ownerName, getGitHubInstanceUrl()) || ownerName;
190193
if (!isValidGitHubOwner(owner)) {
191194
void showAndLogErrorMessage(
192195
this.app.logger,

extensions/ql-vscode/test/unit-tests/common/github-url-identifier-helper.test.ts

Lines changed: 71 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import {
66
} from "../../../src/common/github-url-identifier-helper";
77

88
describe("github url identifier helper", () => {
9+
const githubUrl = new URL("https://github.com");
10+
911
describe("valid GitHub Nwo Or Owner method", () => {
1012
it("should return true for valid owner", () => {
1113
expect(isValidGitHubOwner("github")).toBe(true);
@@ -23,51 +25,96 @@ describe("github url identifier helper", () => {
2325

2426
describe("getNwoFromGitHubUrl method", () => {
2527
it("should handle invalid urls", () => {
26-
expect(getNwoFromGitHubUrl("")).toBe(undefined);
27-
expect(getNwoFromGitHubUrl("https://ww.github.com/foo/bar")).toBe(
28+
expect(getNwoFromGitHubUrl("", githubUrl)).toBe(undefined);
29+
expect(
30+
getNwoFromGitHubUrl("https://ww.github.com/foo/bar", githubUrl),
31+
).toBe(undefined);
32+
expect(
33+
getNwoFromGitHubUrl("https://tenant.ghe.com/foo/bar", githubUrl),
34+
).toBe(undefined);
35+
expect(getNwoFromGitHubUrl("https://www.github.com/foo", githubUrl)).toBe(
2836
undefined,
2937
);
30-
expect(getNwoFromGitHubUrl("https://www.github.com/foo")).toBe(undefined);
31-
expect(getNwoFromGitHubUrl("foo")).toBe(undefined);
32-
expect(getNwoFromGitHubUrl("foo/bar")).toBe(undefined);
38+
expect(getNwoFromGitHubUrl("foo", githubUrl)).toBe(undefined);
39+
expect(getNwoFromGitHubUrl("foo/bar", githubUrl)).toBe(undefined);
3340
});
3441

3542
it("should handle valid urls", () => {
36-
expect(getNwoFromGitHubUrl("github.com/foo/bar")).toBe("foo/bar");
37-
expect(getNwoFromGitHubUrl("www.github.com/foo/bar")).toBe("foo/bar");
38-
expect(getNwoFromGitHubUrl("https://github.com/foo/bar")).toBe("foo/bar");
39-
expect(getNwoFromGitHubUrl("http://github.com/foo/bar")).toBe("foo/bar");
40-
expect(getNwoFromGitHubUrl("https://www.github.com/foo/bar")).toBe(
43+
expect(getNwoFromGitHubUrl("github.com/foo/bar", githubUrl)).toBe(
44+
"foo/bar",
45+
);
46+
expect(getNwoFromGitHubUrl("www.github.com/foo/bar", githubUrl)).toBe(
47+
"foo/bar",
48+
);
49+
expect(getNwoFromGitHubUrl("https://github.com/foo/bar", githubUrl)).toBe(
4150
"foo/bar",
4251
);
43-
expect(getNwoFromGitHubUrl("https://github.com/foo/bar/sub/pages")).toBe(
52+
expect(getNwoFromGitHubUrl("http://github.com/foo/bar", githubUrl)).toBe(
4453
"foo/bar",
4554
);
55+
expect(
56+
getNwoFromGitHubUrl("https://www.github.com/foo/bar", githubUrl),
57+
).toBe("foo/bar");
58+
expect(
59+
getNwoFromGitHubUrl("https://github.com/foo/bar/sub/pages", githubUrl),
60+
).toBe("foo/bar");
61+
expect(
62+
getNwoFromGitHubUrl(
63+
"https://tenant.ghe.com/foo/bar",
64+
new URL("https://tenant.ghe.com"),
65+
),
66+
).toBe("foo/bar");
4667
});
4768
});
4869

4970
describe("getOwnerFromGitHubUrl method", () => {
5071
it("should handle invalid urls", () => {
51-
expect(getOwnerFromGitHubUrl("")).toBe(undefined);
52-
expect(getOwnerFromGitHubUrl("https://ww.github.com/foo/bar")).toBe(
53-
undefined,
54-
);
55-
expect(getOwnerFromGitHubUrl("foo")).toBe(undefined);
56-
expect(getOwnerFromGitHubUrl("foo/bar")).toBe(undefined);
72+
expect(getOwnerFromGitHubUrl("", githubUrl)).toBe(undefined);
73+
expect(
74+
getOwnerFromGitHubUrl("https://ww.github.com/foo/bar", githubUrl),
75+
).toBe(undefined);
76+
expect(
77+
getOwnerFromGitHubUrl("https://tenant.ghe.com/foo/bar", githubUrl),
78+
).toBe(undefined);
79+
expect(getOwnerFromGitHubUrl("foo", githubUrl)).toBe(undefined);
80+
expect(getOwnerFromGitHubUrl("foo/bar", githubUrl)).toBe(undefined);
5781
});
5882

5983
it("should handle valid urls", () => {
60-
expect(getOwnerFromGitHubUrl("http://github.com/foo/bar")).toBe("foo");
61-
expect(getOwnerFromGitHubUrl("https://github.com/foo/bar")).toBe("foo");
62-
expect(getOwnerFromGitHubUrl("https://www.github.com/foo/bar")).toBe(
84+
expect(
85+
getOwnerFromGitHubUrl("http://github.com/foo/bar", githubUrl),
86+
).toBe("foo");
87+
expect(
88+
getOwnerFromGitHubUrl("https://github.com/foo/bar", githubUrl),
89+
).toBe("foo");
90+
expect(
91+
getOwnerFromGitHubUrl("https://www.github.com/foo/bar", githubUrl),
92+
).toBe("foo");
93+
expect(
94+
getOwnerFromGitHubUrl(
95+
"https://github.com/foo/bar/sub/pages",
96+
githubUrl,
97+
),
98+
).toBe("foo");
99+
expect(
100+
getOwnerFromGitHubUrl("https://www.github.com/foo", githubUrl),
101+
).toBe("foo");
102+
expect(getOwnerFromGitHubUrl("github.com/foo", githubUrl)).toBe("foo");
103+
expect(getOwnerFromGitHubUrl("www.github.com/foo", githubUrl)).toBe(
63104
"foo",
64105
);
65106
expect(
66-
getOwnerFromGitHubUrl("https://github.com/foo/bar/sub/pages"),
107+
getOwnerFromGitHubUrl(
108+
"https://tenant.ghe.com/foo/bar",
109+
new URL("https://tenant.ghe.com"),
110+
),
111+
).toBe("foo");
112+
expect(
113+
getOwnerFromGitHubUrl(
114+
"https://tenant.ghe.com/foo",
115+
new URL("https://tenant.ghe.com"),
116+
),
67117
).toBe("foo");
68-
expect(getOwnerFromGitHubUrl("https://www.github.com/foo")).toBe("foo");
69-
expect(getOwnerFromGitHubUrl("github.com/foo")).toBe("foo");
70-
expect(getOwnerFromGitHubUrl("www.github.com/foo")).toBe("foo");
71118
});
72119
});
73120
});

0 commit comments

Comments
 (0)