Skip to content

Commit 7ce4194

Browse files
authored
fix: detect SSH remotes with non-git user prefixes (e.g. gitlab@) (pingdotgg#3649)
1 parent dc0ff8f commit 7ce4194

6 files changed

Lines changed: 72 additions & 16 deletions

File tree

‎apps/server/src/git/GitManager.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import {
4242
} from "@t3tools/shared/git";
4343
import {
4444
getChangeRequestTerminologyForKind,
45+
isSshRemoteUrl,
4546
type ChangeRequestTerminology,
4647
} from "@t3tools/shared/sourceControl";
4748

@@ -574,8 +575,7 @@ function toResolvedPullRequest(pr: {
574575

575576
function shouldPreferSshRemote(url: string | null): boolean {
576577
if (!url) return false;
577-
const trimmed = url.trim();
578-
return trimmed.startsWith("git@") || trimmed.startsWith("ssh://");
578+
return isSshRemoteUrl(url);
579579
}
580580

581581
function toPullRequestHeadRemoteInfo(pr: {

‎apps/server/src/sourceControl/BitbucketApi.ts‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ import {
1414
} from "@t3tools/contracts";
1515
import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/unstable/http";
1616
import { sanitizeBranchFragment } from "@t3tools/shared/git";
17-
import { detectSourceControlProviderFromRemoteUrl } from "@t3tools/shared/sourceControl";
17+
import {
18+
detectSourceControlProviderFromRemoteUrl,
19+
isSshRemoteUrl,
20+
} from "@t3tools/shared/sourceControl";
1821

1922
import {
2023
BitbucketPullRequestListSchema,
@@ -448,9 +451,9 @@ function requireRepositoryLocator(
448451

449452
function parseBitbucketRemoteUrl(remoteUrl: string): BitbucketRepositoryLocator | null {
450453
const trimmed = remoteUrl.trim();
451-
if (trimmed.startsWith("git@")) {
452-
const pathStart = trimmed.indexOf(":");
453-
return pathStart < 0 ? null : parseBitbucketRepositorySlug(trimmed.slice(pathStart + 1));
454+
const scpMatch = /^[a-zA-Z0-9._-]+@[^:/]+:(.+)$/.exec(trimmed);
455+
if (scpMatch?.[1]) {
456+
return parseBitbucketRepositorySlug(scpMatch[1]);
454457
}
455458

456459
try {
@@ -494,8 +497,8 @@ function defaultChangeRequestTargetBranch(input: {
494497
}
495498

496499
function shouldPreferSshRemote(originRemoteUrl: string | null): boolean {
497-
const trimmed = originRemoteUrl?.trim() ?? "";
498-
return trimmed.startsWith("git@") || trimmed.startsWith("ssh://");
500+
if (!originRemoteUrl) return false;
501+
return isSshRemoteUrl(originRemoteUrl);
499502
}
500503

501504
function selectCloneUrl(input: {

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ describe("normalizeGitRemoteUrl", () => {
4040
"gitlab.company.com/team/project",
4141
);
4242
});
43+
44+
it("normalizes SCP-like remotes with non-git SSH users", () => {
45+
expect(normalizeGitRemoteUrl("[email protected]:group/project.git")).toBe(
46+
"gitlab.example.com/group/project",
47+
);
48+
expect(normalizeGitRemoteUrl("[email protected]:workspace/repo.git")).toBe(
49+
"bitbucket.org/workspace/repo",
50+
);
51+
});
4352
});
4453

4554
describe("parseGitHubRepositoryNameWithOwnerFromRemoteUrl", () => {

‎packages/shared/src/git.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,9 @@ export function normalizeGitRemoteUrl(value: string): string {
133133
}
134134
}
135135

136-
const scpStyleHostAndPath = /^git@([^:/\s]+)[:/]([^/\s]+(?:\/[^/\s]+)+)$/i.exec(normalized);
136+
const scpStyleHostAndPath = /^[a-zA-Z0-9._-]+@([^:/\s]+):([^/\s]+(?:\/[^/\s]+)+)$/i.exec(
137+
normalized,
138+
);
137139
if (scpStyleHostAndPath?.[1] && scpStyleHostAndPath[2]) {
138140
return `${scpStyleHostAndPath[1]}/${scpStyleHostAndPath[2]}`;
139141
}

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { describe, expect, it } from "vite-plus/test";
33
import {
44
detectSourceControlProviderFromRemoteUrl,
55
getChangeRequestTerminologyForKind,
6+
isSshRemoteUrl,
67
resolveChangeRequestPresentation,
78
} from "./sourceControl.ts";
89

@@ -120,4 +121,42 @@ describe("detectSourceControlProviderFromRemoteUrl", () => {
120121
)?.kind,
121122
).toBe("unknown");
122123
});
124+
125+
it("detects SSH remotes with non-git SSH users (e.g. gitlab@, deploy@)", () => {
126+
expect(
127+
detectSourceControlProviderFromRemoteUrl("[email protected]:group/project.git")?.kind,
128+
).toBe("gitlab");
129+
expect(
130+
detectSourceControlProviderFromRemoteUrl("[email protected]:group/project.git")
131+
?.baseUrl,
132+
).toBe("https://gitlab.example.com");
133+
expect(detectSourceControlProviderFromRemoteUrl("[email protected]:owner/repo.git")?.kind).toBe(
134+
"github",
135+
);
136+
expect(
137+
detectSourceControlProviderFromRemoteUrl("[email protected]:workspace/repo.git")?.kind,
138+
).toBe("bitbucket");
139+
});
140+
});
141+
142+
describe("isSshRemoteUrl", () => {
143+
it("recognises SCP-like SSH URLs with any SSH user prefix", () => {
144+
expect(isSshRemoteUrl("[email protected]:owner/repo.git")).toBe(true);
145+
expect(isSshRemoteUrl("[email protected]:group/project.git")).toBe(true);
146+
expect(isSshRemoteUrl("[email protected]:workspace/repo.git")).toBe(true);
147+
});
148+
149+
it("recognises ssh:// URLs with any case", () => {
150+
expect(isSshRemoteUrl("ssh://[email protected]/group/project.git")).toBe(true);
151+
expect(isSshRemoteUrl("ssh://[email protected]:22/group/project.git")).toBe(true);
152+
expect(isSshRemoteUrl("SSH://[email protected]/group/project.git")).toBe(true);
153+
expect(isSshRemoteUrl("SsH://[email protected]/group/project.git")).toBe(true);
154+
});
155+
156+
it("returns false for HTTPS, local paths, and SCP-like paths without a colon", () => {
157+
expect(isSshRemoteUrl("https://gitlab.example.com/group/project.git")).toBe(false);
158+
expect(isSshRemoteUrl("/home/user/repos/project")).toBe(false);
159+
expect(isSshRemoteUrl("")).toBe(false);
160+
expect(isSshRemoteUrl("[email protected]/project/repo")).toBe(false);
161+
});
123162
});

‎packages/shared/src/sourceControl.ts‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,19 +133,22 @@ export function getChangeRequestTerminologyForKind(
133133
};
134134
}
135135

136+
const SCP_SSH_REMOTE_PATTERN = /^[a-zA-Z0-9._-]+@([^:/]+):/;
137+
138+
export function isSshRemoteUrl(remoteUrl: string): boolean {
139+
const trimmed = remoteUrl.trim();
140+
return SCP_SSH_REMOTE_PATTERN.test(trimmed) || trimmed.toLowerCase().startsWith("ssh://");
141+
}
142+
136143
function parseRemoteHost(remoteUrl: string): string | null {
137144
const trimmed = remoteUrl.trim();
138145
if (trimmed.length === 0) {
139146
return null;
140147
}
141148

142-
if (trimmed.startsWith("git@")) {
143-
const hostWithPath = trimmed.slice("git@".length);
144-
const separatorIndex = hostWithPath.search(/[:/]/);
145-
if (separatorIndex <= 0) {
146-
return null;
147-
}
148-
return hostWithPath.slice(0, separatorIndex).toLowerCase();
149+
const scpMatch = SCP_SSH_REMOTE_PATTERN.exec(trimmed);
150+
if (scpMatch?.[1]) {
151+
return scpMatch[1].toLowerCase();
149152
}
150153

151154
try {

0 commit comments

Comments
 (0)