Skip to content

Commit dcf305f

Browse files
committed
fix(github): isolate template directory discovery
1 parent 496e0de commit dcf305f

2 files changed

Lines changed: 59 additions & 28 deletions

File tree

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,25 @@ it.effect("finds case variants from a repository subdirectory", () =>
136136
),
137137
);
138138

139+
it.effect.each([
140+
".github/pull_request_template.md",
141+
".github/PuLl_ReQuEsT_TeMpLaTe.TxT",
142+
"pull_request_template.md",
143+
"docs/pull_request_template.md",
144+
])("preserves $0 when unrelated direct documents exhaust discovery", (relativePath) =>
145+
runWithTempDirectory((cwd) =>
146+
Effect.gen(function* () {
147+
yield* writeTemplate(cwd, relativePath, "GitHub template");
148+
for (let index = 0; index < 600; index += 1) {
149+
yield* writeTemplate(cwd, `docs/${index}-${"a".repeat(180)}.md`, "guide");
150+
}
151+
yield* commitTemplates(cwd);
152+
153+
assert.strictEqual(Option.getOrUndefined(yield* detectTemplate(cwd)), "GitHub template");
154+
}),
155+
),
156+
);
157+
139158
it.effect("does not let unrelated nested documents exhaust the template listing", () =>
140159
runWithTempDirectory((cwd) =>
141160
Effect.gen(function* () {

‎packages/source-control-github/src/server/gitHubChangeRequestTemplate.ts‎

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,7 @@ const TEMPLATE_DIRECTORIES = [
2929
"docs/PULL_REQUEST_TEMPLATE",
3030
] as const;
3131

32-
// Recursive listings can exhaust the byte cap on unrelated documents and hide a valid template.
33-
const TREE_PATHS = [
34-
".",
35-
".github/",
36-
"docs/",
37-
...TEMPLATE_DIRECTORIES.map((directory) => `${directory}/`),
38-
];
32+
const TEMPLATE_LOCATIONS = [".github/", "", "docs/"] as const;
3933

4034
type ExecuteGit = SourceControlHost.SourceControlHost["Service"]["git"]["execute"];
4135

@@ -145,33 +139,51 @@ export const detect = Effect.fn("gitHubChangeRequestTemplate.detect")(function*
145139
return yield* Effect.gen(function* () {
146140
// Worktree paths can be replaced between validation and open. Read regular blobs from the
147141
// committed base tree so repository-controlled symlinks and path races never reach the host filesystem.
148-
const result = yield* executeGit({
149-
operation: "gitHubChangeRequestTemplate.listTemplates",
150-
cwd,
151-
args: ["ls-tree", "-z", "--full-tree", treeish, "--", ...TREE_PATHS],
152-
maxOutputBytes: TREE_LIST_MAX_BYTES,
153-
appendTruncationMarker: true,
154-
});
155-
if (result.stdoutTruncated) {
156-
return Option.none();
157-
}
142+
const listTemplates = (paths: ReadonlyArray<string>) =>
143+
executeGit({
144+
operation: "gitHubChangeRequestTemplate.listTemplates",
145+
cwd,
146+
args: ["ls-tree", "-z", "--full-tree", treeish, "--", ...paths],
147+
maxOutputBytes: TREE_LIST_MAX_BYTES,
148+
appendTruncationMarker: true,
149+
});
158150

159-
const entries = parseTemplateTreeEntries(result.stdout);
160-
for (const templatePath of TEMPLATE_PATHS) {
161-
const candidates = entries
162-
.filter((entry) => entry.path.toLowerCase() === templatePath.toLowerCase())
163-
.toSorted(
164-
(left, right) => Number(right.path === templatePath) - Number(left.path === templatePath),
165-
);
166-
for (const entry of candidates) {
167-
const template = yield* readTemplateBlob({ cwd, executeGit, entry });
168-
if (Option.isSome(template)) {
169-
return template;
151+
// A lower-priority directory must not exhaust an earlier template's listing budget.
152+
for (const location of TEMPLATE_LOCATIONS) {
153+
const templatePaths = TEMPLATE_PATHS.filter(
154+
(path) => path.slice(0, path.lastIndexOf("/") + 1) === location,
155+
);
156+
let result = yield* listTemplates([location || "."]);
157+
if (result.stdoutTruncated) {
158+
result = yield* listTemplates(templatePaths);
159+
}
160+
if (result.stdoutTruncated) {
161+
return Option.none();
162+
}
163+
164+
const entries = parseTemplateTreeEntries(result.stdout);
165+
for (const templatePath of templatePaths) {
166+
const candidates = entries
167+
.filter((entry) => entry.path.toLowerCase() === templatePath.toLowerCase())
168+
.toSorted(
169+
(left, right) =>
170+
Number(right.path === templatePath) - Number(left.path === templatePath),
171+
);
172+
for (const entry of candidates) {
173+
const template = yield* readTemplateBlob({ cwd, executeGit, entry });
174+
if (Option.isSome(template)) {
175+
return template;
176+
}
170177
}
171178
}
172179
}
173180

174181
for (const directory of TEMPLATE_DIRECTORIES) {
182+
const result = yield* listTemplates([`${directory}/`]);
183+
if (result.stdoutTruncated) {
184+
return Option.none();
185+
}
186+
const entries = parseTemplateTreeEntries(result.stdout);
175187
const directoryTemplate = yield* readTemplateDirectory({
176188
cwd,
177189
executeGit,

0 commit comments

Comments
 (0)