From fe23e352686adf42126a98b7a5e1cdf5a94aedce Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 21 Oct 2026 12:37:25 +0000 Subject: [PATCH 1/3] fix(server): read Azure DevOps pull request templates --- .../GitManager.azureDevOpsTemplate.test.ts | 162 ++++++++++++++ .../azureDevOpsChangeRequestTemplate.test.ts | 199 ++++++++++++++++++ .../AzureDevOpsSourceControlProvider.ts | 5 + .../azureDevOpsChangeRequestTemplate.ts | 81 +++++++ 4 files changed, 447 insertions(+) create mode 100644 apps/server/src/git/GitManager.azureDevOpsTemplate.test.ts create mode 100644 apps/server/src/sourceControl/azureDevOpsChangeRequestTemplate.test.ts create mode 100644 packages/source-control-azure-devops/src/server/azureDevOpsChangeRequestTemplate.ts diff --git a/apps/server/src/git/GitManager.azureDevOpsTemplate.test.ts b/apps/server/src/git/GitManager.azureDevOpsTemplate.test.ts new file mode 100644 index 000000000000..59f8725cc3dc --- /dev/null +++ b/apps/server/src/git/GitManager.azureDevOpsTemplate.test.ts @@ -0,0 +1,162 @@ +import * as NodeServices from "@effect/platform-node/NodeServices"; +import { assert, it } from "@effect/vitest"; +import * as Effect from "effect/Effect"; +import * as FileSystem from "effect/FileSystem"; +import * as Layer from "effect/Layer"; +import * as Path from "effect/Path"; +import * as AzureDevOpsCli from "@t3tools/source-control-azure-devops/server/AzureDevOpsCli"; +import * as AzureDevOpsSourceControlProvider from "@t3tools/source-control-azure-devops/server/AzureDevOpsSourceControlProvider"; +import * as TestSourceControlHost from "@t3tools/source-control-testing/TestSourceControlHost"; + +import * as ServerConfig from "../config.ts"; +import * as GitVcsDriver from "../vcs/GitVcsDriver.ts"; +import * as VcsProcess from "../vcs/VcsProcess.ts"; + +import * as HostProcess from "@t3tools/shared/HostProcess"; +import * as GitManager from "./GitManager.ts"; +import * as TextGeneration from "../textGeneration/TextGeneration.ts"; +import * as SourceControlProviderRegistry from "../sourceControl/SourceControlProviderRegistry.ts"; +import * as ProviderRegistry from "../provider/ProviderRegistry.ts"; +import * as ProjectSetupScriptRunner from "../project/ProjectSetupScriptRunner.ts"; +import * as ServerSettings from "../serverSettings.ts"; +import * as ProjectionStore from "../orchestration-v2/ProjectionStore.ts"; +import * as ProjectStore from "../orchestration-v2/ProjectStore.ts"; +import * as SqlitePersistence from "../persistence/Sqlite.ts"; + +const layerPrTemplateDetectionTest = GitVcsDriver.layer.pipe( + Layer.provide( + ServerConfig.layerTest(process.cwd(), { + prefix: "t3-pr-template-test-", + }), + ), + Layer.provideMerge(VcsProcess.layer), + Layer.provideMerge(NodeServices.layer), +); + +const runGit = (cwd: string, args: ReadonlyArray) => + Effect.gen(function* () { + const git = yield* GitVcsDriver.GitVcsDriver; + return yield* git.execute({ + operation: "PrTemplateDetection.test.runGit", + cwd, + args, + }); + }); + +const runWithTempDirectory = ( + test: (cwd: string) => Effect.Effect, +) => + Effect.scoped( + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-pr-template-" }); + yield* runGit(cwd, ["init", "--initial-branch=main"]); + yield* runGit(cwd, ["config", "user.email", "test@example.com"]); + yield* runGit(cwd, ["config", "user.name", "Test User"]); + return yield* test(cwd); + }), + ).pipe(Effect.provide(layerPrTemplateDetectionTest)); + +const writeTemplate = (cwd: string, relativePath: string, contents: string) => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const templatePath = path.join(cwd, relativePath); + yield* fileSystem.makeDirectory(path.dirname(templatePath), { recursive: true }); + yield* fileSystem.writeFileString(templatePath, contents); + return templatePath; + }); + +const commitTemplates = (cwd: string) => + Effect.gen(function* () { + yield* runGit(cwd, ["add", "-A"]); + yield* runGit(cwd, ["commit", "--allow-empty", "-m", "Add pull request templates"]); + }); + +it.effect("GitManager passes the Azure default-branch template to PR generation when enabled", () => + runWithTempDirectory((cwd) => + Effect.gen(function* () { + yield* runGit(cwd, ["config", "core.autocrlf", "false"]); + const templatePath = ".azuredevops/pull_request_template.md"; + yield* writeTemplate(cwd, templatePath, "Default branch template"); + yield* commitTemplates(cwd); + const fs = yield* FileSystem.FileSystem; + const remote = yield* fs.makeTempDirectoryScoped({ prefix: "t3-azure-template-remote-" }); + yield* runGit(remote, ["init", "--bare", "--initial-branch=main"]); + yield* runGit(cwd, ["remote", "add", "origin", remote]); + yield* runGit(cwd, ["push", "-u", "origin", "main"]); + yield* runGit(cwd, ["checkout", "-b", "feature/azure-template"]); + yield* writeTemplate(cwd, templatePath, "Feature branch template"); + yield* writeTemplate(cwd, "feature.txt", "Feature work"); + yield* commitTemplates(cwd); + yield* runGit(cwd, ["push", "-u", "origin", "feature/azure-template"]); + + const git = yield* GitVcsDriver.GitVcsDriver; + let created = false; + const provider = yield* AzureDevOpsSourceControlProvider.make.pipe( + Effect.provide( + Layer.mergeAll( + Layer.mock(AzureDevOpsCli.AzureDevOpsCli)({ + getDefaultBranch: () => Effect.succeed("main"), + listPullRequests: () => Effect.succeed([]), + createPullRequest: ({ bodyFile }) => + Effect.gen(function* () { + assert.strictEqual( + yield* fs.readFileString(bodyFile).pipe(Effect.orDie), + "Generated body", + ); + created = true; + }), + }), + TestSourceControlHost.layer({ git: { execute: git.execute } }), + ), + ), + ); + const generated: TextGeneration.PrContentGenerationInput[] = []; + const services = Layer.mergeAll( + Layer.mock(TextGeneration.TextGeneration)({ + generatePrContent: (input) => { + generated.push(input); + return Effect.succeed({ title: "Azure template PR", body: "Generated body" }); + }, + }), + Layer.mock(SourceControlProviderRegistry.SourceControlProviderRegistry)({ + resolve: () => Effect.succeed(provider), + resolveHandle: () => Effect.succeed({ provider, context: null }), + get: () => Effect.succeed(provider), + discover: Effect.succeed([]), + resolveLink: () => undefined, + }), + Layer.mock(ProviderRegistry.ProviderRegistry)({ + getProviders: Effect.succeed([]), + }), + Layer.mock(ProjectSetupScriptRunner.ProjectSetupScriptRunner)({ + runForThread: () => Effect.succeed({ status: "no-script" as const }), + }), + ServerSettings.ServerSettingsService.layerTest({ + sourceControlWritingStyle: { followChangeRequestTemplates: true }, + }), + Layer.merge(ProjectionStore.layer, ProjectStore.layer).pipe( + Layer.provide(SqlitePersistence.layerMemory), + ), + ); + const manager = yield* GitManager.make.pipe( + Effect.provideService(HostProcess.Environment, {}), + Effect.provide(services), + ); + + const result = yield* manager.runStackedAction({ + actionId: "azure-template-test", + cwd, + action: "create_pr", + }); + + assert.strictEqual(result.pr.status, "created"); + assert.isTrue(created); + assert.lengthOf(generated, 1); + assert.strictEqual(generated[0]?.baseBranch, "main"); + assert.include(generated[0]?.diffPatch ?? "", "+Feature work"); + assert.strictEqual(generated[0]?.changeRequestTemplate, "Default branch template"); + }), + ), +); diff --git a/apps/server/src/sourceControl/azureDevOpsChangeRequestTemplate.test.ts b/apps/server/src/sourceControl/azureDevOpsChangeRequestTemplate.test.ts new file mode 100644 index 000000000000..9715f0e4b885 --- /dev/null +++ b/apps/server/src/sourceControl/azureDevOpsChangeRequestTemplate.test.ts @@ -0,0 +1,199 @@ +import * as NodeServices from "@effect/platform-node/NodeServices"; +import { assert, it } from "@effect/vitest"; +import * as Effect from "effect/Effect"; +import * as FileSystem from "effect/FileSystem"; +import * as Layer from "effect/Layer"; +import * as Option from "effect/Option"; +import * as Path from "effect/Path"; +import * as AzureDevOpsCli from "@t3tools/source-control-azure-devops/server/AzureDevOpsCli"; +import * as AzureDevOpsSourceControlProvider from "@t3tools/source-control-azure-devops/server/AzureDevOpsSourceControlProvider"; +import * as GitLabCli from "@t3tools/source-control-gitlab/server/GitLabCli"; +import * as GitLabSourceControlProvider from "@t3tools/source-control-gitlab/server/GitLabSourceControlProvider"; +import * as TestSourceControlHost from "@t3tools/source-control-testing/TestSourceControlHost"; + +import * as ServerConfig from "../config.ts"; +import * as GitVcsDriver from "../vcs/GitVcsDriver.ts"; +import * as VcsProcess from "../vcs/VcsProcess.ts"; + +const layerPrTemplateDetectionTest = GitVcsDriver.layer.pipe( + Layer.provide( + ServerConfig.layerTest(process.cwd(), { + prefix: "t3-pr-template-test-", + }), + ), + Layer.provideMerge(VcsProcess.layer), + Layer.provideMerge(NodeServices.layer), +); + +const runGit = (cwd: string, args: ReadonlyArray) => + Effect.gen(function* () { + const git = yield* GitVcsDriver.GitVcsDriver; + return yield* git.execute({ + operation: "PrTemplateDetection.test.runGit", + cwd, + args, + }); + }); + +const runWithTempDirectory = ( + test: (cwd: string) => Effect.Effect, +) => + Effect.scoped( + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-pr-template-" }); + yield* runGit(cwd, ["init", "--initial-branch=main"]); + yield* runGit(cwd, ["config", "user.email", "test@example.com"]); + yield* runGit(cwd, ["config", "user.name", "Test User"]); + return yield* test(cwd); + }), + ).pipe(Effect.provide(layerPrTemplateDetectionTest)); + +const writeTemplate = (cwd: string, relativePath: string, contents: string) => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const templatePath = path.join(cwd, relativePath); + yield* fileSystem.makeDirectory(path.dirname(templatePath), { recursive: true }); + yield* fileSystem.writeFileString(templatePath, contents); + return templatePath; + }); + +const commitTemplates = (cwd: string) => + Effect.gen(function* () { + yield* runGit(cwd, ["add", "-A"]); + yield* runGit(cwd, ["commit", "--allow-empty", "-m", "Add pull request templates"]); + }); + +const makeProvider = Effect.gen(function* () { + const git = yield* GitVcsDriver.GitVcsDriver; + return yield* AzureDevOpsSourceControlProvider.make.pipe( + Effect.provide( + Layer.mergeAll( + Layer.mock(AzureDevOpsCli.AzureDevOpsCli)({}), + TestSourceControlHost.layer({ git: { execute: git.execute } }), + ), + ), + ); +}); + +const readTemplate = (cwd: string, treeish = "main") => + Effect.gen(function* () { + const provider = yield* makeProvider; + assert.isFunction(provider.readChangeRequestTemplate); + return yield* provider.readChangeRequestTemplate!({ cwd, treeish }); + }); + +it.effect( + "reads the Azure default-branch template instead of feature-branch or uncommitted content", + () => + runWithTempDirectory((cwd) => + Effect.gen(function* () { + const templatePath = ".azuredevops/pull_request_template.md"; + yield* writeTemplate(cwd, templatePath, "Default branch template"); + yield* commitTemplates(cwd); + yield* runGit(cwd, ["checkout", "-b", "feature"]); + yield* writeTemplate(cwd, templatePath, "Feature branch template"); + yield* commitTemplates(cwd); + yield* writeTemplate(cwd, templatePath, "Uncommitted template"); + + assert.deepStrictEqual(yield* readTemplate(cwd), Option.some("Default branch template")); + }), + ), +); + +// Microsoft searches these locations in order; extension choice must not override folder precedence. +it.effect.each([ + [".azuredevops/pull_request_template.txt", ".vsts/pull_request_template.md"], + [".vsts/pull_request_template.txt", "docs/pull_request_template.md"], + ["docs/pull_request_template.txt", "pull_request_template.md"], +] as const)("prefers %s over %s", ([preferred, fallback]) => + runWithTempDirectory((cwd) => + Effect.gen(function* () { + yield* writeTemplate(cwd, preferred, "Preferred template"); + yield* writeTemplate(cwd, fallback, "Fallback template"); + yield* commitTemplates(cwd); + + assert.deepStrictEqual(yield* readTemplate(cwd), Option.some("Preferred template")); + }), + ), +); + +it.effect.each([ + ".AzUrEdEvOpS/PuLl_ReQuEsT_TeMpLaTe.MD", + ".VsTs/PULL_REQUEST_TEMPLATE.TxT", + "DoCs/Pull_Request_Template.mD", + "PULL_REQUEST_TEMPLATE.TXT", +])("recognizes case-insensitive Azure template path %s", (templatePath) => + runWithTempDirectory((cwd) => + Effect.gen(function* () { + yield* writeTemplate(cwd, templatePath, "Case-insensitive template"); + yield* commitTemplates(cwd); + + assert.deepStrictEqual(yield* readTemplate(cwd), Option.some("Case-insensitive template")); + }), + ), +); + +it.effect("returns the same absent value as GitLab for a repository without templates", () => + runWithTempDirectory((cwd) => + Effect.gen(function* () { + yield* commitTemplates(cwd); + const git = yield* GitVcsDriver.GitVcsDriver; + const gitlab = yield* GitLabSourceControlProvider.make.pipe( + Effect.provide( + Layer.mergeAll( + Layer.mock(GitLabCli.GitLabCli)({}), + TestSourceControlHost.layer({ git: { execute: git.execute } }), + ), + ), + ); + const absent = yield* gitlab.readChangeRequestTemplate!({ cwd, treeish: "main" }); + assert.deepStrictEqual(absent, Option.none()); + assert.deepStrictEqual(yield* readTemplate(cwd), absent); + }), + ), +); + +it.effect("ignores blank templates", () => + runWithTempDirectory((cwd) => + Effect.gen(function* () { + yield* writeTemplate(cwd, ".azuredevops/pull_request_template.md", " \n\t "); + yield* commitTemplates(cwd); + + assert.deepStrictEqual(yield* readTemplate(cwd), Option.none()); + }), + ), +); + +it.effect("ignores symlinks and nested templates", () => + runWithTempDirectory((cwd) => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const target = yield* writeTemplate(cwd, "template.md", "Not a default template"); + yield* fileSystem.makeDirectory(`${cwd}/.azuredevops`); + yield* fileSystem.symlink(target, `${cwd}/.azuredevops/pull_request_template.md`); + yield* writeTemplate(cwd, "docs/nested/pull_request_template.md", "Nested template"); + yield* commitTemplates(cwd); + + assert.deepStrictEqual(yield* readTemplate(cwd), Option.none()); + }), + ), +); + +it.effect("bounds template content and marks truncation", () => + runWithTempDirectory((cwd) => + Effect.gen(function* () { + yield* writeTemplate(cwd, "pull_request_template.md", "x".repeat(10_000)); + yield* commitTemplates(cwd); + + const template = yield* readTemplate(cwd); + assert.isTrue(Option.isSome(template)); + if (Option.isSome(template)) { + assert.isTrue(template.value.startsWith("x".repeat(8_000))); + assert.isTrue(template.value.endsWith("[truncated]")); + assert.isBelow(template.value.length, 8_100); + } + }), + ), +); diff --git a/packages/source-control-azure-devops/src/server/AzureDevOpsSourceControlProvider.ts b/packages/source-control-azure-devops/src/server/AzureDevOpsSourceControlProvider.ts index da254510c741..d67638ea3179 100644 --- a/packages/source-control-azure-devops/src/server/AzureDevOpsSourceControlProvider.ts +++ b/packages/source-control-azure-devops/src/server/AzureDevOpsSourceControlProvider.ts @@ -2,6 +2,8 @@ import { SourceControlProviderKind } from "@t3tools/contracts"; import * as Effect from "effect/Effect"; import { SourceControlProviderError, type ChangeRequest } from "@t3tools/contracts"; +import * as AzureDevOpsChangeRequestTemplate from "./azureDevOpsChangeRequestTemplate.ts"; +import * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost"; import * as AzureDevOpsCli from "./AzureDevOpsCli.ts"; import type { NormalizedAzureDevOpsPullRequestRecord } from "./azureDevOpsPullRequests.ts"; import * as SourceControlProvider from "@t3tools/source-control-core/server/SourceControlProvider"; @@ -75,9 +77,12 @@ function toChangeRequest(summary: NormalizedAzureDevOpsPullRequestRecord): Chang export const make = Effect.gen(function* () { const azure = yield* AzureDevOpsCli.AzureDevOpsCli; + const { git } = yield* SourceControlHost.SourceControlHost; return SourceControlProvider.SourceControlProvider.of({ kind: SourceControlProviderKind.make("azure-devops"), + readChangeRequestTemplate: ({ cwd, treeish }) => + AzureDevOpsChangeRequestTemplate.detect(cwd, treeish, git.execute), listChangeRequests: (input) => { const source = SourceControlProvider.sourceControlRefFromInput(input); return azure diff --git a/packages/source-control-azure-devops/src/server/azureDevOpsChangeRequestTemplate.ts b/packages/source-control-azure-devops/src/server/azureDevOpsChangeRequestTemplate.ts new file mode 100644 index 000000000000..af56eabe5c42 --- /dev/null +++ b/packages/source-control-azure-devops/src/server/azureDevOpsChangeRequestTemplate.ts @@ -0,0 +1,81 @@ +import * as Effect from "effect/Effect"; +import * as Option from "effect/Option"; + +import type * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost"; + +const TEMPLATE_DIRECTORIES = [".azuredevops", ".vsts", "docs", ""]; +const TEMPLATE_MAX_BYTES = 8_000; +const TREE_LIST_MAX_BYTES = 100_000; +const TRUNCATION_MARKER = "[truncated]"; + +type ExecuteGit = SourceControlHost.SourceControlHost["Service"]["git"]["execute"]; + +export const detect = Effect.fn("azureDevOpsChangeRequestTemplate.detect")( + function* (cwd: string, treeish: string, executeGit: ExecuteGit) { + // Inspect only the root and template directories; never recurse through the repository. + const root = yield* executeGit({ + operation: "azureDevOpsChangeRequestTemplate.listTemplates", + cwd, + args: ["ls-tree", "-z", "--full-tree", treeish], + maxOutputBytes: TREE_LIST_MAX_BYTES, + appendTruncationMarker: true, + }); + if (root.stdoutTruncated) { + return Option.none(); + } + + const rootEntries = root.stdout.split("\0"); + let objectId: string | undefined; + for (const directory of TEMPLATE_DIRECTORIES) { + let entries = rootEntries; + if (directory !== "") { + const trees = rootEntries.flatMap((record) => { + const entry = /^040000 tree ([0-9a-f]{40,64})\t(.+)$/.exec(record); + return entry?.[1] && entry[2]?.toLowerCase() === directory ? [entry[1]] : []; + }); + entries = []; + for (const tree of trees) { + const listing = yield* executeGit({ + operation: "azureDevOpsChangeRequestTemplate.listTemplates", + cwd, + args: ["ls-tree", "-z", "--full-tree", tree], + maxOutputBytes: TREE_LIST_MAX_BYTES, + appendTruncationMarker: true, + }); + if (listing.stdoutTruncated) { + return Option.none(); + } + entries.push(...listing.stdout.split("\0")); + } + } + // Only regular committed blobs qualify, so symlinks never reach the host filesystem. + objectId = entries + .map((record) => /^100(?:644|755) blob ([0-9a-f]{40,64})\t(.+)$/.exec(record)) + .find((entry) => /^pull_request_template\.(?:md|txt)$/i.test(entry?.[2] ?? ""))?.[1]; + if (objectId) { + break; + } + } + if (!objectId) { + return Option.none(); + } + + const blob = yield* executeGit({ + operation: "azureDevOpsChangeRequestTemplate.readTemplate", + cwd, + args: ["cat-file", "blob", objectId], + maxOutputBytes: TEMPLATE_MAX_BYTES, + appendTruncationMarker: true, + }); + const template = blob.stdout.trim(); + if (template.length === 0) { + return Option.none(); + } + return Option.some( + blob.stdoutTruncated && !template.endsWith(TRUNCATION_MARKER) + ? `${template}\n\n${TRUNCATION_MARKER}` + : template, + ); + }, + Effect.orElseSucceed(() => Option.none()), +); From 9e33bf9e5afe006bcc5cfd4b62fed794b9b6dbac Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 21 Oct 2026 12:41:20 +0000 Subject: [PATCH 2/3] fix(azure-devops): read templates from local default branch --- .../GitManager.azureDevOpsTemplate.test.ts | 189 ++++++++++-------- .../azureDevOpsChangeRequestTemplate.test.ts | 23 ++- .../AzureDevOpsSourceControlProvider.ts | 22 +- 3 files changed, 148 insertions(+), 86 deletions(-) diff --git a/apps/server/src/git/GitManager.azureDevOpsTemplate.test.ts b/apps/server/src/git/GitManager.azureDevOpsTemplate.test.ts index 59f8725cc3dc..a89f6934e2a3 100644 --- a/apps/server/src/git/GitManager.azureDevOpsTemplate.test.ts +++ b/apps/server/src/git/GitManager.azureDevOpsTemplate.test.ts @@ -73,90 +73,115 @@ const commitTemplates = (cwd: string) => yield* runGit(cwd, ["commit", "--allow-empty", "-m", "Add pull request templates"]); }); -it.effect("GitManager passes the Azure default-branch template to PR generation when enabled", () => - runWithTempDirectory((cwd) => - Effect.gen(function* () { - yield* runGit(cwd, ["config", "core.autocrlf", "false"]); - const templatePath = ".azuredevops/pull_request_template.md"; - yield* writeTemplate(cwd, templatePath, "Default branch template"); - yield* commitTemplates(cwd); - const fs = yield* FileSystem.FileSystem; - const remote = yield* fs.makeTempDirectoryScoped({ prefix: "t3-azure-template-remote-" }); - yield* runGit(remote, ["init", "--bare", "--initial-branch=main"]); - yield* runGit(cwd, ["remote", "add", "origin", remote]); - yield* runGit(cwd, ["push", "-u", "origin", "main"]); - yield* runGit(cwd, ["checkout", "-b", "feature/azure-template"]); - yield* writeTemplate(cwd, templatePath, "Feature branch template"); - yield* writeTemplate(cwd, "feature.txt", "Feature work"); - yield* commitTemplates(cwd); - yield* runGit(cwd, ["push", "-u", "origin", "feature/azure-template"]); +it.effect.each(["main", "different release template", "missing release template"])( + "uses the Azure default template with %s", + (target) => + runWithTempDirectory((cwd) => + Effect.gen(function* () { + yield* runGit(cwd, ["config", "core.autocrlf", "false"]); + const templatePath = ".azuredevops/pull_request_template.md"; + yield* writeTemplate(cwd, templatePath, "Default branch template"); + yield* commitTemplates(cwd); + const fs = yield* FileSystem.FileSystem; + const remote = yield* fs.makeTempDirectoryScoped({ prefix: "t3-azure-template-remote-" }); + yield* runGit(remote, ["init", "--bare", "--initial-branch=main"]); + yield* runGit(cwd, ["remote", "add", "origin", remote]); + yield* runGit(cwd, ["push", "-u", "origin", "main"]); + yield* runGit(cwd, ["remote", "set-head", "origin", "main"]); + if (target !== "main") { + yield* runGit(cwd, ["checkout", "-b", "release"]); + if (target === "missing release template") { + yield* fs.remove(`${cwd}/${templatePath}`); + } else { + yield* writeTemplate(cwd, templatePath, "Release branch template"); + } + yield* commitTemplates(cwd); + yield* runGit(cwd, ["push", "-u", "origin", "release"]); + } + yield* runGit(cwd, ["checkout", "-b", "feature/azure-template"]); + if (target !== "main") { + yield* runGit(cwd, [ + "config", + "branch.feature/azure-template.t3-merge-base-ref", + "refs/remotes/origin/release", + ]); + } + yield* writeTemplate(cwd, templatePath, "Feature branch template"); + yield* writeTemplate(cwd, "feature.txt", "Feature work"); + yield* commitTemplates(cwd); + yield* runGit(cwd, ["push", "-u", "origin", "feature/azure-template"]); - const git = yield* GitVcsDriver.GitVcsDriver; - let created = false; - const provider = yield* AzureDevOpsSourceControlProvider.make.pipe( - Effect.provide( - Layer.mergeAll( - Layer.mock(AzureDevOpsCli.AzureDevOpsCli)({ - getDefaultBranch: () => Effect.succeed("main"), - listPullRequests: () => Effect.succeed([]), - createPullRequest: ({ bodyFile }) => - Effect.gen(function* () { - assert.strictEqual( - yield* fs.readFileString(bodyFile).pipe(Effect.orDie), - "Generated body", - ); - created = true; - }), - }), - TestSourceControlHost.layer({ git: { execute: git.execute } }), + const git = yield* GitVcsDriver.GitVcsDriver; + let created = false; + const provider = yield* AzureDevOpsSourceControlProvider.make.pipe( + Effect.provide( + Layer.mergeAll( + Layer.mock(AzureDevOpsCli.AzureDevOpsCli)({ + getDefaultBranch: () => Effect.succeed("main"), + listPullRequests: () => Effect.succeed([]), + createPullRequest: ({ bodyFile }) => + Effect.gen(function* () { + assert.strictEqual( + yield* fs.readFileString(bodyFile).pipe(Effect.orDie), + "Generated body", + ); + created = true; + }), + }), + TestSourceControlHost.layer({ + git: { + execute: git.execute, + resolvePrimaryRemoteName: git.resolvePrimaryRemoteName, + }, + }), + ), + ), + ); + const generated: TextGeneration.PrContentGenerationInput[] = []; + const services = Layer.mergeAll( + Layer.mock(TextGeneration.TextGeneration)({ + generatePrContent: (input) => { + generated.push(input); + return Effect.succeed({ title: "Azure template PR", body: "Generated body" }); + }, + }), + Layer.mock(SourceControlProviderRegistry.SourceControlProviderRegistry)({ + resolve: () => Effect.succeed(provider), + resolveHandle: () => Effect.succeed({ provider, context: null }), + get: () => Effect.succeed(provider), + discover: Effect.succeed([]), + resolveLink: () => undefined, + }), + Layer.mock(ProviderRegistry.ProviderRegistry)({ + getProviders: Effect.succeed([]), + }), + Layer.mock(ProjectSetupScriptRunner.ProjectSetupScriptRunner)({ + runForThread: () => Effect.succeed({ status: "no-script" as const }), + }), + ServerSettings.ServerSettingsService.layerTest({ + sourceControlWritingStyle: { followChangeRequestTemplates: true }, + }), + Layer.merge(ProjectionStore.layer, ProjectStore.layer).pipe( + Layer.provide(SqlitePersistence.layerMemory), ), - ), - ); - const generated: TextGeneration.PrContentGenerationInput[] = []; - const services = Layer.mergeAll( - Layer.mock(TextGeneration.TextGeneration)({ - generatePrContent: (input) => { - generated.push(input); - return Effect.succeed({ title: "Azure template PR", body: "Generated body" }); - }, - }), - Layer.mock(SourceControlProviderRegistry.SourceControlProviderRegistry)({ - resolve: () => Effect.succeed(provider), - resolveHandle: () => Effect.succeed({ provider, context: null }), - get: () => Effect.succeed(provider), - discover: Effect.succeed([]), - resolveLink: () => undefined, - }), - Layer.mock(ProviderRegistry.ProviderRegistry)({ - getProviders: Effect.succeed([]), - }), - Layer.mock(ProjectSetupScriptRunner.ProjectSetupScriptRunner)({ - runForThread: () => Effect.succeed({ status: "no-script" as const }), - }), - ServerSettings.ServerSettingsService.layerTest({ - sourceControlWritingStyle: { followChangeRequestTemplates: true }, - }), - Layer.merge(ProjectionStore.layer, ProjectStore.layer).pipe( - Layer.provide(SqlitePersistence.layerMemory), - ), - ); - const manager = yield* GitManager.make.pipe( - Effect.provideService(HostProcess.Environment, {}), - Effect.provide(services), - ); + ); + const manager = yield* GitManager.make.pipe( + Effect.provideService(HostProcess.Environment, {}), + Effect.provide(services), + ); - const result = yield* manager.runStackedAction({ - actionId: "azure-template-test", - cwd, - action: "create_pr", - }); + const result = yield* manager.runStackedAction({ + actionId: "azure-template-test", + cwd, + action: "create_pr", + }); - assert.strictEqual(result.pr.status, "created"); - assert.isTrue(created); - assert.lengthOf(generated, 1); - assert.strictEqual(generated[0]?.baseBranch, "main"); - assert.include(generated[0]?.diffPatch ?? "", "+Feature work"); - assert.strictEqual(generated[0]?.changeRequestTemplate, "Default branch template"); - }), - ), + assert.strictEqual(result.pr.status, "created"); + assert.isTrue(created); + assert.lengthOf(generated, 1); + assert.strictEqual(generated[0]?.baseBranch, target === "main" ? "main" : "release"); + assert.include(generated[0]?.diffPatch ?? "", "+Feature work"); + assert.strictEqual(generated[0]?.changeRequestTemplate, "Default branch template"); + }), + ), ); diff --git a/apps/server/src/sourceControl/azureDevOpsChangeRequestTemplate.test.ts b/apps/server/src/sourceControl/azureDevOpsChangeRequestTemplate.test.ts index 9715f0e4b885..b9918407e072 100644 --- a/apps/server/src/sourceControl/azureDevOpsChangeRequestTemplate.test.ts +++ b/apps/server/src/sourceControl/azureDevOpsChangeRequestTemplate.test.ts @@ -45,6 +45,7 @@ const runWithTempDirectory = ( yield* runGit(cwd, ["init", "--initial-branch=main"]); yield* runGit(cwd, ["config", "user.email", "test@example.com"]); yield* runGit(cwd, ["config", "user.name", "Test User"]); + yield* runGit(cwd, ["remote", "add", "origin", cwd]); return yield* test(cwd); }), ).pipe(Effect.provide(layerPrTemplateDetectionTest)); @@ -63,6 +64,8 @@ const commitTemplates = (cwd: string) => Effect.gen(function* () { yield* runGit(cwd, ["add", "-A"]); yield* runGit(cwd, ["commit", "--allow-empty", "-m", "Add pull request templates"]); + yield* runGit(cwd, ["update-ref", "refs/remotes/origin/main", "main"]); + yield* runGit(cwd, ["symbolic-ref", "refs/remotes/origin/HEAD", "refs/remotes/origin/main"]); }); const makeProvider = Effect.gen(function* () { @@ -71,7 +74,9 @@ const makeProvider = Effect.gen(function* () { Effect.provide( Layer.mergeAll( Layer.mock(AzureDevOpsCli.AzureDevOpsCli)({}), - TestSourceControlHost.layer({ git: { execute: git.execute } }), + TestSourceControlHost.layer({ + git: { execute: git.execute, resolvePrimaryRemoteName: git.resolvePrimaryRemoteName }, + }), ), ), ); @@ -144,7 +149,9 @@ it.effect("returns the same absent value as GitLab for a repository without temp Effect.provide( Layer.mergeAll( Layer.mock(GitLabCli.GitLabCli)({}), - TestSourceControlHost.layer({ git: { execute: git.execute } }), + TestSourceControlHost.layer({ + git: { execute: git.execute, resolvePrimaryRemoteName: git.resolvePrimaryRemoteName }, + }), ), ), ); @@ -197,3 +204,15 @@ it.effect("bounds template content and marks truncation", () => }), ), ); + +it.effect("returns no template when the local remote default branch is unknown", () => + runWithTempDirectory((cwd) => + Effect.gen(function* () { + yield* writeTemplate(cwd, "pull_request_template.md", "Target branch template"); + yield* commitTemplates(cwd); + yield* runGit(cwd, ["symbolic-ref", "--delete", "refs/remotes/origin/HEAD"]); + + assert.deepStrictEqual(yield* readTemplate(cwd), Option.none()); + }), + ), +); diff --git a/packages/source-control-azure-devops/src/server/AzureDevOpsSourceControlProvider.ts b/packages/source-control-azure-devops/src/server/AzureDevOpsSourceControlProvider.ts index d67638ea3179..8b24e6c0639b 100644 --- a/packages/source-control-azure-devops/src/server/AzureDevOpsSourceControlProvider.ts +++ b/packages/source-control-azure-devops/src/server/AzureDevOpsSourceControlProvider.ts @@ -1,5 +1,6 @@ import { SourceControlProviderKind } from "@t3tools/contracts"; import * as Effect from "effect/Effect"; +import * as Option from "effect/Option"; import { SourceControlProviderError, type ChangeRequest } from "@t3tools/contracts"; import * as AzureDevOpsChangeRequestTemplate from "./azureDevOpsChangeRequestTemplate.ts"; @@ -81,8 +82,25 @@ export const make = Effect.gen(function* () { return SourceControlProvider.SourceControlProvider.of({ kind: SourceControlProviderKind.make("azure-devops"), - readChangeRequestTemplate: ({ cwd, treeish }) => - AzureDevOpsChangeRequestTemplate.detect(cwd, treeish, git.execute), + readChangeRequestTemplate: Effect.fn( + "AzureDevOpsSourceControlProvider.readChangeRequestTemplate", + )( + function* ({ cwd }) { + // Azure uses the repository default branch even when the PR targets another branch. + const remoteName = yield* git.resolvePrimaryRemoteName(cwd); + const defaultRef = yield* git.execute({ + operation: "AzureDevOpsSourceControlProvider.resolveDefaultBranch", + cwd, + args: ["symbolic-ref", `refs/remotes/${remoteName}/HEAD`], + }); + return yield* AzureDevOpsChangeRequestTemplate.detect( + cwd, + defaultRef.stdout.trim(), + git.execute, + ); + }, + Effect.orElseSucceed(() => Option.none()), + ), listChangeRequests: (input) => { const source = SourceControlProvider.sourceControlRefFromInput(input); return azure From 5fff2877e7d4c9ec3725b2d2c909eaac51e21461 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 11 Nov 2026 05:50:29 +0000 Subject: [PATCH 3/3] test(azure-devops): commit template symlinks through the index Symlink fixtures written to the filesystem fail on Windows hosts without symlink rights; an index mode commits the same tree entry everywhere. Folds the case-insensitive path rows into the precedence table and asserts the exact truncated template. AI-assisted; verified with the tests and tools listed above. --- ...ureDevOpsSourceControlProvider.git.test.ts | 56 ++++++------------- 1 file changed, 17 insertions(+), 39 deletions(-) diff --git a/packages/source-control-azure-devops/src/server/AzureDevOpsSourceControlProvider.git.test.ts b/packages/source-control-azure-devops/src/server/AzureDevOpsSourceControlProvider.git.test.ts index ed831af6f237..4e4d48faf60f 100644 --- a/packages/source-control-azure-devops/src/server/AzureDevOpsSourceControlProvider.git.test.ts +++ b/packages/source-control-azure-devops/src/server/AzureDevOpsSourceControlProvider.git.test.ts @@ -17,7 +17,6 @@ import * as AzureDevOpsSourceControlProvider from "./AzureDevOpsSourceControlPro const withRepository = ( test: (input: { - readonly cwd: string; readonly git: (...args: string[]) => Effect.Effect; readonly write: (path: string, contents: string) => Effect.Effect; readonly commit: Effect.Effect; @@ -52,7 +51,6 @@ const withRepository = ( Effect.provide(Layer.mock(AzureDevOpsCli.AzureDevOpsCli)({})), ); return yield* test({ - cwd, git, write, commit, @@ -93,11 +91,11 @@ it.effect("reads the default-branch template whatever branch the pull request ta ), ); -// Microsoft searches these locations in order; extension choice must not override folder precedence. +// Microsoft searches these locations in order, ignoring case; extension must not override folder. it.effect.each([ - [".azuredevops/pull_request_template.txt", ".vsts/pull_request_template.md"], - [".vsts/pull_request_template.txt", "docs/pull_request_template.md"], - ["docs/pull_request_template.txt", "pull_request_template.md"], + [".AzureDevOps/PULL_REQUEST_TEMPLATE.txt", ".vsts/pull_request_template.md"], + [".VSTS/pull_request_template.TXT", "docs/pull_request_template.md"], + ["Docs/Pull_Request_Template.txt", "pull_request_template.md"], ] as const)("prefers %s over %s", ([preferred, fallback]) => withRepository(({ git, write, commit, readTemplate }) => Effect.gen(function* () { @@ -111,25 +109,8 @@ it.effect.each([ ), ); -it.effect.each([ - ".AzUrEdEvOpS/PuLl_ReQuEsT_TeMpLaTe.MD", - ".VsTs/PULL_REQUEST_TEMPLATE.TxT", - "DoCs/Pull_Request_Template.mD", - "PULL_REQUEST_TEMPLATE.TXT", -])("recognizes case-insensitive Azure template path %s", (templatePath) => - withRepository(({ git, write, commit, readTemplate }) => - Effect.gen(function* () { - yield* write(templatePath, "Case-insensitive template"); - yield* commit; - yield* publishMain(git); - - assert.deepStrictEqual(yield* readTemplate(), Option.some("Case-insensitive template")); - }), - ), -); - it.effect("ignores missing, blank, symlinked, and nested templates", () => - withRepository(({ cwd, git, write, commit, readTemplate }) => + withRepository(({ git, write, commit, readTemplate }) => Effect.gen(function* () { yield* commit; yield* publishMain(git); @@ -142,15 +123,17 @@ it.effect("ignores missing, blank, symlinked, and nested templates", () => yield* git("rm", "-q", ".azuredevops/pull_request_template.md"); yield* write("template.md", "Not a default template"); - yield* Effect.sync(() => { - NodeFS.mkdirSync(NodePath.join(cwd, ".azuredevops"), { recursive: true }); - NodeFS.symlinkSync( - NodePath.join(cwd, "template.md"), - NodePath.join(cwd, ".azuredevops/pull_request_template.md"), - ); - }); yield* write("docs/nested/pull_request_template.md", "Nested template"); yield* commit; + // An index mode commits a symlink even on hosts without symlink support. + const blob = yield* git("rev-parse", "HEAD:template.md"); + yield* git( + "update-index", + "--add", + "--cacheinfo", + `120000,${blob},.azuredevops/pull_request_template.md`, + ); + yield* git("commit", "-m", "Add a symlinked template"); yield* publishMain(git); assert.deepStrictEqual(yield* readTemplate(), Option.none()); }), @@ -160,17 +143,12 @@ it.effect("ignores missing, blank, symlinked, and nested templates", () => it.effect("bounds template content and marks truncation", () => withRepository(({ git, write, commit, readTemplate }) => Effect.gen(function* () { - yield* write("pull_request_template.md", "x".repeat(10_000)); + const prefix = "x".repeat(8_000); + yield* write("pull_request_template.md", prefix + "EXCLUDED_SUFFIX"); yield* commit; yield* publishMain(git); - const template = yield* readTemplate(); - assert.isTrue(Option.isSome(template)); - if (Option.isSome(template)) { - assert.isTrue(template.value.startsWith("x".repeat(8_000))); - assert.isTrue(template.value.endsWith("[truncated]")); - assert.isBelow(template.value.length, 8_100); - } + assert.deepStrictEqual(yield* readTemplate(), Option.some(prefix + "\n\n[truncated]")); }), ), );