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