1- import type { GitCommandError } from "@t3tools/contracts" ;
1+ import { type SourceControlProviderKind , type GitCommandError } from "@t3tools/contracts" ;
22import * as Effect from "effect/Effect" ;
33import * as Option from "effect/Option" ;
44
@@ -8,7 +8,7 @@ const TEMPLATE_MAX_BYTES = 8_000;
88const TREE_LIST_MAX_BYTES = 100_000 ;
99const 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
2861type 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