Skip to content

Commit 85a2679

Browse files
committed
fix(server): detect GitLab and Azure DevOps change request templates
Follow change request templates was gated on GitHub providers and only scanned GitHub-style template paths. Detect the default merge request template for GitLab and the pull request template for Azure DevOps by passing provider kind into template detection.
1 parent 9946541 commit 85a2679

3 files changed

Lines changed: 115 additions & 13 deletions

File tree

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2019,10 +2019,11 @@ export const make = Effect.gen(function* () {
20192019
const baseRangeRef = yield* resolveBaseRangeRef(cwd, baseBranch);
20202020
const rangeContext = yield* gitCore.readRangeContext(cwd, baseRangeRef);
20212021
const policy = yield* resolveStylePolicy(cwd, settings);
2022-
const changeRequestTemplate =
2023-
settings.style.followChangeRequestTemplates && provider.kind === "github"
2024-
? Option.getOrUndefined(yield* detectPrTemplate(cwd, baseRangeRef, gitCore.execute))
2025-
: undefined;
2022+
const changeRequestTemplate = settings.style.followChangeRequestTemplates
2023+
? Option.getOrUndefined(
2024+
yield* detectPrTemplate(cwd, baseRangeRef, gitCore.execute, provider.kind),
2025+
)
2026+
: undefined;
20262027

20272028
const generated = yield* textGeneration.generatePrContent({
20282029
cwd,

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

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as FileSystem from "effect/FileSystem";
55
import * as Layer from "effect/Layer";
66
import * as Option from "effect/Option";
77
import * as Path from "effect/Path";
8+
import type { SourceControlProviderKind } from "@t3tools/contracts";
89

910
import { ServerConfig } from "../config.ts";
1011
import * as GitVcsDriver from "../vcs/GitVcsDriver.ts";
@@ -27,6 +28,11 @@ const TEMPLATE_DIRECTORIES = [
2728
"docs/PULL_REQUEST_TEMPLATE",
2829
] as const;
2930

31+
const GITLAB_TEMPLATE_PATHS = [
32+
".gitlab/merge_request_templates/Default.md",
33+
".gitlab/merge_request_templates/default.md",
34+
] as const;
35+
3036
const PrTemplateDetectionTestLayer = GitVcsDriver.layer.pipe(
3137
Layer.provide(
3238
ServerConfig.layerTest(process.cwd(), {
@@ -77,10 +83,14 @@ const commitTemplates = (cwd: string) =>
7783
yield* runGit(cwd, ["commit", "--allow-empty", "-m", "Add pull request templates"]);
7884
});
7985

80-
const detectTemplate = (cwd: string, treeish = "HEAD") =>
86+
const detectTemplate = (
87+
cwd: string,
88+
treeish = "HEAD",
89+
providerKind: SourceControlProviderKind = "github",
90+
) =>
8191
Effect.gen(function* () {
8292
const git = yield* GitVcsDriver.GitVcsDriver;
83-
return yield* detectPrTemplate(cwd, treeish, git.execute);
93+
return yield* detectPrTemplate(cwd, treeish, git.execute, providerKind);
8494
});
8595

8696
it.effect.each(SINGLE_TEMPLATE_PATHS)("recognizes $0", (relativePath) =>
@@ -140,6 +150,58 @@ it.effect.each(TEMPLATE_DIRECTORIES)("recognizes the $0 directory", (relativeDir
140150
),
141151
);
142152

153+
it.effect.each(GITLAB_TEMPLATE_PATHS)(
154+
"recognizes the GitLab merge request template at $0",
155+
(relativePath) =>
156+
runWithTempDirectory((cwd) =>
157+
Effect.gen(function* () {
158+
yield* writeTemplate(cwd, relativePath, `template from ${relativePath}`);
159+
yield* commitTemplates(cwd);
160+
161+
const template = yield* detectTemplate(cwd, "HEAD", "gitlab");
162+
assert.strictEqual(Option.getOrUndefined(template), `template from ${relativePath}`);
163+
}),
164+
),
165+
);
166+
167+
it.effect("recognizes the Azure DevOps pull request template", () =>
168+
runWithTempDirectory((cwd) =>
169+
Effect.gen(function* () {
170+
const relativePath = ".azuredevops/pull_request_template.md";
171+
yield* writeTemplate(cwd, relativePath, `template from ${relativePath}`);
172+
yield* commitTemplates(cwd);
173+
174+
const template = yield* detectTemplate(cwd, "HEAD", "azure-devops");
175+
assert.strictEqual(Option.getOrUndefined(template), `template from ${relativePath}`);
176+
}),
177+
),
178+
);
179+
180+
it.effect("only detects templates for the active provider kind", () =>
181+
runWithTempDirectory((cwd) =>
182+
Effect.gen(function* () {
183+
yield* writeTemplate(cwd, ".github/pull_request_template.md", "github template");
184+
yield* writeTemplate(cwd, ".gitlab/merge_request_templates/Default.md", "gitlab template");
185+
yield* writeTemplate(cwd, ".azuredevops/pull_request_template.md", "azure template");
186+
yield* commitTemplates(cwd);
187+
188+
assert.strictEqual(
189+
Option.getOrUndefined(yield* detectTemplate(cwd, "HEAD", "github")),
190+
"github template",
191+
);
192+
assert.strictEqual(
193+
Option.getOrUndefined(yield* detectTemplate(cwd, "HEAD", "gitlab")),
194+
"gitlab template",
195+
);
196+
assert.strictEqual(
197+
Option.getOrUndefined(yield* detectTemplate(cwd, "HEAD", "azure-devops")),
198+
"azure template",
199+
);
200+
assert.isTrue(Option.isNone(yield* detectTemplate(cwd, "HEAD", "bitbucket")));
201+
}),
202+
),
203+
);
204+
143205
it.effect.skipIf(!symlinksSupported)(
144206
"skips unusable directory entries and uses the one valid template",
145207
() =>

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

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { GitCommandError } from "@t3tools/contracts";
1+
import { type SourceControlProviderKind, type GitCommandError } from "@t3tools/contracts";
22
import * as Effect from "effect/Effect";
33
import * as Option from "effect/Option";
44

@@ -8,7 +8,7 @@ const TEMPLATE_MAX_BYTES = 8_000;
88
const TREE_LIST_MAX_BYTES = 100_000;
99
const TRUNCATION_MARKER = "[truncated]";
1010

11-
const TEMPLATE_PATHS = [
11+
const GITHUB_TEMPLATE_PATHS = [
1212
".github/pull_request_template.md",
1313
".github/PULL_REQUEST_TEMPLATE.md",
1414
"pull_request_template.md",
@@ -17,13 +17,46 @@ const TEMPLATE_PATHS = [
1717
"docs/PULL_REQUEST_TEMPLATE.md",
1818
] as const;
1919

20-
const TEMPLATE_DIRECTORIES = [
20+
const GITHUB_TEMPLATE_DIRECTORIES = [
2121
".github/PULL_REQUEST_TEMPLATE",
2222
"PULL_REQUEST_TEMPLATE",
2323
"docs/PULL_REQUEST_TEMPLATE",
2424
] as const;
2525

26-
const TREE_PATHS = [...TEMPLATE_PATHS, ...TEMPLATE_DIRECTORIES] as const;
26+
// GitLab treats the default merge request template as "Default.md" (case-insensitive).
27+
// https://docs.gitlab.com/user/project/description_templates/
28+
const GITLAB_TEMPLATE_PATHS = [
29+
".gitlab/merge_request_templates/Default.md",
30+
".gitlab/merge_request_templates/default.md",
31+
] as const;
32+
33+
// Azure Repos applies pull_request_template.md from the repository's .azuredevops folder
34+
// before any other documented location.
35+
// https://learn.microsoft.com/en-us/azure/devops/repos/git/pull-request-templates
36+
const AZURE_DEVOPS_TEMPLATE_PATHS = [".azuredevops/pull_request_template.md"] as const;
37+
38+
interface ChangeRequestTemplatePaths {
39+
readonly paths: ReadonlyArray<string>;
40+
readonly directories: ReadonlyArray<string>;
41+
}
42+
43+
function templatePathsForProvider(
44+
providerKind: SourceControlProviderKind,
45+
): ChangeRequestTemplatePaths | null {
46+
switch (providerKind) {
47+
case "gitlab":
48+
return { paths: [...GITLAB_TEMPLATE_PATHS], directories: [] };
49+
case "azure-devops":
50+
return { paths: [...AZURE_DEVOPS_TEMPLATE_PATHS], directories: [] };
51+
case "github":
52+
case "forgejo":
53+
return { paths: [...GITHUB_TEMPLATE_PATHS], directories: [...GITHUB_TEMPLATE_DIRECTORIES] };
54+
default:
55+
// bitbucket and unknown providers have no repository-file change request template
56+
// convention, matching the historical GitHub-only behavior.
57+
return null;
58+
}
59+
}
2760

2861
type ExecuteGit = GitVcsDriver.GitVcsDriver["Service"]["execute"];
2962

@@ -129,14 +162,20 @@ export const detectPrTemplate = Effect.fn("detectPrTemplate")(function* (
129162
cwd: string,
130163
treeish: string,
131164
executeGit: ExecuteGit,
165+
providerKind: SourceControlProviderKind,
132166
) {
133167
return yield* Effect.gen(function* () {
168+
const templatePaths = templatePathsForProvider(providerKind);
169+
if (templatePaths === null) {
170+
return Option.none();
171+
}
172+
const treePaths = [...templatePaths.paths, ...templatePaths.directories] as const;
134173
// Worktree paths can be replaced between validation and open. Read regular blobs from the
135174
// committed base tree so repository-controlled symlinks and path races never reach the host filesystem.
136175
const result = yield* executeGit({
137176
operation: "PrTemplateDetection.listTemplates",
138177
cwd,
139-
args: ["ls-tree", "-r", "-z", "--full-tree", treeish, "--", ...TREE_PATHS],
178+
args: ["ls-tree", "-r", "-z", "--full-tree", treeish, "--", ...treePaths],
140179
maxOutputBytes: TREE_LIST_MAX_BYTES,
141180
appendTruncationMarker: true,
142181
});
@@ -146,7 +185,7 @@ export const detectPrTemplate = Effect.fn("detectPrTemplate")(function* (
146185

147186
const entries = parseTemplateTreeEntries(result.stdout);
148187
const entriesByPath = new Map(entries.map((entry) => [entry.path, entry]));
149-
for (const templatePath of TEMPLATE_PATHS) {
188+
for (const templatePath of templatePaths.paths) {
150189
const entry = entriesByPath.get(templatePath);
151190
if (!entry) {
152191
continue;
@@ -157,7 +196,7 @@ export const detectPrTemplate = Effect.fn("detectPrTemplate")(function* (
157196
}
158197
}
159198

160-
for (const directory of TEMPLATE_DIRECTORIES) {
199+
for (const directory of templatePaths.directories) {
161200
const directoryTemplate = yield* readTemplateDirectory({
162201
cwd,
163202
executeGit,

0 commit comments

Comments
 (0)