|
| 1 | +import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 2 | +import { assert, it } from "@effect/vitest"; |
| 3 | +import * as Effect from "effect/Effect"; |
| 4 | +import * as FileSystem from "effect/FileSystem"; |
| 5 | +import * as Layer from "effect/Layer"; |
| 6 | +import * as Option from "effect/Option"; |
| 7 | +import * as Path from "effect/Path"; |
| 8 | + |
| 9 | +import * as ServerConfig from "../config.ts"; |
| 10 | +import * as GitVcsDriver from "../vcs/GitVcsDriver.ts"; |
| 11 | +import * as VcsProcess from "../vcs/VcsProcess.ts"; |
| 12 | +import * as AzureDevOpsChangeRequestTemplate from "@t3tools/source-control-azure-devops/server/azureDevOpsChangeRequestTemplate"; |
| 13 | +import * as GitLabChangeRequestTemplate from "@t3tools/source-control-gitlab/server/gitLabChangeRequestTemplate"; |
| 14 | + |
| 15 | +const layerTest = GitVcsDriver.layer.pipe( |
| 16 | + Layer.provide( |
| 17 | + ServerConfig.layerTest(process.cwd(), { |
| 18 | + prefix: "t3-change-request-template-test-", |
| 19 | + }), |
| 20 | + ), |
| 21 | + Layer.provideMerge(VcsProcess.layer), |
| 22 | + Layer.provideMerge(NodeServices.layer), |
| 23 | +); |
| 24 | + |
| 25 | +const runGit = (cwd: string, args: ReadonlyArray<string>) => |
| 26 | + Effect.gen(function* () { |
| 27 | + const git = yield* GitVcsDriver.GitVcsDriver; |
| 28 | + return yield* git.execute({ |
| 29 | + operation: "changeRequestTemplate.test.runGit", |
| 30 | + cwd, |
| 31 | + args, |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | +const runWithTempDirectory = <A, E, R>( |
| 36 | + test: (cwd: string) => Effect.Effect<A, E, R | FileSystem.FileSystem | Path.Path>, |
| 37 | +) => |
| 38 | + Effect.scoped( |
| 39 | + Effect.gen(function* () { |
| 40 | + const fileSystem = yield* FileSystem.FileSystem; |
| 41 | + const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-change-request-" }); |
| 42 | + yield* runGit(cwd, ["init", "--initial-branch=main"]); |
| 43 | + yield* runGit(cwd, ["config", "user.email", "[email protected]"]); |
| 44 | + yield* runGit(cwd, ["config", "user.name", "Test User"]); |
| 45 | + return yield* test(cwd); |
| 46 | + }), |
| 47 | + ).pipe(Effect.provide(layerTest)); |
| 48 | + |
| 49 | +const writeFile = (cwd: string, relativePath: string, contents: string) => |
| 50 | + Effect.gen(function* () { |
| 51 | + const fileSystem = yield* FileSystem.FileSystem; |
| 52 | + const path = yield* Path.Path; |
| 53 | + const filePath = path.join(cwd, relativePath); |
| 54 | + yield* fileSystem.makeDirectory(path.dirname(filePath), { recursive: true }); |
| 55 | + yield* fileSystem.writeFileString(filePath, contents); |
| 56 | + }); |
| 57 | + |
| 58 | +const commitAll = (cwd: string) => |
| 59 | + Effect.gen(function* () { |
| 60 | + yield* runGit(cwd, ["add", "-A"]); |
| 61 | + yield* runGit(cwd, ["commit", "--allow-empty", "-m", "Add templates"]); |
| 62 | + }); |
| 63 | + |
| 64 | +const detectGitLab = (cwd: string, treeish = "HEAD") => |
| 65 | + Effect.gen(function* () { |
| 66 | + const git = yield* GitVcsDriver.GitVcsDriver; |
| 67 | + return Option.getOrUndefined( |
| 68 | + yield* GitLabChangeRequestTemplate.detect(cwd, treeish, git.execute), |
| 69 | + ); |
| 70 | + }); |
| 71 | + |
| 72 | +const detectAzureDevOps = (cwd: string, treeish = "HEAD") => |
| 73 | + Effect.gen(function* () { |
| 74 | + const git = yield* GitVcsDriver.GitVcsDriver; |
| 75 | + return Option.getOrUndefined( |
| 76 | + yield* AzureDevOpsChangeRequestTemplate.detect(cwd, treeish, git.execute), |
| 77 | + ); |
| 78 | + }); |
| 79 | + |
| 80 | +const MERGE_REQUEST_TEMPLATES = ".gitlab/merge_request_templates"; |
| 81 | + |
| 82 | +it.effect.each(["Default.md", "default.md", "DEFAULT.MD"])("GitLab recognizes %s", (name) => |
| 83 | + runWithTempDirectory((cwd) => |
| 84 | + Effect.gen(function* () { |
| 85 | + yield* writeFile(cwd, `${MERGE_REQUEST_TEMPLATES}/${name}`, "## GitLab default\n"); |
| 86 | + yield* commitAll(cwd); |
| 87 | + |
| 88 | + assert.strictEqual(yield* detectGitLab(cwd), "## GitLab default"); |
| 89 | + }), |
| 90 | + ), |
| 91 | +); |
| 92 | + |
| 93 | +it.effect("GitLab prefers Default.md over the other templates in the directory", () => |
| 94 | + runWithTempDirectory((cwd) => |
| 95 | + Effect.gen(function* () { |
| 96 | + yield* writeFile(cwd, `${MERGE_REQUEST_TEMPLATES}/Bugfix.md`, "bugfix template"); |
| 97 | + yield* writeFile(cwd, `${MERGE_REQUEST_TEMPLATES}/Default.md`, "default template"); |
| 98 | + yield* writeFile(cwd, `${MERGE_REQUEST_TEMPLATES}/Feature.md`, "feature template"); |
| 99 | + yield* commitAll(cwd); |
| 100 | + |
| 101 | + assert.strictEqual(yield* detectGitLab(cwd), "default template"); |
| 102 | + }), |
| 103 | + ), |
| 104 | +); |
| 105 | + |
| 106 | +it.effect("GitLab does not treat a lone non-default template as the default", () => |
| 107 | + runWithTempDirectory((cwd) => |
| 108 | + Effect.gen(function* () { |
| 109 | + yield* writeFile(cwd, `${MERGE_REQUEST_TEMPLATES}/Bugfix.md`, "bugfix template"); |
| 110 | + // Another host's template location is not GitLab's. |
| 111 | + yield* writeFile(cwd, ".github/pull_request_template.md", "github template"); |
| 112 | + yield* commitAll(cwd); |
| 113 | + |
| 114 | + assert.isUndefined(yield* detectGitLab(cwd)); |
| 115 | + }), |
| 116 | + ), |
| 117 | +); |
| 118 | + |
| 119 | +it.effect("GitLab reads the template from the requested base tree", () => |
| 120 | + runWithTempDirectory((cwd) => |
| 121 | + Effect.gen(function* () { |
| 122 | + yield* writeFile(cwd, "README.md", "initial\n"); |
| 123 | + yield* commitAll(cwd); |
| 124 | + yield* runGit(cwd, ["branch", "feature"]); |
| 125 | + yield* writeFile(cwd, `${MERGE_REQUEST_TEMPLATES}/Default.md`, "base template"); |
| 126 | + yield* commitAll(cwd); |
| 127 | + yield* runGit(cwd, ["checkout", "feature"]); |
| 128 | + |
| 129 | + assert.isUndefined(yield* detectGitLab(cwd)); |
| 130 | + assert.strictEqual(yield* detectGitLab(cwd, "main"), "base template"); |
| 131 | + }), |
| 132 | + ), |
| 133 | +); |
| 134 | + |
| 135 | +const AZURE_DEVOPS_TEMPLATE_PATHS = [ |
| 136 | + ".azuredevops/pull_request_template.md", |
| 137 | + ".azuredevops/pull_request_template.txt", |
| 138 | + ".vsts/pull_request_template.md", |
| 139 | + "docs/pull_request_template.md", |
| 140 | + "pull_request_template.md", |
| 141 | + "pull_request_template.txt", |
| 142 | +] as const; |
| 143 | + |
| 144 | +it.effect.each(AZURE_DEVOPS_TEMPLATE_PATHS)("Azure DevOps recognizes %s", (relativePath) => |
| 145 | + runWithTempDirectory((cwd) => |
| 146 | + Effect.gen(function* () { |
| 147 | + yield* writeFile(cwd, relativePath, `template from ${relativePath}`); |
| 148 | + yield* commitAll(cwd); |
| 149 | + |
| 150 | + assert.strictEqual(yield* detectAzureDevOps(cwd), `template from ${relativePath}`); |
| 151 | + }), |
| 152 | + ), |
| 153 | +); |
| 154 | + |
| 155 | +it.effect("Azure DevOps uses the first location in its documented order", () => |
| 156 | + runWithTempDirectory((cwd) => |
| 157 | + Effect.gen(function* () { |
| 158 | + yield* writeFile(cwd, "pull_request_template.md", "root template"); |
| 159 | + yield* writeFile(cwd, "docs/pull_request_template.md", "docs template"); |
| 160 | + yield* writeFile(cwd, ".vsts/pull_request_template.md", "vsts template"); |
| 161 | + yield* writeFile(cwd, ".azuredevops/pull_request_template.md", "azuredevops template"); |
| 162 | + yield* commitAll(cwd); |
| 163 | + |
| 164 | + assert.strictEqual(yield* detectAzureDevOps(cwd), "azuredevops template"); |
| 165 | + }), |
| 166 | + ), |
| 167 | +); |
| 168 | + |
| 169 | +it.effect("Azure DevOps skips a blank template and falls through to the next location", () => |
| 170 | + runWithTempDirectory((cwd) => |
| 171 | + Effect.gen(function* () { |
| 172 | + yield* writeFile(cwd, ".azuredevops/pull_request_template.md", " \n"); |
| 173 | + yield* writeFile(cwd, "pull_request_template.md", "root template"); |
| 174 | + yield* commitAll(cwd); |
| 175 | + |
| 176 | + assert.strictEqual(yield* detectAzureDevOps(cwd), "root template"); |
| 177 | + }), |
| 178 | + ), |
| 179 | +); |
| 180 | + |
| 181 | +it.effect("Azure DevOps ignores GitHub and GitLab template locations", () => |
| 182 | + runWithTempDirectory((cwd) => |
| 183 | + Effect.gen(function* () { |
| 184 | + yield* writeFile(cwd, ".github/pull_request_template.md", "github template"); |
| 185 | + yield* writeFile(cwd, `${MERGE_REQUEST_TEMPLATES}/Default.md`, "gitlab template"); |
| 186 | + yield* commitAll(cwd); |
| 187 | + |
| 188 | + assert.isUndefined(yield* detectAzureDevOps(cwd)); |
| 189 | + }), |
| 190 | + ), |
| 191 | +); |
| 192 | + |
| 193 | +it.effect("Azure DevOps reads the template from the requested base tree", () => |
| 194 | + runWithTempDirectory((cwd) => |
| 195 | + Effect.gen(function* () { |
| 196 | + yield* writeFile(cwd, "README.md", "initial\n"); |
| 197 | + yield* commitAll(cwd); |
| 198 | + yield* runGit(cwd, ["branch", "feature"]); |
| 199 | + yield* writeFile(cwd, ".azuredevops/pull_request_template.md", "base template"); |
| 200 | + yield* commitAll(cwd); |
| 201 | + yield* runGit(cwd, ["checkout", "feature"]); |
| 202 | + |
| 203 | + assert.isUndefined(yield* detectAzureDevOps(cwd)); |
| 204 | + assert.strictEqual(yield* detectAzureDevOps(cwd, "main"), "base template"); |
| 205 | + }), |
| 206 | + ), |
| 207 | +); |
0 commit comments