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