Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ describe("pull-request-url", () => {
expect(
normalizeGitHubRepoUrl("ssh://git@github.com/Kitenite/superset.git"),
).toBe("https://github.com/Kitenite/superset");
expect(
normalizeGitHubRepoUrl("https://someone:ghp_x@github.com/acme/foo.git"),
).toBe("https://github.com/acme/foo");
expect(
normalizeGitHubRepoUrl("https://example.com?next=@github.com/acme/foo"),
).toBeNull();
});

test("parses upstream refs with slashes in branch names", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function normalizeGitHubRepoUrl(remoteUrl: string): string | null {
const match = [
/^git@github\.com:(?<owner>[^/]+)\/(?<repo>[^/]+?)(?:\.git)?$/,
/^ssh:\/\/git@github\.com\/(?<owner>[^/]+)\/(?<repo>[^/]+?)(?:\.git)?$/,
/^https:\/\/github\.com\/(?<owner>[^/]+)\/(?<repo>[^/]+?)(?:\.git)?\/?$/,
/^https:\/\/(?:[^@/?#\\]+@)?github\.com\/(?<owner>[^/?#\\]+)\/(?<repo>[^/?#\\]+?)(?:\.git)?\/?$/,
]
.map((pattern) => pattern.exec(trimmedRemoteUrl))
.find((result) => result?.groups?.owner && result.groups.repo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export function normalizeGitHubUrl(remoteUrl: string): string | null {
const patterns = [
/^git@github\.com:(?<nwo>[^/]+\/[^/]+?)(?:\.git)?$/,
/^ssh:\/\/git@github\.com\/(?<nwo>[^/]+\/[^/]+?)(?:\.git)?$/,
/^https:\/\/github\.com\/(?<nwo>[^/]+\/[^/]+?)(?:\.git)?\/?$/,
/^https:\/\/(?:[^@/?#\\]+@)?github\.com\/(?<nwo>[^/?#\\]+\/[^/?#\\]+?)(?:\.git)?\/?$/,
];
for (const pattern of patterns) {
const match = pattern.exec(trimmed);
Expand Down
30 changes: 30 additions & 0 deletions packages/shared/src/github-remote.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { describe, expect, test } from "bun:test";
import { parseGitHubRemote } from "./github-remote";

describe("parseGitHubRemote", () => {
test("accepts HTTPS remotes with userinfo and drops it from the URL", () => {
for (const url of [
"https://someone@github.com/acme/foo.git",
"https://someone:ghp_x@github.com/acme/foo",
]) {
expect(parseGitHubRemote(url)).toEqual({
provider: "github",
owner: "acme",
name: "foo",
url: "https://github.com/acme/foo",
});
}
});

test("rejects HTTPS remotes with a query, fragment, or backslash", () => {
for (const url of [
"https://example.com?next=@github.com/acme/foo.git",
"https://example.com#@github.com/acme/foo",
"https://example.com\\@github.com/acme/foo",
"https://github.com/acme?next/foo",
"https://github.com/acme/foo#bar",
]) {
expect(parseGitHubRemote(url)).toBeNull();
}
});
});
2 changes: 1 addition & 1 deletion packages/shared/src/github-remote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export function parseGitHubRemote(
const patterns = [
/^git@github\.com:(?<owner>[^/]+)\/(?<name>[^/]+?)(?:\.git)?$/,
/^ssh:\/\/git@github\.com\/(?<owner>[^/]+)\/(?<name>[^/]+?)(?:\.git)?$/,
/^https:\/\/github\.com\/(?<owner>[^/]+)\/(?<name>[^/]+?)(?:\.git)?\/?$/,
/^https:\/\/(?:[^@/?#\\]+@)?github\.com\/(?<owner>[^/?#\\]+)\/(?<name>[^/?#\\]+?)(?:\.git)?\/?$/,
];

for (const pattern of patterns) {
Expand Down
Loading