diff --git a/extensions/ql-vscode/src/common/github-url-identifier-helper.ts b/extensions/ql-vscode/src/common/github-url-identifier-helper.ts index 2499ef663fc..748ef2fad90 100644 --- a/extensions/ql-vscode/src/common/github-url-identifier-helper.ts +++ b/extensions/ql-vscode/src/common/github-url-identifier-helper.ts @@ -50,14 +50,20 @@ function getNwoOrOwnerFromGitHubUrl( kind: "owner" | "nwo", ): string | undefined { try { - const uri = new URL(githubUrl); - if (uri.protocol !== "https:") { - return; + let paths: string[]; + const urlElements = githubUrl.split("/"); + if ( + urlElements[0] === "github.com" || + urlElements[0] === "www.github.com" + ) { + paths = githubUrl.split("/").slice(1); + } else { + const uri = new URL(githubUrl); + if (uri.hostname !== "github.com" && uri.hostname !== "www.github.com") { + return; + } + paths = uri.pathname.split("/").filter((segment: string) => segment); } - if (uri.hostname !== "github.com" && uri.hostname !== "www.github.com") { - return; - } - const paths = uri.pathname.split("/").filter((segment: string) => segment); const owner = `${paths[0]}`; if (kind === "owner") { return owner ? owner : undefined; diff --git a/extensions/ql-vscode/test/unit-tests/common/github-url-identifier-helper.test.ts b/extensions/ql-vscode/test/unit-tests/common/github-url-identifier-helper.test.ts index 16d24af8df8..e628e95e9ad 100644 --- a/extensions/ql-vscode/test/unit-tests/common/github-url-identifier-helper.test.ts +++ b/extensions/ql-vscode/test/unit-tests/common/github-url-identifier-helper.test.ts @@ -24,7 +24,6 @@ describe("github url identifier helper", () => { describe("getNwoFromGitHubUrl method", () => { it("should handle invalid urls", () => { expect(getNwoFromGitHubUrl("")).toBe(undefined); - expect(getNwoFromGitHubUrl("http://github.com/foo/bar")).toBe(undefined); expect(getNwoFromGitHubUrl("https://ww.github.com/foo/bar")).toBe( undefined, ); @@ -34,7 +33,10 @@ describe("github url identifier helper", () => { }); 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( "foo/bar", ); @@ -47,9 +49,6 @@ describe("github url identifier helper", () => { describe("getOwnerFromGitHubUrl method", () => { it("should handle invalid urls", () => { expect(getOwnerFromGitHubUrl("")).toBe(undefined); - expect(getOwnerFromGitHubUrl("http://github.com/foo/bar")).toBe( - undefined, - ); expect(getOwnerFromGitHubUrl("https://ww.github.com/foo/bar")).toBe( undefined, ); @@ -58,6 +57,7 @@ describe("github url identifier helper", () => { }); 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( "foo", @@ -66,6 +66,8 @@ describe("github url identifier helper", () => { getOwnerFromGitHubUrl("https://github.com/foo/bar/sub/pages"), ).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"); }); }); });