Skip to content

Commit 83029dd

Browse files
wan54wan
and
wan
authored
fix: handle repo name with multiple segments (#219)
Co-authored-by: wan <[email protected]>
1 parent c84b76d commit 83029dd

File tree

2 files changed

+125
-4
lines changed

2 files changed

+125
-4
lines changed

src/repo.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export function getRepoConfig(repoUrl = ""): RepoConfig {
9090
let repo;
9191
let domain;
9292

93-
let url;
93+
let url: URL;
9494
try {
9595
url = new URL(repoUrl);
9696
} catch {
@@ -108,9 +108,9 @@ export function getRepoConfig(repoUrl = ""): RepoConfig {
108108
provider in providerToDomain ? providerToDomain[provider] : provider;
109109
} else if (url) {
110110
domain = url.hostname;
111-
repo = url.pathname
112-
.split("/")
113-
.slice(1, 3)
111+
const paths = url.pathname.split("/");
112+
repo = paths
113+
.slice(1, paths.length)
114114
.join("/")
115115
.replace(/\.git$/, "");
116116
provider = domainToProvider[domain];

test/repo.test.ts

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import { describe, expect, test } from "vitest";
2+
import { getRepoConfig } from "../src";
3+
4+
describe("repo", () => {
5+
describe("getRepoConfig", () => {
6+
describe("when `m.repo` and `m.provider` are defined", () => {
7+
test.each([
8+
{
9+
input: "github:donaldsh/test",
10+
output: {
11+
domain: "github.com",
12+
provider: "github",
13+
repo: "donaldsh/test",
14+
},
15+
},
16+
{
17+
input: "gitlab:donaldsh/test.git",
18+
output: {
19+
domain: "gitlab.com",
20+
provider: "gitlab",
21+
repo: "donaldsh/test",
22+
},
23+
},
24+
{
25+
input: "gitlab:donaldsh/test.git",
26+
output: {
27+
domain: "gitlab.com",
28+
provider: "gitlab",
29+
repo: "donaldsh/test",
30+
},
31+
},
32+
{
33+
input: "[email protected]:donaldsh/test.git",
34+
output: {
35+
domain: "bitbucket.org",
36+
provider: "bitbucket",
37+
repo: "donaldsh/test",
38+
},
39+
},
40+
{
41+
input: "a@x:b/c",
42+
output: {
43+
domain: "x",
44+
provider: "x",
45+
repo: "b/c",
46+
},
47+
},
48+
])("url=$input should return RepoConfig", ({ input, output }) => {
49+
expect(getRepoConfig(input)).toEqual(output);
50+
});
51+
});
52+
53+
describe("when `url` is defined", () => {
54+
test.each([
55+
{
56+
input: "https://github.com/unjs/changelogen.git",
57+
output: {
58+
domain: "github.com",
59+
provider: "github",
60+
repo: "unjs/changelogen",
61+
},
62+
},
63+
{
64+
input: "https://github.com/unjs/changelogen",
65+
output: {
66+
domain: "github.com",
67+
provider: "github",
68+
repo: "unjs/changelogen",
69+
},
70+
},
71+
{
72+
input: "https://github.com/myproject.git",
73+
output: {
74+
domain: "github.com",
75+
provider: "github",
76+
repo: "myproject",
77+
},
78+
},
79+
{
80+
input: "https://github.com/account/project/sub1/sub2/myproject.git",
81+
output: {
82+
domain: "github.com",
83+
provider: "github",
84+
repo: "account/project/sub1/sub2/myproject",
85+
},
86+
},
87+
])("url=$input should return RepoConfig", ({ input, output }) => {
88+
expect(getRepoConfig(input)).toEqual(output);
89+
});
90+
});
91+
92+
describe("when only `m.repo` is defined", () => {
93+
test.each([
94+
{
95+
input: "donaldsh/test.git",
96+
output: {
97+
domain: "github.com",
98+
provider: "github",
99+
repo: "donaldsh/test",
100+
},
101+
},
102+
{
103+
input: "unjs/changelogen",
104+
output: {
105+
domain: "github.com",
106+
provider: "github",
107+
repo: "unjs/changelogen",
108+
},
109+
},
110+
])("url=$input should return RepoConfig", ({ input, output }) => {
111+
expect(getRepoConfig(input)).toEqual(output);
112+
});
113+
});
114+
115+
describe("when `repoUrl` is empty", () => {
116+
test("should return empty RepoConfig", () => {
117+
expect(getRepoConfig()).toEqual({});
118+
});
119+
});
120+
});
121+
});

0 commit comments

Comments
 (0)