Skip to content

Commit 171a48b

Browse files
fixfonclaude
andcommitted
fix(server): change request templates are followed for GitLab and Azure DevOps
"Follow change request templates" only worked for GitHub because only the GitHub provider implemented readChangeRequestTemplate. Add it for GitLab (.gitlab/merge_request_templates/Default.md) and Azure DevOps (pull_request_template.md|txt in .azuredevops, .vsts, docs, root), and move the safe committed-tree read GitHub used into source-control-core so the three hosts share it. GitHub behavior is unchanged. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
1 parent bbd98a5 commit 171a48b

13 files changed

Lines changed: 585 additions & 93 deletions

File tree

‎apps/server/src/git/GitManager.test.ts‎

Lines changed: 63 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2624,22 +2624,17 @@ it.layer(layerGitManagerTest)("GitManager", (it) => {
26242624
},
26252625
]);
26262626
const calls: SourceControlHost.SourceControlProcessInput[] = [];
2627+
const host = TestSourceControlHost.layer({
2628+
process: {
2629+
run: (input) =>
2630+
Effect.sync(() => {
2631+
calls.push(input);
2632+
return fakeGhOutput(output);
2633+
}),
2634+
},
2635+
});
26272636
const provider = yield* GitLabSourceControlProvider.make.pipe(
2628-
Effect.provide(
2629-
GitLabCli.layer.pipe(
2630-
Layer.provide(
2631-
TestSourceControlHost.layer({
2632-
process: {
2633-
run: (input) =>
2634-
Effect.sync(() => {
2635-
calls.push(input);
2636-
return fakeGhOutput(output);
2637-
}),
2638-
},
2639-
}),
2640-
),
2641-
),
2642-
),
2637+
Effect.provide(Layer.mergeAll(GitLabCli.layer.pipe(Layer.provide(host)), host)),
26432638
);
26442639
const { manager } = yield* makeManager({ sourceControlProvider: provider });
26452640

@@ -5072,6 +5067,59 @@ it.layer(layerGitManagerTest)("GitManager", (it) => {
50725067
}),
50735068
);
50745069

5070+
it.effect("passes a non-GitHub provider's change request template to content generation", () =>
5071+
Effect.gen(function* () {
5072+
const repoDir = yield* makeTempDir("t3code-git-manager-");
5073+
yield* initRepo(repoDir);
5074+
const remoteDir = yield* createBareRemote();
5075+
yield* runGit(repoDir, ["remote", "add", "origin", remoteDir]);
5076+
yield* runGit(repoDir, ["checkout", "-b", "feature-gitlab-template"]);
5077+
NodeFS.writeFileSync(NodePath.join(repoDir, "changes.txt"), "change\n");
5078+
yield* runGit(repoDir, ["add", "changes.txt"]);
5079+
yield* runGit(repoDir, ["commit", "-m", "Feature commit"]);
5080+
yield* runGit(repoDir, ["push", "-u", "origin", "feature-gitlab-template"]);
5081+
yield* runGit(repoDir, ["config", "branch.feature-gitlab-template.gh-merge-base", "main"]);
5082+
let generatedChangeRequestTemplate: string | undefined;
5083+
const templateReads: Array<{ cwd: string; treeish: string }> = [];
5084+
const { service: fakeProvider } = createGitHubProviderWithFakeGh({
5085+
prListSequence: [
5086+
"[]",
5087+
JSON.stringify([
5088+
{
5089+
number: 5,
5090+
title: "Template follows the host",
5091+
url: "https://gitlab.com/group/project/-/merge_requests/5",
5092+
baseRefName: "main",
5093+
headRefName: "feature-gitlab-template",
5094+
},
5095+
]),
5096+
],
5097+
});
5098+
5099+
const { manager } = yield* makeManager({
5100+
sourceControlProvider: {
5101+
...fakeProvider,
5102+
kind: SourceControlProviderKind.make("gitlab"),
5103+
readChangeRequestTemplate: (input) =>
5104+
Effect.sync(() => {
5105+
templateReads.push(input);
5106+
return Option.some("## Merge request checklist");
5107+
}),
5108+
},
5109+
textGeneration: {
5110+
generatePrContent: (input) => {
5111+
generatedChangeRequestTemplate = input.changeRequestTemplate;
5112+
return Effect.succeed({ title: "Template follows the host", body: "Body" });
5113+
},
5114+
},
5115+
});
5116+
yield* runStackedAction(manager, { cwd: repoDir, action: "commit_push_pr" });
5117+
5118+
expect(generatedChangeRequestTemplate).toBe("## Merge request checklist");
5119+
expect(templateReads).toHaveLength(1);
5120+
}),
5121+
);
5122+
50755123
it.effect("generates PR content from branch changes when the remote base advances", () =>
50765124
Effect.gen(function* () {
50775125
const repoDir = yield* makeTempDir("t3code-git-manager-");
Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
import * as NodeServices from "@effect/platform-node/NodeServices";
2+
import { assert, it } from "@effect/vitest";
3+
import * as Effect from "effect/Effect";
4+
import * as FileSystem from "effect/FileSystem";
5+
import * as Layer from "effect/Layer";
6+
import * as Option from "effect/Option";
7+
import * as Path from "effect/Path";
8+
9+
import * as ServerConfig from "../config.ts";
10+
import * as GitVcsDriver from "../vcs/GitVcsDriver.ts";
11+
import * as VcsProcess from "../vcs/VcsProcess.ts";
12+
import * as AzureDevOpsChangeRequestTemplate from "@t3tools/source-control-azure-devops/server/azureDevOpsChangeRequestTemplate";
13+
import * as GitLabChangeRequestTemplate from "@t3tools/source-control-gitlab/server/gitLabChangeRequestTemplate";
14+
15+
const layerTest = GitVcsDriver.layer.pipe(
16+
Layer.provide(
17+
ServerConfig.layerTest(process.cwd(), {
18+
prefix: "t3-change-request-template-test-",
19+
}),
20+
),
21+
Layer.provideMerge(VcsProcess.layer),
22+
Layer.provideMerge(NodeServices.layer),
23+
);
24+
25+
const runGit = (cwd: string, args: ReadonlyArray<string>) =>
26+
Effect.gen(function* () {
27+
const git = yield* GitVcsDriver.GitVcsDriver;
28+
return yield* git.execute({
29+
operation: "changeRequestTemplate.test.runGit",
30+
cwd,
31+
args,
32+
});
33+
});
34+
35+
const runWithTempDirectory = <A, E, R>(
36+
test: (cwd: string) => Effect.Effect<A, E, R | FileSystem.FileSystem | Path.Path>,
37+
) =>
38+
Effect.scoped(
39+
Effect.gen(function* () {
40+
const fileSystem = yield* FileSystem.FileSystem;
41+
const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-change-request-" });
42+
yield* runGit(cwd, ["init", "--initial-branch=main"]);
43+
yield* runGit(cwd, ["config", "user.email", "[email protected]"]);
44+
yield* runGit(cwd, ["config", "user.name", "Test User"]);
45+
return yield* test(cwd);
46+
}),
47+
).pipe(Effect.provide(layerTest));
48+
49+
const writeFile = (cwd: string, relativePath: string, contents: string) =>
50+
Effect.gen(function* () {
51+
const fileSystem = yield* FileSystem.FileSystem;
52+
const path = yield* Path.Path;
53+
const filePath = path.join(cwd, relativePath);
54+
yield* fileSystem.makeDirectory(path.dirname(filePath), { recursive: true });
55+
yield* fileSystem.writeFileString(filePath, contents);
56+
});
57+
58+
const commitAll = (cwd: string) =>
59+
Effect.gen(function* () {
60+
yield* runGit(cwd, ["add", "-A"]);
61+
yield* runGit(cwd, ["commit", "--allow-empty", "-m", "Add templates"]);
62+
});
63+
64+
const detectGitLab = (cwd: string, treeish = "HEAD") =>
65+
Effect.gen(function* () {
66+
const git = yield* GitVcsDriver.GitVcsDriver;
67+
return Option.getOrUndefined(
68+
yield* GitLabChangeRequestTemplate.detect(cwd, treeish, git.execute),
69+
);
70+
});
71+
72+
const detectAzureDevOps = (cwd: string, treeish = "HEAD") =>
73+
Effect.gen(function* () {
74+
const git = yield* GitVcsDriver.GitVcsDriver;
75+
return Option.getOrUndefined(
76+
yield* AzureDevOpsChangeRequestTemplate.detect(cwd, treeish, git.execute),
77+
);
78+
});
79+
80+
const MERGE_REQUEST_TEMPLATES = ".gitlab/merge_request_templates";
81+
82+
it.effect.each(["Default.md", "default.md", "DEFAULT.MD"])("GitLab recognizes %s", (name) =>
83+
runWithTempDirectory((cwd) =>
84+
Effect.gen(function* () {
85+
yield* writeFile(cwd, `${MERGE_REQUEST_TEMPLATES}/${name}`, "## GitLab default\n");
86+
yield* commitAll(cwd);
87+
88+
assert.strictEqual(yield* detectGitLab(cwd), "## GitLab default");
89+
}),
90+
),
91+
);
92+
93+
it.effect("GitLab prefers Default.md over the other templates in the directory", () =>
94+
runWithTempDirectory((cwd) =>
95+
Effect.gen(function* () {
96+
yield* writeFile(cwd, `${MERGE_REQUEST_TEMPLATES}/Bugfix.md`, "bugfix template");
97+
yield* writeFile(cwd, `${MERGE_REQUEST_TEMPLATES}/Default.md`, "default template");
98+
yield* writeFile(cwd, `${MERGE_REQUEST_TEMPLATES}/Feature.md`, "feature template");
99+
yield* commitAll(cwd);
100+
101+
assert.strictEqual(yield* detectGitLab(cwd), "default template");
102+
}),
103+
),
104+
);
105+
106+
it.effect("GitLab does not treat a lone non-default template as the default", () =>
107+
runWithTempDirectory((cwd) =>
108+
Effect.gen(function* () {
109+
yield* writeFile(cwd, `${MERGE_REQUEST_TEMPLATES}/Bugfix.md`, "bugfix template");
110+
// Another host's template location is not GitLab's.
111+
yield* writeFile(cwd, ".github/pull_request_template.md", "github template");
112+
yield* commitAll(cwd);
113+
114+
assert.isUndefined(yield* detectGitLab(cwd));
115+
}),
116+
),
117+
);
118+
119+
it.effect("GitLab reads the template from the requested base tree", () =>
120+
runWithTempDirectory((cwd) =>
121+
Effect.gen(function* () {
122+
yield* writeFile(cwd, "README.md", "initial\n");
123+
yield* commitAll(cwd);
124+
yield* runGit(cwd, ["branch", "feature"]);
125+
yield* writeFile(cwd, `${MERGE_REQUEST_TEMPLATES}/Default.md`, "base template");
126+
yield* commitAll(cwd);
127+
yield* runGit(cwd, ["checkout", "feature"]);
128+
129+
assert.isUndefined(yield* detectGitLab(cwd));
130+
assert.strictEqual(yield* detectGitLab(cwd, "main"), "base template");
131+
}),
132+
),
133+
);
134+
135+
const AZURE_DEVOPS_TEMPLATE_PATHS = [
136+
".azuredevops/pull_request_template.md",
137+
".azuredevops/pull_request_template.txt",
138+
".vsts/pull_request_template.md",
139+
"docs/pull_request_template.md",
140+
"pull_request_template.md",
141+
"pull_request_template.txt",
142+
] as const;
143+
144+
it.effect.each(AZURE_DEVOPS_TEMPLATE_PATHS)("Azure DevOps recognizes %s", (relativePath) =>
145+
runWithTempDirectory((cwd) =>
146+
Effect.gen(function* () {
147+
yield* writeFile(cwd, relativePath, `template from ${relativePath}`);
148+
yield* commitAll(cwd);
149+
150+
assert.strictEqual(yield* detectAzureDevOps(cwd), `template from ${relativePath}`);
151+
}),
152+
),
153+
);
154+
155+
it.effect("Azure DevOps uses the first location in its documented order", () =>
156+
runWithTempDirectory((cwd) =>
157+
Effect.gen(function* () {
158+
yield* writeFile(cwd, "pull_request_template.md", "root template");
159+
yield* writeFile(cwd, "docs/pull_request_template.md", "docs template");
160+
yield* writeFile(cwd, ".vsts/pull_request_template.md", "vsts template");
161+
yield* writeFile(cwd, ".azuredevops/pull_request_template.md", "azuredevops template");
162+
yield* commitAll(cwd);
163+
164+
assert.strictEqual(yield* detectAzureDevOps(cwd), "azuredevops template");
165+
}),
166+
),
167+
);
168+
169+
it.effect("Azure DevOps skips a blank template and falls through to the next location", () =>
170+
runWithTempDirectory((cwd) =>
171+
Effect.gen(function* () {
172+
yield* writeFile(cwd, ".azuredevops/pull_request_template.md", " \n");
173+
yield* writeFile(cwd, "pull_request_template.md", "root template");
174+
yield* commitAll(cwd);
175+
176+
assert.strictEqual(yield* detectAzureDevOps(cwd), "root template");
177+
}),
178+
),
179+
);
180+
181+
it.effect("Azure DevOps ignores GitHub and GitLab template locations", () =>
182+
runWithTempDirectory((cwd) =>
183+
Effect.gen(function* () {
184+
yield* writeFile(cwd, ".github/pull_request_template.md", "github template");
185+
yield* writeFile(cwd, `${MERGE_REQUEST_TEMPLATES}/Default.md`, "gitlab template");
186+
yield* commitAll(cwd);
187+
188+
assert.isUndefined(yield* detectAzureDevOps(cwd));
189+
}),
190+
),
191+
);
192+
193+
it.effect("Azure DevOps reads the template from the requested base tree", () =>
194+
runWithTempDirectory((cwd) =>
195+
Effect.gen(function* () {
196+
yield* writeFile(cwd, "README.md", "initial\n");
197+
yield* commitAll(cwd);
198+
yield* runGit(cwd, ["branch", "feature"]);
199+
yield* writeFile(cwd, ".azuredevops/pull_request_template.md", "base template");
200+
yield* commitAll(cwd);
201+
yield* runGit(cwd, ["checkout", "feature"]);
202+
203+
assert.isUndefined(yield* detectAzureDevOps(cwd));
204+
assert.strictEqual(yield* detectAzureDevOps(cwd, "main"), "base template");
205+
}),
206+
),
207+
);

‎packages/source-control-azure-devops/package.json‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
"types": "./src/client/definition.ts",
88
"import": "./src/client/definition.ts"
99
},
10+
"./server/azureDevOpsChangeRequestTemplate": {
11+
"types": "./src/server/azureDevOpsChangeRequestTemplate.ts",
12+
"import": "./src/server/azureDevOpsChangeRequestTemplate.ts"
13+
},
1014
"./server/AzureDevOpsCli": {
1115
"types": "./src/server/AzureDevOpsCli.ts",
1216
"import": "./src/server/AzureDevOpsCli.ts"

‎packages/source-control-azure-devops/src/server/AzureDevOpsSourceControlProvider.test.ts‎

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,20 @@ import { assert, it } from "@effect/vitest";
33
import * as Effect from "effect/Effect";
44
import * as Layer from "effect/Layer";
55
import * as Option from "effect/Option";
6+
import { ChildProcessSpawner } from "effect/process";
67

8+
import * as TestSourceControlHost from "@t3tools/source-control-testing/TestSourceControlHost";
79
import * as AzureDevOpsCli from "./AzureDevOpsCli.ts";
810
import * as AzureDevOpsSourceControlProvider from "./AzureDevOpsSourceControlProvider.ts";
911

1012
function makeProvider(azure: Partial<AzureDevOpsCli.AzureDevOpsCli["Service"]>) {
1113
return AzureDevOpsSourceControlProvider.make.pipe(
12-
Effect.provide(Layer.mock(AzureDevOpsCli.AzureDevOpsCli)(azure)),
14+
Effect.provide(
15+
Layer.mergeAll(
16+
Layer.mock(AzureDevOpsCli.AzureDevOpsCli)(azure),
17+
TestSourceControlHost.layer(),
18+
),
19+
),
1320
);
1421
}
1522

@@ -120,3 +127,32 @@ it.effect("creates Azure DevOps PRs through provider-neutral input names", () =>
120127
});
121128
}),
122129
);
130+
131+
it.effect("reads the change request template through the host's git", () =>
132+
Effect.gen(function* () {
133+
const gitArgs: Array<ReadonlyArray<string>> = [];
134+
const host = TestSourceControlHost.layer({
135+
git: {
136+
execute: (input) =>
137+
Effect.sync(() => {
138+
gitArgs.push(input.args);
139+
return {
140+
exitCode: ChildProcessSpawner.ExitCode(0),
141+
stdout: "",
142+
stderr: "",
143+
stdoutTruncated: false,
144+
stderrTruncated: false,
145+
};
146+
}),
147+
},
148+
});
149+
const provider = yield* AzureDevOpsSourceControlProvider.make.pipe(
150+
Effect.provide(Layer.mergeAll(Layer.mock(AzureDevOpsCli.AzureDevOpsCli)({}), host)),
151+
);
152+
153+
const template = yield* provider.readChangeRequestTemplate!({ cwd: "/repo", treeish: "main" });
154+
155+
assert.isTrue(Option.isNone(template));
156+
assert.deepStrictEqual(gitArgs[0]?.slice(0, 5), ["ls-tree", "-r", "-z", "--full-tree", "main"]);
157+
}),
158+
);

‎packages/source-control-azure-devops/src/server/AzureDevOpsSourceControlProvider.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import * as Effect from "effect/Effect";
33
import { SourceControlProviderError, type ChangeRequest } from "@t3tools/contracts";
44

55
import * as AzureDevOpsCli from "./AzureDevOpsCli.ts";
6+
import * as AzureDevOpsChangeRequestTemplate from "./azureDevOpsChangeRequestTemplate.ts";
7+
import * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost";
68
import * as SourceControlProvider from "@t3tools/source-control-core/server/SourceControlProvider";
79
import {
810
combinedAuthOutput,
@@ -85,9 +87,12 @@ function toChangeRequest(summary: {
8587

8688
export const make = Effect.gen(function* () {
8789
const azure = yield* AzureDevOpsCli.AzureDevOpsCli;
90+
const { git } = yield* SourceControlHost.SourceControlHost;
8891

8992
return SourceControlProvider.SourceControlProvider.of({
9093
kind: SourceControlProviderKind.make("azure-devops"),
94+
readChangeRequestTemplate: ({ cwd, treeish }) =>
95+
AzureDevOpsChangeRequestTemplate.detect(cwd, treeish, git.execute),
9196
listChangeRequests: (input) => {
9297
const source = SourceControlProvider.sourceControlRefFromInput(input);
9398
return azure

0 commit comments

Comments
 (0)