Skip to content

Commit 9f240c8

Browse files
authored
Merge pull request #2028 from github/nora/polish-url-helper
DP Panel: Drop protocol check when creating a new item
2 parents d4b0611 + 4c54778 commit 9f240c8

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,20 @@ function getNwoOrOwnerFromGitHubUrl(
5050
kind: "owner" | "nwo",
5151
): string | undefined {
5252
try {
53-
const uri = new URL(githubUrl);
54-
if (uri.protocol !== "https:") {
55-
return;
53+
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);
60+
} else {
61+
const uri = new URL(githubUrl);
62+
if (uri.hostname !== "github.com" && uri.hostname !== "www.github.com") {
63+
return;
64+
}
65+
paths = uri.pathname.split("/").filter((segment: string) => segment);
5666
}
57-
if (uri.hostname !== "github.com" && uri.hostname !== "www.github.com") {
58-
return;
59-
}
60-
const paths = uri.pathname.split("/").filter((segment: string) => segment);
6167
const owner = `${paths[0]}`;
6268
if (kind === "owner") {
6369
return owner ? owner : undefined;

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ describe("github url identifier helper", () => {
2424
describe("getNwoFromGitHubUrl method", () => {
2525
it("should handle invalid urls", () => {
2626
expect(getNwoFromGitHubUrl("")).toBe(undefined);
27-
expect(getNwoFromGitHubUrl("http://github.com/foo/bar")).toBe(undefined);
2827
expect(getNwoFromGitHubUrl("https://ww.github.com/foo/bar")).toBe(
2928
undefined,
3029
);
@@ -34,7 +33,10 @@ describe("github url identifier helper", () => {
3433
});
3534

3635
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");
3738
expect(getNwoFromGitHubUrl("https://github.com/foo/bar")).toBe("foo/bar");
39+
expect(getNwoFromGitHubUrl("http://github.com/foo/bar")).toBe("foo/bar");
3840
expect(getNwoFromGitHubUrl("https://www.github.com/foo/bar")).toBe(
3941
"foo/bar",
4042
);
@@ -47,9 +49,6 @@ describe("github url identifier helper", () => {
4749
describe("getOwnerFromGitHubUrl method", () => {
4850
it("should handle invalid urls", () => {
4951
expect(getOwnerFromGitHubUrl("")).toBe(undefined);
50-
expect(getOwnerFromGitHubUrl("http://github.com/foo/bar")).toBe(
51-
undefined,
52-
);
5352
expect(getOwnerFromGitHubUrl("https://ww.github.com/foo/bar")).toBe(
5453
undefined,
5554
);
@@ -58,6 +57,7 @@ describe("github url identifier helper", () => {
5857
});
5958

6059
it("should handle valid urls", () => {
60+
expect(getOwnerFromGitHubUrl("http://github.com/foo/bar")).toBe("foo");
6161
expect(getOwnerFromGitHubUrl("https://github.com/foo/bar")).toBe("foo");
6262
expect(getOwnerFromGitHubUrl("https://www.github.com/foo/bar")).toBe(
6363
"foo",
@@ -66,6 +66,8 @@ describe("github url identifier helper", () => {
6666
getOwnerFromGitHubUrl("https://github.com/foo/bar/sub/pages"),
6767
).toBe("foo");
6868
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");
6971
});
7072
});
7173
});

0 commit comments

Comments
 (0)