@devries-cameron20 / t3code-1 / commits / a7fd4326cf
Fork 0

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

fix(server): read Azure DevOps pull request templates (#22881)

Julius Marminge committed Nov 13, 2026 at 17:21 UTC a7fd4326cffd1f69f32e125a1fa4400b4ac61f7c
4 files changed +278 -2
packages/source-control-azure-devops/src/server/AzureDevOpsSourceControlProvider.git.test.ts new
+165
@@ -0,0 +1,165 @@
1 +// @effect-diagnostics nodeBuiltinImport:off
2 +import * as NodeFS from "node:fs";
3 +import * as NodeOS from "node:os";
4 +import * as NodePath from "node:path";
5 +
6 +import type { GitCommandError } from "@t3tools/contracts";
7 +
8 +import { assert, it } from "@effect/vitest";
9 +import * as Effect from "effect/Effect";
10 +import * as Layer from "effect/Layer";
11 +import * as Option from "effect/Option";
12 +import * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost";
13 +import * as TestSourceControlHost from "@t3tools/source-control-testing/TestSourceControlHost";
14 +
15 +import * as AzureDevOpsCli from "./AzureDevOpsCli.ts";
16 +import * as AzureDevOpsSourceControlProvider from "./AzureDevOpsSourceControlProvider.ts";
17 +
18 +const withRepository = <A, E>(
19 + test: (input: {
20 + readonly git: (...args: string[]) => Effect.Effect<string, GitCommandError>;
21 + readonly write: (path: string, contents: string) => Effect.Effect<void>;
22 + readonly commit: Effect.Effect<void, GitCommandError>;
23 + readonly readTemplate: (treeish?: string) => Effect.Effect<Option.Option<string>>;
24 + }) => Effect.Effect<A, E>,
25 +) =>
26 + Effect.gen(function* () {
27 + const host = yield* SourceControlHost.SourceControlHost;
28 + const cwd = yield* Effect.acquireRelease(
29 + Effect.sync(() => NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-azure-template-"))),
30 + (directory) => Effect.sync(() => NodeFS.rmSync(directory, { recursive: true, force: true })),
31 + );
32 + const git = (...args: string[]) =>
33 + host.git
34 + .execute({ operation: "AzureDevOpsTemplate.test", cwd, args })
35 + .pipe(Effect.map((result) => result.stdout.trim()));
36 + const write = (path: string, contents: string) =>
37 + Effect.sync(() => {
38 + NodeFS.mkdirSync(NodePath.dirname(NodePath.join(cwd, path)), { recursive: true });
39 + NodeFS.writeFileSync(NodePath.join(cwd, path), contents);
40 + });
41 + const commit = Effect.gen(function* () {
42 + yield* git("add", "-A");
43 + yield* git("commit", "--allow-empty", "-m", "Add pull request templates");
44 + });
45 + yield* git("init", "--initial-branch=main");
46 + yield* git("config", "user.email", "test@example.com");
47 + yield* git("config", "user.name", "Test User");
48 + yield* git("config", "core.autocrlf", "false");
49 + yield* git("remote", "add", "origin", cwd);
50 + const provider = yield* AzureDevOpsSourceControlProvider.make.pipe(
51 + Effect.provide(Layer.mock(AzureDevOpsCli.AzureDevOpsCli)({})),
52 + );
53 + return yield* test({
54 + git,
55 + write,
56 + commit,
57 + readTemplate: (treeish = "main") => provider.readChangeRequestTemplate!({ cwd, treeish }),
58 + });
59 + }).pipe(
60 + Effect.scoped,
61 + Effect.provide(
62 + TestSourceControlHost.layerWithRealGit({
63 + git: { resolvePrimaryRemoteName: () => Effect.succeed("origin") },
64 + }),
65 + ),
66 + );
67 +
68 +const publishMain = (git: (...args: string[]) => Effect.Effect<string, GitCommandError>) =>
69 + Effect.gen(function* () {
70 + yield* git("update-ref", "refs/remotes/origin/main", "main");
71 + yield* git("symbolic-ref", "refs/remotes/origin/HEAD", "refs/remotes/origin/main");
72 + });
73 +
74 +it.effect("reads the default-branch template whatever branch the pull request targets", () =>
75 + withRepository(({ git, write, commit, readTemplate }) =>
76 + Effect.gen(function* () {
77 + const templatePath = ".azuredevops/pull_request_template.md";
78 + yield* write(templatePath, "Default branch template");
79 + yield* commit;
80 + yield* publishMain(git);
81 + yield* git("checkout", "-b", "release");
82 + yield* write(templatePath, "Release branch template");
83 + yield* commit;
84 + yield* write(templatePath, "Uncommitted template");
85 +
86 + assert.deepStrictEqual(
87 + yield* readTemplate("release"),
88 + Option.some("Default branch template"),
89 + );
90 + }),
91 + ),
92 +);
93 +
94 +// Microsoft searches these locations in order, ignoring case; extension must not override folder.
95 +it.effect.each([
96 + [".AzureDevOps/PULL_REQUEST_TEMPLATE.txt", ".vsts/pull_request_template.md"],
97 + [".VSTS/pull_request_template.TXT", "docs/pull_request_template.md"],
98 + ["Docs/Pull_Request_Template.txt", "pull_request_template.md"],
99 +] as const)("prefers %s over %s", ([preferred, fallback]) =>
100 + withRepository(({ git, write, commit, readTemplate }) =>
101 + Effect.gen(function* () {
102 + yield* write(preferred, "Preferred template");
103 + yield* write(fallback, "Fallback template");
104 + yield* commit;
105 + yield* publishMain(git);
106 +
107 + assert.deepStrictEqual(yield* readTemplate(), Option.some("Preferred template"));
108 + }),
109 + ),
110 +);
111 +
112 +it.effect("ignores missing, blank, symlinked, and nested templates", () =>
113 + withRepository(({ git, write, commit, readTemplate }) =>
114 + Effect.gen(function* () {
115 + yield* commit;
116 + yield* publishMain(git);
117 + assert.deepStrictEqual(yield* readTemplate(), Option.none());
118 +
119 + yield* write(".azuredevops/pull_request_template.md", " \n\t ");
120 + yield* commit;
121 + yield* publishMain(git);
122 + assert.deepStrictEqual(yield* readTemplate(), Option.none());
123 +
124 + yield* git("rm", "-q", ".azuredevops/pull_request_template.md");
125 + yield* write("template.md", "Not a default template");
126 + yield* write("docs/nested/pull_request_template.md", "Nested template");
127 + yield* commit;
128 + // An index mode commits a symlink even on hosts without symlink support.
129 + const blob = yield* git("rev-parse", "HEAD:template.md");
130 + yield* git(
131 + "update-index",
132 + "--add",
133 + "--cacheinfo",
134 + `120000,${blob},.azuredevops/pull_request_template.md`,
135 + );
136 + yield* git("commit", "-m", "Add a symlinked template");
137 + yield* publishMain(git);
138 + assert.deepStrictEqual(yield* readTemplate(), Option.none());
139 + }),
140 + ),
141 +);
142 +
143 +it.effect("bounds template content and marks truncation", () =>
144 + withRepository(({ git, write, commit, readTemplate }) =>
145 + Effect.gen(function* () {
146 + const prefix = "x".repeat(8_000);
147 + yield* write("pull_request_template.md", prefix + "EXCLUDED_SUFFIX");
148 + yield* commit;
149 + yield* publishMain(git);
150 +
151 + assert.deepStrictEqual(yield* readTemplate(), Option.some(prefix + "\n\n[truncated]"));
152 + }),
153 + ),
154 +);
155 +
156 +it.effect("returns no template when the local remote default branch is unknown", () =>
157 + withRepository(({ write, commit, readTemplate }) =>
158 + Effect.gen(function* () {
159 + yield* write("pull_request_template.md", "Target branch template");
160 + yield* commit;
161 +
162 + assert.deepStrictEqual(yield* readTemplate(), Option.none());
163 + }),
164 + ),
165 +);
packages/source-control-azure-devops/src/server/AzureDevOpsSourceControlProvider.test.ts
+9 -2
@@ -88,7 +88,7 @@ it.effect("names the remote's repository when reading the default branch", () =>
88 remoteUrl,
89 );
90 }
91 - }).pipe(Effect.provide(AzureDevOpsCli.layer.pipe(Layer.provide(support))));
91 + }).pipe(Effect.provide(AzureDevOpsCli.layer.pipe(Layer.provideMerge(support))));
92 });
93
94 it.effect(
@@ -103,5 +103,12 @@ it.effect(
103 }),
104 { headSelectors: ["feature"], limit: 100 },
105 );
106 - }).pipe(Effect.provide(Layer.mock(AzureDevOpsCli.AzureDevOpsCli)({}))),
106 + }).pipe(
107 + Effect.provide(
108 + Layer.mergeAll(
109 + Layer.mock(AzureDevOpsCli.AzureDevOpsCli)({}),
110 + TestSourceControlHost.layer(),
111 + ),
112 + ),
113 + ),
114 );
packages/source-control-azure-devops/src/server/AzureDevOpsSourceControlProvider.ts
+23
@@ -1,7 +1,10 @@
1 import { SourceControlProviderKind } from "@t3tools/contracts";
2 import * as Effect from "effect/Effect";
3 +import * as Option from "effect/Option";
4 import { SourceControlProviderError, type ChangeRequest } from "@t3tools/contracts";
5
6 +import * as AzureDevOpsChangeRequestTemplate from "./azureDevOpsChangeRequestTemplate.ts";
7 +import * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost";
8 import * as AzureDevOpsCli from "./AzureDevOpsCli.ts";
9 import type { NormalizedAzureDevOpsPullRequestRecord } from "./azureDevOpsPullRequests.ts";
10 import * as SourceControlProvider from "@t3tools/source-control-core/server/SourceControlProvider";
@@ -89,6 +92,7 @@ function toChangeRequest(summary: NormalizedAzureDevOpsPullRequestRecord): Chang
92
93 export const make = Effect.gen(function* () {
94 const azure = yield* AzureDevOpsCli.AzureDevOpsCli;
95 + const { git } = yield* SourceControlHost.SourceControlHost;
96
97 return SourceControlProvider.SourceControlProvider.of({
98 kind: SourceControlProviderKind.make("azure-devops"),
@@ -97,6 +101,25 @@ export const make = Effect.gen(function* () {
101 headSelectors: headSelectors.filter((selector) => !selector.includes(":")),
102 limit: 100,
103 }),
104 + readChangeRequestTemplate: Effect.fn(
105 + "AzureDevOpsSourceControlProvider.readChangeRequestTemplate",
106 + )(
107 + function* ({ cwd }) {
108 + // Azure uses the repository default branch even when the PR targets another branch.
109 + const remoteName = yield* git.resolvePrimaryRemoteName(cwd);
110 + const defaultRef = yield* git.execute({
111 + operation: "AzureDevOpsSourceControlProvider.resolveDefaultBranch",
112 + cwd,
113 + args: ["symbolic-ref", `refs/remotes/${remoteName}/HEAD`],
114 + });
115 + return yield* AzureDevOpsChangeRequestTemplate.detect(
116 + cwd,
117 + defaultRef.stdout.trim(),
118 + git.execute,
119 + );
120 + },
121 + Effect.orElseSucceed(() => Option.none()),
122 + ),
123 listChangeRequests: Effect.fn("AzureDevOpsSourceControlProvider.listChangeRequests")(
124 function (input) {
125 const source = SourceControlProvider.sourceControlRefFromInput(input);
packages/source-control-azure-devops/src/server/azureDevOpsChangeRequestTemplate.ts new
+81
@@ -0,0 +1,81 @@
1 +import * as Effect from "effect/Effect";
2 +import * as Option from "effect/Option";
3 +
4 +import type * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost";
5 +
6 +const TEMPLATE_DIRECTORIES = [".azuredevops", ".vsts", "docs", ""];
7 +const TEMPLATE_MAX_BYTES = 8_000;
8 +const TREE_LIST_MAX_BYTES = 100_000;
9 +const TRUNCATION_MARKER = "[truncated]";
10 +
11 +type ExecuteGit = SourceControlHost.SourceControlHost["Service"]["git"]["execute"];
12 +
13 +export const detect = Effect.fn("azureDevOpsChangeRequestTemplate.detect")(
14 + function* (cwd: string, treeish: string, executeGit: ExecuteGit) {
15 + // Inspect only the root and template directories; never recurse through the repository.
16 + const root = yield* executeGit({
17 + operation: "azureDevOpsChangeRequestTemplate.listTemplates",
18 + cwd,
19 + args: ["ls-tree", "-z", "--full-tree", treeish],
20 + maxOutputBytes: TREE_LIST_MAX_BYTES,
21 + appendTruncationMarker: true,
22 + });
23 + if (root.stdoutTruncated) {
24 + return Option.none();
25 + }
26 +
27 + const rootEntries = root.stdout.split("\0");
28 + let objectId: string | undefined;
29 + for (const directory of TEMPLATE_DIRECTORIES) {
30 + let entries = rootEntries;
31 + if (directory !== "") {
32 + const trees = rootEntries.flatMap((record) => {
33 + const entry = /^040000 tree ([0-9a-f]{40,64})\t(.+)$/.exec(record);
34 + return entry?.[1] && entry[2]?.toLowerCase() === directory ? [entry[1]] : [];
35 + });
36 + entries = [];
37 + for (const tree of trees) {
38 + const listing = yield* executeGit({
39 + operation: "azureDevOpsChangeRequestTemplate.listTemplates",
40 + cwd,
41 + args: ["ls-tree", "-z", "--full-tree", tree],
42 + maxOutputBytes: TREE_LIST_MAX_BYTES,
43 + appendTruncationMarker: true,
44 + });
45 + if (listing.stdoutTruncated) {
46 + return Option.none();
47 + }
48 + entries.push(...listing.stdout.split("\0"));
49 + }
50 + }
51 + // Only regular committed blobs qualify, so symlinks never reach the host filesystem.
52 + objectId = entries
53 + .map((record) => /^100(?:644|755) blob ([0-9a-f]{40,64})\t(.+)$/.exec(record))
54 + .find((entry) => /^pull_request_template\.(?:md|txt)$/i.test(entry?.[2] ?? ""))?.[1];
55 + if (objectId) {
56 + break;
57 + }
58 + }
59 + if (!objectId) {
60 + return Option.none();
61 + }
62 +
63 + const blob = yield* executeGit({
64 + operation: "azureDevOpsChangeRequestTemplate.readTemplate",
65 + cwd,
66 + args: ["cat-file", "blob", objectId],
67 + maxOutputBytes: TEMPLATE_MAX_BYTES,
68 + appendTruncationMarker: true,
69 + });
70 + const template = blob.stdout.trim();
71 + if (template.length === 0) {
72 + return Option.none();
73 + }
74 + return Option.some(
75 + blob.stdoutTruncated && !template.endsWith(TRUNCATION_MARKER)
76 + ? `${template}\n\n${TRUNCATION_MARKER}`
77 + : template,
78 + );
79 + },
80 + Effect.orElseSucceed(() => Option.none()),
81 +);