Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle repo name with multiple segments #219

Merged
merged 1 commit into from
Sep 19, 2024
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
8 changes: 4 additions & 4 deletions src/repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export function getRepoConfig(repoUrl = ""): RepoConfig {
let repo;
let domain;

let url;
let url: URL;
try {
url = new URL(repoUrl);
} catch {
Expand All @@ -104,9 +104,9 @@ export function getRepoConfig(repoUrl = ""): RepoConfig {
provider in providerToDomain ? providerToDomain[provider] : provider;
} else if (url) {
domain = url.hostname;
repo = url.pathname
.split("/")
.slice(1, 3)
const paths = url.pathname.split("/");
repo = paths
.slice(1, paths.length)
.join("/")
.replace(/\.git$/, "");
provider = domainToProvider[domain];
Expand Down
121 changes: 121 additions & 0 deletions test/repo.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import { describe, expect, test } from "vitest";
import { getRepoConfig } from "../src";

describe("repo", () => {
describe("getRepoConfig", () => {
describe("when `m.repo` and `m.provider` are defined", () => {
test.each([
{
input: "github:donaldsh/test",
output: {
domain: "github.com",
provider: "github",
repo: "donaldsh/test",
},
},
{
input: "gitlab:donaldsh/test.git",
output: {
domain: "gitlab.com",
provider: "gitlab",
repo: "donaldsh/test",
},
},
{
input: "gitlab:donaldsh/test.git",
output: {
domain: "gitlab.com",
provider: "gitlab",
repo: "donaldsh/test",
},
},
{
input: "[email protected]:donaldsh/test.git",
output: {
domain: "bitbucket.org",
provider: "bitbucket",
repo: "donaldsh/test",
},
},
{
input: "a@x:b/c",
output: {
domain: "x",
provider: "x",
repo: "b/c",
},
},
])("url=$input should return RepoConfig", ({ input, output }) => {
expect(getRepoConfig(input)).toEqual(output);
});
});

describe("when `url` is defined", () => {
test.each([
{
input: "https://github.com/unjs/changelogen.git",
output: {
domain: "github.com",
provider: "github",
repo: "unjs/changelogen",
},
},
{
input: "https://github.com/unjs/changelogen",
output: {
domain: "github.com",
provider: "github",
repo: "unjs/changelogen",
},
},
{
input: "https://github.com/myproject.git",
output: {
domain: "github.com",
provider: "github",
repo: "myproject",
},
},
{
input: "https://github.com/account/project/sub1/sub2/myproject.git",
output: {
domain: "github.com",
provider: "github",
repo: "account/project/sub1/sub2/myproject",
},
},
])("url=$input should return RepoConfig", ({ input, output }) => {
expect(getRepoConfig(input)).toEqual(output);
});
});

describe("when only `m.repo` is defined", () => {
test.each([
{
input: "donaldsh/test.git",
output: {
domain: "github.com",
provider: "github",
repo: "donaldsh/test",
},
},
{
input: "unjs/changelogen",
output: {
domain: "github.com",
provider: "github",
repo: "unjs/changelogen",
},
},
])("url=$input should return RepoConfig", ({ input, output }) => {
expect(getRepoConfig(input)).toEqual(output);
});
});

describe("when `repoUrl` is empty", () => {
test("should return empty RepoConfig", () => {
expect(getRepoConfig()).toEqual({});
});
});
});
});