Repository navigation
fix(server): read Azure DevOps pull request templates #22881
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
juliusmarminge
wants to merge
5
commits into
main
Choose a base branch
from
fix/issue-22871
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+291
−0
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fe23e35
fix(server): read Azure DevOps pull request templates
juliusmarminge 9e33bf9
fix(azure-devops): read templates from local default branch
juliusmarminge eb23179
Merge branch 'main' into fix/issue-22871
juliusmarminge 75dfad3
Merge remote-tracking branch 'origin/main' into HEAD
juliusmarminge de93f2c
Merge remote-tracking branch 'origin/main' into HEAD
juliusmarminge File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
187 changes: 187 additions & 0 deletions
187
packages/source-control-azure-devops/src/server/AzureDevOpsSourceControlProvider.git.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,187 @@ | ||
| // @effect-diagnostics nodeBuiltinImport:off | ||
| import * as NodeFS from "node:fs"; | ||
| import * as NodeOS from "node:os"; | ||
| import * as NodePath from "node:path"; | ||
|
|
||
| import type { GitCommandError } from "@t3tools/contracts"; | ||
|
|
||
| import { assert, it } from "@effect/vitest"; | ||
| import * as Effect from "effect/Effect"; | ||
| import * as Layer from "effect/Layer"; | ||
| import * as Option from "effect/Option"; | ||
| import * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost"; | ||
| import * as TestSourceControlHost from "@t3tools/source-control-testing/TestSourceControlHost"; | ||
|
|
||
| import * as AzureDevOpsCli from "./AzureDevOpsCli.ts"; | ||
| import * as AzureDevOpsSourceControlProvider from "./AzureDevOpsSourceControlProvider.ts"; | ||
|
|
||
| const withRepository = <A, E>( | ||
| test: (input: { | ||
| readonly cwd: string; | ||
| readonly git: (...args: string[]) => Effect.Effect<string, GitCommandError>; | ||
| readonly write: (path: string, contents: string) => Effect.Effect<void>; | ||
| readonly commit: Effect.Effect<void, GitCommandError>; | ||
| readonly readTemplate: (treeish?: string) => Effect.Effect<Option.Option<string>>; | ||
| }) => Effect.Effect<A, E>, | ||
| ) => | ||
| Effect.gen(function* () { | ||
| const host = yield* SourceControlHost.SourceControlHost; | ||
| const cwd = yield* Effect.acquireRelease( | ||
| Effect.sync(() => NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-azure-template-"))), | ||
| (directory) => Effect.sync(() => NodeFS.rmSync(directory, { recursive: true, force: true })), | ||
| ); | ||
| const git = (...args: string[]) => | ||
| host.git | ||
| .execute({ operation: "AzureDevOpsTemplate.test", cwd, args }) | ||
| .pipe(Effect.map((result) => result.stdout.trim())); | ||
| const write = (path: string, contents: string) => | ||
| Effect.sync(() => { | ||
| NodeFS.mkdirSync(NodePath.dirname(NodePath.join(cwd, path)), { recursive: true }); | ||
| NodeFS.writeFileSync(NodePath.join(cwd, path), contents); | ||
| }); | ||
| const commit = Effect.gen(function* () { | ||
| yield* git("add", "-A"); | ||
| yield* git("commit", "--allow-empty", "-m", "Add pull request templates"); | ||
| }); | ||
| yield* git("init", "--initial-branch=main"); | ||
| yield* git("config", "user.email", "test@example.com"); | ||
| yield* git("config", "user.name", "Test User"); | ||
| yield* git("config", "core.autocrlf", "false"); | ||
| yield* git("remote", "add", "origin", cwd); | ||
| const provider = yield* AzureDevOpsSourceControlProvider.make.pipe( | ||
| Effect.provide(Layer.mock(AzureDevOpsCli.AzureDevOpsCli)({})), | ||
| ); | ||
| return yield* test({ | ||
| cwd, | ||
| git, | ||
| write, | ||
| commit, | ||
| readTemplate: (treeish = "main") => provider.readChangeRequestTemplate!({ cwd, treeish }), | ||
| }); | ||
| }).pipe( | ||
| Effect.scoped, | ||
| Effect.provide( | ||
| TestSourceControlHost.layerWithRealGit({ | ||
| git: { resolvePrimaryRemoteName: () => Effect.succeed("origin") }, | ||
| }), | ||
| ), | ||
| ); | ||
|
|
||
| const publishMain = (git: (...args: string[]) => Effect.Effect<string, GitCommandError>) => | ||
| Effect.gen(function* () { | ||
| yield* git("update-ref", "refs/remotes/origin/main", "main"); | ||
| yield* git("symbolic-ref", "refs/remotes/origin/HEAD", "refs/remotes/origin/main"); | ||
| }); | ||
|
|
||
| it.effect("reads the default-branch template whatever branch the pull request targets", () => | ||
| withRepository(({ git, write, commit, readTemplate }) => | ||
| Effect.gen(function* () { | ||
| const templatePath = ".azuredevops/pull_request_template.md"; | ||
| yield* write(templatePath, "Default branch template"); | ||
| yield* commit; | ||
| yield* publishMain(git); | ||
| yield* git("checkout", "-b", "release"); | ||
| yield* write(templatePath, "Release branch template"); | ||
| yield* commit; | ||
| yield* write(templatePath, "Uncommitted template"); | ||
|
|
||
| assert.deepStrictEqual( | ||
| yield* readTemplate("release"), | ||
| Option.some("Default branch template"), | ||
| ); | ||
| }), | ||
| ), | ||
| ); | ||
|
|
||
| // Microsoft searches these locations in order; extension choice must not override folder precedence. | ||
| it.effect.each([ | ||
| [".azuredevops/pull_request_template.txt", ".vsts/pull_request_template.md"], | ||
| [".vsts/pull_request_template.txt", "docs/pull_request_template.md"], | ||
| ["docs/pull_request_template.txt", "pull_request_template.md"], | ||
| ] as const)("prefers %s over %s", ([preferred, fallback]) => | ||
| withRepository(({ git, write, commit, readTemplate }) => | ||
| Effect.gen(function* () { | ||
| yield* write(preferred, "Preferred template"); | ||
| yield* write(fallback, "Fallback template"); | ||
| yield* commit; | ||
| yield* publishMain(git); | ||
|
|
||
| assert.deepStrictEqual(yield* readTemplate(), Option.some("Preferred template")); | ||
| }), | ||
| ), | ||
| ); | ||
|
|
||
| it.effect.each([ | ||
| ".AzUrEdEvOpS/PuLl_ReQuEsT_TeMpLaTe.MD", | ||
| ".VsTs/PULL_REQUEST_TEMPLATE.TxT", | ||
| "DoCs/Pull_Request_Template.mD", | ||
| "PULL_REQUEST_TEMPLATE.TXT", | ||
| ])("recognizes case-insensitive Azure template path %s", (templatePath) => | ||
| withRepository(({ git, write, commit, readTemplate }) => | ||
| Effect.gen(function* () { | ||
| yield* write(templatePath, "Case-insensitive template"); | ||
| yield* commit; | ||
| yield* publishMain(git); | ||
|
|
||
| assert.deepStrictEqual(yield* readTemplate(), Option.some("Case-insensitive template")); | ||
| }), | ||
| ), | ||
| ); | ||
|
|
||
| it.effect("ignores missing, blank, symlinked, and nested templates", () => | ||
| withRepository(({ cwd, git, write, commit, readTemplate }) => | ||
| Effect.gen(function* () { | ||
| yield* commit; | ||
| yield* publishMain(git); | ||
| assert.deepStrictEqual(yield* readTemplate(), Option.none()); | ||
|
|
||
| yield* write(".azuredevops/pull_request_template.md", " \n\t "); | ||
| yield* commit; | ||
| yield* publishMain(git); | ||
| assert.deepStrictEqual(yield* readTemplate(), Option.none()); | ||
|
|
||
| yield* git("rm", "-q", ".azuredevops/pull_request_template.md"); | ||
| yield* write("template.md", "Not a default template"); | ||
| yield* Effect.sync(() => { | ||
| NodeFS.mkdirSync(NodePath.join(cwd, ".azuredevops"), { recursive: true }); | ||
| NodeFS.symlinkSync( | ||
| NodePath.join(cwd, "template.md"), | ||
| NodePath.join(cwd, ".azuredevops/pull_request_template.md"), | ||
| ); | ||
| }); | ||
| yield* write("docs/nested/pull_request_template.md", "Nested template"); | ||
| yield* commit; | ||
| yield* publishMain(git); | ||
| assert.deepStrictEqual(yield* readTemplate(), Option.none()); | ||
| }), | ||
| ), | ||
| ); | ||
|
|
||
| it.effect("bounds template content and marks truncation", () => | ||
| withRepository(({ git, write, commit, readTemplate }) => | ||
| Effect.gen(function* () { | ||
| yield* write("pull_request_template.md", "x".repeat(10_000)); | ||
| yield* commit; | ||
| yield* publishMain(git); | ||
|
|
||
| const template = yield* readTemplate(); | ||
| assert.isTrue(Option.isSome(template)); | ||
| if (Option.isSome(template)) { | ||
| assert.isTrue(template.value.startsWith("x".repeat(8_000))); | ||
| assert.isTrue(template.value.endsWith("[truncated]")); | ||
| assert.isBelow(template.value.length, 8_100); | ||
| } | ||
| }), | ||
| ), | ||
| ); | ||
|
|
||
| it.effect("returns no template when the local remote default branch is unknown", () => | ||
| withRepository(({ write, commit, readTemplate }) => | ||
| Effect.gen(function* () { | ||
| yield* write("pull_request_template.md", "Target branch template"); | ||
| yield* commit; | ||
|
|
||
| assert.deepStrictEqual(yield* readTemplate(), Option.none()); | ||
| }), | ||
| ), | ||
| ); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
packages/source-control-azure-devops/src/server/azureDevOpsChangeRequestTemplate.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| import * as Effect from "effect/Effect"; | ||
| import * as Option from "effect/Option"; | ||
|
|
||
| import type * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost"; | ||
|
|
||
| const TEMPLATE_DIRECTORIES = [".azuredevops", ".vsts", "docs", ""]; | ||
| const TEMPLATE_MAX_BYTES = 8_000; | ||
| const TREE_LIST_MAX_BYTES = 100_000; | ||
| const TRUNCATION_MARKER = "[truncated]"; | ||
|
|
||
| type ExecuteGit = SourceControlHost.SourceControlHost["Service"]["git"]["execute"]; | ||
|
|
||
| export const detect = Effect.fn("azureDevOpsChangeRequestTemplate.detect")( | ||
| function* (cwd: string, treeish: string, executeGit: ExecuteGit) { | ||
| // Inspect only the root and template directories; never recurse through the repository. | ||
| const root = yield* executeGit({ | ||
| operation: "azureDevOpsChangeRequestTemplate.listTemplates", | ||
| cwd, | ||
| args: ["ls-tree", "-z", "--full-tree", treeish], | ||
| maxOutputBytes: TREE_LIST_MAX_BYTES, | ||
| appendTruncationMarker: true, | ||
| }); | ||
| if (root.stdoutTruncated) { | ||
| return Option.none(); | ||
| } | ||
|
|
||
| const rootEntries = root.stdout.split("\0"); | ||
| let objectId: string | undefined; | ||
| for (const directory of TEMPLATE_DIRECTORIES) { | ||
| let entries = rootEntries; | ||
| if (directory !== "") { | ||
| const trees = rootEntries.flatMap((record) => { | ||
| const entry = /^040000 tree ([0-9a-f]{40,64})\t(.+)$/.exec(record); | ||
| return entry?.[1] && entry[2]?.toLowerCase() === directory ? [entry[1]] : []; | ||
| }); | ||
| entries = []; | ||
| for (const tree of trees) { | ||
| const listing = yield* executeGit({ | ||
| operation: "azureDevOpsChangeRequestTemplate.listTemplates", | ||
| cwd, | ||
| args: ["ls-tree", "-z", "--full-tree", tree], | ||
| maxOutputBytes: TREE_LIST_MAX_BYTES, | ||
| appendTruncationMarker: true, | ||
| }); | ||
| if (listing.stdoutTruncated) { | ||
| return Option.none(); | ||
| } | ||
| entries.push(...listing.stdout.split("\0")); | ||
| } | ||
| } | ||
| // Only regular committed blobs qualify, so symlinks never reach the host filesystem. | ||
| objectId = entries | ||
| .map((record) => /^100(?:644|755) blob ([0-9a-f]{40,64})\t(.+)$/.exec(record)) | ||
| .find((entry) => /^pull_request_template\.(?:md|txt)$/i.test(entry?.[2] ?? ""))?.[1]; | ||
| if (objectId) { | ||
| break; | ||
| } | ||
| } | ||
| if (!objectId) { | ||
| return Option.none(); | ||
| } | ||
|
|
||
| const blob = yield* executeGit({ | ||
| operation: "azureDevOpsChangeRequestTemplate.readTemplate", | ||
| cwd, | ||
| args: ["cat-file", "blob", objectId], | ||
| maxOutputBytes: TEMPLATE_MAX_BYTES, | ||
| appendTruncationMarker: true, | ||
| }); | ||
| const template = blob.stdout.trim(); | ||
| if (template.length === 0) { | ||
| return Option.none(); | ||
| } | ||
| return Option.some( | ||
| blob.stdoutTruncated && !template.endsWith(TRUNCATION_MARKER) | ||
| ? `${template}\n\n${TRUNCATION_MARKER}` | ||
| : template, | ||
| ); | ||
| }, | ||
| Effect.orElseSucceed(() => Option.none()), | ||
| ); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Create the symlink in the Git index instead.
If a Windows developer lacks symlink privileges and Developer Mode,
NodeFS.symlinkSynccan fail before this test checks the provider. Create a mode120000entry in the Git index to test the committed symlink without requiring an operating-system symlink. (learn.microsoft.com)