Skip to content

Commit 31eff81

Browse files
committed
fix(server,clients): GitLab clones follow glab's configured git protocol
The GitLab repository lookup now reads `glab config get git_protocol --host <host>` and reports it as preferredCloneProtocol. Clients clone over that transport, and the server's "auto" protocol (used by agents via t3_project_clone) honors it. Without a readable setting, the existing per-provider defaults apply. Refs #13350
1 parent 611132c commit 31eff81

7 files changed

Lines changed: 178 additions & 23 deletions

File tree

‎apps/server/src/sourceControl/GitLabCli.test.ts‎

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -156,18 +156,53 @@ layer("GitLabCli.layer", (it) => {
156156
}),
157157
);
158158

159-
it.effect("reads repository clone URLs", () =>
159+
const projectJson = JSON.stringify({
160+
path_with_namespace: "octocat/t3code",
161+
web_url: "https://gitlab.com/octocat/t3code",
162+
http_url_to_repo: "https://gitlab.com/octocat/t3code.git",
163+
ssh_url_to_repo: "[email protected]:octocat/t3code.git",
164+
});
165+
166+
it.effect("reads repository clone URLs with the host's configured git protocol", () =>
160167
Effect.gen(function* () {
161-
mockedRun.mockReturnValueOnce(
162-
Effect.succeed(
163-
processOutput(
164-
JSON.stringify({
165-
path_with_namespace: "octocat/t3code",
166-
web_url: "https://gitlab.com/octocat/t3code",
167-
http_url_to_repo: "https://gitlab.com/octocat/t3code.git",
168-
ssh_url_to_repo: "[email protected]:octocat/t3code.git",
169-
}),
170-
),
168+
mockedRun
169+
.mockReturnValueOnce(Effect.succeed(processOutput(projectJson)))
170+
.mockReturnValueOnce(Effect.succeed(processOutput("https\n")));
171+
172+
const result = yield* Effect.gen(function* () {
173+
const glab = yield* GitLabCli.GitLabCli;
174+
return yield* glab.getRepositoryCloneUrls({
175+
cwd: "/repo",
176+
repository: "octocat/t3code",
177+
});
178+
});
179+
180+
assert.deepStrictEqual(result, {
181+
nameWithOwner: "octocat/t3code",
182+
url: "https://gitlab.com/octocat/t3code",
183+
sshUrl: "[email protected]:octocat/t3code.git",
184+
preferredCloneProtocol: "https",
185+
});
186+
expect(mockedRun).toHaveBeenLastCalledWith(
187+
expect.objectContaining({
188+
command: "glab",
189+
args: ["config", "get", "git_protocol", "--host", "gitlab.com"],
190+
}),
191+
);
192+
}),
193+
);
194+
195+
it.effect("leaves the clone protocol unset when glab config cannot be read", () =>
196+
Effect.gen(function* () {
197+
mockedRun.mockReturnValueOnce(Effect.succeed(processOutput(projectJson))).mockReturnValueOnce(
198+
Effect.fail(
199+
new VcsProcessExitError({
200+
operation: "GitLabCli.execute",
201+
command: "glab",
202+
cwd: "/repo",
203+
exitCode: 1,
204+
detail: "unknown command",
205+
}),
171206
),
172207
);
173208

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type * as DateTime from "effect/DateTime";
99

1010
import {
1111
TrimmedNonEmptyString,
12+
type SourceControlPreferredCloneProtocol,
1213
type SourceControlRepositoryVisibility,
1314
type VcsError,
1415
} from "@t3tools/contracts";
@@ -258,6 +259,7 @@ export interface GitLabRepositoryCloneUrls {
258259
readonly nameWithOwner: string;
259260
readonly url: string;
260261
readonly sshUrl: string;
262+
readonly preferredCloneProtocol?: SourceControlPreferredCloneProtocol;
261263
}
262264

263265
export class GitLabCli extends Context.Service<
@@ -351,6 +353,27 @@ function normalizeRepositoryCloneUrls(
351353
};
352354
}
353355

356+
/** Maps `glab config get git_protocol` output; glab accepts `ssh`, `https`, and `http`. */
357+
function parseGitProtocol(stdout: string): SourceControlPreferredCloneProtocol | undefined {
358+
switch (stdout.trim()) {
359+
case "ssh":
360+
return "ssh";
361+
case "https":
362+
case "http":
363+
return "https";
364+
default:
365+
return undefined;
366+
}
367+
}
368+
369+
function hostOf(url: string): string | null {
370+
try {
371+
return new URL(url).host || null;
372+
} catch {
373+
return null;
374+
}
375+
}
376+
354377
function stateArgs(state: "open" | "closed" | "merged" | "all"): ReadonlyArray<string> {
355378
switch (state) {
356379
case "open":
@@ -453,6 +476,19 @@ export const make = Effect.gen(function* () {
453476
),
454477
);
455478

479+
// `--host` falls back to the global setting, matching the protocol glab itself
480+
// would clone with. A missing or unreadable setting leaves the caller's default.
481+
const readGitProtocol = (input: { readonly cwd: string; readonly url: string }) => {
482+
const host = hostOf(input.url);
483+
return execute({
484+
cwd: input.cwd,
485+
args: ["config", "get", "git_protocol", ...(host === null ? [] : ["--host", host])],
486+
}).pipe(
487+
Effect.map((result) => parseGitProtocol(result.stdout)),
488+
Effect.orElseSucceed(() => undefined),
489+
);
490+
};
491+
456492
return GitLabCli.of({
457493
execute,
458494
listMergeRequests: (input) =>
@@ -540,6 +576,13 @@ export const make = Effect.gen(function* () {
540576
),
541577
),
542578
Effect.map(normalizeRepositoryCloneUrls),
579+
Effect.flatMap((urls) =>
580+
readGitProtocol({ cwd: input.cwd, url: urls.url }).pipe(
581+
Effect.map((preferredCloneProtocol) =>
582+
preferredCloneProtocol === undefined ? urls : { ...urls, preferredCloneProtocol },
583+
),
584+
),
585+
),
543586
),
544587
createRepository: (input) => {
545588
const { namespacePath, projectPath } = parseRepositoryPath(input.repository);

‎apps/server/src/sourceControl/SourceControlRepositoryService.test.ts‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,48 @@ it.effect("keeps arbitrary Bitbucket request failures out of the repository mess
263263
}),
264264
);
265265

266+
it.effect("clones over the provider's preferred protocol when none is requested", () =>
267+
Effect.gen(function* () {
268+
const fs = yield* FileSystem.FileSystem;
269+
const path = yield* Path.Path;
270+
const parent = yield* fs.makeTempDirectoryScoped({
271+
prefix: "t3-source-control-clone-parent-",
272+
});
273+
const destinationPath = path.join(parent, "t3code");
274+
const cloneUrls = { ...CLONE_URLS, preferredCloneProtocol: "https" as const };
275+
const clonedUrls: Array<string | undefined> = [];
276+
277+
yield* Effect.gen(function* () {
278+
const service = yield* SourceControlRepositoryService.SourceControlRepositoryService;
279+
const result = yield* service.cloneRepository({
280+
provider: "gitlab",
281+
repository: "octocat/t3code",
282+
destinationPath,
283+
});
284+
285+
assert.strictEqual(result.remoteUrl, CLONE_URLS.url);
286+
assert.deepStrictEqual(result.repository, { provider: "gitlab", ...cloneUrls });
287+
assert.deepStrictEqual(clonedUrls, [CLONE_URLS.url]);
288+
}).pipe(
289+
Effect.provide(
290+
layer({
291+
provider: makeProvider({
292+
kind: "gitlab",
293+
getRepositoryCloneUrls: () => Effect.succeed(cloneUrls),
294+
}),
295+
git: {
296+
execute: (input) =>
297+
Effect.sync(() => {
298+
clonedUrls.push(input.args.at(-2));
299+
return processOutput();
300+
}),
301+
},
302+
}),
303+
),
304+
);
305+
}).pipe(Effect.provide(NodeServices.layer)),
306+
);
307+
266308
it.effect("clones a looked-up repository into the requested destination", () =>
267309
Effect.gen(function* () {
268310
const fs = yield* FileSystem.FileSystem;

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ function toRepositoryInfo(
116116
nameWithOwner: urls.nameWithOwner,
117117
url: urls.url,
118118
sshUrl: urls.sshUrl,
119+
...(urls.preferredCloneProtocol === undefined
120+
? {}
121+
: { preferredCloneProtocol: urls.preferredCloneProtocol }),
119122
};
120123
}
121124

@@ -153,13 +156,10 @@ function selectRemoteUrl(
153156
urls: SourceControlRepositoryCloneUrls,
154157
protocol: SourceControlCloneProtocol | undefined,
155158
): string {
156-
switch (protocol ?? "auto") {
157-
case "https":
158-
return urls.url;
159-
case "ssh":
160-
case "auto":
161-
return urls.sshUrl;
162-
}
159+
// "auto" follows the provider CLI's configured protocol, falling back to SSH.
160+
const resolved =
161+
protocol === undefined || protocol === "auto" ? urls.preferredCloneProtocol : protocol;
162+
return resolved === "https" ? urls.url : urls.sshUrl;
163163
}
164164

165165
/** @public Service construction is part of the canonical Effect module API. */

‎packages/client-runtime/src/operations/projects.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,25 @@ describe("add project shared logic", () => {
9595
).toBe("[email protected]:group/project.git");
9696
});
9797

98+
it("follows the protocol the provider CLI is configured for", () => {
99+
expect(
100+
getDefaultCloneUrl({
101+
provider: "gitlab",
102+
url: "https://gitlab.com/group/project",
103+
sshUrl: "[email protected]:group/project.git",
104+
preferredCloneProtocol: "https",
105+
}),
106+
).toBe("https://gitlab.com/group/project");
107+
expect(
108+
getDefaultCloneUrl({
109+
provider: "gitlab",
110+
url: "https://gitlab.com/group/project",
111+
sshUrl: "[email protected]:group/project.git",
112+
preferredCloneProtocol: "ssh",
113+
}),
114+
).toBe("[email protected]:group/project.git");
115+
});
116+
98117
it("derives the clone folder name from any pasted clone URL", () => {
99118
expect(getCloneDirectoryName("https://github.com/owner/repo.git")).toBe("repo");
100119
expect(getCloneDirectoryName("https://github.com/owner/repo")).toBe("repo");

‎packages/client-runtime/src/operations/projects.ts‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,20 @@ export function normalizePastedCloneUrl(input: string): string {
137137
return `https://github.com/${repository}`;
138138
}
139139

140-
/** GitHub and Forgejo default to HTTPS; other providers retain their existing SSH default. */
140+
/**
141+
* Clones over the transport the provider's CLI is configured for when the server reports one.
142+
* Otherwise GitHub and Forgejo default to HTTPS and other providers keep their SSH default.
143+
*/
141144
export function getDefaultCloneUrl(
142-
repository: Pick<SourceControlRepositoryInfo, "provider" | "url" | "sshUrl">,
145+
repository: Pick<
146+
SourceControlRepositoryInfo,
147+
"provider" | "url" | "sshUrl" | "preferredCloneProtocol"
148+
>,
143149
): string {
144-
return repository.provider === "github" || repository.provider === "forgejo"
145-
? repository.url
146-
: repository.sshUrl;
150+
const protocol =
151+
repository.preferredCloneProtocol ??
152+
(repository.provider === "github" || repository.provider === "forgejo" ? "https" : "ssh");
153+
return protocol === "https" ? repository.url : repository.sshUrl;
147154
}
148155

149156
export function sortAddProjectProviderSources(

‎packages/contracts/src/sourceControl.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,18 @@ export const ChangeRequest = Schema.Struct({
4141
});
4242
export type ChangeRequest = typeof ChangeRequest.Type;
4343

44+
/**
45+
* The transport the provider's CLI is configured to use for this host, such as
46+
* `glab config get git_protocol`. Absent when the provider has no such setting.
47+
*/
48+
export const SourceControlPreferredCloneProtocol = Schema.Literals(["ssh", "https"]);
49+
export type SourceControlPreferredCloneProtocol = typeof SourceControlPreferredCloneProtocol.Type;
50+
4451
export const SourceControlRepositoryCloneUrls = Schema.Struct({
4552
nameWithOwner: TrimmedNonEmptyString,
4653
url: TrimmedNonEmptyString,
4754
sshUrl: TrimmedNonEmptyString,
55+
preferredCloneProtocol: Schema.optional(SourceControlPreferredCloneProtocol),
4856
});
4957
export type SourceControlRepositoryCloneUrls = typeof SourceControlRepositoryCloneUrls.Type;
5058

@@ -59,6 +67,7 @@ export const SourceControlRepositoryInfo = Schema.Struct({
5967
nameWithOwner: TrimmedNonEmptyString,
6068
url: TrimmedNonEmptyString,
6169
sshUrl: TrimmedNonEmptyString,
70+
preferredCloneProtocol: Schema.optional(SourceControlPreferredCloneProtocol),
6271
});
6372
export type SourceControlRepositoryInfo = typeof SourceControlRepositoryInfo.Type;
6473

0 commit comments

Comments
 (0)