Open History to see the last commit
1// @effect-diagnostics nodeBuiltinImport:off
2import * as NodeFS from "node:fs";
3import * as NodeOS from "node:os";
4import * as NodePath from "node:path";
5
6import type { GitCommandError } from "@t3tools/contracts";
7
8import { assert, it } from "@effect/vitest";
9import * as Effect from "effect/Effect";
10import * as Layer from "effect/Layer";
11import * as Option from "effect/Option";
12import * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost";
13import * as TestSourceControlHost from "@t3tools/source-control-testing/TestSourceControlHost";
14
15import * as AzureDevOpsCli from "./AzureDevOpsCli.ts";
16import * as AzureDevOpsSourceControlProvider from "./AzureDevOpsSourceControlProvider.ts";
17
18const withRepository = <A, E>(
19 test: (input: {
20 readonly git: (...args: string[]) => Effect.Effect<string, GitCommandError>;
21 readonly write: (path: string, contents: string) => Effect.Effect<void>;
22 readonly commit: Effect.Effect<void, GitCommandError>;
23 readonly readTemplate: (treeish?: string) => Effect.Effect<Option.Option<string>>;
24 }) => Effect.Effect<A, E>,
25) =>
26 Effect.gen(function* () {
27 const host = yield* SourceControlHost.SourceControlHost;
28 const cwd = yield* Effect.acquireRelease(
29 Effect.sync(() => NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-azure-template-"))),
30 (directory) => Effect.sync(() => NodeFS.rmSync(directory, { recursive: true, force: true })),
31 );
32 const git = (...args: string[]) =>
33 host.git
34 .execute({ operation: "AzureDevOpsTemplate.test", cwd, args })
35 .pipe(Effect.map((result) => result.stdout.trim()));
36 const write = (path: string, contents: string) =>
37 Effect.sync(() => {
38 NodeFS.mkdirSync(NodePath.dirname(NodePath.join(cwd, path)), { recursive: true });
39 NodeFS.writeFileSync(NodePath.join(cwd, path), contents);
40 });
41 const commit = Effect.gen(function* () {
42 yield* git("add", "-A");
43 yield* git("commit", "--allow-empty", "-m", "Add pull request templates");
44 });
45 yield* git("init", "--initial-branch=main");
46 yield* git("config", "user.email", "test@example.com");
47 yield* git("config", "user.name", "Test User");
48 yield* git("config", "core.autocrlf", "false");
49 yield* git("remote", "add", "origin", cwd);
50 const provider = yield* AzureDevOpsSourceControlProvider.make.pipe(
51 Effect.provide(Layer.mock(AzureDevOpsCli.AzureDevOpsCli)({})),
52 );
53 return yield* test({
54 git,
55 write,
56 commit,
57 readTemplate: (treeish = "main") => provider.readChangeRequestTemplate!({ cwd, treeish }),
58 });
59 }).pipe(
60 Effect.scoped,
61 Effect.provide(
62 TestSourceControlHost.layerWithRealGit({
63 git: { resolvePrimaryRemoteName: () => Effect.succeed("origin") },
64 }),
65 ),
66 );
67
68const publishMain = (git: (...args: string[]) => Effect.Effect<string, GitCommandError>) =>
69 Effect.gen(function* () {
70 yield* git("update-ref", "refs/remotes/origin/main", "main");
71 yield* git("symbolic-ref", "refs/remotes/origin/HEAD", "refs/remotes/origin/main");
72 });
73
74it.effect("reads the default-branch template whatever branch the pull request targets", () =>
75 withRepository(({ git, write, commit, readTemplate }) =>
76 Effect.gen(function* () {
77 const templatePath = ".azuredevops/pull_request_template.md";
78 yield* write(templatePath, "Default branch template");
79 yield* commit;
80 yield* publishMain(git);
81 yield* git("checkout", "-b", "release");
82 yield* write(templatePath, "Release branch template");
83 yield* commit;
84 yield* write(templatePath, "Uncommitted template");
85
86 assert.deepStrictEqual(
87 yield* readTemplate("release"),
88 Option.some("Default branch template"),
89 );
90 }),
91 ),
92);
93
94it.effect("reads the template when the default ref ends in a nonbreaking space", () =>
95 withRepository(({ git, write, commit, readTemplate }) =>
96 Effect.gen(function* () {
97 yield* write("pull_request_template.md", "Template from the true default branch");
98 yield* commit;
99 const defaultRef = "refs/remotes/origin/release\u00a0";
100 yield* git("update-ref", defaultRef, "HEAD");
101 yield* git("symbolic-ref", "refs/remotes/origin/HEAD", defaultRef);
102
103 assert.deepStrictEqual(
104 yield* readTemplate("main"),
105 Option.some("Template from the true default branch"),
106 );
107 }),
108 ),
109);
110
111// Microsoft searches these locations in order, ignoring case; extension must not override folder.
112it.effect.each([
113 [".AzureDevOps/PULL_REQUEST_TEMPLATE.txt", ".vsts/pull_request_template.md"],
114 [".VSTS/pull_request_template.TXT", "docs/pull_request_template.md"],
115 ["Docs/Pull_Request_Template.txt", "pull_request_template.md"],
116] as const)("prefers %s over %s", ([preferred, fallback]) =>
117 withRepository(({ git, write, commit, readTemplate }) =>
118 Effect.gen(function* () {
119 yield* write(preferred, "Preferred template");
120 yield* write(fallback, "Fallback template");
121 yield* commit;
122 yield* publishMain(git);
123
124 assert.deepStrictEqual(yield* readTemplate(), Option.some("Preferred template"));
125 }),
126 ),
127);
128
129it.effect("ignores missing, blank, symlinked, and nested templates", () =>
130 withRepository(({ git, write, commit, readTemplate }) =>
131 Effect.gen(function* () {
132 yield* commit;
133 yield* publishMain(git);
134 assert.deepStrictEqual(yield* readTemplate(), Option.none());
135
136 yield* write(".azuredevops/pull_request_template.md", " \n\t ");
137 yield* commit;
138 yield* publishMain(git);
139 assert.deepStrictEqual(yield* readTemplate(), Option.none());
140
141 yield* git("rm", "-q", ".azuredevops/pull_request_template.md");
142 yield* write("template.md", "Not a default template");
143 yield* write("docs/nested/pull_request_template.md", "Nested template");
144 yield* commit;
145 // An index mode commits a symlink even on hosts without symlink support.
146 const blob = yield* git("rev-parse", "HEAD:template.md");
147 yield* git(
148 "update-index",
149 "--add",
150 "--cacheinfo",
151 `120000,${blob},.azuredevops/pull_request_template.md`,
152 );
153 yield* git("commit", "-m", "Add a symlinked template");
154 yield* publishMain(git);
155 assert.deepStrictEqual(yield* readTemplate(), Option.none());
156 }),
157 ),
158);
159
160it.effect("bounds template content and marks truncation", () =>
161 withRepository(({ git, write, commit, readTemplate }) =>
162 Effect.gen(function* () {
163 const prefix = "x".repeat(8_000);
164 yield* write("pull_request_template.md", prefix + "EXCLUDED_SUFFIX");
165 yield* commit;
166 yield* publishMain(git);
167
168 assert.deepStrictEqual(yield* readTemplate(), Option.some(prefix + "\n\n[truncated]"));
169 }),
170 ),
171);
172
173it.effect("returns no template when the local remote default branch is unknown", () =>
174 withRepository(({ write, commit, readTemplate }) =>
175 Effect.gen(function* () {
176 yield* write("pull_request_template.md", "Target branch template");
177 yield* commit;
178
179 assert.deepStrictEqual(yield* readTemplate(), Option.none());
180 }),
181 ),
182);