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