Handle GHEC-DR repository URLs
This commit is contained in:
@@ -29,37 +29,45 @@ function validGitHubNwoOrOwner(
|
||||
|
||||
/**
|
||||
* Extracts an NWO from a GitHub URL.
|
||||
* @param githubUrl The GitHub repository URL
|
||||
* @param repositoryUrl The GitHub repository URL
|
||||
* @param githubUrl The URL of the GitHub instance
|
||||
* @return The corresponding NWO, or undefined if the URL is not valid
|
||||
*/
|
||||
export function getNwoFromGitHubUrl(githubUrl: string): string | undefined {
|
||||
return getNwoOrOwnerFromGitHubUrl(githubUrl, "nwo");
|
||||
export function getNwoFromGitHubUrl(
|
||||
repositoryUrl: string,
|
||||
githubUrl: URL,
|
||||
): string | undefined {
|
||||
return getNwoOrOwnerFromGitHubUrl(repositoryUrl, githubUrl, "nwo");
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts an owner from a GitHub URL.
|
||||
* @param githubUrl The GitHub repository URL
|
||||
* @param repositoryUrl The GitHub repository URL
|
||||
* @param githubUrl The URL of the GitHub instance
|
||||
* @return The corresponding Owner, or undefined if the URL is not valid
|
||||
*/
|
||||
export function getOwnerFromGitHubUrl(githubUrl: string): string | undefined {
|
||||
return getNwoOrOwnerFromGitHubUrl(githubUrl, "owner");
|
||||
export function getOwnerFromGitHubUrl(
|
||||
repositoryUrl: string,
|
||||
githubUrl: URL,
|
||||
): string | undefined {
|
||||
return getNwoOrOwnerFromGitHubUrl(repositoryUrl, githubUrl, "owner");
|
||||
}
|
||||
|
||||
function getNwoOrOwnerFromGitHubUrl(
|
||||
githubUrl: string,
|
||||
repositoryUrl: string,
|
||||
githubUrl: URL,
|
||||
kind: "owner" | "nwo",
|
||||
): string | undefined {
|
||||
const validHostnames = [githubUrl.hostname, `www.${githubUrl.hostname}`];
|
||||
|
||||
try {
|
||||
let paths: string[];
|
||||
const urlElements = githubUrl.split("/");
|
||||
if (
|
||||
urlElements[0] === "github.com" ||
|
||||
urlElements[0] === "www.github.com"
|
||||
) {
|
||||
paths = githubUrl.split("/").slice(1);
|
||||
const urlElements = repositoryUrl.split("/");
|
||||
if (validHostnames.includes(urlElements[0])) {
|
||||
paths = repositoryUrl.split("/").slice(1);
|
||||
} else {
|
||||
const uri = new URL(githubUrl);
|
||||
if (uri.hostname !== "github.com" && uri.hostname !== "www.github.com") {
|
||||
const uri = new URL(repositoryUrl);
|
||||
if (!validHostnames.includes(uri.hostname)) {
|
||||
return;
|
||||
}
|
||||
paths = uri.pathname.split("/").filter((segment: string) => segment);
|
||||
|
||||
@@ -140,6 +140,14 @@ export function getGhecDrUri(): Uri | undefined {
|
||||
}
|
||||
}
|
||||
|
||||
export function getGitHubInstanceUrl(): URL {
|
||||
const ghecDrUri = getGhecDrUri();
|
||||
if (ghecDrUri) {
|
||||
return new URL(ghecDrUri.toString());
|
||||
}
|
||||
return GITHUB_URL;
|
||||
}
|
||||
|
||||
const ROOT_SETTING = new Setting("codeQL");
|
||||
|
||||
// Telemetry configuration
|
||||
@@ -622,11 +630,7 @@ export class VariantAnalysisConfigListener
|
||||
}
|
||||
|
||||
public get githubUrl(): URL {
|
||||
const ghecDrUri = getGhecDrUri();
|
||||
if (ghecDrUri) {
|
||||
return new URL(ghecDrUri.toString());
|
||||
}
|
||||
return GITHUB_URL;
|
||||
return getGitHubInstanceUrl();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -29,6 +29,7 @@ import {
|
||||
addDatabaseSourceToWorkspace,
|
||||
allowHttp,
|
||||
downloadTimeout,
|
||||
getGitHubInstanceUrl,
|
||||
isCanary,
|
||||
} from "../config";
|
||||
import { showAndLogInformationMessage } from "../common/logging";
|
||||
@@ -180,7 +181,8 @@ export class DatabaseFetcher {
|
||||
makeSelected = true,
|
||||
addSourceArchiveFolder = addDatabaseSourceToWorkspace(),
|
||||
): Promise<DatabaseItem | undefined> {
|
||||
const nwo = getNwoFromGitHubUrl(githubRepo) || githubRepo;
|
||||
const nwo =
|
||||
getNwoFromGitHubUrl(githubRepo, getGitHubInstanceUrl()) || githubRepo;
|
||||
if (!isValidGitHubNwo(nwo)) {
|
||||
throw new Error(`Invalid GitHub repository: ${githubRepo}`);
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ import type { App } from "../../common/app";
|
||||
import { QueryLanguage } from "../../common/query-language";
|
||||
import { getCodeSearchRepositories } from "../code-search-api";
|
||||
import { showAndLogErrorMessage } from "../../common/logging";
|
||||
import { getGitHubInstanceUrl } from "../../config";
|
||||
|
||||
export interface RemoteDatabaseQuickPickItem extends QuickPickItem {
|
||||
remoteDatabaseKind: string;
|
||||
@@ -155,7 +156,8 @@ export class DbPanel extends DisposableObject {
|
||||
return;
|
||||
}
|
||||
|
||||
const nwo = getNwoFromGitHubUrl(repoName) || repoName;
|
||||
const nwo =
|
||||
getNwoFromGitHubUrl(repoName, getGitHubInstanceUrl()) || repoName;
|
||||
if (!isValidGitHubNwo(nwo)) {
|
||||
void showAndLogErrorMessage(
|
||||
this.app.logger,
|
||||
@@ -186,7 +188,8 @@ export class DbPanel extends DisposableObject {
|
||||
return;
|
||||
}
|
||||
|
||||
const owner = getOwnerFromGitHubUrl(ownerName) || ownerName;
|
||||
const owner =
|
||||
getOwnerFromGitHubUrl(ownerName, getGitHubInstanceUrl()) || ownerName;
|
||||
if (!isValidGitHubOwner(owner)) {
|
||||
void showAndLogErrorMessage(
|
||||
this.app.logger,
|
||||
|
||||
@@ -6,6 +6,8 @@ import {
|
||||
} from "../../../src/common/github-url-identifier-helper";
|
||||
|
||||
describe("github url identifier helper", () => {
|
||||
const githubUrl = new URL("https://github.com");
|
||||
|
||||
describe("valid GitHub Nwo Or Owner method", () => {
|
||||
it("should return true for valid owner", () => {
|
||||
expect(isValidGitHubOwner("github")).toBe(true);
|
||||
@@ -23,51 +25,96 @@ describe("github url identifier helper", () => {
|
||||
|
||||
describe("getNwoFromGitHubUrl method", () => {
|
||||
it("should handle invalid urls", () => {
|
||||
expect(getNwoFromGitHubUrl("")).toBe(undefined);
|
||||
expect(getNwoFromGitHubUrl("https://ww.github.com/foo/bar")).toBe(
|
||||
expect(getNwoFromGitHubUrl("", githubUrl)).toBe(undefined);
|
||||
expect(
|
||||
getNwoFromGitHubUrl("https://ww.github.com/foo/bar", githubUrl),
|
||||
).toBe(undefined);
|
||||
expect(
|
||||
getNwoFromGitHubUrl("https://tenant.ghe.com/foo/bar", githubUrl),
|
||||
).toBe(undefined);
|
||||
expect(getNwoFromGitHubUrl("https://www.github.com/foo", githubUrl)).toBe(
|
||||
undefined,
|
||||
);
|
||||
expect(getNwoFromGitHubUrl("https://www.github.com/foo")).toBe(undefined);
|
||||
expect(getNwoFromGitHubUrl("foo")).toBe(undefined);
|
||||
expect(getNwoFromGitHubUrl("foo/bar")).toBe(undefined);
|
||||
expect(getNwoFromGitHubUrl("foo", githubUrl)).toBe(undefined);
|
||||
expect(getNwoFromGitHubUrl("foo/bar", githubUrl)).toBe(undefined);
|
||||
});
|
||||
|
||||
it("should handle valid urls", () => {
|
||||
expect(getNwoFromGitHubUrl("github.com/foo/bar")).toBe("foo/bar");
|
||||
expect(getNwoFromGitHubUrl("www.github.com/foo/bar")).toBe("foo/bar");
|
||||
expect(getNwoFromGitHubUrl("https://github.com/foo/bar")).toBe("foo/bar");
|
||||
expect(getNwoFromGitHubUrl("http://github.com/foo/bar")).toBe("foo/bar");
|
||||
expect(getNwoFromGitHubUrl("https://www.github.com/foo/bar")).toBe(
|
||||
expect(getNwoFromGitHubUrl("github.com/foo/bar", githubUrl)).toBe(
|
||||
"foo/bar",
|
||||
);
|
||||
expect(getNwoFromGitHubUrl("https://github.com/foo/bar/sub/pages")).toBe(
|
||||
expect(getNwoFromGitHubUrl("www.github.com/foo/bar", githubUrl)).toBe(
|
||||
"foo/bar",
|
||||
);
|
||||
expect(getNwoFromGitHubUrl("https://github.com/foo/bar", githubUrl)).toBe(
|
||||
"foo/bar",
|
||||
);
|
||||
expect(getNwoFromGitHubUrl("http://github.com/foo/bar", githubUrl)).toBe(
|
||||
"foo/bar",
|
||||
);
|
||||
expect(
|
||||
getNwoFromGitHubUrl("https://www.github.com/foo/bar", githubUrl),
|
||||
).toBe("foo/bar");
|
||||
expect(
|
||||
getNwoFromGitHubUrl("https://github.com/foo/bar/sub/pages", githubUrl),
|
||||
).toBe("foo/bar");
|
||||
expect(
|
||||
getNwoFromGitHubUrl(
|
||||
"https://tenant.ghe.com/foo/bar",
|
||||
new URL("https://tenant.ghe.com"),
|
||||
),
|
||||
).toBe("foo/bar");
|
||||
});
|
||||
});
|
||||
|
||||
describe("getOwnerFromGitHubUrl method", () => {
|
||||
it("should handle invalid urls", () => {
|
||||
expect(getOwnerFromGitHubUrl("")).toBe(undefined);
|
||||
expect(getOwnerFromGitHubUrl("https://ww.github.com/foo/bar")).toBe(
|
||||
undefined,
|
||||
);
|
||||
expect(getOwnerFromGitHubUrl("foo")).toBe(undefined);
|
||||
expect(getOwnerFromGitHubUrl("foo/bar")).toBe(undefined);
|
||||
expect(getOwnerFromGitHubUrl("", githubUrl)).toBe(undefined);
|
||||
expect(
|
||||
getOwnerFromGitHubUrl("https://ww.github.com/foo/bar", githubUrl),
|
||||
).toBe(undefined);
|
||||
expect(
|
||||
getOwnerFromGitHubUrl("https://tenant.ghe.com/foo/bar", githubUrl),
|
||||
).toBe(undefined);
|
||||
expect(getOwnerFromGitHubUrl("foo", githubUrl)).toBe(undefined);
|
||||
expect(getOwnerFromGitHubUrl("foo/bar", githubUrl)).toBe(undefined);
|
||||
});
|
||||
|
||||
it("should handle valid urls", () => {
|
||||
expect(getOwnerFromGitHubUrl("http://github.com/foo/bar")).toBe("foo");
|
||||
expect(getOwnerFromGitHubUrl("https://github.com/foo/bar")).toBe("foo");
|
||||
expect(getOwnerFromGitHubUrl("https://www.github.com/foo/bar")).toBe(
|
||||
expect(
|
||||
getOwnerFromGitHubUrl("http://github.com/foo/bar", githubUrl),
|
||||
).toBe("foo");
|
||||
expect(
|
||||
getOwnerFromGitHubUrl("https://github.com/foo/bar", githubUrl),
|
||||
).toBe("foo");
|
||||
expect(
|
||||
getOwnerFromGitHubUrl("https://www.github.com/foo/bar", githubUrl),
|
||||
).toBe("foo");
|
||||
expect(
|
||||
getOwnerFromGitHubUrl(
|
||||
"https://github.com/foo/bar/sub/pages",
|
||||
githubUrl,
|
||||
),
|
||||
).toBe("foo");
|
||||
expect(
|
||||
getOwnerFromGitHubUrl("https://www.github.com/foo", githubUrl),
|
||||
).toBe("foo");
|
||||
expect(getOwnerFromGitHubUrl("github.com/foo", githubUrl)).toBe("foo");
|
||||
expect(getOwnerFromGitHubUrl("www.github.com/foo", githubUrl)).toBe(
|
||||
"foo",
|
||||
);
|
||||
expect(
|
||||
getOwnerFromGitHubUrl("https://github.com/foo/bar/sub/pages"),
|
||||
getOwnerFromGitHubUrl(
|
||||
"https://tenant.ghe.com/foo/bar",
|
||||
new URL("https://tenant.ghe.com"),
|
||||
),
|
||||
).toBe("foo");
|
||||
expect(
|
||||
getOwnerFromGitHubUrl(
|
||||
"https://tenant.ghe.com/foo",
|
||||
new URL("https://tenant.ghe.com"),
|
||||
),
|
||||
).toBe("foo");
|
||||
expect(getOwnerFromGitHubUrl("https://www.github.com/foo")).toBe("foo");
|
||||
expect(getOwnerFromGitHubUrl("github.com/foo")).toBe("foo");
|
||||
expect(getOwnerFromGitHubUrl("www.github.com/foo")).toBe("foo");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user