Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as FileSystem from "effect/FileSystem";
5import * as Layer from "effect/Layer";
6import * as Option from "effect/Option";
7import * as Path from "effect/Path";
8
9import { ServerConfig } from "../config.ts";
10import * as GitVcsDriver from "../vcs/GitVcsDriver.ts";
11import * as VcsProcess from "../vcs/VcsProcess.ts";
12import { detectPrTemplate } from "./PrTemplateDetection.ts";
13
14const SINGLE_TEMPLATE_PATHS = [
15 ".github/pull_request_template.md",
16 ".github/PULL_REQUEST_TEMPLATE.md",
17 "pull_request_template.md",
18 "PULL_REQUEST_TEMPLATE.md",
19 "docs/pull_request_template.md",
20 "docs/PULL_REQUEST_TEMPLATE.md",
21] as const;
22
23const TEMPLATE_DIRECTORIES = [
24 ".github/PULL_REQUEST_TEMPLATE",
25 "PULL_REQUEST_TEMPLATE",
26 "docs/PULL_REQUEST_TEMPLATE",
27] as const;
28
29const PrTemplateDetectionTestLayer = GitVcsDriver.layer.pipe(
30 Layer.provide(
31 ServerConfig.layerTest(process.cwd(), {
32 prefix: "t3-pr-template-test-",
33 }),
34 ),
35 Layer.provideMerge(VcsProcess.layer),
36 Layer.provideMerge(NodeServices.layer),
37);
38
39const runGit = (cwd: string, args: ReadonlyArray<string>) =>
40 Effect.gen(function* () {
41 const git = yield* GitVcsDriver.GitVcsDriver;
42 return yield* git.execute({
43 operation: "PrTemplateDetection.test.runGit",
44 cwd,
45 args,
46 });
47 });
48
49const runWithTempDirectory = <A, E, R>(
50 test: (cwd: string) => Effect.Effect<A, E, R | FileSystem.FileSystem | Path.Path>,
51) =>
52 Effect.scoped(
53 Effect.gen(function* () {
54 const fileSystem = yield* FileSystem.FileSystem;
55 const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-pr-template-" });
56 yield* runGit(cwd, ["init", "--initial-branch=main"]);
57 yield* runGit(cwd, ["config", "user.email", "test@example.com"]);
58 yield* runGit(cwd, ["config", "user.name", "Test User"]);
59 return yield* test(cwd);
60 }),
61 ).pipe(Effect.provide(PrTemplateDetectionTestLayer));
62
63const writeTemplate = (cwd: string, relativePath: string, contents: string) =>
64 Effect.gen(function* () {
65 const fileSystem = yield* FileSystem.FileSystem;
66 const path = yield* Path.Path;
67 const templatePath = path.join(cwd, relativePath);
68 yield* fileSystem.makeDirectory(path.dirname(templatePath), { recursive: true });
69 yield* fileSystem.writeFileString(templatePath, contents);
70 return templatePath;
71 });
72
73const commitTemplates = (cwd: string) =>
74 Effect.gen(function* () {
75 yield* runGit(cwd, ["add", "-A"]);
76 yield* runGit(cwd, ["commit", "--allow-empty", "-m", "Add pull request templates"]);
77 });
78
79const detectTemplate = (cwd: string, treeish = "HEAD") =>
80 Effect.gen(function* () {
81 const git = yield* GitVcsDriver.GitVcsDriver;
82 return yield* detectPrTemplate(cwd, treeish, git.execute);
83 });
84
85it.effect.each(SINGLE_TEMPLATE_PATHS)("recognizes $0", (relativePath) =>
86 runWithTempDirectory((cwd) =>
87 Effect.gen(function* () {
88 yield* writeTemplate(cwd, relativePath, `template from ${relativePath}`);
89 yield* commitTemplates(cwd);
90
91 const template = yield* detectTemplate(cwd);
92 assert.strictEqual(Option.getOrUndefined(template), `template from ${relativePath}`);
93 }),
94 ),
95);
96
97it.effect("reads templates from the requested base tree", () =>
98 runWithTempDirectory((cwd) =>
99 Effect.gen(function* () {
100 yield* writeTemplate(cwd, "README.md", "initial\n");
101 yield* commitTemplates(cwd);
102 yield* runGit(cwd, ["branch", "feature"]);
103 yield* writeTemplate(cwd, ".github/pull_request_template.md", "base template");
104 yield* commitTemplates(cwd);
105 yield* runGit(cwd, ["checkout", "feature"]);
106
107 assert.isTrue(Option.isNone(yield* detectTemplate(cwd)));
108 assert.strictEqual(
109 Option.getOrUndefined(yield* detectTemplate(cwd, "main")),
110 "base template",
111 );
112 }),
113 ),
114);
115
116it.effect("uses the first non-empty template in the configured path order", () =>
117 runWithTempDirectory((cwd) =>
118 Effect.gen(function* () {
119 yield* writeTemplate(cwd, ".github/pull_request_template.md", " \n");
120 yield* writeTemplate(cwd, ".github/PULL_REQUEST_TEMPLATE.md", " ## Preferred template \n");
121 yield* writeTemplate(cwd, "pull_request_template.md", "## Later template");
122 yield* commitTemplates(cwd);
123
124 const template = yield* detectTemplate(cwd);
125 assert.strictEqual(Option.getOrUndefined(template), "## Preferred template");
126 }),
127 ),
128);
129
130it.effect.each(TEMPLATE_DIRECTORIES)("recognizes the $0 directory", (relativeDirectory) =>
131 runWithTempDirectory((cwd) =>
132 Effect.gen(function* () {
133 yield* writeTemplate(cwd, `${relativeDirectory}/template.MD`, "directory template");
134 yield* commitTemplates(cwd);
135
136 const template = yield* detectTemplate(cwd);
137 assert.strictEqual(Option.getOrUndefined(template), "directory template");
138 }),
139 ),
140);
141
142it.effect("skips unusable directory entries and uses the one valid template", () =>
143 runWithTempDirectory((cwd) =>
144 Effect.gen(function* () {
145 const fileSystem = yield* FileSystem.FileSystem;
146 const path = yield* Path.Path;
147 const templateDirectory = path.join(cwd, ".github", "PULL_REQUEST_TEMPLATE");
148 yield* fileSystem.makeDirectory(path.join(templateDirectory, "b-directory.md"), {
149 recursive: true,
150 });
151 yield* fileSystem.writeFileString(path.join(templateDirectory, "a-empty.md"), " \n");
152 yield* fileSystem.symlink(
153 path.join(templateDirectory, "missing.md"),
154 path.join(templateDirectory, "c-broken.md"),
155 );
156 yield* fileSystem.writeFileString(path.join(templateDirectory, "z-valid.md"), "valid");
157 yield* commitTemplates(cwd);
158
159 const template = yield* detectTemplate(cwd);
160 assert.strictEqual(Option.getOrUndefined(template), "valid");
161 }),
162 ),
163);
164
165it.effect("does not guess between multiple directory templates", () =>
166 runWithTempDirectory((cwd) =>
167 Effect.gen(function* () {
168 yield* writeTemplate(cwd, ".github/PULL_REQUEST_TEMPLATE/a.md", "first");
169 yield* writeTemplate(cwd, ".github/PULL_REQUEST_TEMPLATE/b.md", "second");
170 yield* writeTemplate(cwd, "PULL_REQUEST_TEMPLATE/fallback.md", "fallback");
171 yield* commitTemplates(cwd);
172
173 const template = yield* detectTemplate(cwd);
174 assert.isTrue(Option.isNone(template));
175 }),
176 ),
177);
178
179it.effect("rejects a committed template symlink escaping the repository", () =>
180 runWithTempDirectory((cwd) =>
181 Effect.gen(function* () {
182 const fileSystem = yield* FileSystem.FileSystem;
183 const path = yield* Path.Path;
184 const outsideDirectory = yield* fileSystem.makeTempDirectoryScoped({
185 prefix: "t3-pr-template-outside-",
186 });
187 const outsideTemplate = path.join(outsideDirectory, "secret.md");
188 yield* fileSystem.writeFileString(outsideTemplate, "LOCAL_SECRET_SENTINEL");
189 const escapedTemplatePath = path.join(cwd, ".github", "pull_request_template.md");
190 yield* fileSystem.makeDirectory(path.dirname(escapedTemplatePath), { recursive: true });
191 yield* fileSystem.symlink(outsideTemplate, escapedTemplatePath);
192 yield* writeTemplate(cwd, "pull_request_template.md", "safe template");
193 yield* commitTemplates(cwd);
194
195 const template = yield* detectTemplate(cwd);
196 assert.strictEqual(Option.getOrUndefined(template), "safe template");
197 assert.notInclude(
198 Option.getOrElse(template, () => ""),
199 "LOCAL_SECRET_SENTINEL",
200 );
201 }),
202 ),
203);
204
205it.effect("reads the committed template when a worktree parent is replaced", () =>
206 runWithTempDirectory((cwd) =>
207 Effect.gen(function* () {
208 const fileSystem = yield* FileSystem.FileSystem;
209 const path = yield* Path.Path;
210 const outsideDirectory = yield* fileSystem.makeTempDirectoryScoped({
211 prefix: "t3-pr-template-outside-",
212 });
213 const templatePath = yield* writeTemplate(
214 cwd,
215 ".github/pull_request_template.md",
216 "committed template",
217 );
218 yield* commitTemplates(cwd);
219 yield* writeTemplate(outsideDirectory, "pull_request_template.md", "LOCAL_SECRET_SENTINEL");
220
221 const templateDirectory = path.dirname(templatePath);
222 yield* fileSystem.rename(templateDirectory, path.join(cwd, ".github-original"));
223 yield* fileSystem.symlink(outsideDirectory, templateDirectory);
224
225 const template = yield* detectTemplate(cwd);
226 assert.strictEqual(Option.getOrUndefined(template), "committed template");
227 assert.notInclude(
228 Option.getOrElse(template, () => ""),
229 "LOCAL_SECRET_SENTINEL",
230 );
231 }),
232 ),
233);
234
235it.effect("bounds template reads and marks truncated content", () =>
236 runWithTempDirectory((cwd) =>
237 Effect.gen(function* () {
238 const prefix = "a".repeat(8_000);
239 yield* writeTemplate(cwd, ".github/pull_request_template.md", `${prefix}SECRET_SENTINEL`);
240 yield* commitTemplates(cwd);
241
242 const template = Option.getOrThrow(yield* detectTemplate(cwd));
243 assert.strictEqual(template, `${prefix}\n\n[truncated]`);
244 assert.lengthOf(template.match(/\[truncated\]/g) ?? [], 1);
245 assert.notInclude(template, "SECRET_SENTINEL");
246 }),
247 ),
248);