Skip to content

Commit 4905cc8

Browse files
committed
Simplified repo parsing
1 parent 7fca879 commit 4905cc8

File tree

2 files changed

+17
-52
lines changed

2 files changed

+17
-52
lines changed

public/scripts/workflow.js

Lines changed: 17 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,51 @@
11
function parseRepositoryURL(repoURL) {
2+
// Remove any trailing .git suffix and trim whitespace
23
repoURL = repoURL.trim().replace(/\.git$/i, "");
34

4-
// SSH and git protocol (e.g., [email protected]:owner/repo or git://host/owner/repo)
5-
const m = repoURL.match(
6-
/^(git@|ssh:\/\/git@|git:\/\/)([^:/]+)[:/]((?:[^/]+\/)+[^/]+)$/i,
7-
);
5+
// Normalize all input to HTTP(S) URL for easier parsing
6+
let m = repoURL.match(/^(git@|ssh:\/\/git@|git:\/\/)([^:/]+)[:\/]((?:[^/]+\/)+[^/]+)$/i);
87
if (m) {
9-
const host = m[2];
10-
const parts = m[3].split("/").filter(Boolean);
11-
// GitHub Enterprise SSH URL: git@github.com:enterprises/enterprise/owner/repo
12-
if (
13-
host.toLowerCase() === "github.com" &&
14-
parts[0] === "enterprises" &&
15-
parts.length >= 4
16-
) {
17-
return {
18-
origin: `https://github.com`,
19-
enterprise: parts[1],
20-
owner: parts[2],
21-
repo: parts[3],
22-
};
23-
} else if (parts.length === 2) {
24-
// Standard SSH URL: git@host:owner/repo
25-
return {
26-
origin: `https://${host}`,
27-
owner: parts[0],
28-
repo: parts[1],
29-
};
30-
}
31-
// Unrecognized SSH format
32-
return null;
33-
}
34-
35-
// Shorthand and domain/owner/repo (e.g., owner/repo or host/owner/repo)
36-
if (!/^\w+:\/\//.test(repoURL)) {
8+
// SSH or git: git@github.com:owner/repo or ssh://[email protected]/owner/repo or git://github.com/owner/repo
9+
repoURL = `https://${m[2]}/${m[3]}`;
10+
} else if (!/^\w+:\/\//.test(repoURL)) {
11+
// Shorthand: owner/repo or host/owner/repo
3712
const parts = repoURL.split("/").filter(Boolean);
38-
if (parts.length === 2) {
39-
// owner/repo shorthand (assume github.com)
40-
return {
41-
origin: "https://github.com",
42-
owner: parts[0],
43-
repo: parts[1],
44-
};
45-
} else if (parts.length >= 3 && parts[0].includes(".")) {
46-
// treat as URL, fall through to URL parsing
47-
repoURL = "https://" + repoURL;
48-
} else {
49-
// Unrecognized shorthand
50-
return null;
13+
if (parts.length >= 3 && parts[0].includes(".")) {
14+
// host/owner/repo
15+
repoURL = `https://${repoURL}`;
16+
} else if (parts.length >= 2 && !parts[0].includes(".")) {
17+
// owner/repo
18+
repoURL = `https://github.com/${parts[0]}/${parts[1]}`;
5119
}
5220
}
5321

54-
// HTTP(S) URLs (e.g., https://github.com/owner/repo)
22+
// Parse the normalized URL and extract components
5523
try {
5624
const url = new URL(repoURL);
5725
const parts = url.pathname.split("/").filter(Boolean);
58-
// GitHub Enterprise HTTP URL: https://github.com/enterprises/enterprise/owner/repo
5926
if (
6027
url.host.toLowerCase() === "github.com" &&
6128
parts[0] === "enterprises" &&
6229
parts.length >= 4
6330
) {
31+
// Enterprise: http(s)://github.com/enterprises/enterprise/owner/repo
6432
return {
6533
origin: url.origin,
6634
enterprise: parts[1],
6735
owner: parts[2],
6836
repo: parts[3],
6937
};
7038
} else if (parts.length >= 2) {
71-
// Standard HTTP(S) URL: https://host/owner/repo
39+
// Standard: http(s)://host/owner/repo
7240
return {
7341
origin: url.origin,
7442
owner: parts[0],
7543
repo: parts[1],
7644
};
7745
}
7846
} catch {
79-
// Not a valid URL, fall through
47+
// Not a valid URL
8048
}
81-
82-
// If none of the above matched, return null
8349
return null;
8450
}
8551

tests/workflow.test.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,6 @@ describe("parseRepositoryURL", () => {
230230
expect(parseRepositoryURL("owner")).toBeNull();
231231
expect(parseRepositoryURL("")).toBeNull();
232232
expect(parseRepositoryURL("/owner/")).toBeNull();
233-
expect(parseRepositoryURL("owner/repo/extra")).toBeNull();
234233
});
235234
test("invalid URLs", () => {
236235
expect(parseRepositoryURL("https://github.com/")).toBeNull();

0 commit comments

Comments
 (0)