Skip to content

Commit 88108cc

Browse files
committed
fix(shared): match source-control providers by DNS label
1 parent 6676f9c commit 88108cc

2 files changed

Lines changed: 36 additions & 3 deletions

File tree

‎packages/shared/src/sourceControl.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,33 @@ describe("detectSourceControlProviderFromRemoteUrl", () => {
7575
baseUrl: "https://self-hosted.example.test:8443",
7676
});
7777
});
78+
79+
it("matches self-hosted providers by complete DNS labels", () => {
80+
expect(
81+
detectSourceControlProviderFromRemoteUrl("https://github.example.com/owner/repo.git")?.kind,
82+
).toBe("github");
83+
expect(
84+
detectSourceControlProviderFromRemoteUrl("https://gitlab.example.com/group/repo.git")?.kind,
85+
).toBe("gitlab");
86+
expect(
87+
detectSourceControlProviderFromRemoteUrl("https://bitbucket.example.com/workspace/repo.git")
88+
?.kind,
89+
).toBe("bitbucket");
90+
});
91+
92+
it("does not match provider names embedded in unrelated DNS labels", () => {
93+
expect(
94+
detectSourceControlProviderFromRemoteUrl("https://notgithub.example.com/owner/repo.git")
95+
?.kind,
96+
).toBe("unknown");
97+
expect(
98+
detectSourceControlProviderFromRemoteUrl("https://notgitlab.example.com/group/repo.git")
99+
?.kind,
100+
).toBe("unknown");
101+
expect(
102+
detectSourceControlProviderFromRemoteUrl(
103+
"https://notbitbucket.example.com/workspace/repo.git",
104+
)?.kind,
105+
).toBe("unknown");
106+
});
78107
});

‎packages/shared/src/sourceControl.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,20 +167,24 @@ function toBaseUrl(host: string): string {
167167
return `https://${host}`;
168168
}
169169

170+
function hasDnsLabel(host: string, label: string): boolean {
171+
return host.split(".").includes(label);
172+
}
173+
170174
function isGitHubHost(host: string): boolean {
171-
return host === "github.com" || host.includes("github");
175+
return host === "github.com" || hasDnsLabel(host, "github");
172176
}
173177

174178
function isGitLabHost(host: string): boolean {
175-
return host === "gitlab.com" || host.includes("gitlab");
179+
return host === "gitlab.com" || hasDnsLabel(host, "gitlab");
176180
}
177181

178182
function isAzureDevOpsHost(host: string): boolean {
179183
return host === "dev.azure.com" || host.endsWith(".visualstudio.com");
180184
}
181185

182186
function isBitbucketHost(host: string): boolean {
183-
return host === "bitbucket.org" || host.includes("bitbucket");
187+
return host === "bitbucket.org" || hasDnsLabel(host, "bitbucket");
184188
}
185189

186190
export function detectSourceControlProviderFromRemoteUrl(

0 commit comments

Comments
 (0)