Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions extensions/ql-vscode/src/common/github-url-identifier-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand All @@ -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");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we also add a check for just "github.com/foo/bar"?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea!

expect(getNwoFromGitHubUrl("https://www.github.com/foo/bar")).toBe(
"foo/bar",
);
Expand All @@ -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,
);
Expand All @@ -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",
Expand All @@ -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");
});
});
});