@devries-cameron20 / t3code Mirror · read-only / commits / 2dfc4a9c98
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 13:30 UTC

fix(azure-devops): derive CLI scope from checkout remotes (#21838)

Co-authored-by: Ved Pandey <vedprakash2302@gmail.com>

Julius Marminge committed Nov 14, 2026 at 14:55 UTC 2dfc4a9c983ef732083bc3d2580a75cf7a677db0
5 files changed +387 -80
packages/source-control-azure-devops/src/server/AzureDevOpsCli.test.ts
+139 -3
@@ -1,5 +1,5 @@
1 import * as NodeServices from "@effect/platform-node/NodeServices";
2 -import { assert, it, afterEach, describe, vi } from "@effect/vitest";
2 +import { assert, it, afterEach, describe, expect, vi } from "@effect/vitest";
3 import * as Effect from "effect/Effect";
4 import * as Layer from "effect/Layer";
5 import * as Option from "effect/Option";
@@ -13,6 +13,7 @@ import * as HostProcess from "@t3tools/shared/HostProcess";
13 import { SpawnExecutableResolution } from "@t3tools/shared/shell";
14 import { AzureCliInstallLocation, type AzureCliInstall } from "./azureCliLauncher.ts";
15 import * as AzureDevOpsCli from "./AzureDevOpsCli.ts";
16 +import { primaryRemoteUrl, scopeAzureCliArgs } from "./AzureDevOpsCli.ts";
17
18 const processOutput = (stdout: string): SourceControlHost.SourceControlProcessOutput => ({
19 exitCode: ChildProcessSpawner.ExitCode(0),
@@ -26,7 +27,13 @@ const mockRun = vi.fn<SourceControlHost.SourceControlHost["Service"]["process"][
27
28 // The CLI layer is exercised on a host that spawns `az` itself, so the launcher upgrade stays out of these assertions.
29 const layerSupport = Layer.mergeAll(
29 - TestSourceControlHost.layer({ process: { run: mockRun } }),
30 + TestSourceControlHost.layer({
31 + // No remotes: these cases leave `--detect true` to az.
32 + process: {
33 + run: (input) =>
34 + input.command === "git" ? Effect.succeed(processOutput("")) : mockRun(input),
35 + },
36 + }),
37 NodeServices.layer,
38 Layer.succeed(HostProcess.Platform, "linux"),
39 );
@@ -65,6 +72,8 @@ describe("AzureDevOpsCli.layer", () => {
72 const result = yield* az.getPullRequest({
73 cwd: "/repo",
74 reference: "#42",
75 + // The selected remote numbers the pull request, whatever the checkout's primary remote.
76 + remoteUrl: "git@ssh.dev.azure.com:v3/acme/project/repo",
77 });
78
79 assert.strictEqual(result.number, 42);
@@ -102,12 +111,14 @@ describe("AzureDevOpsCli.layer", () => {
111 "pr",
112 "show",
113 "--detect",
105 - "true",
114 + "false",
115 "--id",
116 "42",
117 "--only-show-errors",
118 "--output",
119 "json",
120 + "--organization",
121 + "https://dev.azure.com/acme",
122 ],
123 cwd: "/repo",
124 notFoundDetail: "Pull request not found.",
@@ -395,3 +406,128 @@ describe("azureCliInvocation", () => {
406 }),
407 );
408 });
409 +
410 +describe("scopeAzureCliArgs", () => {
411 + const origin = "https://dev.azure.com/acme/Project/_git/Repo";
412 + const show = ["repos", "show", "--detect", "true"];
413 +
414 + it.each([
415 + [
416 + ["repos", "show"],
417 + ["--project=Project", "--repository=Repo"],
418 + ],
419 + [["repos", "create", "--name", "New"], ["--project=Project"]],
420 + [
421 + ["repos", "pr", "list"],
422 + ["--project=Project", "--repository=Repo"],
423 + ],
424 + [
425 + ["repos", "pr", "create"],
426 + ["--project=Project", "--repository=Repo"],
427 + ],
428 + [["repos", "pr", "show", "--id", "42"], []],
429 + [["devops", "invoke", "--area", "git"], []],
430 + ])("scopes %j to the commands' supported arguments", (command, scope) => {
431 + expect(scopeAzureCliArgs([...command, "--detect", "true"], origin)).toEqual([
432 + ...command,
433 + "--detect",
434 + "false",
435 + "--organization",
436 + "https://dev.azure.com/acme",
437 + ...scope,
438 + ]);
439 + });
440 +
441 + it.each([
442 + ["https://dev.azure.com/acme/My%20Project/_git/My%20Repo", "https://dev.azure.com/acme"],
443 + ["https://user@dev.azure.com/acme/My%20Project/_git/My%20Repo", "https://dev.azure.com/acme"],
444 + [
445 + "https://acme.visualstudio.com/DefaultCollection/My%20Project/_git/My%20Repo",
446 + "https://acme.visualstudio.com",
447 + ],
448 + ["https://acme.visualstudio.com/My%20Project/_git/My%20Repo", "https://acme.visualstudio.com"],
449 + ["git@ssh.dev.azure.com:v3/acme/My%20Project/My%20Repo", "https://dev.azure.com/acme"],
450 + ["ssh://git@ssh.dev.azure.com:22/v3/acme/My%20Project/My%20Repo", "https://dev.azure.com/acme"],
451 + ["git@vs-ssh.visualstudio.com:v3/acme/My%20Project/My%20Repo", "https://acme.visualstudio.com"],
452 + ])("reads the organization of %s", (url, organization) => {
453 + expect(scopeAzureCliArgs(show, url)).toEqual([
454 + "repos",
455 + "show",
456 + "--detect",
457 + "false",
458 + "--organization",
459 + organization,
460 + "--project=My Project",
461 + "--repository=My Repo",
462 + ]);
463 + });
464 +
465 + it("joins names starting with `-` to their options", () => {
466 + expect(
467 + scopeAzureCliArgs(
468 + ["repos", "pr", "list", "--detect", "true"],
469 + "https://dev.azure.com/acme/-Project/_git/-Repo",
470 + ),
471 + ).toEqual(expect.arrayContaining(["--project=-Project", "--repository=-Repo"]));
472 + });
473 +
474 + it("keeps the caller's project and repository", () => {
475 + for (const selection of [
476 + ["--project", "Chosen", "--repository", "Chosen"],
477 + ["-p", "Chosen", "-r", "Chosen"],
478 + ["--repository=Chosen", "--project", "Chosen"],
479 + ]) {
480 + expect(
481 + scopeAzureCliArgs(["repos", "show", ...selection, "--detect", "true"], origin),
482 + ).toEqual([
483 + "repos",
484 + "show",
485 + ...selection,
486 + "--detect",
487 + "false",
488 + "--organization",
489 + "https://dev.azure.com/acme",
490 + ]);
491 + }
492 + });
493 +
494 + it.each([
495 + [["account", "show", "--detect", "true"], origin],
496 + [["repos", "show"], origin],
497 + [["repos", "show", "--detect", "false"], origin],
498 + [["repos", "show", "--detect", "true", "--org", "https://dev.azure.com/explicit"], origin],
499 + [show, "https://github.com/acme/repo"],
500 + [show, "https://dev.azure.com/acme"],
501 + [show, "https://dev.azure.com/acme/%XX/_git/Repo"],
502 + [show, "git@ssh.dev.azure.com:v4/acme/Project/Repo"],
503 + [show, "git@vs-ssh.visualstudio.com:v3/acme.invalid/Project/Repo"],
504 + // az would replace the value with the contents of a local file.
505 + [show, "https://dev.azure.com/acme/Project/_git/%40.env"],
506 + ])("leaves %j to az when %s gives no scope", (args, url) => {
507 + expect(scopeAzureCliArgs(args, url)).toEqual(args);
508 + });
509 +});
510 +
511 +describe("primaryRemoteUrl", () => {
512 + const remotes = (...entries: ReadonlyArray<readonly [string, string]>) =>
513 + entries.map(([name, url]) => `${name}\t${url} (fetch)\n${name}\tpush-url (push)\n`).join("");
514 + const azure = (name: string) => [name, `https://dev.azure.com/${name}/P/_git/R`] as const;
515 +
516 + it.each([
517 + // Where T3 publishes the branch and creates its pull requests.
518 + [remotes(azure("upstream"), azure("origin")), azure("origin")[1]],
519 + [
520 + remotes(["github", "https://github.com/acme/repo"], azure("zebra"), azure("alpha")),
521 + azure("zebra")[1],
522 + ],
523 + // A non-Azure origin leaves detection to az rather than scoping another repository.
524 + [
525 + remotes(azure("upstream"), ["origin", "https://github.com/acme/repo"]),
526 + "https://github.com/acme/repo",
527 + ],
528 + [remotes(), null],
529 + [`origin\t${azure("origin")[1]} (fetch)`, null],
530 + ])("picks the scoping remote from %j", (output, url) => {
531 + expect(primaryRemoteUrl(output)).toBe(url);
532 + });
533 +});
packages/source-control-azure-devops/src/server/AzureDevOpsCli.ts
+179 -24
@@ -221,6 +221,8 @@ export class AzureDevOpsCli extends Context.Service<
221 readonly args: ReadonlyArray<string>;
222 readonly timeoutMs?: number;
223 readonly maxOutputBytes?: number;
224 + /** The remote T3 selected; without one, the checkout's primary remote scopes the command. */
225 + readonly remoteUrl?: string;
226 }) => Effect.Effect<SourceControlHost.SourceControlProcessOutput, AzureDevOpsCliError>;
227
228 readonly listPullRequests: (input: {
@@ -229,16 +231,19 @@ export class AzureDevOpsCli extends Context.Service<
231 readonly source?: SourceControlProvider.SourceControlRefSelector;
232 readonly state: "open" | "closed" | "merged" | "all";
233 readonly limit?: number;
234 + readonly remoteUrl?: string;
235 }) => Effect.Effect<ReadonlyArray<NormalizedAzureDevOpsPullRequestRecord>, AzureDevOpsCliError>;
236
237 readonly getPullRequest: (input: {
238 readonly cwd: string;
239 readonly reference: string;
240 + readonly remoteUrl?: string;
241 }) => Effect.Effect<NormalizedAzureDevOpsPullRequestRecord, AzureDevOpsCliError>;
242
243 readonly getRepositoryCloneUrls: (input: {
244 readonly cwd: string;
245 readonly repository: string;
246 + readonly remoteUrl?: string;
247 }) => Effect.Effect<AzureDevOpsRepositoryCloneUrls, AzureDevOpsCliError>;
248
249 readonly createRepository: (input: {
@@ -255,11 +260,13 @@ export class AzureDevOpsCli extends Context.Service<
260 readonly target?: SourceControlProvider.SourceControlRefSelector;
261 readonly title: string;
262 readonly bodyFile: string;
263 + readonly remoteUrl?: string;
264 }) => Effect.Effect<void, AzureDevOpsCliError>;
265
266 readonly getDefaultBranch: (input: {
267 readonly cwd: string;
268 readonly repository: string;
269 + readonly remoteUrl?: string;
270 }) => Effect.Effect<string | null, AzureDevOpsCliError>;
271 }
272 >()("@t3tools/source-control-azure-devops/server/AzureDevOpsCli") {}
@@ -349,6 +356,113 @@ function decodeAzureDevOpsJson<S extends Schema.Top>(
356 );
357 }
358
359 +/** The remote that scopes `az` when T3 selected none, from `git remote -v` output. */
360 +export function primaryRemoteUrl(output: string) {
361 + if (!output.endsWith("\n")) return null;
362 + const remotes = new Map<string, string>();
363 + for (const line of output.split("\n")) {
364 + const trimmed = line.trim();
365 + if (trimmed.length === 0) continue;
366 + const match = /^(\S+)\s+(\S+)\s+\((fetch|push)\)$/u.exec(trimmed);
367 + if (!match?.[1] || !match[2]) return null;
368 + if (match[3] === "fetch" && !remotes.has(match[1])) remotes.set(match[1], match[2]);
369 + }
370 + // Match the provider context SourceControlProviderRegistry binds, and `az`'s own detection.
371 + return (
372 + remotes.get("origin") ??
373 + [...remotes.values()].find((url) => scopeFromRemoteUrl(url) !== null) ??
374 + null
375 + );
376 +}
377 +
378 +function scopeFromRemoteUrl(remote: string) {
379 + try {
380 + const scp = /^(?:[^@/]+@)?([^/:]+):(.+)$/u.exec(remote);
381 + const url = new URL(
382 + remote.includes("://") ? remote : scp ? `ssh://${scp[1]}/${scp[2]}` : remote,
383 + );
384 + if (url.search || url.hash) return null;
385 + const legacy = url.hostname.endsWith(".visualstudio.com");
386 + const parts = url.pathname.slice(1).split("/").map(decodeURIComponent);
387 + let organization: string | undefined;
388 + if (
389 + url.protocol === "ssh:" &&
390 + (url.hostname === "ssh.dev.azure.com" || url.hostname === "vs-ssh.visualstudio.com")
391 + ) {
392 + if (parts.length !== 4 || parts.shift() !== "v3") return null;
393 + organization = parts.shift();
394 + } else if (url.protocol === "https:") {
395 + if (url.hostname === "dev.azure.com") organization = parts.shift();
396 + else {
397 + organization = /^([^.]+)\.visualstudio\.com$/u.exec(url.hostname)?.[1];
398 + if (!organization) return null;
399 + if (parts[0]?.toLowerCase() === "defaultcollection") parts.shift();
400 + }
401 + if (parts.length !== 3 || parts[1] !== "_git") return null;
402 + parts.splice(1, 1);
403 + } else return null;
404 + const [project, repository] = parts;
405 + if (!organization || !project || !repository) return null;
406 + // `az` replaces a value starting with `@` by the contents of that local file.
407 + if ([organization, project, repository].some((part) => /^@|[\p{Cc}/\\]/u.test(part))) {
408 + return null;
409 + }
410 + if (legacy && !/^[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?$/u.test(organization))
411 + return null;
412 + return {
413 + // az keys organization-scoped credentials by the original host family.
414 + organization: legacy
415 + ? `https://${organization}.visualstudio.com`
416 + : `https://dev.azure.com/${encodeURIComponent(organization)}`,
417 + project,
418 + repository,
419 + };
420 + } catch {
421 + return null;
422 + }
423 +}
424 +
425 +const remoteOf = (input: { readonly remoteUrl?: string }) =>
426 + input.remoteUrl === undefined ? {} : { remoteUrl: input.remoteUrl };
427 +
428 +const hasOption = (args: ReadonlyArray<string>, pattern: RegExp) =>
429 + args.some((arg) => pattern.test(arg));
430 +
431 +/** Whether `az` would run `--detect true`, which inspects the checkout from wherever `az` runs. */
432 +const detectsScope = (args: ReadonlyArray<string>) => {
433 + const detect = args.indexOf("--detect");
434 + return (
435 + args[0] !== "account" &&
436 + detect !== -1 &&
437 + args[detect + 1] === "true" &&
438 + !hasOption(args, /^(?:--organization|--org)(?:=|$)/u)
439 + );
440 +};
441 +
442 +/** Replaces `--detect true` with the scope of an Azure remote. */
443 +export function scopeAzureCliArgs(
444 + args: ReadonlyArray<string>,
445 + remoteUrl: string,
446 +): ReadonlyArray<string> {
447 + const scope = detectsScope(args) ? scopeFromRemoteUrl(remoteUrl) : null;
448 + if (!scope) return args;
449 + const scoped = [...args, "--organization", scope.organization];
450 + scoped[args.indexOf("--detect") + 1] = "false";
451 + // PR-ID commands and devops invoke accept only organization scope.
452 + const acceptsProject =
453 + args[0] === "repos" &&
454 + (args[1] === "show" ||
455 + args[1] === "create" ||
456 + (args[1] === "pr" && (args[2] === "list" || args[2] === "create")));
457 + if (!acceptsProject) return scoped;
458 + // Joined so a name starting with `-` stays the option's value.
459 + if (!hasOption(args, /^(?:--project|-p)(?:=|$)/u)) scoped.push(`--project=${scope.project}`);
460 + if (args[1] !== "create" && !hasOption(args, /^(?:--repository|-r)(?:=|$)/u)) {
461 + scoped.push(`--repository=${scope.repository}`);
462 + }
463 + return scoped;
464 +}
465 +
466 const classifyAzFailure = (stderr: string): VcsProcessExitFailureKind | null => {
467 const normalized = stderr.toLowerCase();
468 if (normalized.includes("az devops login") || /please run '?az login'?/.test(normalized)) {
@@ -398,32 +512,68 @@ export const make = Effect.gen(function* () {
512 const { process } = yield* SourceControlHost.SourceControlHost;
513 const invocation = yield* azureCliInvocation();
514
401 - const execute: AzureDevOpsCli["Service"]["execute"] = (input) =>
402 - process
403 - .run({
404 - operation: "AzureDevOpsCli.execute",
405 - command: invocation.command,
406 - args: [...invocation.args, ...input.args],
407 - cwd: input.cwd,
408 - ...(invocation.env === undefined ? {} : { env: invocation.env }),
409 - classifyFailure: classifyAzFailure,
410 - notFoundDetail: "Pull request not found.",
411 - timeoutMs: input.timeoutMs ?? DEFAULT_TIMEOUT_MS,
412 - ...(input.maxOutputBytes === undefined ? {} : { maxOutputBytes: input.maxOutputBytes }),
413 - })
414 - .pipe(
415 - Effect.mapError((error) =>
416 - AzureDevOpsCommandFailedError.fromVcsError(
417 - {
418 - operation: "execute",
419 - command: "az",
515 + const execute: AzureDevOpsCli["Service"]["execute"] = Effect.fn("AzureDevOpsCli.execute")(
516 + function* (input) {
517 + let args = input.args;
518 + if (detectsScope(args)) {
519 + // Windows az running in WSL cannot inspect the server's Linux checkout.
520 + const remoteUrl =
521 + input.remoteUrl ??
522 + (yield* process
523 + .run({
524 + operation: "AzureDevOpsCli.resolveScope",
525 + command: "git",
526 + args: ["remote", "-v"],
527 cwd: input.cwd,
421 - argumentCount: input.args.length,
422 - },
423 - error,
528 + env: {
529 + GIT_DIR: undefined,
530 + GIT_WORK_TREE: undefined,
531 + GIT_COMMON_DIR: undefined,
532 + GIT_INDEX_FILE: undefined,
533 + GIT_OBJECT_DIRECTORY: undefined,
534 + GIT_ALTERNATE_OBJECT_DIRECTORIES: undefined,
535 + },
536 + timeoutMs: input.timeoutMs ?? DEFAULT_TIMEOUT_MS,
537 + maxOutputBytes: 64 * 1024,
538 + allowNonZeroExit: true,
539 + })
540 + .pipe(
541 + Effect.map((remotes) =>
542 + remotes.exitCode === 0 && !remotes.stdoutTruncated && !remotes.stdoutInvalidUtf8
543 + ? primaryRemoteUrl(remotes.stdout)
544 + : null,
545 + ),
546 + Effect.orElseSucceed(() => null),
547 + ));
548 + if (remoteUrl !== null) args = scopeAzureCliArgs(args, remoteUrl);
549 + }
550 + return yield* process
551 + .run({
552 + operation: "AzureDevOpsCli.execute",
553 + command: invocation.command,
554 + args: [...invocation.args, ...args],
555 + cwd: input.cwd,
556 + ...(invocation.env === undefined ? {} : { env: invocation.env }),
557 + classifyFailure: classifyAzFailure,
558 + notFoundDetail: "Pull request not found.",
559 + timeoutMs: input.timeoutMs ?? DEFAULT_TIMEOUT_MS,
560 + ...(input.maxOutputBytes === undefined ? {} : { maxOutputBytes: input.maxOutputBytes }),
561 + })
562 + .pipe(
563 + Effect.mapError((error) =>
564 + AzureDevOpsCommandFailedError.fromVcsError(
565 + {
566 + operation: "execute",
567 + command: "az",
568 + cwd: input.cwd,
569 + argumentCount: input.args.length,
570 + },
571 + error,
572 + ),
573 ),
425 - ),
426 - );
574 + );
575 + },
576 + );
577
578 const executeJson = (input: Parameters<AzureDevOpsCli["Service"]["execute"]>[0]) =>
579 execute({
@@ -441,6 +591,7 @@ export const make = Effect.gen(function* () {
591 "";
592 return executeJson({
593 cwd: input.cwd,
594 + ...remoteOf(input),
595 args: [
596 "repos",
597 "pr",
@@ -483,6 +634,7 @@ export const make = Effect.gen(function* () {
634 getPullRequest: Effect.fn("AzureDevOpsCli.getPullRequest")(function (input) {
635 return executeJson({
636 cwd: input.cwd,
637 + ...remoteOf(input),
638 args: [
639 "repos",
640 "pr",
@@ -519,6 +671,7 @@ export const make = Effect.gen(function* () {
671 const repository = parseRepositorySpecifier(input.repository);
672 return executeJson({
673 cwd: input.cwd,
674 + ...remoteOf(input),
675 args: [
676 "repos",
677 "show",
@@ -566,6 +719,7 @@ export const make = Effect.gen(function* () {
719 createPullRequest: Effect.fn("AzureDevOpsCli.createPullRequest")(function (input) {
720 return execute({
721 cwd: input.cwd,
722 + ...remoteOf(input),
723 args: [
724 "repos",
725 "pr",
@@ -588,6 +742,7 @@ export const make = Effect.gen(function* () {
742 const repository = parseRepositorySpecifier(input.repository);
743 return executeJson({
744 cwd: input.cwd,
745 + ...remoteOf(input),
746 args: [
747 "repos",
748 "show",
packages/source-control-azure-devops/src/server/AzureDevOpsPullRequestCli.ts
-1
@@ -314,7 +314,6 @@ export const make = Effect.gen(function* () {
314 const azure = yield* AzureDevOpsCli.AzureDevOpsCli;
315 const fileSystem = yield* FileSystem.FileSystem;
316
317 - // Let `az` parse all three remote formats.
317 const detectArgs = ["--detect", "true"] as const;
318
319 const executeJson = (input: {
packages/source-control-azure-devops/src/server/AzureDevOpsSourceControlProvider.test.ts
+13 -14
@@ -53,15 +53,15 @@ it.effect("names the remote's repository when reading the default branch", () =>
53 );
54 return Effect.gen(function* () {
55 const provider = yield* AzureDevOpsSourceControlProvider.make;
56 - for (const [remoteUrl, repository] of [
57 - ["https://acme@dev.azure.com/acme/project/_git/My%20Repo", "My Repo"],
58 - ["https://dev.azure.com/acme/project/_git/My%20Repo?view=x#readme", "My Repo"],
59 - ["https://acme.visualstudio.com/DefaultCollection/project/_git/My%20Repo/", "My Repo"],
60 - ["git@ssh.dev.azure.com:v3/acme/project/My%20Repo", "My Repo"],
61 - ["ssh://git@ssh.dev.azure.com/v3/acme/project/My%20Repo", "My Repo"],
62 - ["https://dev.azure.com/acme/project/_git/service.git", "service.git"],
63 - ["https://dev.azure.com/acme/project/_git/-repo", "-repo"],
64 - ["https://dev.azure.com/acme/project/_git/%40.env", null],
56 + for (const [remoteUrl, repository, scoped] of [
57 + ["https://acme@dev.azure.com/acme/project/_git/My%20Repo", "My Repo", true],
58 + ["https://dev.azure.com/acme/project/_git/My%20Repo?view=x#readme", "My Repo", false],
59 + ["https://acme.visualstudio.com/DefaultCollection/project/_git/My%20Repo/", "My Repo", false],
60 + ["git@ssh.dev.azure.com:v3/acme/project/My%20Repo", "My Repo", true],
61 + ["ssh://git@ssh.dev.azure.com/v3/acme/project/My%20Repo", "My Repo", true],
62 + ["https://dev.azure.com/acme/project/_git/service.git", "service.git", true],
63 + ["https://dev.azure.com/acme/project/_git/-repo", "-repo", true],
64 + ["https://dev.azure.com/acme/project/_git/%40.env", null, false],
65 ] as const) {
66 run.mockClear();
67 const branch = yield* provider.getDefaultBranch({
@@ -82,11 +82,10 @@ it.effect("names the remote's repository when reading the default branch", () =>
82 continue;
83 }
84 assert.strictEqual(branch, "master", remoteUrl);
85 - assert.include(
86 - [...(run.mock.calls[0]?.[0].args ?? [])],
87 - `--repository=${repository}`,
88 - remoteUrl,
89 - );
85 + const args = [...(run.mock.calls.find(([input]) => input.command !== "git")?.[0].args ?? [])];
86 + assert.include(args, `--repository=${repository}`, remoteUrl);
87 + // The selected remote names the project, not the checkout's primary remote.
88 + if (scoped) assert.include(args, "--project=project", remoteUrl);
89 }
90 }).pipe(Effect.provide(AzureDevOpsCli.layer.pipe(Layer.provideMerge(support))));
91 });
packages/source-control-azure-devops/src/server/AzureDevOpsSourceControlProvider.ts
+56 -38
@@ -72,6 +72,10 @@ function repositoryNameFromRemote(remoteUrl: string): string | null {
72 return name.startsWith("@") ? null : name;
73 }
74
75 +/** The remote T3 selected, which scopes `az` instead of the checkout's primary remote. */
76 +const remoteOf = (input: { readonly context?: { readonly remoteUrl: string } | undefined }) =>
77 + input.context === undefined ? {} : { remoteUrl: input.context.remoteUrl };
78 +
79 function toChangeRequest(summary: NormalizedAzureDevOpsPullRequestRecord): ChangeRequest {
80 return {
81 provider: SourceControlProviderKind.make("azure-devops"),
@@ -126,6 +130,7 @@ export const make = Effect.gen(function* () {
130 return azure
131 .listPullRequests({
132 cwd: input.cwd,
133 + ...remoteOf(input),
134 headSelector: input.headSelector,
135 ...(source !== undefined ? { source } : {}),
136 state: input.state,
@@ -152,23 +157,25 @@ export const make = Effect.gen(function* () {
157 ),
158 getChangeRequest: Effect.fn("AzureDevOpsSourceControlProvider.getChangeRequest")(
159 function (input) {
155 - return azure.getPullRequest(input).pipe(
156 - Effect.map(toChangeRequest),
157 - Effect.mapError(
158 - (error) =>
159 - new SourceControlProviderError({
160 - provider: SourceControlProviderKind.make("azure-devops"),
161 - operation: "getChangeRequest",
162 - command: error.command,
163 - cwd: input.cwd,
164 - reference: SourceControlProvider.transportSafeSourceControlErrorValue(
165 - input.reference,
166 - ),
167 - detail: error.detail,
168 - cause: error,
169 - }),
170 - ),
171 - );
160 + return azure
161 + .getPullRequest({ cwd: input.cwd, reference: input.reference, ...remoteOf(input) })
162 + .pipe(
163 + Effect.map(toChangeRequest),
164 + Effect.mapError(
165 + (error) =>
166 + new SourceControlProviderError({
167 + provider: SourceControlProviderKind.make("azure-devops"),
168 + operation: "getChangeRequest",
169 + command: error.command,
170 + cwd: input.cwd,
171 + reference: SourceControlProvider.transportSafeSourceControlErrorValue(
172 + input.reference,
173 + ),
174 + detail: error.detail,
175 + cause: error,
176 + }),
177 + ),
178 + );
179 },
180 ),
181 createChangeRequest: Effect.fn("AzureDevOpsSourceControlProvider.createChangeRequest")(
@@ -177,6 +184,7 @@ export const make = Effect.gen(function* () {
184 return azure
185 .createPullRequest({
186 cwd: input.cwd,
187 + ...remoteOf(input),
188 baseBranch: input.baseRefName,
189 headSelector: input.headSelector,
190 ...(source !== undefined ? { source } : {}),
@@ -204,22 +212,28 @@ export const make = Effect.gen(function* () {
212 ),
213 getRepositoryCloneUrls: Effect.fn("AzureDevOpsSourceControlProvider.getRepositoryCloneUrls")(
214 function (input) {
207 - return azure.getRepositoryCloneUrls(input).pipe(
208 - Effect.mapError(
209 - (error) =>
210 - new SourceControlProviderError({
211 - provider: SourceControlProviderKind.make("azure-devops"),
212 - operation: "getRepositoryCloneUrls",
213 - command: error.command,
214 - cwd: input.cwd,
215 - repository: SourceControlProvider.transportSafeSourceControlErrorValue(
216 - input.repository,
217 - ),
218 - detail: error.detail,
219 - cause: error,
220 - }),
221 - ),
222 - );
215 + return azure
216 + .getRepositoryCloneUrls({
217 + cwd: input.cwd,
218 + repository: input.repository,
219 + ...remoteOf(input),
220 + })
221 + .pipe(
222 + Effect.mapError(
223 + (error) =>
224 + new SourceControlProviderError({
225 + provider: SourceControlProviderKind.make("azure-devops"),
226 + operation: "getRepositoryCloneUrls",
227 + command: error.command,
228 + cwd: input.cwd,
229 + repository: SourceControlProvider.transportSafeSourceControlErrorValue(
230 + input.repository,
231 + ),
232 + detail: error.detail,
233 + cause: error,
234 + }),
235 + ),
236 + );
237 },
238 ),
239 createRepository: Effect.fn("AzureDevOpsSourceControlProvider.createRepository")(
@@ -244,10 +258,10 @@ export const make = Effect.gen(function* () {
258 ),
259 getDefaultBranch: Effect.fn("AzureDevOpsSourceControlProvider.getDefaultBranch")(
260 function (input) {
247 - const repository =
248 - input.context === undefined ? null : repositoryNameFromRemote(input.context.remoteUrl);
249 - if (repository === null) return Effect.succeed(null);
250 - return azure.getDefaultBranch({ cwd: input.cwd, repository }).pipe(
261 + const remoteUrl = input.context?.remoteUrl;
262 + const repository = remoteUrl === undefined ? null : repositoryNameFromRemote(remoteUrl);
263 + if (remoteUrl === undefined || repository === null) return Effect.succeed(null);
264 + return azure.getDefaultBranch({ cwd: input.cwd, repository, remoteUrl }).pipe(
265 Effect.mapError(
266 (error) =>
267 new SourceControlProviderError({
@@ -266,7 +280,11 @@ export const make = Effect.gen(function* () {
280 // and its pull then merges the pull request into the current branch.
281 checkoutChangeRequest: Effect.fn("AzureDevOpsSourceControlProvider.checkoutChangeRequest")(
282 function* (input) {
269 - const pullRequest = yield* azure.getPullRequest(input);
283 + const pullRequest = yield* azure.getPullRequest({
284 + cwd: input.cwd,
285 + reference: input.reference,
286 + ...remoteOf(input),
287 + });
288 if (pullRequest.isCrossRepository)
289 return yield* new SourceControlProviderError({
290 provider: SourceControlProviderKind.make("azure-devops"),