fix: detect SSH remotes with non-git user prefixes (e.g. gitlab@) (#3649)

This commit is contained in:
JackatDJL authored and GitHub committed 2026-08-15 12:26:45 +00:00
1 parent dc0ff8f13e
commit 7ce4194707
6 files changed
+72 -16

No files matched your search

+2 -2
View File
@@ -42,6 +42,7 @@ import {
} from "@t3tools/shared/git";
import {
getChangeRequestTerminologyForKind,
isSshRemoteUrl,
type ChangeRequestTerminology,
} from "@t3tools/shared/sourceControl";
@@ -574,8 +575,7 @@ function toResolvedPullRequest(pr: {
function shouldPreferSshRemote(url: string | null): boolean {
if (!url) return false;
const trimmed = url.trim();
return trimmed.startsWith("git@") || trimmed.startsWith("ssh://");
return isSshRemoteUrl(url);
}
function toPullRequestHeadRemoteInfo(pr: {
@@ -14,7 +14,10 @@ import {
} from "@t3tools/contracts";
import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/unstable/http";
import { sanitizeBranchFragment } from "@t3tools/shared/git";
import { detectSourceControlProviderFromRemoteUrl } from "@t3tools/shared/sourceControl";
import {
detectSourceControlProviderFromRemoteUrl,
isSshRemoteUrl,
} from "@t3tools/shared/sourceControl";
import {
BitbucketPullRequestListSchema,
@@ -448,9 +451,9 @@ function requireRepositoryLocator(
function parseBitbucketRemoteUrl(remoteUrl: string): BitbucketRepositoryLocator | null {
const trimmed = remoteUrl.trim();
if (trimmed.startsWith("git@")) {
const pathStart = trimmed.indexOf(":");
return pathStart < 0 ? null : parseBitbucketRepositorySlug(trimmed.slice(pathStart + 1));
const scpMatch = /^[a-zA-Z0-9._-]+@[^:/]+:(.+)$/.exec(trimmed);
if (scpMatch?.[1]) {
return parseBitbucketRepositorySlug(scpMatch[1]);
}
try {
@@ -494,8 +497,8 @@ function defaultChangeRequestTargetBranch(input: {
}
function shouldPreferSshRemote(originRemoteUrl: string | null): boolean {
const trimmed = originRemoteUrl?.trim() ?? "";
return trimmed.startsWith("git@") || trimmed.startsWith("ssh://");
if (!originRemoteUrl) return false;
return isSshRemoteUrl(originRemoteUrl);
}
function selectCloneUrl(input: {
+9
View File
@@ -40,6 +40,15 @@ describe("normalizeGitRemoteUrl", () => {
"gitlab.company.com/team/project",
);
});
it("normalizes SCP-like remotes with non-git SSH users", () => {
expect(normalizeGitRemoteUrl("[email protected]:group/project.git")).toBe(
"gitlab.example.com/group/project",
);
expect(normalizeGitRemoteUrl("[email protected]:workspace/repo.git")).toBe(
"bitbucket.org/workspace/repo",
);
});
});
describe("parseGitHubRepositoryNameWithOwnerFromRemoteUrl", () => {
+3 -1
View File
@@ -133,7 +133,9 @@ export function normalizeGitRemoteUrl(value: string): string {
}
}
const scpStyleHostAndPath = /^git@([^:/\s]+)[:/]([^/\s]+(?:\/[^/\s]+)+)$/i.exec(normalized);
const scpStyleHostAndPath = /^[a-zA-Z0-9._-]+@([^:/\s]+):([^/\s]+(?:\/[^/\s]+)+)$/i.exec(
normalized,
);
if (scpStyleHostAndPath?.[1] && scpStyleHostAndPath[2]) {
return `${scpStyleHostAndPath[1]}/${scpStyleHostAndPath[2]}`;
}
+39
View File
@@ -3,6 +3,7 @@ import { describe, expect, it } from "vite-plus/test";
import {
detectSourceControlProviderFromRemoteUrl,
getChangeRequestTerminologyForKind,
isSshRemoteUrl,
resolveChangeRequestPresentation,
} from "./sourceControl.ts";
@@ -120,4 +121,42 @@ describe("detectSourceControlProviderFromRemoteUrl", () => {
)?.kind,
).toBe("unknown");
});
it("detects SSH remotes with non-git SSH users (e.g. gitlab@, deploy@)", () => {
expect(
detectSourceControlProviderFromRemoteUrl("[email protected]:group/project.git")?.kind,
).toBe("gitlab");
expect(
detectSourceControlProviderFromRemoteUrl("[email protected]:group/project.git")
?.baseUrl,
).toBe("https://gitlab.example.com");
expect(detectSourceControlProviderFromRemoteUrl("[email protected]:owner/repo.git")?.kind).toBe(
"github",
);
expect(
detectSourceControlProviderFromRemoteUrl("[email protected]:workspace/repo.git")?.kind,
).toBe("bitbucket");
});
});
describe("isSshRemoteUrl", () => {
it("recognises SCP-like SSH URLs with any SSH user prefix", () => {
expect(isSshRemoteUrl("[email protected]:owner/repo.git")).toBe(true);
expect(isSshRemoteUrl("[email protected]:group/project.git")).toBe(true);
expect(isSshRemoteUrl("[email protected]:workspace/repo.git")).toBe(true);
});
it("recognises ssh:// URLs with any case", () => {
expect(isSshRemoteUrl("ssh://[email protected]/group/project.git")).toBe(true);
expect(isSshRemoteUrl("ssh://[email protected]:22/group/project.git")).toBe(true);
expect(isSshRemoteUrl("SSH://[email protected]/group/project.git")).toBe(true);
expect(isSshRemoteUrl("SsH://[email protected]/group/project.git")).toBe(true);
});
it("returns false for HTTPS, local paths, and SCP-like paths without a colon", () => {
expect(isSshRemoteUrl("https://gitlab.example.com/group/project.git")).toBe(false);
expect(isSshRemoteUrl("/home/user/repos/project")).toBe(false);
expect(isSshRemoteUrl("")).toBe(false);
expect(isSshRemoteUrl("[email protected]/project/repo")).toBe(false);
});
});
+10 -7
View File
@@ -133,19 +133,22 @@ export function getChangeRequestTerminologyForKind(
};
}
const SCP_SSH_REMOTE_PATTERN = /^[a-zA-Z0-9._-]+@([^:/]+):/;
export function isSshRemoteUrl(remoteUrl: string): boolean {
const trimmed = remoteUrl.trim();
return SCP_SSH_REMOTE_PATTERN.test(trimmed) || trimmed.toLowerCase().startsWith("ssh://");
}
function parseRemoteHost(remoteUrl: string): string | null {
const trimmed = remoteUrl.trim();
if (trimmed.length === 0) {
return null;
}
if (trimmed.startsWith("git@")) {
const hostWithPath = trimmed.slice("git@".length);
const separatorIndex = hostWithPath.search(/[:/]/);
if (separatorIndex <= 0) {
return null;
}
return hostWithPath.slice(0, separatorIndex).toLowerCase();
const scpMatch = SCP_SSH_REMOTE_PATTERN.exec(trimmed);
if (scpMatch?.[1]) {
return scpMatch[1].toLowerCase();
}
try {