refactor(source-control): Azure DevOps lives in @t3tools/source-control-azure-devops (#17592)
Co-authored-by: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
f6e45028ef
commit
d01febb509
30 files changed
+169
-47
No files matched your search
File diff suppressed because it is too large.
Load diff
@@ -1,744 +0,0 @@
|
||||
import * as Context from "effect/Context";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Layer from "effect/Layer";
|
||||
import * as Result from "effect/Result";
|
||||
import * as Schema from "effect/Schema";
|
||||
import type {
|
||||
PullRequestAction,
|
||||
PullRequestComment,
|
||||
PullRequestInvolvement,
|
||||
PullRequestListState,
|
||||
PullRequestMergeMethod,
|
||||
} from "@t3tools/contracts";
|
||||
|
||||
import * as AzureDevOpsCli from "../sourceControl/AzureDevOpsCli.ts";
|
||||
import {
|
||||
decodeItemContentJson,
|
||||
decodeIterationChangesJson,
|
||||
decodeIterationsJson,
|
||||
decodePullRequestJson,
|
||||
decodePullRequestListJson,
|
||||
decodeThreadsJson,
|
||||
decodeViewerJson,
|
||||
type AzureDevOpsChangeEntry,
|
||||
type AzureDevOpsItemContent,
|
||||
type AzureDevOpsIteration,
|
||||
type AzureDevOpsPullRequest,
|
||||
type AzureDevOpsRepositoryLocation,
|
||||
} from "./azureDevOpsPullRequestJson.ts";
|
||||
import type { ProviderListCursor } from "@t3tools/source-control-core/server/PullRequestProvider";
|
||||
|
||||
/**
|
||||
* Names the read that produced unusable output, so a failure reports the call it came from
|
||||
* rather than borrowing another operation's message.
|
||||
*/
|
||||
export class AzureDevOpsPullRequestReadError extends Schema.TaggedError<AzureDevOpsPullRequestReadError>()(
|
||||
"AzureDevOpsPullRequestReadError",
|
||||
{
|
||||
command: Schema.Literal("az"),
|
||||
cwd: Schema.String,
|
||||
operation: Schema.String,
|
||||
cause: Schema.Defect(),
|
||||
},
|
||||
) {
|
||||
get detail(): string {
|
||||
return `Azure CLI returned an unreadable ${this.operation} response.`;
|
||||
}
|
||||
|
||||
override get message(): string {
|
||||
return `Azure CLI failed in ${this.operation}: ${this.detail}`;
|
||||
}
|
||||
}
|
||||
|
||||
/** Not a decode failure: az answered, the account it answered for just has no name. */
|
||||
export class AzureDevOpsViewerUnavailableError extends Schema.TaggedError<AzureDevOpsViewerUnavailableError>()(
|
||||
"AzureDevOpsViewerUnavailableError",
|
||||
{
|
||||
command: Schema.Literal("az"),
|
||||
cwd: Schema.String,
|
||||
},
|
||||
) {
|
||||
get detail(): string {
|
||||
return "Azure CLI returned no account for the current sign-in.";
|
||||
}
|
||||
|
||||
override get message(): string {
|
||||
return `Azure CLI failed in getViewer: ${this.detail}`;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Not a decode failure either: az answered with a well-formed pull request that simply carries
|
||||
* no branch or link, which is a response this cannot place rather than one it cannot read.
|
||||
*/
|
||||
export class AzureDevOpsPullRequestIncompleteError extends Schema.TaggedError<AzureDevOpsPullRequestIncompleteError>()(
|
||||
"AzureDevOpsPullRequestIncompleteError",
|
||||
{
|
||||
command: Schema.Literal("az"),
|
||||
cwd: Schema.String,
|
||||
number: Schema.Int,
|
||||
},
|
||||
) {
|
||||
get detail(): string {
|
||||
return "Azure DevOps returned no branch or link for the pull request.";
|
||||
}
|
||||
|
||||
override get message(): string {
|
||||
return `Azure CLI failed in getPullRequest: ${this.detail}`;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Not a decode failure: the reader named a reviewer `az` would read as a flag of its own. The
|
||||
* reviewers travel as argv rather than in a request body — `az repos pr reviewer` takes them no
|
||||
* other way — so anything that could leave the value position is refused rather than sent.
|
||||
*/
|
||||
export class AzureDevOpsReviewerNameError extends Schema.TaggedError<AzureDevOpsReviewerNameError>()(
|
||||
"AzureDevOpsReviewerNameError",
|
||||
{
|
||||
command: Schema.Literal("az"),
|
||||
cwd: Schema.String,
|
||||
},
|
||||
) {
|
||||
get detail(): string {
|
||||
return "A reviewer is named by an email address or an identity id.";
|
||||
}
|
||||
|
||||
override get message(): string {
|
||||
return `Azure CLI failed in setPullRequestReviewers: ${this.detail}`;
|
||||
}
|
||||
}
|
||||
|
||||
export type AzureDevOpsPullRequestCliError =
|
||||
| AzureDevOpsCli.AzureDevOpsCliError
|
||||
| AzureDevOpsPullRequestReadError
|
||||
| AzureDevOpsPullRequestIncompleteError
|
||||
| AzureDevOpsReviewerNameError
|
||||
| AzureDevOpsViewerUnavailableError;
|
||||
|
||||
/** The version every REST call below is pinned to, so a new default cannot reshape a response. */
|
||||
const REST_API_VERSION = "7.1";
|
||||
const PULL_REQUEST_LIST_MAX_OUTPUT_BYTES = 16 * 1024 * 1024;
|
||||
/**
|
||||
* A full page of change entries is two thousand files, each carrying its path, its url and
|
||||
* several object ids, which is past the megabyte a read is given by default. Output cut at that
|
||||
* ceiling arrives here as JSON that will not parse, so a change large enough to be paged would
|
||||
* report itself as a host returning nonsense rather than as the ordinary page it is.
|
||||
*/
|
||||
const CHANGE_ENTRIES_MAX_OUTPUT_BYTES = 8 * 1024 * 1024;
|
||||
/**
|
||||
* Four times the megabyte of file the other hosts hand over, because Azure has no route that
|
||||
* serves the bytes themselves: the file arrives inside a JSON envelope, escaped if it is text and
|
||||
* base64 if it is not, and both are larger than the file they carry.
|
||||
*/
|
||||
const ITEM_CONTENT_MAX_OUTPUT_BYTES = 4 * 1024 * 1024;
|
||||
/**
|
||||
* What a review's own history is given. Neither route pages, so each answers with the whole of it
|
||||
* at once and grows with how long the review ran rather than with how large the change is. Cut at
|
||||
* the default, both arrive as JSON that stops mid-string, and a long review would report its host
|
||||
* as answering with nonsense.
|
||||
*/
|
||||
const REVIEW_HISTORY_MAX_OUTPUT_BYTES = 8 * 1024 * 1024;
|
||||
|
||||
/** Azure's own ceiling for one page of an iteration's changes. */
|
||||
const CHANGE_ENTRIES_PER_PAGE = 2000;
|
||||
|
||||
/**
|
||||
* Where following the pages stops, counted in the entries Azure was asked to skip rather than in
|
||||
* the files that survived decoding: a page can be entirely folders and other entries a review has
|
||||
* nothing to show for, and bounding on what was kept would follow such a change forever. Every
|
||||
* page is an `az` process of its own, so the read gives up and reports itself incomplete rather
|
||||
* than presenting five pages as the whole change.
|
||||
*/
|
||||
const MAX_CHANGE_ENTRIES = 10_000;
|
||||
|
||||
/** What an iteration changed, and whether following its pages reached the end of it. */
|
||||
export interface AzureDevOpsIterationChanges {
|
||||
readonly changes: ReadonlyArray<AzureDevOpsChangeEntry>;
|
||||
readonly truncated: boolean;
|
||||
}
|
||||
|
||||
export class AzureDevOpsPullRequestCli extends Context.Service<
|
||||
AzureDevOpsPullRequestCli,
|
||||
{
|
||||
readonly getViewer: (input: {
|
||||
readonly cwd: string;
|
||||
}) => Effect.Effect<string, AzureDevOpsPullRequestCliError>;
|
||||
|
||||
readonly listPullRequests: (input: {
|
||||
readonly cwd: string;
|
||||
readonly repository: string;
|
||||
readonly state: PullRequestListState;
|
||||
readonly involvement: PullRequestInvolvement;
|
||||
readonly viewer: string;
|
||||
readonly limit: number;
|
||||
/**
|
||||
* Where to carry on from. Azure has no date filter for a pull request listing, so the only
|
||||
* part of a cursor it can use is how many rows have already been handed over.
|
||||
*/
|
||||
readonly cursor?: ProviderListCursor | undefined;
|
||||
}) => Effect.Effect<
|
||||
{
|
||||
readonly items: ReadonlyArray<AzureDevOpsPullRequest>;
|
||||
readonly truncated: boolean;
|
||||
/** Raw Azure rows consumed to produce this page, including malformed rows. */
|
||||
readonly cursorAdvance: number;
|
||||
},
|
||||
AzureDevOpsPullRequestCliError
|
||||
>;
|
||||
|
||||
readonly getPullRequest: (input: {
|
||||
readonly cwd: string;
|
||||
readonly number: number;
|
||||
}) => Effect.Effect<AzureDevOpsPullRequest, AzureDevOpsPullRequestCliError>;
|
||||
|
||||
/** Threads are not reachable through `az repos pr`, so they come from the REST API. */
|
||||
readonly listThreads: (input: {
|
||||
readonly cwd: string;
|
||||
readonly location: AzureDevOpsRepositoryLocation;
|
||||
readonly number: number;
|
||||
}) => Effect.Effect<ReadonlyArray<PullRequestComment>, AzureDevOpsPullRequestCliError>;
|
||||
|
||||
/**
|
||||
* The pushes a pull request has had, oldest first. Azure hangs the changed files off an
|
||||
* iteration rather than off the pull request, so reading a diff starts here.
|
||||
*/
|
||||
readonly listIterations: (input: {
|
||||
readonly cwd: string;
|
||||
readonly location: AzureDevOpsRepositoryLocation;
|
||||
readonly number: number;
|
||||
}) => Effect.Effect<ReadonlyArray<AzureDevOpsIteration>, AzureDevOpsPullRequestCliError>;
|
||||
|
||||
/**
|
||||
* What one iteration changed, against the merge base rather than against the previous push,
|
||||
* which is the whole of the pull request rather than the latest slice of it.
|
||||
*/
|
||||
readonly listIterationChanges: (input: {
|
||||
readonly cwd: string;
|
||||
readonly location: AzureDevOpsRepositoryLocation;
|
||||
readonly number: number;
|
||||
readonly iterationId: number;
|
||||
}) => Effect.Effect<AzureDevOpsIterationChanges, AzureDevOpsPullRequestCliError>;
|
||||
|
||||
/**
|
||||
* One file's text at one commit. Azure has no diff route that carries content, so both sides
|
||||
* of every changed file are read this way and the patch is made from them here.
|
||||
*/
|
||||
readonly readItemContent: (input: {
|
||||
readonly cwd: string;
|
||||
readonly location: AzureDevOpsRepositoryLocation;
|
||||
readonly path: string;
|
||||
readonly commit: string;
|
||||
}) => Effect.Effect<AzureDevOpsItemContent, AzureDevOpsPullRequestCliError>;
|
||||
|
||||
readonly runPullRequestAction: (input: {
|
||||
readonly cwd: string;
|
||||
readonly number: number;
|
||||
readonly action: PullRequestAction;
|
||||
readonly mergeMethod?: PullRequestMergeMethod;
|
||||
}) => Effect.Effect<void, AzureDevOpsPullRequestCliError>;
|
||||
|
||||
/** Rewrites the pull request's own words, through the same command that moves it. */
|
||||
readonly updatePullRequest: (input: {
|
||||
readonly cwd: string;
|
||||
readonly number: number;
|
||||
readonly title?: string | undefined;
|
||||
readonly body?: string | undefined;
|
||||
}) => Effect.Effect<void, AzureDevOpsPullRequestCliError>;
|
||||
|
||||
/**
|
||||
* Adds reviewers to a pull request, or takes them off it. `az repos pr reviewer` is the whole
|
||||
* of what Azure offers here: it adds and removes named identities, and has no counterpart that
|
||||
* says who could be named.
|
||||
*/
|
||||
readonly setPullRequestReviewers: (input: {
|
||||
readonly cwd: string;
|
||||
readonly number: number;
|
||||
readonly reviewers: ReadonlyArray<string>;
|
||||
readonly requested: boolean;
|
||||
}) => Effect.Effect<void, AzureDevOpsPullRequestCliError>;
|
||||
}
|
||||
>()("t3/pullRequest/AzureDevOpsPullRequestCli") {}
|
||||
|
||||
function statusArgs(state: PullRequestListState): ReadonlyArray<string> {
|
||||
switch (state) {
|
||||
case "open":
|
||||
return ["--status", "active"];
|
||||
case "merged":
|
||||
return ["--status", "completed"];
|
||||
case "closed":
|
||||
return ["--status", "abandoned"];
|
||||
case "all":
|
||||
return ["--status", "all"];
|
||||
}
|
||||
}
|
||||
|
||||
function involvementArgs(input: {
|
||||
readonly involvement: PullRequestInvolvement;
|
||||
readonly viewer: string;
|
||||
}): ReadonlyArray<string> {
|
||||
switch (input.involvement) {
|
||||
case "authored":
|
||||
return ["--creator", input.viewer];
|
||||
case "reviewing":
|
||||
return ["--reviewer", input.viewer];
|
||||
case "all":
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Azure moves a pull request by setting its state rather than by named commands: completing it
|
||||
* is the merge, abandoning it is the close, and reactivating it is the reopen. Squashing is a
|
||||
* completion option rather than a strategy of its own.
|
||||
*/
|
||||
function actionArgs(
|
||||
action: PullRequestAction,
|
||||
mergeMethod: PullRequestMergeMethod | undefined,
|
||||
): ReadonlyArray<string> {
|
||||
switch (action) {
|
||||
case "merge":
|
||||
return ["--status", "completed", "--squash", mergeMethod === "squash" ? "true" : "false"];
|
||||
// Auto-complete is Azure's own name for it: the pull request stays active and Azure completes
|
||||
// it once its policies pass. The squash choice is stored with it, as it is for a merge now.
|
||||
case "enable-auto-merge":
|
||||
return [
|
||||
"--auto-complete",
|
||||
"true",
|
||||
...(mergeMethod === undefined
|
||||
? []
|
||||
: ["--squash", mergeMethod === "squash" ? "true" : "false"]),
|
||||
];
|
||||
case "disable-auto-merge":
|
||||
return ["--auto-complete", "false"];
|
||||
case "ready":
|
||||
return ["--draft", "false"];
|
||||
case "draft":
|
||||
return ["--draft", "true"];
|
||||
case "close":
|
||||
return ["--status", "abandoned"];
|
||||
// Never reached: this host does not declare the action, so nothing offers it.
|
||||
case "update-branch":
|
||||
return [];
|
||||
case "reopen":
|
||||
return ["--status", "active"];
|
||||
// Never reached: this host does not declare the action, so the service refuses it first.
|
||||
case "revert":
|
||||
case "approve-workflows":
|
||||
throw new Error(`Azure DevOps pull request action ${action} is unsupported`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A reviewer Azure could be given: an email address, a display name or an identity guid, and
|
||||
* nothing that starts with a dash. The dash is the whole point — these are argv, and a value that
|
||||
* looks like a flag stops being a value.
|
||||
*/
|
||||
function isReviewerName(value: string): boolean {
|
||||
const name = value.trim();
|
||||
return name.length > 0 && !name.startsWith("-");
|
||||
}
|
||||
|
||||
/** @public Service construction is part of the canonical Effect module API. */
|
||||
export const make = Effect.gen(function* () {
|
||||
const azure = yield* AzureDevOpsCli.AzureDevOpsCli;
|
||||
|
||||
// Every command resolves the organization, project and repository from the checkout, which is
|
||||
// what the rest of the Azure wrapper does. The remote takes three shapes and only `az` knows
|
||||
// how to read all of them.
|
||||
const detectArgs = ["--detect", "true"] as const;
|
||||
|
||||
const executeJson = (input: {
|
||||
readonly cwd: string;
|
||||
readonly args: ReadonlyArray<string>;
|
||||
readonly maxOutputBytes?: number;
|
||||
}) =>
|
||||
azure.execute({
|
||||
cwd: input.cwd,
|
||||
args: [...input.args, "--only-show-errors", "--output", "json"],
|
||||
...(input.maxOutputBytes === undefined ? {} : { maxOutputBytes: input.maxOutputBytes }),
|
||||
});
|
||||
|
||||
/**
|
||||
* A REST route reached through `az devops invoke`, which addresses it by area, resource and
|
||||
* route parameters rather than by URL. Used in place of `az rest` because it signs in the way
|
||||
* the azure-devops extension does: `az rest` mints its own token against the tenant `az`
|
||||
* defaults to, and an organisation in any other tenant answers that with a sign-in page, which
|
||||
* arrives here as unreadable output rather than as a failure.
|
||||
*/
|
||||
const invoke = <A>(input: {
|
||||
readonly cwd: string;
|
||||
readonly operation: string;
|
||||
readonly resource: string;
|
||||
readonly routeParameters: ReadonlyArray<string>;
|
||||
readonly queryParameters?: ReadonlyArray<string>;
|
||||
readonly maxOutputBytes?: number;
|
||||
readonly decode: (raw: string) => Result.Result<A, unknown>;
|
||||
}): Effect.Effect<A, AzureDevOpsPullRequestCliError> =>
|
||||
executeJson({
|
||||
cwd: input.cwd,
|
||||
...(input.maxOutputBytes === undefined ? {} : { maxOutputBytes: input.maxOutputBytes }),
|
||||
args: [
|
||||
"devops",
|
||||
"invoke",
|
||||
...detectArgs,
|
||||
"--area",
|
||||
"git",
|
||||
"--resource",
|
||||
input.resource,
|
||||
"--api-version",
|
||||
REST_API_VERSION,
|
||||
"--route-parameters",
|
||||
...input.routeParameters,
|
||||
...(input.queryParameters === undefined
|
||||
? []
|
||||
: ["--query-parameters", ...input.queryParameters]),
|
||||
],
|
||||
}).pipe(
|
||||
Effect.flatMap((result) => {
|
||||
const decoded = input.decode(result.stdout.trim());
|
||||
return Result.isSuccess(decoded)
|
||||
? Effect.succeed(decoded.success)
|
||||
: Effect.fail(
|
||||
new AzureDevOpsPullRequestReadError({
|
||||
command: "az",
|
||||
cwd: input.cwd,
|
||||
operation: input.operation,
|
||||
cause: decoded.failure,
|
||||
}),
|
||||
);
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* Azure names its own items with a leading slash, which the repository paths carried around
|
||||
* here have had taken off so they match the patch and the viewed mark. Put it back on the way
|
||||
* out, because the items route is documented in Azure's own spelling.
|
||||
*/
|
||||
const toItemPath = (path: string) => (path.startsWith("/") ? path : `/${path}`);
|
||||
|
||||
const repositoryRoute = (location: AzureDevOpsRepositoryLocation): ReadonlyArray<string> => [
|
||||
`project=${location.project}`,
|
||||
`repositoryId=${location.repository}`,
|
||||
];
|
||||
|
||||
const pullRequestRoute = (input: {
|
||||
readonly location: AzureDevOpsRepositoryLocation;
|
||||
readonly number: number;
|
||||
}): ReadonlyArray<string> => [
|
||||
...repositoryRoute(input.location),
|
||||
`pullRequestId=${input.number}`,
|
||||
];
|
||||
|
||||
/**
|
||||
* Azure pages by raw offset. Keep reading when malformed rows leave the decoded page short, and
|
||||
* retain the raw count so the next public cursor skips every row this walk consumed.
|
||||
*/
|
||||
const listPullRequestPage = (input: {
|
||||
readonly cwd: string;
|
||||
readonly repository: string;
|
||||
readonly state: PullRequestListState;
|
||||
readonly involvement: PullRequestInvolvement;
|
||||
readonly viewer: string;
|
||||
readonly limit: number;
|
||||
readonly skip: number;
|
||||
readonly cursorAdvance: number;
|
||||
readonly items: ReadonlyArray<AzureDevOpsPullRequest>;
|
||||
}): Effect.Effect<
|
||||
{
|
||||
readonly items: ReadonlyArray<AzureDevOpsPullRequest>;
|
||||
readonly truncated: boolean;
|
||||
readonly cursorAdvance: number;
|
||||
},
|
||||
AzureDevOpsPullRequestCliError
|
||||
> => {
|
||||
const remaining = input.limit - input.items.length;
|
||||
const top = remaining + 1;
|
||||
return executeJson({
|
||||
cwd: input.cwd,
|
||||
maxOutputBytes: PULL_REQUEST_LIST_MAX_OUTPUT_BYTES,
|
||||
args: [
|
||||
"repos",
|
||||
"pr",
|
||||
"list",
|
||||
...detectArgs,
|
||||
"--repository",
|
||||
input.repository,
|
||||
...statusArgs(input.state),
|
||||
...involvementArgs(input),
|
||||
// A web link per row, which is the only url that needs no assembling.
|
||||
"--include-links",
|
||||
...(input.skip === 0 ? [] : ["--skip", String(input.skip)]),
|
||||
"--top",
|
||||
String(top),
|
||||
],
|
||||
}).pipe(
|
||||
Effect.flatMap((result) => {
|
||||
const raw = result.stdout.trim();
|
||||
if (raw.length === 0) {
|
||||
return Effect.succeed({
|
||||
items: input.items,
|
||||
truncated: false,
|
||||
cursorAdvance: input.cursorAdvance,
|
||||
});
|
||||
}
|
||||
const decoded = decodePullRequestListJson(raw);
|
||||
if (!Result.isSuccess(decoded)) {
|
||||
return Effect.fail(
|
||||
new AzureDevOpsPullRequestReadError({
|
||||
command: "az",
|
||||
cwd: input.cwd,
|
||||
operation: "listPullRequests",
|
||||
cause: decoded.failure,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
const lastItemIndex = decoded.success.rawIndexes[remaining - 1];
|
||||
if (lastItemIndex !== undefined) {
|
||||
const consumed = lastItemIndex + 1;
|
||||
return Effect.succeed({
|
||||
items: [...input.items, ...decoded.success.items.slice(0, remaining)],
|
||||
// A full raw response may have more rows even when malformed entries used the probe.
|
||||
truncated: consumed < decoded.success.rawCount || decoded.success.rawCount === top,
|
||||
cursorAdvance: input.cursorAdvance + consumed,
|
||||
});
|
||||
}
|
||||
|
||||
const items = [...input.items, ...decoded.success.items];
|
||||
if (decoded.success.rawCount < top) {
|
||||
return Effect.succeed({
|
||||
items,
|
||||
truncated: false,
|
||||
cursorAdvance: input.cursorAdvance + decoded.success.rawCount,
|
||||
});
|
||||
}
|
||||
return listPullRequestPage({
|
||||
...input,
|
||||
skip: input.skip + decoded.success.rawCount,
|
||||
cursorAdvance: input.cursorAdvance + decoded.success.rawCount,
|
||||
items,
|
||||
});
|
||||
}),
|
||||
);
|
||||
};
|
||||
|
||||
return AzureDevOpsPullRequestCli.of({
|
||||
getViewer: (input) =>
|
||||
executeJson({ cwd: input.cwd, args: ["account", "show", "--query", "user"] }).pipe(
|
||||
Effect.flatMap((result): Effect.Effect<string, AzureDevOpsPullRequestCliError> => {
|
||||
// `--query user` narrows the payload to the account, so it is nested back under the
|
||||
// key the decoder reads to keep one shape for the signed-in user.
|
||||
const decoded = decodeViewerJson(`{"user":${result.stdout.trim() || "null"}}`);
|
||||
if (!Result.isSuccess(decoded)) {
|
||||
return Effect.fail(
|
||||
new AzureDevOpsPullRequestReadError({
|
||||
command: "az",
|
||||
cwd: input.cwd,
|
||||
operation: "getViewer",
|
||||
cause: decoded.failure,
|
||||
}),
|
||||
);
|
||||
}
|
||||
return decoded.success === null
|
||||
? Effect.fail(new AzureDevOpsViewerUnavailableError({ command: "az", cwd: input.cwd }))
|
||||
: Effect.succeed(decoded.success);
|
||||
}),
|
||||
),
|
||||
|
||||
listPullRequests: (input) =>
|
||||
listPullRequestPage({
|
||||
cwd: input.cwd,
|
||||
repository: input.repository,
|
||||
state: input.state,
|
||||
involvement: input.involvement,
|
||||
viewer: input.viewer,
|
||||
limit: input.limit,
|
||||
// Azure counts rather than filters, so a slice carries on by stepping over every raw row
|
||||
// the prior slice consumed. That is an offset into a list that can shift underneath it:
|
||||
// a pull request opened between two slices moves everything down one, and the row on the
|
||||
// seam is the one that pays for it.
|
||||
skip: input.cursor?.delivered ?? 0,
|
||||
cursorAdvance: 0,
|
||||
items: [],
|
||||
}),
|
||||
|
||||
getPullRequest: (input) =>
|
||||
executeJson({
|
||||
cwd: input.cwd,
|
||||
args: ["repos", "pr", "show", ...detectArgs, "--id", String(input.number)],
|
||||
}).pipe(
|
||||
Effect.flatMap(
|
||||
(result): Effect.Effect<AzureDevOpsPullRequest, AzureDevOpsPullRequestCliError> => {
|
||||
const decoded = decodePullRequestJson(result.stdout.trim());
|
||||
if (!Result.isSuccess(decoded)) {
|
||||
return Effect.fail(
|
||||
new AzureDevOpsPullRequestReadError({
|
||||
command: "az",
|
||||
cwd: input.cwd,
|
||||
operation: "getPullRequest",
|
||||
cause: decoded.failure,
|
||||
}),
|
||||
);
|
||||
}
|
||||
// Null means Azure answered with too little to place the pull request. Nothing
|
||||
// failed underneath it, so it is its own outcome rather than a decode failure.
|
||||
return decoded.success === null
|
||||
? Effect.fail(
|
||||
new AzureDevOpsPullRequestIncompleteError({
|
||||
command: "az",
|
||||
cwd: input.cwd,
|
||||
number: input.number,
|
||||
}),
|
||||
)
|
||||
: Effect.succeed(decoded.success);
|
||||
},
|
||||
),
|
||||
),
|
||||
|
||||
listThreads: (input) =>
|
||||
invoke({
|
||||
cwd: input.cwd,
|
||||
operation: "listThreads",
|
||||
resource: "pullRequestThreads",
|
||||
routeParameters: pullRequestRoute(input),
|
||||
maxOutputBytes: REVIEW_HISTORY_MAX_OUTPUT_BYTES,
|
||||
decode: decodeThreadsJson,
|
||||
}),
|
||||
|
||||
listIterations: (input) =>
|
||||
invoke({
|
||||
cwd: input.cwd,
|
||||
operation: "listIterations",
|
||||
resource: "pullRequestIterations",
|
||||
routeParameters: pullRequestRoute(input),
|
||||
maxOutputBytes: REVIEW_HISTORY_MAX_OUTPUT_BYTES,
|
||||
decode: decodeIterationsJson,
|
||||
}),
|
||||
|
||||
listIterationChanges: (input) => {
|
||||
const page = (skip: number) =>
|
||||
invoke({
|
||||
cwd: input.cwd,
|
||||
operation: "listIterationChanges",
|
||||
resource: "pullRequestIterationChanges",
|
||||
routeParameters: [...pullRequestRoute(input), `iterationId=${input.iterationId}`],
|
||||
// Azure pages this route at 1000 entries by default; this is its own maximum per page,
|
||||
// and it names where the next page starts rather than answering with the whole change.
|
||||
queryParameters: [`$top=${CHANGE_ENTRIES_PER_PAGE}`, `$skip=${skip}`],
|
||||
maxOutputBytes: CHANGE_ENTRIES_MAX_OUTPUT_BYTES,
|
||||
decode: decodeIterationChangesJson,
|
||||
});
|
||||
const from = (
|
||||
skip: number,
|
||||
collected: ReadonlyArray<AzureDevOpsChangeEntry>,
|
||||
): Effect.Effect<AzureDevOpsIterationChanges, AzureDevOpsPullRequestCliError> =>
|
||||
page(skip).pipe(
|
||||
Effect.flatMap((answer) => {
|
||||
const changes = [...collected, ...answer.changes];
|
||||
// The last page names no page after it, and only that is the end of the change.
|
||||
if (answer.nextSkip === null) return Effect.succeed({ changes, truncated: false });
|
||||
// A page pointing at where the read already is would be followed forever, and one
|
||||
// past the ceiling is a change nobody will read to the end of. Both stop the read
|
||||
// and both say so, rather than presenting part of a change as the whole of it.
|
||||
return answer.nextSkip <= skip || answer.nextSkip >= MAX_CHANGE_ENTRIES
|
||||
? Effect.succeed({ changes, truncated: true })
|
||||
: from(answer.nextSkip, changes);
|
||||
}),
|
||||
);
|
||||
return from(0, []);
|
||||
},
|
||||
|
||||
readItemContent: (input) =>
|
||||
invoke({
|
||||
cwd: input.cwd,
|
||||
operation: "readItemContent",
|
||||
resource: "items",
|
||||
routeParameters: repositoryRoute(input.location),
|
||||
queryParameters: [
|
||||
`path=${toItemPath(input.path)}`,
|
||||
"versionDescriptor.versionType=commit",
|
||||
`versionDescriptor.version=${input.commit}`,
|
||||
"includeContent=true",
|
||||
// Azure leaves `contentMetadata` out unless this is asked for, and with it goes its own
|
||||
// word on whether the file is binary, which is the only reliable one, since a binary
|
||||
// file arrives encoded rather than as the bytes it is on the host.
|
||||
"includeContentMetadata=true",
|
||||
// Without this Azure answers with the file's own bytes rather than with a JSON
|
||||
// envelope, and `az devops invoke` refuses anything it cannot parse as JSON.
|
||||
"$format=json",
|
||||
],
|
||||
maxOutputBytes: ITEM_CONTENT_MAX_OUTPUT_BYTES,
|
||||
decode: decodeItemContentJson,
|
||||
}),
|
||||
|
||||
setPullRequestReviewers: (input) =>
|
||||
input.reviewers.some((reviewer) => !isReviewerName(reviewer))
|
||||
? Effect.fail(new AzureDevOpsReviewerNameError({ command: "az", cwd: input.cwd }))
|
||||
: azure
|
||||
.execute({
|
||||
cwd: input.cwd,
|
||||
args: [
|
||||
"repos",
|
||||
"pr",
|
||||
"reviewer",
|
||||
input.requested ? "add" : "remove",
|
||||
...detectArgs,
|
||||
"--id",
|
||||
String(input.number),
|
||||
// One `--reviewers` takes them all, because az reads the flag as a list and a
|
||||
// second one would replace the first rather than add to it.
|
||||
"--reviewers",
|
||||
...input.reviewers,
|
||||
"--only-show-errors",
|
||||
"--output",
|
||||
"json",
|
||||
],
|
||||
})
|
||||
.pipe(Effect.asVoid),
|
||||
|
||||
runPullRequestAction: (input) =>
|
||||
azure
|
||||
.execute({
|
||||
cwd: input.cwd,
|
||||
args: [
|
||||
"repos",
|
||||
"pr",
|
||||
"update",
|
||||
...detectArgs,
|
||||
"--id",
|
||||
String(input.number),
|
||||
...actionArgs(input.action, input.mergeMethod),
|
||||
"--only-show-errors",
|
||||
"--output",
|
||||
"json",
|
||||
],
|
||||
})
|
||||
.pipe(Effect.asVoid),
|
||||
|
||||
updatePullRequest: (input) =>
|
||||
azure
|
||||
.execute({
|
||||
cwd: input.cwd,
|
||||
args: [
|
||||
"repos",
|
||||
"pr",
|
||||
"update",
|
||||
...detectArgs,
|
||||
"--id",
|
||||
String(input.number),
|
||||
// One argument rather than a flag and a value beside it: a description usually opens
|
||||
// with a bullet, and az reads a dash in the next argv slot as a flag of its own.
|
||||
// `--description` also takes several strings, and this keeps the whole text as one.
|
||||
...(input.title === undefined ? [] : [`--title=${input.title}`]),
|
||||
...(input.body === undefined ? [] : [`--description=${input.body}`]),
|
||||
"--only-show-errors",
|
||||
"--output",
|
||||
"json",
|
||||
],
|
||||
})
|
||||
.pipe(Effect.asVoid),
|
||||
});
|
||||
});
|
||||
|
||||
export const layer = Layer.effect(AzureDevOpsPullRequestCli, make);
|
||||
@@ -1,420 +0,0 @@
|
||||
import { describe, expect, it } from "@effect/vitest";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Layer from "effect/Layer";
|
||||
|
||||
import * as AzureDevOpsPullRequestCli from "./AzureDevOpsPullRequestCli.ts";
|
||||
import {
|
||||
LOCATION_CACHE_CAPACITY,
|
||||
make,
|
||||
MAX_DIFF_SPAWNS,
|
||||
} from "./AzureDevOpsPullRequestProvider.ts";
|
||||
import {
|
||||
byteLength,
|
||||
MAX_DIFF_SLICE_BYTES,
|
||||
MAX_DIFF_SLICE_FILES,
|
||||
MAX_FILE_DIFF_EDITS,
|
||||
parseAzureDevOpsDiffCursor,
|
||||
} from "./azureDevOpsDiff.ts";
|
||||
import type { AzureDevOpsChangeEntry } from "./azureDevOpsPullRequestJson.ts";
|
||||
|
||||
const ITERATION = { id: 3, headCommit: "head", mergeBaseCommit: "base" };
|
||||
|
||||
const PULL_REQUEST = {
|
||||
number: 7,
|
||||
title: "Pull request 7",
|
||||
url: "https://dev.azure.com/acme/web/_git/web/pullrequest/7",
|
||||
author: null,
|
||||
headBranch: "feat/page",
|
||||
baseBranch: "main",
|
||||
state: "open" as const,
|
||||
isDraft: false,
|
||||
mergeability: "mergeable" as const,
|
||||
createdAt: "2026-07-01T00:00:00Z",
|
||||
updatedAt: "2026-07-02T00:00:00Z",
|
||||
closedAt: null,
|
||||
body: "",
|
||||
reviewRequestLogins: [],
|
||||
reviewers: [],
|
||||
location: { project: "acme", repository: "web" },
|
||||
autoMergeEnabled: false,
|
||||
};
|
||||
|
||||
function change(
|
||||
path: string,
|
||||
changeKind: AzureDevOpsChangeEntry["changeKind"] = "change",
|
||||
): AzureDevOpsChangeEntry {
|
||||
return { path, oldPath: path, changeKind, objectId: "8f80", originalObjectId: "0ca4" };
|
||||
}
|
||||
|
||||
/**
|
||||
* A file whose two sides share no line, so its patch is `lines` removals and `lines` additions of
|
||||
* `width` characters each: the diff work and the patch bytes one file costs are both dialled from
|
||||
* here, and they are what a slice is bounded by. Each line carries its own number and a prefix
|
||||
* as well, so `width` is a floor on how long a line is rather than its byte count.
|
||||
*/
|
||||
function side(prefix: string, lines: number, width: number): string {
|
||||
const pad = "z".repeat(width);
|
||||
return `${Array.from({ length: lines }, (_, line) => `${prefix} ${line} ${pad}`).join("\n")}\n`;
|
||||
}
|
||||
|
||||
const readSlice = (input: {
|
||||
readonly paths: ReadonlyArray<string>;
|
||||
readonly lines: number;
|
||||
readonly width: number;
|
||||
/** Paths the host refuses, which is one file's problem rather than the read's. */
|
||||
readonly refused?: ReadonlyArray<string>;
|
||||
/** Paths the change creates, so the host has nothing to hand back for their old side. */
|
||||
readonly created?: ReadonlyArray<string>;
|
||||
readonly cursor?: string;
|
||||
}) =>
|
||||
Effect.gen(function* () {
|
||||
const reads: string[] = [];
|
||||
const refused = new Set(input.refused ?? []);
|
||||
const created = new Set(input.created ?? []);
|
||||
let inFlight = 0;
|
||||
let peakInFlight = 0;
|
||||
|
||||
const provider = yield* make.pipe(
|
||||
Effect.provide(
|
||||
Layer.mock(AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli)({
|
||||
getPullRequest: () => Effect.succeed(PULL_REQUEST),
|
||||
listIterations: () => Effect.succeed([ITERATION]),
|
||||
listIterationChanges: () =>
|
||||
Effect.succeed({
|
||||
changes: input.paths.map((path) =>
|
||||
change(path, created.has(path) ? "new" : "change"),
|
||||
),
|
||||
truncated: false,
|
||||
}),
|
||||
readItemContent: (item) =>
|
||||
Effect.gen(function* () {
|
||||
reads.push(item.path);
|
||||
inFlight += 1;
|
||||
peakInFlight = Math.max(peakInFlight, inFlight);
|
||||
// Every read suspends before it answers, as a subprocess would, so what runs at
|
||||
// once is the scheduler's answer rather than an artefact of resolving inline. The
|
||||
// later a file is listed the sooner it answers, to leave the assembled patch
|
||||
// nothing but the change list to take its order from.
|
||||
const answersAfter = input.paths.length - input.paths.indexOf(item.path);
|
||||
for (let turn = 0; turn < answersAfter; turn += 1) yield* Effect.yieldNow;
|
||||
inFlight -= 1;
|
||||
if (refused.has(item.path)) {
|
||||
return yield* new AzureDevOpsPullRequestCli.AzureDevOpsPullRequestReadError({
|
||||
command: "az",
|
||||
cwd: "/w",
|
||||
operation: "readItemContent",
|
||||
cause: "refused",
|
||||
});
|
||||
}
|
||||
const isOldSide = item.commit !== ITERATION.headCommit;
|
||||
if (isOldSide && created.has(item.path)) return { contents: "", isBinary: false };
|
||||
return {
|
||||
contents: side(isOldSide ? "old" : "new", input.lines, input.width),
|
||||
isBinary: false,
|
||||
};
|
||||
}),
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
const slice = yield* provider.getDiff({
|
||||
cwd: "/w",
|
||||
repository: "acme/web",
|
||||
host: "dev.azure.com",
|
||||
number: 7,
|
||||
...(input.cursor === undefined ? {} : { cursor: input.cursor }),
|
||||
});
|
||||
return { slice, reads, peakInFlight };
|
||||
});
|
||||
|
||||
/** Which files the patch carries a section for, in the order it carries them. */
|
||||
function patchedPaths(patch: string): ReadonlyArray<string> {
|
||||
return [...patch.matchAll(/^diff --git a\/(?<path>\S+) b\//gmu)].map(
|
||||
(match) => match.groups?.path ?? "",
|
||||
);
|
||||
}
|
||||
|
||||
describe("getChangeRequestSummary", () => {
|
||||
it.effect("costs the one pull request read, not the iterations changedFiles needs", () =>
|
||||
Effect.gen(function* () {
|
||||
let pullRequestReads = 0;
|
||||
|
||||
const provider = yield* make.pipe(
|
||||
Effect.provide(
|
||||
// listIterations and listIterationChanges are left unimplemented here, so a summary
|
||||
// that reached for either would die with UnimplementedError instead of this passing.
|
||||
Layer.mock(AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli)({
|
||||
getPullRequest: () => {
|
||||
pullRequestReads += 1;
|
||||
return Effect.succeed(PULL_REQUEST);
|
||||
},
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
const readSummary = provider.getChangeRequestSummary;
|
||||
if (readSummary === undefined) return yield* Effect.die("summary read was not implemented");
|
||||
const summary = yield* readSummary({
|
||||
cwd: "/w",
|
||||
repository: "acme/web",
|
||||
host: "dev.azure.com",
|
||||
number: 7,
|
||||
});
|
||||
|
||||
expect(pullRequestReads).toBe(1);
|
||||
expect(summary.title).toBe(PULL_REQUEST.title);
|
||||
expect(summary.changedFiles).toBeUndefined();
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
describe("getChangeRequest", () => {
|
||||
it.effect("still reports the file count the detail panel needs", () =>
|
||||
Effect.gen(function* () {
|
||||
const provider = yield* make.pipe(
|
||||
Effect.provide(
|
||||
Layer.mock(AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli)({
|
||||
getPullRequest: () => Effect.succeed(PULL_REQUEST),
|
||||
listIterations: () => Effect.succeed([ITERATION]),
|
||||
listIterationChanges: () =>
|
||||
Effect.succeed({ changes: [change("a.ts"), change("b.ts")], truncated: false }),
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
const detail = yield* provider.getChangeRequest({
|
||||
cwd: "/w",
|
||||
repository: "acme/web",
|
||||
host: "dev.azure.com",
|
||||
number: 7,
|
||||
});
|
||||
|
||||
expect(detail.changedFiles).toBe(2);
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
describe("getDiff reads", () => {
|
||||
it.effect("holds every reader together to one request's worth of processes", () =>
|
||||
Effect.gen(function* () {
|
||||
// The fan-out inside a read bounds one Code tab. Two people opening two Azure reviews at
|
||||
// once are two reads, so without a ceiling above them both they are twice a request's
|
||||
// processes, each paying a Python interpreter's start-up on the same machine.
|
||||
const paths = ["a.ts", "b.ts", "c.ts", "d.ts", "e.ts", "f.ts"];
|
||||
let inFlight = 0;
|
||||
let peakInFlight = 0;
|
||||
|
||||
const provider = yield* make.pipe(
|
||||
Effect.provide(
|
||||
Layer.mock(AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli)({
|
||||
getPullRequest: () => Effect.succeed(PULL_REQUEST),
|
||||
listIterations: () => Effect.succeed([ITERATION]),
|
||||
listIterationChanges: () =>
|
||||
Effect.succeed({ changes: paths.map((path) => change(path)), truncated: false }),
|
||||
readItemContent: () =>
|
||||
Effect.gen(function* () {
|
||||
inFlight += 1;
|
||||
peakInFlight = Math.max(peakInFlight, inFlight);
|
||||
// Suspends before answering, as a subprocess would, so what is out at once is the
|
||||
// scheduler's answer rather than an artefact of resolving inline.
|
||||
yield* Effect.yieldNow;
|
||||
yield* Effect.yieldNow;
|
||||
inFlight -= 1;
|
||||
return { contents: side("new", 2, 4), isBinary: false };
|
||||
}),
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
const readDiff = (number: number) =>
|
||||
provider.getDiff({ cwd: "/w", repository: "acme/web", host: "dev.azure.com", number });
|
||||
|
||||
yield* Effect.all([readDiff(7), readDiff(8)], { concurrency: 2 });
|
||||
|
||||
expect(peakInFlight).toBeLessThanOrEqual(MAX_DIFF_SPAWNS);
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("leaves a run of files it could not diff at all for the next slice", () =>
|
||||
Effect.gen(function* () {
|
||||
// A binary, oversize, purely renamed or unreadable entry is a header apiece, a couple of
|
||||
// hundred bytes with no edits in it, so a change made of them spends neither budget: the
|
||||
// byte one would take well over a thousand of them, and the edit one never fills at all.
|
||||
// A listing holds up to ten thousand entries and each still costs its two reads, which is
|
||||
// what a file count is here to bound.
|
||||
const paths = Array.from({ length: MAX_DIFF_SLICE_FILES + 20 }, (_, at) => `gen/a${at}.bin`);
|
||||
const read = yield* readSlice({ paths, lines: 2, width: 4, refused: paths });
|
||||
|
||||
expect(patchedPaths(read.slice.patch)).toHaveLength(MAX_DIFF_SLICE_FILES);
|
||||
// Well inside the byte budget, so the file count is what stopped it rather than either of
|
||||
// the budgets that were already there.
|
||||
expect(byteLength(read.slice.patch)).toBeLessThan(MAX_DIFF_SLICE_BYTES);
|
||||
expect(parseAzureDevOpsDiffCursor(read.slice.nextCursor)?.fileIndex).toBe(
|
||||
MAX_DIFF_SLICE_FILES,
|
||||
);
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("asks for both sides of several files at once rather than one side at a time", () =>
|
||||
Effect.gen(function* () {
|
||||
// Each file is two `az` invocations, each paying a Python interpreter's start-up, so a
|
||||
// slice read one side after another is most of what the Code tab waits for.
|
||||
const read = yield* readSlice({
|
||||
paths: ["a.ts", "b.ts", "c.ts", "d.ts"],
|
||||
lines: 2,
|
||||
width: 4,
|
||||
});
|
||||
|
||||
expect(read.peakInFlight).toBeGreaterThan(2);
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("holds the number of files it reads at once down", () =>
|
||||
Effect.gen(function* () {
|
||||
// The host throttles, and `az` is a process on the same machine the reader runs agents on,
|
||||
// so a long change is read in batches rather than all at once.
|
||||
const paths = Array.from({ length: 24 }, (_, file) => `file-${file}.ts`);
|
||||
const read = yield* readSlice({ paths, lines: 2, width: 4 });
|
||||
|
||||
expect(read.reads).toHaveLength(paths.length * 2);
|
||||
expect(read.peakInFlight).toBeLessThanOrEqual(8);
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("keeps the patch in the order the change was listed, whoever answered first", () =>
|
||||
Effect.gen(function* () {
|
||||
const paths = ["a.ts", "b.ts", "c.ts", "d.ts", "e.ts"];
|
||||
const read = yield* readSlice({ paths, lines: 2, width: 4 });
|
||||
|
||||
expect(patchedPaths(read.slice.patch)).toEqual(paths);
|
||||
expect(read.slice.nextCursor).toBeNull();
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("leaves a file the host refused listed without its hunks, in its place", () =>
|
||||
Effect.gen(function* () {
|
||||
const paths = ["a.ts", "b.ts", "c.ts"];
|
||||
const read = yield* readSlice({ paths, lines: 2, width: 4, refused: ["b.ts"] });
|
||||
|
||||
expect(patchedPaths(read.slice.patch)).toEqual(paths);
|
||||
expect(read.slice.truncated).toBe(true);
|
||||
// Its section ends at its header, and the files around it still carry their hunks.
|
||||
expect(read.slice.patch).toContain("+++ b/b.ts\ndiff --git a/c.ts");
|
||||
expect(read.slice.patch.match(/^@@ /gmu)).toHaveLength(2);
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
describe("what one diff slice spends", () => {
|
||||
it.effect("stops on the byte ceiling without carrying what it read past it", () =>
|
||||
Effect.gen(function* () {
|
||||
// Two of these fill the slice, and the batch they were read in reached two files further.
|
||||
// Those two belong to the next slice: carrying them would put the request past a ceiling
|
||||
// that is there to bound what one answer weighs.
|
||||
const paths = ["a.ts", "b.ts", "c.ts", "d.ts", "e.ts", "f.ts"];
|
||||
const read = yield* readSlice({ paths, lines: 100, width: 900 });
|
||||
|
||||
expect(read.slice.patch.length).toBeGreaterThan(MAX_DIFF_SLICE_BYTES);
|
||||
expect(patchedPaths(read.slice.patch)).toEqual(["a.ts", "b.ts"]);
|
||||
expect(read.slice.nextCursor).toBe(`${ITERATION.id}:2`);
|
||||
expect(new Set(read.reads)).toEqual(new Set(["a.ts", "b.ts", "c.ts", "d.ts"]));
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("narrows what it reads at once as the slice fills", () =>
|
||||
Effect.gen(function* () {
|
||||
// Four files fit inside the budget and the fifth is past half of what is left of it, so
|
||||
// reading four more would throw most of them away and read them again next slice. Every
|
||||
// file is two `az` invocations, so the batch is judged against what the files before it
|
||||
// weighed rather than left at its full width to the last file.
|
||||
const paths = ["a.ts", "b.ts", "c.ts", "d.ts", "e.ts", "f.ts", "g.ts", "h.ts"];
|
||||
const read = yield* readSlice({ paths, lines: 50, width: 450 });
|
||||
|
||||
expect(read.slice.nextCursor).not.toBeNull();
|
||||
expect(new Set(read.reads)).toEqual(new Set(patchedPaths(read.slice.patch)));
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("stops once the diff work one request may do is spent", () =>
|
||||
Effect.gen(function* () {
|
||||
// Short lines are cheap on the wire and dear to diff, so the byte ceiling alone would let
|
||||
// one request hold the thread through a dozen of them. Each of these is half of what one
|
||||
// file is allowed, and the slice ends while there is still room for another.
|
||||
const paths = Array.from({ length: 12 }, (_, file) => `file-${file}.ts`);
|
||||
const read = yield* readSlice({ paths, lines: MAX_FILE_DIFF_EDITS / 4, width: 1 });
|
||||
|
||||
expect(read.slice.patch.length).toBeLessThan(MAX_DIFF_SLICE_BYTES);
|
||||
expect(patchedPaths(read.slice.patch)).toEqual(paths.slice(0, 5));
|
||||
expect(read.slice.nextCursor).toBe(`${ITERATION.id}:5`);
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("carries a whole new file and stops the slice on what it weighed", () =>
|
||||
Effect.gen(function* () {
|
||||
// A creation has no edit distance to search out, so no edit bound applies to it and its
|
||||
// section is the whole file. What keeps a run of them from filling one answer is the bytes
|
||||
// they weighed, which the slice has to be charged for.
|
||||
const paths = ["new.ts", "b.ts", "c.ts", "d.ts"];
|
||||
const read = yield* readSlice({ paths, lines: 8_000, width: 30, created: ["new.ts"] });
|
||||
|
||||
expect(patchedPaths(read.slice.patch)).toEqual(["new.ts"]);
|
||||
expect(read.slice.patch).toContain("--- /dev/null");
|
||||
expect(read.slice.patch).toContain("@@ -0,0 +1,8000 @@");
|
||||
expect(read.slice.patch.length).toBeGreaterThan(MAX_DIFF_SLICE_BYTES);
|
||||
expect(read.slice.nextCursor).toBe(`${ITERATION.id}:1`);
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("carries on from where the last slice stopped", () =>
|
||||
Effect.gen(function* () {
|
||||
const paths = ["a.ts", "b.ts", "c.ts", "d.ts", "e.ts", "f.ts"];
|
||||
const read = yield* readSlice({
|
||||
paths,
|
||||
lines: 100,
|
||||
width: 900,
|
||||
cursor: `${ITERATION.id}:2`,
|
||||
});
|
||||
|
||||
expect(patchedPaths(read.slice.patch)).toEqual(["c.ts", "d.ts"]);
|
||||
expect(read.slice.nextCursor).toBe(`${ITERATION.id}:4`);
|
||||
}),
|
||||
);
|
||||
it.effect("keeps the pull request being read, not the one looked up first", () =>
|
||||
Effect.gen(function* () {
|
||||
// Where a pull request lives is read from the pull request itself, so an evicted entry
|
||||
// costs a whole pull request read before any file can be asked for. Ordered by insertion
|
||||
// alone a hit does not renew its entry, so the review being worked through is the first
|
||||
// thing dropped once a listing has walked a cache's worth of cold pull requests.
|
||||
const HOT = 7;
|
||||
const readsOf = new Map<number, number>();
|
||||
|
||||
const provider = yield* make.pipe(
|
||||
Effect.provide(
|
||||
Layer.mock(AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli)({
|
||||
getPullRequest: (input) =>
|
||||
Effect.sync(() => {
|
||||
readsOf.set(input.number, (readsOf.get(input.number) ?? 0) + 1);
|
||||
return { ...PULL_REQUEST, number: input.number };
|
||||
}),
|
||||
listIterations: () => Effect.succeed([ITERATION]),
|
||||
listIterationChanges: () =>
|
||||
Effect.succeed({ changes: [change("a.ts")], truncated: false }),
|
||||
readItemContent: () => Effect.succeed({ contents: side("new", 2, 4), isBinary: false }),
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
const readDiff = (number: number) =>
|
||||
provider.getDiff({ cwd: "/w", repository: "acme/web", host: "dev.azure.com", number });
|
||||
|
||||
yield* readDiff(HOT);
|
||||
// A cache's worth of cold pull requests, with the open one read in between each of them.
|
||||
for (let filled = 0; filled < LOCATION_CACHE_CAPACITY; filled += 1) {
|
||||
yield* readDiff(HOT + 1 + filled);
|
||||
yield* readDiff(HOT);
|
||||
}
|
||||
|
||||
expect(readsOf.get(HOT)).toBe(1);
|
||||
}),
|
||||
);
|
||||
});
|
||||
@@ -1,647 +0,0 @@
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Semaphore from "effect/Semaphore";
|
||||
import type { PullRequestCapabilities, PullRequestViewerPermissions } from "@t3tools/contracts";
|
||||
|
||||
import * as AzureDevOpsPullRequestCli from "./AzureDevOpsPullRequestCli.ts";
|
||||
import {
|
||||
azureDevOpsFilePatch,
|
||||
azureDevOpsUnreadableFilePatch,
|
||||
formatAzureDevOpsDiffCursor,
|
||||
parseAzureDevOpsDiffCursor,
|
||||
MAX_DIFF_SLICE_BYTES,
|
||||
MAX_DIFF_SLICE_EDITS,
|
||||
MAX_DIFF_SLICE_FILES,
|
||||
byteLength,
|
||||
MAX_FILE_DIFF_EDITS,
|
||||
type AzureDevOpsFileTexts,
|
||||
} from "./azureDevOpsDiff.ts";
|
||||
import {
|
||||
PullRequestProviderError,
|
||||
type PullRequestProviderFailure,
|
||||
type ProviderChangeRequest,
|
||||
type ProviderChangeRequestActivity,
|
||||
type ProviderChangeRequestDetail,
|
||||
type ProviderChangeRequestSummary,
|
||||
type ProviderDiffSlice,
|
||||
type PullRequestProviderApi,
|
||||
} from "@t3tools/source-control-core/server/PullRequestProvider";
|
||||
import type {
|
||||
AzureDevOpsChangeEntry,
|
||||
AzureDevOpsItemContent,
|
||||
AzureDevOpsIteration,
|
||||
AzureDevOpsPullRequest,
|
||||
AzureDevOpsRepositoryLocation,
|
||||
} from "./azureDevOpsPullRequestJson.ts";
|
||||
|
||||
/**
|
||||
* How many of a slice's files are read at once. Every file is two `az` invocations, each paying a
|
||||
* Python interpreter's start-up, so reading them one after another is most of what the Code tab
|
||||
* waits for. Four files means eight processes at once: the fan-out the GitHub CLI reads its
|
||||
* per-file stats with here, and low enough not to swamp the host's throttling or the machine.
|
||||
*/
|
||||
const DIFF_FILE_CONCURRENCY = 4;
|
||||
|
||||
/**
|
||||
* How many `az` processes this build will have out at once, counted across every reader rather
|
||||
* than per request. The fan-out above bounds one Code tab, so two people opening two Azure reviews
|
||||
* had sixteen Python interpreters starting at once and nothing above them. Held at what one
|
||||
* request at full width spends, so a second reader waits behind the first instead of adding to it.
|
||||
*/
|
||||
export const MAX_DIFF_SPAWNS = 2 * DIFF_FILE_CONCURRENCY;
|
||||
|
||||
/** How many pull requests' repository locations one provider remembers at once. */
|
||||
export const LOCATION_CACHE_CAPACITY = 128;
|
||||
|
||||
const CAPABILITIES: PullRequestCapabilities = {
|
||||
// Azure serves no patch of its own, so the one the Code tab reads is built here out of the
|
||||
// files an iteration changed and both sides of each of them.
|
||||
diff: true,
|
||||
// Reading a conversation is a plain REST read, but posting one is not something this can
|
||||
// claim without having run it, so the composer stays hidden.
|
||||
comment: false,
|
||||
actions: [
|
||||
"merge",
|
||||
"ready",
|
||||
"draft",
|
||||
"close",
|
||||
"reopen",
|
||||
"enable-auto-merge",
|
||||
"disable-auto-merge",
|
||||
],
|
||||
// Azure squashes as a completion option; it has no rebase strategy of its own.
|
||||
mergeMethods: ["merge", "squash"],
|
||||
// `az repos pr list` filters by status, creator, reviewer and branch, and by no text at all.
|
||||
search: false,
|
||||
reactions: false,
|
||||
// The patch has lines to write against, but writing a remark at all is what Azure is not
|
||||
// offered for here, so nothing in a review is either.
|
||||
review: { inlineComment: false, reply: false, resolve: false, verdicts: [] },
|
||||
// `az repos pr reviewer add` and `remove` name identities, and nothing anywhere in `az repos`
|
||||
// lists the ones this repository could name — that lives behind the identity and graph APIs, a
|
||||
// different service with its own permissions. So the page takes a name here rather than being
|
||||
// handed a menu built out of a guess.
|
||||
reviewers: { request: true, listCandidates: false },
|
||||
// A new title and description travel on the same `az repos pr update` that moves a pull request.
|
||||
// Rewriting a remark is false for the same reason posting one is: this cannot put a remark on
|
||||
// Azure DevOps at all, so there is nothing here it could rewrite either.
|
||||
edit: { changeRequest: true, comment: false },
|
||||
// Azure does keep a viewed record of its own, but only behind the undocumented contribution
|
||||
// endpoint its web UI talks to, keyed on an iteration so a push would drop every mark anyway.
|
||||
// So they are kept here instead, and the client says whose they are rather than implying the
|
||||
// Azure DevOps page will show them.
|
||||
viewedFiles: "environment",
|
||||
};
|
||||
|
||||
/**
|
||||
* Everything this host offers, granted to whoever is signed in. Azure DevOps states no permission
|
||||
* anywhere `az repos pr show` or `az repos pr list` reach: the answer lives in the security
|
||||
* namespaces, behind identity descriptors and token paths that would be several calls per pull
|
||||
* request to resolve.
|
||||
*
|
||||
* So the actions stay live and a viewer who may not take one is told so by Azure, at the moment
|
||||
* they try. That is the safer half of an unknown: hiding a control from someone entitled to it
|
||||
* leaves them no way through and no reason given.
|
||||
*/
|
||||
const AZURE_DEVOPS_VIEWER_PERMISSIONS: PullRequestViewerPermissions = {
|
||||
actions: CAPABILITIES.actions,
|
||||
comment: CAPABILITIES.comment,
|
||||
resolve: CAPABILITIES.review.resolve,
|
||||
verdicts: CAPABILITIES.review.verdicts,
|
||||
requestReviewers: CAPABILITIES.reviewers.request,
|
||||
};
|
||||
|
||||
/** The CLI tags that mean the tool itself is unusable, rather than one request failing. */
|
||||
export function azureDevOpsProviderFailure(
|
||||
error: AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCliError,
|
||||
): PullRequestProviderFailure {
|
||||
if (error._tag === "AzureDevOpsCliUnavailableError") return { reason: "missing-tool" };
|
||||
if (error._tag === "AzureDevOpsCliAuthenticationError") return { reason: "unauthenticated" };
|
||||
if (error._tag === "AzureDevOpsCliRateLimitError") return { reason: "rate-limited" };
|
||||
if (error._tag === "AzureDevOpsPullRequestNotFoundError") return { reason: "not-found" };
|
||||
return { reason: "failed" };
|
||||
}
|
||||
|
||||
function toChangeRequest(pullRequest: AzureDevOpsPullRequest): ProviderChangeRequest {
|
||||
return {
|
||||
number: pullRequest.number,
|
||||
title: pullRequest.title,
|
||||
url: pullRequest.url,
|
||||
author: pullRequest.author,
|
||||
headBranch: pullRequest.headBranch,
|
||||
baseBranch: pullRequest.baseBranch,
|
||||
state: pullRequest.state,
|
||||
isDraft: pullRequest.isDraft,
|
||||
mergeability: pullRequest.mergeability,
|
||||
// Azure counts a pull request's files but never its lines, and counting them here would mean
|
||||
// reading every file on both sides of every row of a listing.
|
||||
additions: 0,
|
||||
deletions: 0,
|
||||
createdAt: pullRequest.createdAt,
|
||||
closedAt: pullRequest.state === "closed" ? pullRequest.closedAt : null,
|
||||
mergedAt: pullRequest.state === "merged" ? pullRequest.closedAt : null,
|
||||
updatedAt: pullRequest.updatedAt,
|
||||
reviewRequestLogins: pullRequest.reviewRequestLogins,
|
||||
// Azure keeps labels on work items rather than on the pull request.
|
||||
labels: [],
|
||||
};
|
||||
}
|
||||
|
||||
export const make = Effect.gen(function* () {
|
||||
const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
|
||||
// Made once with the provider, which the registry builds once, so this is the whole build's
|
||||
// allowance rather than one request's.
|
||||
const diffSpawns = yield* Semaphore.make(MAX_DIFF_SPAWNS);
|
||||
const readItemContent = (input: Parameters<typeof cli.readItemContent>[0]) =>
|
||||
diffSpawns.withPermits(1)(cli.readItemContent(input));
|
||||
|
||||
const fail =
|
||||
(operation: string) => (error: AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCliError) =>
|
||||
new PullRequestProviderError({
|
||||
provider: "azure-devops",
|
||||
operation,
|
||||
...azureDevOpsProviderFailure(error),
|
||||
detail: error.detail,
|
||||
cause: error,
|
||||
});
|
||||
|
||||
/** Refuses what the capabilities already say this host cannot do. */
|
||||
const unsupported = (operation: string) =>
|
||||
Effect.fail(
|
||||
new PullRequestProviderError({
|
||||
provider: "azure-devops",
|
||||
operation,
|
||||
reason: "failed",
|
||||
detail: "Azure DevOps reviews cannot be written from here yet.",
|
||||
}),
|
||||
);
|
||||
|
||||
/** A pull request Azure could not place has no diff to read, which reads as an empty one. */
|
||||
const EMPTY_DIFF_SLICE: ProviderDiffSlice = { patch: "", truncated: false, nextCursor: null };
|
||||
|
||||
/**
|
||||
* Where a pull request's repository lives, which is the route every other read of it needs and
|
||||
* the one thing only the pull request itself states. A pull request cannot move between
|
||||
* repositories, so it is remembered rather than re-read: the marks alone would otherwise pay for
|
||||
* a whole pull request read every time they checked whether a file had been pushed to. Least
|
||||
* recently used, so a listing walking cold pull requests cannot evict the one being read.
|
||||
*/
|
||||
const locations = new Map<string, AzureDevOpsRepositoryLocation>();
|
||||
|
||||
const locationOf = (input: { readonly cwd: string; readonly number: number }) => {
|
||||
const key = `${input.cwd} ${input.number}`;
|
||||
const held = locations.get(key);
|
||||
if (held !== undefined) {
|
||||
locations.delete(key);
|
||||
locations.set(key, held);
|
||||
return Effect.succeed(held);
|
||||
}
|
||||
return cli.getPullRequest({ cwd: input.cwd, number: input.number }).pipe(
|
||||
Effect.map((pullRequest) => {
|
||||
const location = pullRequest.location;
|
||||
if (location === null) return null;
|
||||
if (locations.size >= LOCATION_CACHE_CAPACITY) {
|
||||
const oldest = locations.keys().next().value;
|
||||
if (oldest !== undefined) locations.delete(oldest);
|
||||
}
|
||||
locations.set(key, location);
|
||||
return location;
|
||||
}),
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* Everything a diff read needs before it can ask for a file: where the repository lives, and
|
||||
* which pushes the pull request has had. A client names neither, and the iterations are read
|
||||
* afresh every time because the newest one is what a push adds.
|
||||
*/
|
||||
const diffScope = (input: { readonly cwd: string; readonly number: number }) =>
|
||||
Effect.gen(function* () {
|
||||
const location = yield* locationOf(input);
|
||||
if (location === null) return null;
|
||||
const iterations = yield* cli.listIterations({
|
||||
cwd: input.cwd,
|
||||
location,
|
||||
number: input.number,
|
||||
});
|
||||
return { location, iterations };
|
||||
});
|
||||
|
||||
const EMPTY_ITEM: AzureDevOpsItemContent = { contents: "", isBinary: false };
|
||||
|
||||
/**
|
||||
* Both sides of one changed file, read at once because neither answer depends on the other and
|
||||
* `az` pays a Python interpreter's start-up for each. Only the sides a change actually has are
|
||||
* asked for: Azure answers for a file that is not at a commit with a failure rather than with
|
||||
* nothing.
|
||||
*/
|
||||
const readTexts = (input: {
|
||||
readonly cwd: string;
|
||||
readonly location: AzureDevOpsRepositoryLocation;
|
||||
readonly iteration: AzureDevOpsIteration;
|
||||
readonly change: Pick<AzureDevOpsChangeEntry, "changeKind" | "path" | "oldPath">;
|
||||
}) =>
|
||||
Effect.all(
|
||||
[
|
||||
input.change.changeKind === "new"
|
||||
? Effect.succeed(EMPTY_ITEM)
|
||||
: readItemContent({
|
||||
cwd: input.cwd,
|
||||
location: input.location,
|
||||
path: input.change.oldPath,
|
||||
commit: input.iteration.mergeBaseCommit,
|
||||
}),
|
||||
input.change.changeKind === "deleted"
|
||||
? Effect.succeed(EMPTY_ITEM)
|
||||
: readItemContent({
|
||||
cwd: input.cwd,
|
||||
location: input.location,
|
||||
path: input.change.path,
|
||||
commit: input.iteration.headCommit,
|
||||
}),
|
||||
],
|
||||
{ concurrency: 2 },
|
||||
).pipe(
|
||||
Effect.map(([oldItem, newItem]): AzureDevOpsFileTexts => ({
|
||||
oldContents: oldItem.contents,
|
||||
newContents: newItem.contents,
|
||||
// Azure hands a file it calls binary over in an encoding of its own, so its own word on
|
||||
// that is taken rather than looked for in bytes it may never have sent verbatim.
|
||||
binary: oldItem.isBinary || newItem.isBinary,
|
||||
})),
|
||||
);
|
||||
|
||||
/**
|
||||
* What the whole pull request changed, taken from its latest push. An iteration's changes are
|
||||
* reported against the merge base rather than against the push before it, so the newest one is
|
||||
* the whole of the change rather than the last slice of it.
|
||||
*/
|
||||
const listLatestChanges = (input: {
|
||||
readonly cwd: string;
|
||||
readonly location: AzureDevOpsRepositoryLocation;
|
||||
readonly number: number;
|
||||
readonly iterations: ReadonlyArray<AzureDevOpsIteration>;
|
||||
}) => {
|
||||
const latest = input.iterations.at(-1);
|
||||
return latest === undefined
|
||||
? Effect.succeed({
|
||||
changes: [],
|
||||
truncated: false,
|
||||
} as AzureDevOpsPullRequestCli.AzureDevOpsIterationChanges)
|
||||
: cli.listIterationChanges({
|
||||
cwd: input.cwd,
|
||||
location: input.location,
|
||||
number: input.number,
|
||||
iterationId: latest.id,
|
||||
});
|
||||
};
|
||||
|
||||
const provider: PullRequestProviderApi = {
|
||||
kind: "azure-devops",
|
||||
capabilities: CAPABILITIES,
|
||||
|
||||
getViewer: (input) =>
|
||||
cli.getViewer({ cwd: input.cwd }).pipe(Effect.mapError(fail("getViewer"))),
|
||||
|
||||
// `input.query` is deliberately dropped: `az repos pr list` filters by status, creator,
|
||||
// reviewer and branch, and has nothing that matches text. Sending it as one of those would
|
||||
// narrow by the wrong thing, so the page comes back unnarrowed and the caller filters it.
|
||||
listChangeRequests: (input) =>
|
||||
cli
|
||||
.listPullRequests({
|
||||
cwd: input.cwd,
|
||||
repository: input.repository,
|
||||
state: input.state,
|
||||
involvement: input.involvement,
|
||||
viewer: input.viewer,
|
||||
limit: input.limit,
|
||||
cursor: input.cursor,
|
||||
})
|
||||
.pipe(
|
||||
Effect.mapError(fail("listChangeRequests")),
|
||||
Effect.map((batch) => ({
|
||||
items: batch.items.map(toChangeRequest),
|
||||
truncated: batch.truncated,
|
||||
cursorAdvance: batch.cursorAdvance,
|
||||
// Azure answers in one order whether or not it is being carried on from, so a slice
|
||||
// can always be stepped past — by counting, which is all Azure offers.
|
||||
continues: true,
|
||||
})),
|
||||
),
|
||||
|
||||
// The polled path a linked thread's row stays live on: one `az` read, no iterations or
|
||||
// changes behind it, since the file count that would cost is not shown here.
|
||||
getChangeRequestSummary: (input) =>
|
||||
cli.getPullRequest({ cwd: input.cwd, number: input.number }).pipe(
|
||||
Effect.mapError(fail("getChangeRequestSummary")),
|
||||
Effect.map((pullRequest): ProviderChangeRequestSummary => ({
|
||||
number: pullRequest.number,
|
||||
title: pullRequest.title,
|
||||
url: pullRequest.url,
|
||||
author: pullRequest.author,
|
||||
headBranch: pullRequest.headBranch,
|
||||
baseBranch: pullRequest.baseBranch,
|
||||
state: pullRequest.state,
|
||||
isDraft: pullRequest.isDraft,
|
||||
mergeability: pullRequest.mergeability,
|
||||
closedAt: pullRequest.state === "closed" ? pullRequest.closedAt : null,
|
||||
mergedAt: pullRequest.state === "merged" ? pullRequest.closedAt : null,
|
||||
updatedAt: pullRequest.updatedAt,
|
||||
})),
|
||||
),
|
||||
|
||||
getChangeRequest: (input) =>
|
||||
Effect.gen(function* () {
|
||||
const pullRequest = yield* cli.getPullRequest({ cwd: input.cwd, number: input.number });
|
||||
const location = pullRequest.location;
|
||||
// The file count is two reads past the pull request itself, and it is the only thing
|
||||
// riding on them, so a failure leaves it unknown rather than losing the whole detail.
|
||||
const changedFiles =
|
||||
location === null
|
||||
? 0
|
||||
: yield* cli.listIterations({ cwd: input.cwd, location, number: input.number }).pipe(
|
||||
Effect.flatMap((iterations) =>
|
||||
listLatestChanges({
|
||||
cwd: input.cwd,
|
||||
location,
|
||||
number: input.number,
|
||||
iterations,
|
||||
}),
|
||||
),
|
||||
Effect.map((listed) => listed.changes.length),
|
||||
Effect.orElseSucceed(() => 0),
|
||||
);
|
||||
const detail: ProviderChangeRequestDetail = {
|
||||
...toChangeRequest(pullRequest),
|
||||
body: pullRequest.body,
|
||||
changedFiles,
|
||||
mergedAt: pullRequest.state === "merged" ? pullRequest.closedAt : null,
|
||||
closedAt: pullRequest.state === "closed" ? pullRequest.closedAt : null,
|
||||
reviewers: pullRequest.reviewers,
|
||||
checks: [],
|
||||
mergeCapabilities: { merge: true, squash: true, rebase: false },
|
||||
viewerPermissions: AZURE_DEVOPS_VIEWER_PERMISSIONS,
|
||||
autoMergeEnabled: pullRequest.autoMergeEnabled,
|
||||
...(pullRequest.autoMergeMethod === undefined
|
||||
? {}
|
||||
: { autoMergeMethod: pullRequest.autoMergeMethod }),
|
||||
};
|
||||
return detail;
|
||||
}).pipe(Effect.mapError(fail("getChangeRequest"))),
|
||||
|
||||
getChangeRequestActivity: (input) =>
|
||||
cli.getPullRequest({ cwd: input.cwd, number: input.number }).pipe(
|
||||
Effect.mapError(fail("getChangeRequestActivity")),
|
||||
Effect.flatMap((pullRequest) =>
|
||||
(pullRequest.location === null
|
||||
? Effect.succeed({ comments: [], truncated: true })
|
||||
: cli
|
||||
.listThreads({
|
||||
cwd: input.cwd,
|
||||
location: pullRequest.location,
|
||||
number: input.number,
|
||||
})
|
||||
.pipe(
|
||||
Effect.map((comments) => ({ comments, truncated: false })),
|
||||
Effect.orElseSucceed(() => ({ comments: [], truncated: true })),
|
||||
)
|
||||
).pipe(
|
||||
Effect.map((conversation): ProviderChangeRequestActivity => ({
|
||||
comments: conversation.comments,
|
||||
commentCount: conversation.comments.length,
|
||||
commentsTruncated: conversation.truncated,
|
||||
reviewThreads: [],
|
||||
commits: [],
|
||||
})),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
// No request at all: Azure has nothing to say about the viewer that a pull request read can
|
||||
// reach, so the answer is the same constant the detail carries.
|
||||
getViewerPermissions: () => Effect.succeed(AZURE_DEVOPS_VIEWER_PERMISSIONS),
|
||||
|
||||
// `input.commit` is deliberately dropped: Azure states no commit list on a pull request, so
|
||||
// the Code tab has nothing to scope itself to and always asks for the whole change.
|
||||
getDiff: (input) =>
|
||||
Effect.gen(function* () {
|
||||
const scope = yield* diffScope(input);
|
||||
if (scope === null) return EMPTY_DIFF_SLICE;
|
||||
const cursor = parseAzureDevOpsDiffCursor(input.cursor);
|
||||
// Reading on stays with the push the first slice was taken against. A push landing
|
||||
// mid-read would otherwise renumber the files and hand the reader one twice, or none.
|
||||
const iteration =
|
||||
cursor === null
|
||||
? scope.iterations.at(-1)
|
||||
: scope.iterations.find((candidate) => candidate.id === cursor.iterationId);
|
||||
if (iteration === undefined) return EMPTY_DIFF_SLICE;
|
||||
const listed = yield* cli.listIterationChanges({
|
||||
cwd: input.cwd,
|
||||
location: scope.location,
|
||||
number: input.number,
|
||||
iterationId: iteration.id,
|
||||
});
|
||||
const changes = listed.changes;
|
||||
|
||||
const sections: string[] = [];
|
||||
let truncated = listed.truncated;
|
||||
let bytes = 0;
|
||||
let edits = 0;
|
||||
let index = cursor?.fileIndex ?? 0;
|
||||
let full = false;
|
||||
// How many files to read at once. Every file costs a request a side, and everything read
|
||||
// past the point the slice fills is thrown away and read again by the next slice, so the
|
||||
// batch narrows as the budgets do: what is left of each, over what a file has spent of it
|
||||
// on average so far, which is the only estimate there is before a file is read. At least
|
||||
// one, so a file heavier than the whole budget still moves the cursor.
|
||||
const batchWidth = () => {
|
||||
if (sections.length === 0) return DIFF_FILE_CONCURRENCY;
|
||||
const admits = (left: number, spent: number) =>
|
||||
Math.ceil(left / Math.max(1, spent / sections.length));
|
||||
return Math.max(
|
||||
1,
|
||||
Math.min(
|
||||
DIFF_FILE_CONCURRENCY,
|
||||
// The file budget needs no estimate: a file spends exactly one of it.
|
||||
MAX_DIFF_SLICE_FILES - sections.length,
|
||||
admits(MAX_DIFF_SLICE_BYTES - bytes, bytes),
|
||||
admits(MAX_DIFF_SLICE_EDITS - MAX_FILE_DIFF_EDITS - edits, edits),
|
||||
),
|
||||
);
|
||||
};
|
||||
while (!full && index < changes.length) {
|
||||
const batch = changes.slice(index, index + batchWidth());
|
||||
const read = yield* Effect.forEach(
|
||||
batch,
|
||||
(change) =>
|
||||
// A pair Azure refuses is one file rather than the whole slice: an oversize blob or
|
||||
// a path `az` will not carry through leaves that file listed without its hunks, and
|
||||
// everything around it still renders.
|
||||
readTexts({ cwd: input.cwd, location: scope.location, iteration, change }).pipe(
|
||||
// Only what is this one file's problem. A signed-out CLI, a rate limit or no `az`
|
||||
// at all is the read failing rather than the file, and belongs to the caller,
|
||||
// which pauses the host rather than showing every file as unreadable.
|
||||
Effect.catchTags({
|
||||
AzureDevOpsPullRequestNotFoundError: () => Effect.succeed(null),
|
||||
AzureDevOpsCommandFailedError: () => Effect.succeed(null),
|
||||
AzureDevOpsPullRequestReadError: () => Effect.succeed(null),
|
||||
}),
|
||||
Effect.map((texts) => ({ change, texts })),
|
||||
),
|
||||
{ concurrency: DIFF_FILE_CONCURRENCY },
|
||||
);
|
||||
for (const { change, texts } of read) {
|
||||
// The diff is synchronous and the reads no longer stand between one file and the next
|
||||
// to let anything else on the server run, so the thread is handed back here.
|
||||
yield* Effect.yieldNow;
|
||||
const file =
|
||||
texts === null
|
||||
? azureDevOpsUnreadableFilePatch(change)
|
||||
: azureDevOpsFilePatch({ change, texts });
|
||||
sections.push(file.section);
|
||||
bytes += byteLength(file.section);
|
||||
edits += file.edits;
|
||||
truncated = truncated || file.truncated;
|
||||
index += 1;
|
||||
// A file whose diff was given up on spent the whole of what one file is allowed and
|
||||
// has only a header to show for it, and a file the diff never ran on at all weighs
|
||||
// almost nothing in either budget and still costs its two reads. So the file count
|
||||
// bounds the request alongside the bytes.
|
||||
// Checked after the file is added rather than before it, so every slice carries at
|
||||
// least one: a section heavier than the whole budget would otherwise never be added,
|
||||
// and the read would answer the same slice forever without moving the cursor.
|
||||
if (
|
||||
bytes >= MAX_DIFF_SLICE_BYTES ||
|
||||
edits + MAX_FILE_DIFF_EDITS > MAX_DIFF_SLICE_EDITS ||
|
||||
sections.length >= MAX_DIFF_SLICE_FILES ||
|
||||
file.abandoned
|
||||
) {
|
||||
full = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const slice: ProviderDiffSlice = {
|
||||
patch: sections.join(""),
|
||||
truncated,
|
||||
nextCursor:
|
||||
index >= changes.length
|
||||
? null
|
||||
: formatAzureDevOpsDiffCursor({ iterationId: iteration.id, fileIndex: index }),
|
||||
};
|
||||
return slice;
|
||||
}).pipe(Effect.mapError(fail("getDiff"))),
|
||||
|
||||
// The patch is built from whole files, so opening the lines around a hunk is the same two
|
||||
// reads over again rather than a wider request. Read against the latest iteration: nothing in
|
||||
// the request says which push the reader is looking at, and expansion is stale after a
|
||||
// mid-review push on every host here anyway.
|
||||
getDiffFileContents: (input) =>
|
||||
Effect.gen(function* () {
|
||||
const scope = yield* diffScope(input);
|
||||
const iteration = scope?.iterations.at(-1);
|
||||
if (scope === null || iteration === undefined) return { oldContents: "", newContents: "" };
|
||||
return yield* readTexts({
|
||||
cwd: input.cwd,
|
||||
location: scope.location,
|
||||
iteration,
|
||||
change: {
|
||||
changeKind: input.changeType,
|
||||
path: input.newPath,
|
||||
oldPath: input.oldPath,
|
||||
},
|
||||
});
|
||||
}).pipe(Effect.mapError(fail("getDiffFileContents"))),
|
||||
|
||||
/**
|
||||
* What the head has of each marked file, which is the blob Azure already names on the change
|
||||
* it reports. One read covers every path, since the latest iteration lists the whole change.
|
||||
*
|
||||
* A path the change does not carry is at the empty revision, which is where a file the pull
|
||||
* request deletes sits. When the change was too long to follow to its end, those paths are
|
||||
* left out instead: reporting them as deleted would clear a file nobody has read.
|
||||
*/
|
||||
getFileRevisions: (input) =>
|
||||
Effect.gen(function* () {
|
||||
const revisions = new Map<string, string>();
|
||||
if (input.paths.length === 0) return { revisions };
|
||||
const scope = yield* diffScope(input);
|
||||
if (scope === null) return { revisions };
|
||||
const listed = yield* listLatestChanges({
|
||||
...scope,
|
||||
cwd: input.cwd,
|
||||
number: input.number,
|
||||
});
|
||||
const marked = new Set(input.paths);
|
||||
for (const change of listed.changes) {
|
||||
if (!marked.has(change.path) || change.objectId === null) continue;
|
||||
revisions.set(change.path, change.objectId);
|
||||
}
|
||||
if (!listed.truncated) {
|
||||
for (const path of input.paths) {
|
||||
if (!revisions.has(path)) revisions.set(path, "");
|
||||
}
|
||||
}
|
||||
return { revisions };
|
||||
}).pipe(Effect.mapError(fail("getFileRevisions"))),
|
||||
|
||||
runAction: (input) =>
|
||||
cli
|
||||
.runPullRequestAction({
|
||||
cwd: input.cwd,
|
||||
number: input.number,
|
||||
action: input.action,
|
||||
...(input.mergeMethod === undefined ? {} : { mergeMethod: input.mergeMethod }),
|
||||
})
|
||||
.pipe(Effect.mapError(fail("runAction"))),
|
||||
|
||||
updateChangeRequest: (input) =>
|
||||
cli
|
||||
.updatePullRequest({
|
||||
cwd: input.cwd,
|
||||
number: input.number,
|
||||
title: input.title,
|
||||
body: input.body,
|
||||
})
|
||||
.pipe(Effect.mapError(fail("updateChangeRequest"))),
|
||||
|
||||
// Never called: `capabilities.reviewers.listCandidates` is false, and the service refuses the
|
||||
// list without it.
|
||||
listReviewerCandidates: () =>
|
||||
Effect.fail(
|
||||
new PullRequestProviderError({
|
||||
provider: "azure-devops",
|
||||
operation: "listReviewerCandidates",
|
||||
reason: "failed",
|
||||
detail: "Azure DevOps cannot say who may review a pull request.",
|
||||
}),
|
||||
),
|
||||
|
||||
setReviewerRequest: (input) =>
|
||||
cli
|
||||
.setPullRequestReviewers({
|
||||
cwd: input.cwd,
|
||||
number: input.number,
|
||||
// Azure names an identity by an email address or a guid, and has no team to ask, so a
|
||||
// candidate's id is the whole of what it takes.
|
||||
reviewers: input.reviewers.map((reviewer) => reviewer.id),
|
||||
requested: input.requested,
|
||||
})
|
||||
.pipe(Effect.mapError(fail("setReviewerRequest"))),
|
||||
|
||||
// Never called: `capabilities.comment` is false, and the service refuses a comment without it.
|
||||
comment: () => unsupported("comment"),
|
||||
|
||||
// Declared unsupported above, so the service refuses these before a provider is reached.
|
||||
// They exist because every provider answers the whole port.
|
||||
submitReview: () => unsupported("submitReview"),
|
||||
|
||||
replyToThread: () => unsupported("replyToThread"),
|
||||
|
||||
setThreadResolution: () => unsupported("setThreadResolution"),
|
||||
|
||||
setReaction: () => unsupported("setReaction"),
|
||||
};
|
||||
|
||||
return provider;
|
||||
});
|
||||
@@ -1,11 +1,11 @@
|
||||
import { assert, it } from "@effect/vitest";
|
||||
|
||||
import * as AzureDevOpsCli from "../sourceControl/AzureDevOpsCli.ts";
|
||||
import * as AzureDevOpsCli from "@t3tools/source-control-azure-devops/server/AzureDevOpsCli";
|
||||
import * as BitbucketApi from "../sourceControl/BitbucketApi.ts";
|
||||
import * as GitHubApi from "../sourceControl/GitHubApi.ts";
|
||||
import * as SourceControlRateLimit from "@t3tools/source-control-core/server/SourceControlRateLimit";
|
||||
import * as GitLabCli from "../sourceControl/GitLabCli.ts";
|
||||
import { azureDevOpsProviderFailure } from "./AzureDevOpsPullRequestProvider.ts";
|
||||
import { azureDevOpsProviderFailure } from "@t3tools/source-control-azure-devops/server/AzureDevOpsPullRequestProvider";
|
||||
import { bitbucketProviderFailure } from "./BitbucketPullRequestProvider.ts";
|
||||
import { gitHubProviderFailure } from "./GitHubPullRequestProvider.ts";
|
||||
import { gitLabProviderFailure } from "./GitLabPullRequestProvider.ts";
|
||||
|
||||
@@ -3,12 +3,9 @@ import * as Effect from "effect/Effect";
|
||||
import * as Layer from "effect/Layer";
|
||||
import type { SourceControlProviderKind } from "@t3tools/contracts";
|
||||
|
||||
import * as AzureDevOpsCli from "../sourceControl/AzureDevOpsCli.ts";
|
||||
import * as BitbucketApi from "../sourceControl/BitbucketApi.ts";
|
||||
import * as GitHubApi from "../sourceControl/GitHubApi.ts";
|
||||
import * as GitLabCli from "../sourceControl/GitLabCli.ts";
|
||||
import * as AzureDevOpsPullRequestCli from "./AzureDevOpsPullRequestCli.ts";
|
||||
import * as AzureDevOpsPullRequestProvider from "./AzureDevOpsPullRequestProvider.ts";
|
||||
import * as BitbucketPullRequestApi from "./BitbucketPullRequestApi.ts";
|
||||
import * as BitbucketPullRequestProvider from "./BitbucketPullRequestProvider.ts";
|
||||
import * as GitHubPullRequestApi from "./GitHubPullRequestApi.ts";
|
||||
@@ -49,7 +46,6 @@ export const make = Effect.gen(function* () {
|
||||
GitHubPullRequestProvider.make,
|
||||
GitLabPullRequestProvider.make,
|
||||
BitbucketPullRequestProvider.make,
|
||||
AzureDevOpsPullRequestProvider.make,
|
||||
]);
|
||||
const drivers = yield* Effect.forEach(BuiltInDrivers.BUILT_IN_SOURCE_CONTROL_DRIVERS, (driver) =>
|
||||
driver.make.pipe(Effect.map((instance) => instance.pullRequests)),
|
||||
@@ -72,5 +68,4 @@ export const layer = Layer.effect(PullRequestProviderRegistry, make).pipe(
|
||||
Layer.provide(GitLabPullRequestCli.layer.pipe(Layer.provide(GitLabCli.layer))),
|
||||
Layer.provide(BuiltInDrivers.layer),
|
||||
Layer.provide(BitbucketPullRequestApi.layer.pipe(Layer.provide(BitbucketApi.layer))),
|
||||
Layer.provide(AzureDevOpsPullRequestCli.layer.pipe(Layer.provide(AzureDevOpsCli.layer))),
|
||||
);
|
||||
@@ -1,459 +0,0 @@
|
||||
import { unquoteGitPatchPath } from "@t3tools/shared/gitPatchPath";
|
||||
import { describe, expect, it } from "vite-plus/test";
|
||||
|
||||
import {
|
||||
azureDevOpsFilePatch,
|
||||
azureDevOpsUnreadableFilePatch,
|
||||
byteLength,
|
||||
formatAzureDevOpsDiffCursor,
|
||||
MAX_FILE_DIFF_EDITS,
|
||||
parseAzureDevOpsDiffCursor,
|
||||
} from "./azureDevOpsDiff.ts";
|
||||
import type { AzureDevOpsChangeEntry } from "./azureDevOpsPullRequestJson.ts";
|
||||
|
||||
function change(overrides: Partial<AzureDevOpsChangeEntry> = {}): AzureDevOpsChangeEntry {
|
||||
return {
|
||||
path: "README.md",
|
||||
oldPath: "README.md",
|
||||
changeKind: "change",
|
||||
objectId: "8f80",
|
||||
originalObjectId: "0ca4",
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
function texts(oldContents: string, newContents: string, binary = false) {
|
||||
return { oldContents, newContents, binary };
|
||||
}
|
||||
|
||||
describe("azureDevOpsFilePatch", () => {
|
||||
it("writes a changed file as the unified patch every diff viewer already reads", () => {
|
||||
const patch = azureDevOpsFilePatch({
|
||||
change: change(),
|
||||
texts: texts("one\ntwo\nthree\n", "one\ntwo again\nthree\n"),
|
||||
});
|
||||
|
||||
expect(patch.truncated).toBe(false);
|
||||
expect(patch.section).toBe(
|
||||
[
|
||||
"diff --git a/README.md b/README.md",
|
||||
"--- a/README.md",
|
||||
"+++ b/README.md",
|
||||
"@@ -1,3 +1,3 @@",
|
||||
" one",
|
||||
"-two",
|
||||
"+two again",
|
||||
" three",
|
||||
"",
|
||||
].join("\n"),
|
||||
);
|
||||
});
|
||||
|
||||
it("names the side a new file does not have as /dev/null", () => {
|
||||
const patch = azureDevOpsFilePatch({
|
||||
change: change({ path: "DEMO.md", oldPath: "DEMO.md", changeKind: "new" }),
|
||||
texts: texts("", "hello\n"),
|
||||
});
|
||||
|
||||
expect(patch.section).toContain("new file mode 100644");
|
||||
expect(patch.section).toContain("--- /dev/null");
|
||||
expect(patch.section).toContain("+++ b/DEMO.md");
|
||||
// Git points the range a new file does not have at line zero, not at line one.
|
||||
expect(patch.section).toContain("@@ -0,0 +1 @@");
|
||||
expect(patch.section).toContain("+hello");
|
||||
});
|
||||
|
||||
it("names the side a deleted file no longer has as /dev/null", () => {
|
||||
const patch = azureDevOpsFilePatch({
|
||||
change: change({ path: "OLD.md", oldPath: "OLD.md", changeKind: "deleted" }),
|
||||
texts: texts("gone\n", ""),
|
||||
});
|
||||
|
||||
expect(patch.section).toContain("deleted file mode 100644");
|
||||
expect(patch.section).toContain("--- a/OLD.md");
|
||||
expect(patch.section).toContain("+++ /dev/null");
|
||||
expect(patch.section).toContain("@@ -1 +0,0 @@");
|
||||
expect(patch.section).toContain("-gone");
|
||||
});
|
||||
|
||||
it("keeps the carriage returns of a file with Windows line endings", () => {
|
||||
// They are part of the line rather than around it, so a patch that dropped them would ask
|
||||
// the reader to look at a change that is not the one on the host.
|
||||
const patch = azureDevOpsFilePatch({
|
||||
change: change(),
|
||||
texts: texts("one\r\ntwo\r\n", "one\r\ntwo again\r\n"),
|
||||
});
|
||||
|
||||
expect(patch.section).toContain("-two\r");
|
||||
expect(patch.section).toContain("+two again\r");
|
||||
});
|
||||
|
||||
it("keeps a file that only moved, which has no hunks to give", () => {
|
||||
const patch = azureDevOpsFilePatch({
|
||||
change: change({ path: "docs/new.md", oldPath: "docs/old.md", changeKind: "rename-pure" }),
|
||||
texts: texts("same\n", "same\n"),
|
||||
});
|
||||
|
||||
expect(patch.truncated).toBe(false);
|
||||
expect(patch.section).toBe(
|
||||
[
|
||||
"diff --git a/docs/old.md b/docs/new.md",
|
||||
"rename from docs/old.md",
|
||||
"rename to docs/new.md",
|
||||
"--- a/docs/old.md",
|
||||
"+++ b/docs/new.md",
|
||||
"",
|
||||
].join("\n"),
|
||||
);
|
||||
});
|
||||
|
||||
it("reports a binary file as changed rather than spelling it out", () => {
|
||||
const patch = azureDevOpsFilePatch({
|
||||
change: change({ path: "logo.png", oldPath: "logo.png" }),
|
||||
texts: texts("PNG\u0000old", "PNG\u0000new"),
|
||||
});
|
||||
|
||||
expect(patch.truncated).toBe(true);
|
||||
expect(patch.section).toContain("Binary files a/logo.png and b/logo.png differ");
|
||||
});
|
||||
|
||||
it("shows an overlong file as changed without its hunks", () => {
|
||||
const patch = azureDevOpsFilePatch({
|
||||
change: change({ path: "bundle.js", oldPath: "bundle.js" }),
|
||||
texts: texts("a\n".repeat(400_000), "b\n".repeat(400_000)),
|
||||
});
|
||||
|
||||
expect(patch.truncated).toBe(true);
|
||||
expect(patch.section).toBe(
|
||||
["diff --git a/bundle.js b/bundle.js", "--- a/bundle.js", "+++ b/bundle.js", ""].join("\n"),
|
||||
);
|
||||
});
|
||||
|
||||
it("takes the host's word that a file is binary, whatever its bytes look like", () => {
|
||||
// Azure hands such a file over base64-encoded, so nothing in the text it sent gives it away.
|
||||
const patch = azureDevOpsFilePatch({
|
||||
change: change({ path: "logo.png", oldPath: "logo.png" }),
|
||||
texts: texts("b2xk", "bmV3", true),
|
||||
});
|
||||
|
||||
expect(patch.truncated).toBe(true);
|
||||
expect(patch.section).toContain("Binary files a/logo.png and b/logo.png differ");
|
||||
});
|
||||
|
||||
it("counts an overlong file in bytes rather than in characters", () => {
|
||||
// Three bytes each, so a ceiling counted in code units would let three times the size through.
|
||||
const patch = azureDevOpsFilePatch({
|
||||
change: change({ path: "notes.md", oldPath: "notes.md" }),
|
||||
texts: texts("\u4e00".repeat(200_000), "\u4e8c".repeat(200_000)),
|
||||
});
|
||||
|
||||
expect(patch.truncated).toBe(true);
|
||||
expect(patch.section).toBe(
|
||||
["diff --git a/notes.md b/notes.md", "--- a/notes.md", "+++ b/notes.md", ""].join("\n"),
|
||||
);
|
||||
});
|
||||
|
||||
const lineRange = (count: number, prefix: string) =>
|
||||
Array.from({ length: count }, (_, line) => `${prefix} ${line}`).join("\n");
|
||||
|
||||
it("lists a file too far apart to diff without its hunks", () => {
|
||||
// Sharing no line at all costs one edit per line on each side, so this pair is twice the
|
||||
// ceiling apart. Left to itself the search costs about the square of that and would hold the
|
||||
// whole server, every websocket client with it, while it worked out a patch nobody reads.
|
||||
// Writing both sides out instead would read as a genuine rewrite: the ceiling is a distance
|
||||
// rather than a proportion, so a long file reaches it having changed in one corner, and that
|
||||
// corner would be buried in a wall of red and green.
|
||||
const lines = (prefix: string) =>
|
||||
Array.from({ length: MAX_FILE_DIFF_EDITS }, (_, line) => `${prefix} ${line}`).join("\n");
|
||||
const patch = azureDevOpsFilePatch({
|
||||
change: change({ path: "generated.ts", oldPath: "generated.ts" }),
|
||||
texts: texts(`${lines("old")}\n`, `${lines("new")}\n`),
|
||||
});
|
||||
|
||||
expect(patch.truncated).toBe(true);
|
||||
// And it says the search was given up on, because the reader of a run of files is meant to
|
||||
// stop rather than spend that work again on each of the ones behind it.
|
||||
expect(patch.abandoned).toBe(true);
|
||||
expect(patch.section).toBe(
|
||||
[
|
||||
"diff --git a/generated.ts b/generated.ts",
|
||||
"--- a/generated.ts",
|
||||
"+++ b/generated.ts",
|
||||
"",
|
||||
].join("\n"),
|
||||
);
|
||||
});
|
||||
|
||||
it("writes out a wholly new file however many lines it has", () => {
|
||||
// Nothing on the old side means there was no edit distance to search out, so this is the
|
||||
// minimal patch and not a stand-in for one. Fifteen thousand lines of thirty bytes is the
|
||||
// shape this used to lose: inside the byte ceiling that gates every file, and well past every
|
||||
// bound a two-sided file answers to, none of which is protecting against anything here.
|
||||
const contents = `${Array.from({ length: 15_000 }, () => "x".repeat(29)).join("\n")}\n`;
|
||||
const patch = azureDevOpsFilePatch({
|
||||
change: change({ path: "DEMO.md", oldPath: "DEMO.md", changeKind: "new" }),
|
||||
texts: texts("", contents),
|
||||
});
|
||||
|
||||
expect(byteLength(contents)).toBeLessThan(512 * 1024);
|
||||
expect(patch.truncated).toBe(false);
|
||||
expect(patch.abandoned).toBe(false);
|
||||
expect(patch.edits).toBe(15_000);
|
||||
expect(patch.section).toContain("@@ -0,0 +1,15000 @@");
|
||||
// Only the `+++` of the header on top of the file's own lines, so nothing was dropped out of
|
||||
// the middle.
|
||||
expect(patch.section.match(/^\+/gmu)).toHaveLength(15_001);
|
||||
});
|
||||
|
||||
it("keeps a wholly new file too heavy to write out listed without its hunks", () => {
|
||||
// Every line carries a marker, so a side of very short lines answers with up to twice its own
|
||||
// bytes: these 200,000 one-character lines fit the ceiling each side is read under and weigh
|
||||
// about 600KB written out, more than twice what a whole slice may carry. There is no smaller
|
||||
// true patch for a creation to fall back to, so it is listed without its hunks, the same as a
|
||||
// side too big to read at all, rather than sent at a size the slice budget exists to prevent.
|
||||
const contents = `${Array.from({ length: 200_000 }, () => "x").join("\n")}\n`;
|
||||
const patch = azureDevOpsFilePatch({
|
||||
change: change({ path: "bundle.min.js", oldPath: "bundle.min.js", changeKind: "new" }),
|
||||
texts: texts("", contents),
|
||||
});
|
||||
|
||||
expect(byteLength(contents)).toBeLessThan(512 * 1024);
|
||||
expect(patch.truncated).toBe(true);
|
||||
expect(patch.abandoned).toBe(false);
|
||||
// It still cost the walk over its lines, which is what the slice is charged for.
|
||||
expect(patch.edits).toBe(200_000);
|
||||
expect(patch.section).toBe(
|
||||
[
|
||||
"diff --git a/bundle.min.js b/bundle.min.js",
|
||||
"new file mode 100644",
|
||||
"--- /dev/null",
|
||||
"+++ b/bundle.min.js",
|
||||
"",
|
||||
].join("\n"),
|
||||
);
|
||||
});
|
||||
|
||||
it("writes out a wholly deleted file however many lines it had", () => {
|
||||
const contents = `${lineRange(20_000, "line")}\n`;
|
||||
const patch = azureDevOpsFilePatch({
|
||||
change: change({ path: "OLD.md", oldPath: "OLD.md", changeKind: "deleted" }),
|
||||
texts: texts(contents, ""),
|
||||
});
|
||||
|
||||
expect(patch.truncated).toBe(false);
|
||||
expect(patch.abandoned).toBe(false);
|
||||
expect(patch.edits).toBe(20_000);
|
||||
expect(patch.section).toContain("@@ -1,20000 +0,0 @@");
|
||||
expect(patch.section.match(/^-line /gmu)).toHaveLength(20_000);
|
||||
expect(patch.section.match(/^-/gmu)).toHaveLength(20_001);
|
||||
});
|
||||
|
||||
it("gives an empty new file no hunk to read", () => {
|
||||
// A file with nothing on either side has no lines to claim were replaced, and git writes it
|
||||
// as a header alone.
|
||||
const patch = azureDevOpsFilePatch({
|
||||
change: change({ path: "EMPTY.md", oldPath: "EMPTY.md", changeKind: "new" }),
|
||||
texts: texts("", ""),
|
||||
});
|
||||
|
||||
expect(patch.edits).toBe(0);
|
||||
expect(patch.section).not.toContain("@@");
|
||||
});
|
||||
|
||||
it("marks a wholly new file whose last line has no newline after it", () => {
|
||||
const patch = azureDevOpsFilePatch({
|
||||
change: change({ path: "NOTES.md", oldPath: "NOTES.md", changeKind: "new" }),
|
||||
texts: texts("", "one\ntwo"),
|
||||
});
|
||||
|
||||
expect(patch.section).toBe(
|
||||
[
|
||||
"diff --git a/NOTES.md b/NOTES.md",
|
||||
"new file mode 100644",
|
||||
"--- /dev/null",
|
||||
"+++ b/NOTES.md",
|
||||
"@@ -0,0 +1,2 @@",
|
||||
"+one",
|
||||
"+two",
|
||||
"\\ No newline at end of file",
|
||||
"",
|
||||
].join("\n"),
|
||||
);
|
||||
});
|
||||
|
||||
it("keeps a file it did diff out of the giving up", () => {
|
||||
const patch = azureDevOpsFilePatch({
|
||||
change: change(),
|
||||
texts: texts("one\ntwo\n", "one\ntwo again\n"),
|
||||
});
|
||||
|
||||
expect(patch.abandoned).toBe(false);
|
||||
});
|
||||
|
||||
it("counts what the diff worked out, which is what the file cost to diff", () => {
|
||||
// The caller spends a budget of these across a slice, so they have to be the edits the search
|
||||
// actually made: one line replaced is a removal and an addition, and the three lines of
|
||||
// context around them cost nothing.
|
||||
const patch = azureDevOpsFilePatch({
|
||||
change: change(),
|
||||
texts: texts("one\ntwo\nthree\nfour\n", "one\ntwo again\nthree\nfour\n"),
|
||||
});
|
||||
|
||||
expect(patch.edits).toBe(2);
|
||||
});
|
||||
|
||||
it("counts nothing for a file it never diffed", () => {
|
||||
const patch = azureDevOpsFilePatch({
|
||||
change: change({ path: "logo.png", oldPath: "logo.png" }),
|
||||
texts: texts("PNG\u0000old", "PNG\u0000new"),
|
||||
});
|
||||
|
||||
expect(patch.edits).toBe(0);
|
||||
});
|
||||
|
||||
it("lists a file whose hunks outweigh its sides without them", () => {
|
||||
// A handful of very long lines is a few edits and nowhere near the edit ceiling, and the
|
||||
// patch carries both sides in full with three lines of context around each hunk, so the
|
||||
// section comes out heavier than either side was. What one file weighs is what a slice's
|
||||
// budget is spent in, so the edit ceiling alone does not bound this.
|
||||
const line = `${"a".repeat(400 * 1024)}\n`;
|
||||
const patch = azureDevOpsFilePatch({
|
||||
change: change({ path: "min.js", oldPath: "min.js" }),
|
||||
texts: texts(line, `${"b".repeat(400 * 1024)}\n`),
|
||||
});
|
||||
|
||||
expect(patch.edits).toBeLessThan(MAX_FILE_DIFF_EDITS);
|
||||
expect(patch.truncated).toBe(true);
|
||||
expect(patch.section).toBe(
|
||||
["diff --git a/min.js b/min.js", "--- a/min.js", "+++ b/min.js", ""].join("\n"),
|
||||
);
|
||||
expect(byteLength(patch.section)).toBeLessThan(byteLength(line));
|
||||
});
|
||||
|
||||
it("marks a file that does not end in a newline, as git does", () => {
|
||||
const patch = azureDevOpsFilePatch({
|
||||
change: change(),
|
||||
texts: texts("one\n", "two"),
|
||||
});
|
||||
|
||||
expect(patch.section).toContain("\\ No newline at end of file");
|
||||
});
|
||||
});
|
||||
|
||||
describe("azureDevOpsUnreadableFilePatch", () => {
|
||||
it("keeps a file the host would not hand over, listed without its hunks", () => {
|
||||
const patch = azureDevOpsUnreadableFilePatch(change({ path: "huge.bin", oldPath: "huge.bin" }));
|
||||
|
||||
expect(patch.truncated).toBe(true);
|
||||
expect(patch.section).toBe(
|
||||
["diff --git a/huge.bin b/huge.bin", "--- a/huge.bin", "+++ b/huge.bin", ""].join("\n"),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("a file Azure names something a patch header cannot carry plainly", () => {
|
||||
it("writes each side as git's quoted form, the side letter inside the quotes", () => {
|
||||
const patch = azureDevOpsFilePatch({
|
||||
change: change({ path: "notes\treadme.md", oldPath: "notes\treadme.md" }),
|
||||
texts: texts("one\n", "two\n"),
|
||||
});
|
||||
|
||||
expect(patch.section).toBe(
|
||||
[
|
||||
'diff --git "a/notes\\treadme.md" "b/notes\\treadme.md"',
|
||||
'--- "a/notes\\treadme.md"',
|
||||
'+++ "b/notes\\treadme.md"',
|
||||
"@@ -1 +1 @@",
|
||||
"-one",
|
||||
"+two",
|
||||
"",
|
||||
].join("\n"),
|
||||
);
|
||||
});
|
||||
|
||||
it("keeps a name holding a newline on the one header line it belongs to", () => {
|
||||
const patch = azureDevOpsFilePatch({
|
||||
change: change({ path: "line\nfile.txt", oldPath: "line\nfile.txt" }),
|
||||
texts: texts("one\n", "two\n"),
|
||||
});
|
||||
|
||||
// Written as itself the name would start a line of its own, and a reader would take what
|
||||
// followed for a header the patch never had.
|
||||
expect(patch.section.split("\n").slice(0, 3)).toEqual([
|
||||
'diff --git "a/line\\nfile.txt" "b/line\\nfile.txt"',
|
||||
'--- "a/line\\nfile.txt"',
|
||||
'+++ "b/line\\nfile.txt"',
|
||||
]);
|
||||
});
|
||||
|
||||
it("quotes the names a rename states, which carry no side letter", () => {
|
||||
const patch = azureDevOpsFilePatch({
|
||||
change: change({
|
||||
path: "docs/new\tname.md",
|
||||
oldPath: "docs/old\tname.md",
|
||||
changeKind: "rename-pure",
|
||||
}),
|
||||
texts: texts("same\n", "same\n"),
|
||||
});
|
||||
|
||||
expect(patch.section).toBe(
|
||||
[
|
||||
'diff --git "a/docs/old\\tname.md" "b/docs/new\\tname.md"',
|
||||
'rename from "docs/old\\tname.md"',
|
||||
'rename to "docs/new\\tname.md"',
|
||||
'--- "a/docs/old\\tname.md"',
|
||||
'+++ "b/docs/new\\tname.md"',
|
||||
"",
|
||||
].join("\n"),
|
||||
);
|
||||
});
|
||||
|
||||
it("quotes the sides of the one line a binary file gets", () => {
|
||||
const patch = azureDevOpsFilePatch({
|
||||
change: change({ path: "logo\tmark.png", oldPath: "logo\tmark.png" }),
|
||||
texts: texts("PNG\u0000old", "PNG\u0000new"),
|
||||
});
|
||||
|
||||
expect(patch.section).toContain(
|
||||
'Binary files "a/logo\\tmark.png" and "b/logo\\tmark.png" differ',
|
||||
);
|
||||
});
|
||||
|
||||
it("hands a reader of the header back the name Azure gave", () => {
|
||||
const path = 'every\t\n"kind"\\of.md';
|
||||
const patch = azureDevOpsFilePatch({
|
||||
change: change({ path, oldPath: path }),
|
||||
texts: texts("one\n", "two\n"),
|
||||
});
|
||||
const [header, oldLine, newLine] = patch.section.split("\n");
|
||||
|
||||
expect(header).not.toContain("\t");
|
||||
expect(unquoteGitPatchPath(oldLine?.slice(4) ?? "")).toBe(`a/${path}`);
|
||||
expect(unquoteGitPatchPath(newLine?.slice(4) ?? "")).toBe(`b/${path}`);
|
||||
});
|
||||
});
|
||||
|
||||
describe("a diff cursor", () => {
|
||||
it("carries the push it was taken against back to the next slice", () => {
|
||||
const cursor = formatAzureDevOpsDiffCursor({ iterationId: 3, fileIndex: 12 });
|
||||
|
||||
expect(parseAzureDevOpsDiffCursor(cursor)).toEqual({ iterationId: 3, fileIndex: 12 });
|
||||
});
|
||||
|
||||
it("reads anything it did not write as no position at all", () => {
|
||||
// Which starts the read from the top rather than failing it: a cursor is the client's to
|
||||
// hand back, and nothing downstream is worth refusing a whole diff over.
|
||||
for (const raw of [undefined, null, "", "abc", "1", "0:4", "1:-2", "1:2:3"]) {
|
||||
expect(parseAzureDevOpsDiffCursor(raw)).toBeNull();
|
||||
}
|
||||
});
|
||||
|
||||
it("refuses a half it did not write rather than reading it as the first file", () => {
|
||||
// `Number` is wider than the cursor: an empty, padded or hex half would otherwise pass as a
|
||||
// position, and the read would resume against an iteration the client never saw instead of
|
||||
// starting again from the latest one.
|
||||
for (const raw of ["1:", ":4", "1: ", " 1:4", "1:0x2", "0x1:2", "1e2:0", "1:4.0"]) {
|
||||
expect(parseAzureDevOpsDiffCursor(raw)).toBeNull();
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -1,273 +0,0 @@
|
||||
import { quoteGitPatchPath } from "@t3tools/shared/gitPatchPath";
|
||||
import { structuredPatch } from "diff";
|
||||
|
||||
import type { AzureDevOpsChangeEntry } from "./azureDevOpsPullRequestJson.ts";
|
||||
|
||||
/**
|
||||
* How far a diff read got, and which push it was reading. Azure hangs a pull request's changed
|
||||
* files off an iteration, so a push landing mid-read would renumber the list under a cursor that
|
||||
* did not also pin the iteration.
|
||||
*/
|
||||
export interface AzureDevOpsDiffCursor {
|
||||
readonly iterationId: number;
|
||||
readonly fileIndex: number;
|
||||
}
|
||||
|
||||
const CURSOR_SEPARATOR = ":";
|
||||
|
||||
/**
|
||||
* Restricts each half to plain decimal, so `Number` cannot read a foreign cursor's `0x3` as
|
||||
* three.
|
||||
*/
|
||||
const CURSOR_COMPONENT = /^\d+$/;
|
||||
|
||||
export function formatAzureDevOpsDiffCursor(cursor: AzureDevOpsDiffCursor): string {
|
||||
return `${cursor.iterationId}${CURSOR_SEPARATOR}${cursor.fileIndex}`;
|
||||
}
|
||||
|
||||
/** Null for anything this did not write, which starts the read from the top rather than failing. */
|
||||
export function parseAzureDevOpsDiffCursor(
|
||||
raw: string | null | undefined,
|
||||
): AzureDevOpsDiffCursor | null {
|
||||
if (raw === null || raw === undefined) return null;
|
||||
const [iteration, file, ...rest] = raw.split(CURSOR_SEPARATOR);
|
||||
if (rest.length > 0) return null;
|
||||
if (iteration === undefined || file === undefined) return null;
|
||||
if (!CURSOR_COMPONENT.test(iteration) || !CURSOR_COMPONENT.test(file)) return null;
|
||||
const iterationId = Number(iteration);
|
||||
const fileIndex = Number(file);
|
||||
if (!Number.isSafeInteger(iterationId) || iterationId <= 0) return null;
|
||||
if (!Number.isSafeInteger(fileIndex) || fileIndex < 0) return null;
|
||||
return { iterationId, fileIndex };
|
||||
}
|
||||
|
||||
/** The two texts of one changed file, empty on whichever side the change does not have. */
|
||||
export interface AzureDevOpsFileTexts {
|
||||
readonly oldContents: string;
|
||||
readonly newContents: string;
|
||||
/** Azure's own flag for a file it hands back base64-encoded instead of as text. */
|
||||
readonly binary: boolean;
|
||||
}
|
||||
|
||||
export interface AzureDevOpsFilePatch {
|
||||
readonly section: string;
|
||||
/** The file changed but its hunks are not in the section, so the patch has a hole in it. */
|
||||
readonly truncated: boolean;
|
||||
/**
|
||||
* The diff was given up on partway, having spent the whole edit budget, so a caller reading a
|
||||
* run of files stops here rather than paying that again for each one behind it.
|
||||
*/
|
||||
readonly abandoned: boolean;
|
||||
/**
|
||||
* Lines added or removed, which is the edit distance the diff had to search out, and what a
|
||||
* slice's budget is spent in: a file of short lines is cheap on the wire and dear to diff.
|
||||
*/
|
||||
readonly edits: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Beyond this a file is shown as changed without its hunks. Azure hands back whole files rather
|
||||
* than a patch, so an oversize file is paid for twice over before anything can be diffed.
|
||||
*/
|
||||
const MAX_FILE_BYTES = 512 * 1024;
|
||||
|
||||
/** Git's own default, and what the hunks from this repo's other hosts are already cut to. */
|
||||
const PATCH_CONTEXT_LINES = 3;
|
||||
|
||||
/**
|
||||
* How far apart one file's two sides may be before it is listed without its hunks. The line diff
|
||||
* costs about the square of the edit distance, so an unbounded pair sharing almost nothing could
|
||||
* hold the whole server. Bounded in edits rather than milliseconds so a change slices the same
|
||||
* way on every machine.
|
||||
*/
|
||||
export const MAX_FILE_DIFF_EDITS = 2_000;
|
||||
|
||||
/**
|
||||
* A backstop for a machine slower than the edit ceiling was tuned for. The ceiling rather than
|
||||
* this is what decides a patch's shape: a timeout that fires would slice the same change one way
|
||||
* here and another on a busier host.
|
||||
*/
|
||||
const MAX_FILE_DIFF_MILLIS = 2_000;
|
||||
|
||||
/** How much diff work one slice does before the rest is left for the next request. */
|
||||
export const MAX_DIFF_SLICE_EDITS = 6_000;
|
||||
|
||||
/** How much patch one slice carries before the rest is left for the next request. */
|
||||
export const MAX_DIFF_SLICE_BYTES = 256 * 1024;
|
||||
|
||||
/**
|
||||
* How many files one slice carries however little each one weighs. A binary, oversize, or
|
||||
* unreadable entry is just a header, so neither budget above stops a run of thousands of them.
|
||||
*/
|
||||
export const MAX_DIFF_SLICE_FILES = 300;
|
||||
|
||||
/** Git's own note for a side whose last line has no newline after it. */
|
||||
const NO_NEWLINE_MARKER = "\\ No newline at end of file";
|
||||
|
||||
/** A text's lines, without the empty one that a trailing newline leaves behind a split. */
|
||||
function contentLines(contents: string): ReadonlyArray<string> {
|
||||
if (contents === "") return [];
|
||||
const lines = contents.split("\n");
|
||||
if (lines.at(-1) === "") lines.pop();
|
||||
return lines;
|
||||
}
|
||||
|
||||
/** A NUL byte is git's own test for it, and it survives Azure's JSON envelope intact. */
|
||||
function isBinary(contents: string): boolean {
|
||||
return contents.includes("\u0000");
|
||||
}
|
||||
|
||||
/**
|
||||
* What a file costs on the wire, which is its bytes rather than its code units: a ceiling counted
|
||||
* in characters lets a file of three-byte glyphs through at three times the intended size.
|
||||
*/
|
||||
export const byteLength = (contents: string) => Buffer.byteLength(contents, "utf8");
|
||||
|
||||
/**
|
||||
* Git points an empty range at the line before it, which is line zero for a file that is wholly
|
||||
* new or wholly gone, and writes a single line as its number alone.
|
||||
*/
|
||||
function hunkRange(start: number, lines: number): string {
|
||||
if (lines === 0) return `${start - 1},0`;
|
||||
return lines === 1 ? String(start) : `${start},${lines}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* The `diff --git` preamble a viewer reads a file's identity and fate from. Azure reports no file
|
||||
* mode, so the ordinary one stands in. Names are quoted the way git quotes them, prefix inside the
|
||||
* quoting: a header reader stops a bare name at its first tab or newline, so a path holding either
|
||||
* would be truncated and its viewed mark would land on the wrong file.
|
||||
*/
|
||||
function patchHeader(change: AzureDevOpsChangeEntry): string {
|
||||
const oldSide = quoteGitPatchPath(`a/${change.oldPath}`);
|
||||
const newSide = quoteGitPatchPath(`b/${change.path}`);
|
||||
const lines = [`diff --git ${oldSide} ${newSide}`];
|
||||
if (change.changeKind === "new") lines.push("new file mode 100644");
|
||||
if (change.changeKind === "deleted") lines.push("deleted file mode 100644");
|
||||
if (change.changeKind === "rename-pure" || change.changeKind === "rename-changed") {
|
||||
lines.push(
|
||||
`rename from ${quoteGitPatchPath(change.oldPath)}`,
|
||||
`rename to ${quoteGitPatchPath(change.path)}`,
|
||||
);
|
||||
}
|
||||
lines.push(
|
||||
`--- ${change.changeKind === "new" ? "/dev/null" : oldSide}`,
|
||||
`+++ ${change.changeKind === "deleted" ? "/dev/null" : newSide}`,
|
||||
);
|
||||
return lines.join("\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* A file written out as wholly replaced: every old line gone, every new line arrived, in one
|
||||
* hunk, which needs no edit-distance search at all.
|
||||
*/
|
||||
function replacementSection(header: string, texts: AzureDevOpsFileTexts): string {
|
||||
const oldLines = contentLines(texts.oldContents);
|
||||
const newLines = contentLines(texts.newContents);
|
||||
const noNewline = (contents: string, lines: ReadonlyArray<string>) =>
|
||||
lines.length > 0 && !contents.endsWith("\n") ? [NO_NEWLINE_MARKER] : [];
|
||||
return [
|
||||
header,
|
||||
`@@ -${hunkRange(1, oldLines.length)} +${hunkRange(1, newLines.length)} @@`,
|
||||
...oldLines.map((line) => `-${line}`),
|
||||
...noNewline(texts.oldContents, oldLines),
|
||||
...newLines.map((line) => `+${line}`),
|
||||
...noNewline(texts.newContents, newLines),
|
||||
"",
|
||||
].join("\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* One file's section of a unified patch, built here because Azure has no route that returns one:
|
||||
* its diff routes name the files that changed, and the contents are a separate read per side.
|
||||
*/
|
||||
export function azureDevOpsFilePatch(input: {
|
||||
readonly change: AzureDevOpsChangeEntry;
|
||||
readonly texts: AzureDevOpsFileTexts;
|
||||
}): AzureDevOpsFilePatch {
|
||||
const header = patchHeader(input.change);
|
||||
const { oldContents, newContents } = input.texts;
|
||||
|
||||
if (input.texts.binary || isBinary(oldContents) || isBinary(newContents)) {
|
||||
// Git's own wording for a file it will not spell out.
|
||||
const oldSide = quoteGitPatchPath(`a/${input.change.oldPath}`);
|
||||
const newSide = quoteGitPatchPath(`b/${input.change.path}`);
|
||||
const binary = `Binary files ${oldSide} and ${newSide} differ`;
|
||||
return { section: `${header}\n${binary}\n`, truncated: true, abandoned: false, edits: 0 };
|
||||
}
|
||||
if (byteLength(oldContents) > MAX_FILE_BYTES || byteLength(newContents) > MAX_FILE_BYTES) {
|
||||
return { section: `${header}\n`, truncated: true, abandoned: false, edits: 0 };
|
||||
}
|
||||
|
||||
// A creation or deletion has nothing on one side, so writing both sides in full is already the
|
||||
// minimal patch and needs no diff search.
|
||||
const created = oldContents === "" && newContents !== "";
|
||||
const deleted = newContents === "" && oldContents !== "";
|
||||
if (created || deleted) {
|
||||
const contents = created ? newContents : oldContents;
|
||||
const lines = contentLines(contents);
|
||||
// A `+`/`-` marker on every line can put a side that just fits the size ceiling over it.
|
||||
// Checked against bytes-plus-marker-count rather than building the section, since that upper
|
||||
// bound is cheaper and this is exactly the file shape that would cost the most to build.
|
||||
if (byteLength(contents) + lines.length > MAX_FILE_BYTES) {
|
||||
return { section: `${header}\n`, truncated: true, abandoned: false, edits: lines.length };
|
||||
}
|
||||
const section = replacementSection(header, input.texts);
|
||||
if (byteLength(section) > MAX_FILE_BYTES) {
|
||||
return { section: `${header}\n`, truncated: true, abandoned: false, edits: lines.length };
|
||||
}
|
||||
return { section, truncated: false, abandoned: false, edits: lines.length };
|
||||
}
|
||||
|
||||
const patch = structuredPatch(
|
||||
`a/${input.change.oldPath}`,
|
||||
`b/${input.change.path}`,
|
||||
oldContents,
|
||||
newContents,
|
||||
undefined,
|
||||
undefined,
|
||||
{
|
||||
context: PATCH_CONTEXT_LINES,
|
||||
maxEditLength: MAX_FILE_DIFF_EDITS,
|
||||
timeout: MAX_FILE_DIFF_MILLIS,
|
||||
},
|
||||
);
|
||||
// Hitting the edit ceiling lists the file without hunks rather than falling back to a full
|
||||
// replacement: the ceiling is a distance, not a proportion, so a long file can reach it having
|
||||
// changed in one small corner, and both sides in full would bury that corner in a wall of text.
|
||||
if (patch === undefined) {
|
||||
return {
|
||||
section: `${header}\n`,
|
||||
truncated: true,
|
||||
abandoned: true,
|
||||
edits: MAX_FILE_DIFF_EDITS,
|
||||
};
|
||||
}
|
||||
|
||||
let edits = 0;
|
||||
const hunks = patch.hunks.map((hunk) => {
|
||||
for (const line of hunk.lines) {
|
||||
if (line.startsWith("+") || line.startsWith("-")) edits += 1;
|
||||
}
|
||||
return [
|
||||
`@@ -${hunkRange(hunk.oldStart, hunk.oldLines)} +${hunkRange(hunk.newStart, hunk.newLines)} @@`,
|
||||
...hunk.lines,
|
||||
].join("\n");
|
||||
});
|
||||
// A pure rename has no hunks to give but is still listed, since dropping it would take the file
|
||||
// out of the change altogether.
|
||||
const section = hunks.length === 0 ? `${header}\n` : `${header}\n${hunks.join("\n")}\n`;
|
||||
// The edit ceiling bounds edit distance, not the hunks' size: a handful of very long lines plus
|
||||
// context can still exceed the size ceiling even well inside the edit ceiling.
|
||||
if (byteLength(section) > MAX_FILE_BYTES) {
|
||||
return { section: `${header}\n`, truncated: true, abandoned: false, edits };
|
||||
}
|
||||
return { section, truncated: false, abandoned: false, edits };
|
||||
}
|
||||
|
||||
/** A file listed without its hunks, for when the host would not hand one of its two sides over. */
|
||||
export function azureDevOpsUnreadableFilePatch(
|
||||
change: AzureDevOpsChangeEntry,
|
||||
): AzureDevOpsFilePatch {
|
||||
return { section: `${patchHeader(change)}\n`, truncated: true, abandoned: false, edits: 0 };
|
||||
}
|
||||
@@ -1,652 +0,0 @@
|
||||
import * as Result from "effect/Result";
|
||||
import { describe, expect, it } from "vite-plus/test";
|
||||
|
||||
import {
|
||||
decodeItemContentJson,
|
||||
decodeIterationChangesJson,
|
||||
decodeIterationsJson,
|
||||
decodePullRequestJson,
|
||||
decodePullRequestListJson,
|
||||
decodeThreadsJson,
|
||||
decodeViewerJson,
|
||||
} from "./azureDevOpsPullRequestJson.ts";
|
||||
|
||||
const REST_URL =
|
||||
"https://dev.azure.com/acme/_apis/git/repositories/6f9c9b7f-0000-0000-0000-000000000000/pullRequests/42";
|
||||
|
||||
/** Shaped after Azure's `GitPullRequest`, trimmed to the fields that are read. */
|
||||
function pullRequest(overrides: Record<string, unknown> = {}): Record<string, unknown> {
|
||||
return {
|
||||
pullRequestId: 42,
|
||||
title: "Add the change requests page",
|
||||
description: "Ships the page.",
|
||||
status: "active",
|
||||
isDraft: false,
|
||||
mergeStatus: "succeeded",
|
||||
createdBy: { displayName: "Bilal Hassan", uniqueName: "[email protected]" },
|
||||
sourceRefName: "refs/heads/feat/page",
|
||||
targetRefName: "refs/heads/main",
|
||||
creationDate: "2026-07-01T00:00:00Z",
|
||||
url: REST_URL,
|
||||
repository: { name: "web", project: { name: "platform" } },
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
function expectSuccess<A>(result: Result.Result<A, unknown>): A {
|
||||
expect(Result.isSuccess(result)).toBe(true);
|
||||
if (!Result.isSuccess(result)) throw new Error("expected a successful decode");
|
||||
return result.success;
|
||||
}
|
||||
|
||||
const asJson = (value: unknown) => JSON.stringify(value);
|
||||
|
||||
describe("decodePullRequestListJson", () => {
|
||||
it("reads a pull request as a change request", () => {
|
||||
const batch = expectSuccess(decodePullRequestListJson(asJson([pullRequest()])));
|
||||
|
||||
expect(batch.items).toHaveLength(1);
|
||||
expect(batch.items[0]).toMatchObject({
|
||||
number: 42,
|
||||
title: "Add the change requests page",
|
||||
// The login is an email, because that is what `az account show` reports to compare with.
|
||||
author: { login: "[email protected]", name: "Bilal Hassan" },
|
||||
// Azure prefixes its refs, which no other host does.
|
||||
headBranch: "feat/page",
|
||||
baseBranch: "main",
|
||||
state: "open",
|
||||
isDraft: false,
|
||||
mergeability: "mergeable",
|
||||
});
|
||||
});
|
||||
|
||||
it("assembles a browser url when Azure reports no web link", () => {
|
||||
const batch = expectSuccess(decodePullRequestListJson(asJson([pullRequest()])));
|
||||
|
||||
expect(batch.items[0]?.url).toBe("https://dev.azure.com/acme/platform/_git/web/pullrequest/42");
|
||||
});
|
||||
|
||||
it("prefers the web link Azure sends when asked for one", () => {
|
||||
const batch = expectSuccess(
|
||||
decodePullRequestListJson(
|
||||
asJson([
|
||||
pullRequest({
|
||||
_links: {
|
||||
web: { href: "https://dev.azure.com/acme/platform/_git/web/pullrequest/42" },
|
||||
},
|
||||
}),
|
||||
]),
|
||||
),
|
||||
);
|
||||
|
||||
expect(batch.items[0]?.url).toBe("https://dev.azure.com/acme/platform/_git/web/pullrequest/42");
|
||||
});
|
||||
|
||||
it.each([
|
||||
["active", "open"],
|
||||
["completed", "merged"],
|
||||
["abandoned", "closed"],
|
||||
["something new", "open"],
|
||||
])("reads the %s status as %s", (status, expected) => {
|
||||
const batch = expectSuccess(decodePullRequestListJson(asJson([pullRequest({ status })])));
|
||||
|
||||
expect(batch.items[0]?.state).toBe(expected);
|
||||
});
|
||||
|
||||
it.each([
|
||||
["succeeded", "mergeable"],
|
||||
["conflicts", "conflicting"],
|
||||
["rejectedByPolicy", "conflicting"],
|
||||
["queued", "unknown"],
|
||||
["notSet", "unknown"],
|
||||
])("reads the %s merge status as %s", (mergeStatus, expected) => {
|
||||
const batch = expectSuccess(decodePullRequestListJson(asJson([pullRequest({ mergeStatus })])));
|
||||
|
||||
expect(batch.items[0]?.mergeability).toBe(expected);
|
||||
});
|
||||
|
||||
it("stands the closing time in for a last-touched time Azure does not keep", () => {
|
||||
const batch = expectSuccess(
|
||||
decodePullRequestListJson(
|
||||
asJson([pullRequest({ status: "completed", closedDate: "2026-07-05T00:00:00Z" })]),
|
||||
),
|
||||
);
|
||||
|
||||
expect(batch.items[0]).toMatchObject({
|
||||
createdAt: "2026-07-01T00:00:00Z",
|
||||
updatedAt: "2026-07-05T00:00:00Z",
|
||||
});
|
||||
});
|
||||
|
||||
it("skips a malformed row but still counts it, so paging does not stop early", () => {
|
||||
const batch = expectSuccess(
|
||||
decodePullRequestListJson(asJson([{ pullRequestId: "nope" }, pullRequest()])),
|
||||
);
|
||||
|
||||
expect(batch.items).toHaveLength(1);
|
||||
expect(batch.rawCount).toBe(2);
|
||||
expect(batch.rawIndexes).toEqual([1]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("decodePullRequestJson", () => {
|
||||
it("reads reviewers as review requests", () => {
|
||||
const detail = expectSuccess(
|
||||
decodePullRequestJson(
|
||||
asJson(
|
||||
pullRequest({
|
||||
reviewers: [{ displayName: "Julius", uniqueName: "[email protected]", vote: 10 }],
|
||||
}),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
expect(detail?.reviewRequestLogins).toEqual(["[email protected]"]);
|
||||
expect(detail?.reviewers).toEqual([
|
||||
{ login: "[email protected]", name: "Julius", avatarUrl: null },
|
||||
]);
|
||||
});
|
||||
|
||||
it("reads auto-complete from whoever armed it, and its absence as nobody", () => {
|
||||
const armed = expectSuccess(
|
||||
decodePullRequestJson(
|
||||
asJson(pullRequest({ autoCompleteSetBy: { displayName: "Bilal Hassan" } })),
|
||||
),
|
||||
);
|
||||
expect(armed?.autoMergeEnabled).toBe(true);
|
||||
|
||||
// Azure leaves the field out entirely rather than sending it empty, so its absence is the
|
||||
// whole of what it says about auto-complete being off.
|
||||
expect(expectSuccess(decodePullRequestJson(asJson(pullRequest())))?.autoMergeEnabled).toBe(
|
||||
false,
|
||||
);
|
||||
});
|
||||
|
||||
it("keeps the strategy stored with auto-complete", () => {
|
||||
const armed = expectSuccess(
|
||||
decodePullRequestJson(
|
||||
asJson(
|
||||
pullRequest({
|
||||
autoCompleteSetBy: { displayName: "Bilal Hassan" },
|
||||
completionOptions: { mergeStrategy: "squash" },
|
||||
}),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
expect(armed).toMatchObject({ autoMergeEnabled: true, autoMergeMethod: "squash" });
|
||||
|
||||
const unspecified = expectSuccess(
|
||||
decodePullRequestJson(
|
||||
asJson(
|
||||
pullRequest({
|
||||
autoCompleteSetBy: { displayName: "Bilal Hassan" },
|
||||
completionOptions: { squashMerge: false },
|
||||
}),
|
||||
),
|
||||
),
|
||||
);
|
||||
expect(unspecified?.autoMergeEnabled).toBe(true);
|
||||
expect(unspecified?.autoMergeMethod).toBeUndefined();
|
||||
});
|
||||
|
||||
it("works out where the repository lives from what Azure returned", () => {
|
||||
const detail = expectSuccess(decodePullRequestJson(asJson(pullRequest())));
|
||||
|
||||
expect(detail?.location).toEqual({ project: "platform", repository: "web" });
|
||||
});
|
||||
|
||||
it("reports no repository location when Azure said too little to name one", () => {
|
||||
// A web link places the pull request, but with no repository named there is nothing to
|
||||
// address the routes that read its files and its conversation.
|
||||
const detail = expectSuccess(
|
||||
decodePullRequestJson(
|
||||
asJson(
|
||||
pullRequest({
|
||||
url: null,
|
||||
repository: null,
|
||||
_links: {
|
||||
web: { href: "https://dev.azure.com/acme/platform/_git/web/pullrequest/42" },
|
||||
},
|
||||
}),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
expect(detail?.location).toBeNull();
|
||||
});
|
||||
|
||||
it("returns nothing when Azure gave no way to place the pull request at all", () => {
|
||||
const detail = expectSuccess(
|
||||
decodePullRequestJson(asJson(pullRequest({ url: null, repository: null }))),
|
||||
);
|
||||
|
||||
expect(detail).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("decodeViewerJson", () => {
|
||||
it("reads the signed-in account name", () => {
|
||||
expect(expectSuccess(decodeViewerJson(asJson({ user: { name: "[email protected]" } })))).toBe(
|
||||
"[email protected]",
|
||||
);
|
||||
});
|
||||
|
||||
it("returns nothing when nobody is signed in", () => {
|
||||
expect(expectSuccess(decodeViewerJson(asJson({ user: null })))).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("decodeThreadsJson", () => {
|
||||
it("takes every real comment of every thread, oldest first", () => {
|
||||
const comments = expectSuccess(
|
||||
decodeThreadsJson(
|
||||
asJson({
|
||||
value: [
|
||||
{
|
||||
id: 2,
|
||||
comments: [
|
||||
{
|
||||
id: 1,
|
||||
content: "Second remark.",
|
||||
author: { displayName: "Julius", uniqueName: "[email protected]" },
|
||||
publishedDate: "2026-07-03T00:00:00Z",
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
id: 1,
|
||||
comments: [
|
||||
// Azure's own activity notes are events rather than remarks.
|
||||
{ id: 1, content: "Bilal voted", commentType: "system", publishedDate: "x" },
|
||||
{
|
||||
id: 2,
|
||||
content: "First remark.",
|
||||
author: { displayName: "Bilal", uniqueName: "[email protected]" },
|
||||
publishedDate: "2026-07-02T00:00:00Z",
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
expect(comments.map((comment) => comment.body)).toEqual(["First remark.", "Second remark."]);
|
||||
expect(comments[0]).toMatchObject({
|
||||
kind: "issue-comment",
|
||||
author: { login: "[email protected]" },
|
||||
});
|
||||
});
|
||||
|
||||
it("reads a thread pinned to a file as a review comment", () => {
|
||||
const comments = expectSuccess(
|
||||
decodeThreadsJson(
|
||||
asJson({
|
||||
value: [
|
||||
{
|
||||
id: 3,
|
||||
threadContext: { filePath: "/src/app.ts" },
|
||||
comments: [{ id: 1, content: "Rename this.", publishedDate: "2026-07-02T00:00:00Z" }],
|
||||
},
|
||||
],
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
expect(comments[0]).toMatchObject({ kind: "review-comment", path: "/src/app.ts" });
|
||||
});
|
||||
|
||||
it("keeps the replies under a thread, which are as much of the conversation", () => {
|
||||
const comments = expectSuccess(
|
||||
decodeThreadsJson(
|
||||
asJson({
|
||||
value: [
|
||||
{
|
||||
id: 4,
|
||||
threadContext: { filePath: "/src/app.ts" },
|
||||
comments: [
|
||||
{ id: 1, content: "Rename this.", publishedDate: "2026-07-02T00:00:00Z" },
|
||||
{ id: 2, content: "Renamed.", publishedDate: "2026-07-02T01:00:00Z" },
|
||||
{ id: 3, content: "Thanks.", publishedDate: "2026-07-02T02:00:00Z" },
|
||||
],
|
||||
},
|
||||
],
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
expect(comments.map((comment) => comment.id)).toEqual(["4:1", "4:2", "4:3"]);
|
||||
});
|
||||
|
||||
it("drops deleted threads and threads with nothing to show", () => {
|
||||
const comments = expectSuccess(
|
||||
decodeThreadsJson(
|
||||
asJson({
|
||||
value: [
|
||||
{
|
||||
id: 1,
|
||||
isDeleted: true,
|
||||
comments: [{ id: 1, content: "gone", publishedDate: "2026-07-02T00:00:00Z" }],
|
||||
},
|
||||
{ id: 2, comments: [] },
|
||||
{
|
||||
id: 3,
|
||||
comments: [{ id: 1, content: " ", publishedDate: "2026-07-02T00:00:00Z" }],
|
||||
},
|
||||
],
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
expect(comments).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("decodeIterationsJson", () => {
|
||||
const iteration = (id: number, head: string, base: string) => ({
|
||||
id,
|
||||
sourceRefCommit: { commitId: head },
|
||||
commonRefCommit: { commitId: base },
|
||||
targetRefCommit: { commitId: base },
|
||||
});
|
||||
|
||||
it("reads every push in order, oldest first", () => {
|
||||
const iterations = expectSuccess(
|
||||
decodeIterationsJson(
|
||||
asJson({ value: [iteration(2, "bbb", "base"), iteration(1, "aaa", "base")] }),
|
||||
),
|
||||
);
|
||||
|
||||
expect(iterations.map((entry) => entry.id)).toEqual([1, 2]);
|
||||
expect(iterations.at(-1)).toEqual({ id: 2, headCommit: "bbb", mergeBaseCommit: "base" });
|
||||
});
|
||||
|
||||
it("skips a push Azure could not place both ends of", () => {
|
||||
// A patch is taken over a range, and an iteration missing either end names no range at all.
|
||||
const iterations = expectSuccess(
|
||||
decodeIterationsJson(
|
||||
asJson({
|
||||
value: [{ id: 1, sourceRefCommit: { commitId: "aaa" } }, iteration(2, "bbb", "base")],
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
expect(iterations.map((entry) => entry.id)).toEqual([2]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("decodeIterationChangesJson", () => {
|
||||
it("names each changed file without the slash Azure leads its paths with", () => {
|
||||
const page = expectSuccess(
|
||||
decodeIterationChangesJson(
|
||||
asJson({
|
||||
changeEntries: [
|
||||
{ changeType: "add", item: { path: "/DEMO.md", objectId: "ec00" } },
|
||||
{
|
||||
changeType: "edit",
|
||||
item: { path: "/README.md", objectId: "8f80", originalObjectId: "0ca4" },
|
||||
},
|
||||
{ changeType: "delete", item: { path: "/OLD.md", originalObjectId: "1111" } },
|
||||
],
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
expect(page.changes.map((change) => [change.path, change.changeKind])).toEqual([
|
||||
["DEMO.md", "new"],
|
||||
["README.md", "change"],
|
||||
["OLD.md", "deleted"],
|
||||
]);
|
||||
});
|
||||
|
||||
it("keeps a space at the end of a file's name, which belongs to the name", () => {
|
||||
// Git will carry a name that ends in a space, and the patch and the viewed mark are both
|
||||
// keyed by it. Tidying it here files the change under a name nothing else uses.
|
||||
const page = expectSuccess(
|
||||
decodeIterationChangesJson(
|
||||
asJson({
|
||||
changeEntries: [
|
||||
{ changeType: "edit", item: { path: "/docs/readme.md ", objectId: "ec00" } },
|
||||
{
|
||||
changeType: "rename",
|
||||
sourceServerItem: "/docs/old.md ",
|
||||
item: { path: "/docs/moved.md", objectId: "aaaa", originalObjectId: "aaaa" },
|
||||
},
|
||||
],
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
expect(page.changes.map((change) => [change.path, change.oldPath])).toEqual([
|
||||
["docs/readme.md ", "docs/readme.md "],
|
||||
["docs/moved.md", "docs/old.md "],
|
||||
]);
|
||||
});
|
||||
|
||||
it("reads a rename as one file that moved, and says whether it also changed", () => {
|
||||
const page = expectSuccess(
|
||||
decodeIterationChangesJson(
|
||||
asJson({
|
||||
changeEntries: [
|
||||
{
|
||||
changeType: "rename",
|
||||
sourceServerItem: "/docs/old.md",
|
||||
item: { path: "/docs/new.md", objectId: "aaaa", originalObjectId: "aaaa" },
|
||||
},
|
||||
{
|
||||
changeType: "edit, rename",
|
||||
sourceServerItem: "/src/old.ts",
|
||||
item: { path: "/src/new.ts", objectId: "bbbb", originalObjectId: "cccc" },
|
||||
},
|
||||
],
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
expect(page.changes).toEqual([
|
||||
{
|
||||
path: "docs/new.md",
|
||||
oldPath: "docs/old.md",
|
||||
changeKind: "rename-pure",
|
||||
objectId: "aaaa",
|
||||
originalObjectId: "aaaa",
|
||||
},
|
||||
{
|
||||
path: "src/new.ts",
|
||||
oldPath: "src/old.ts",
|
||||
changeKind: "rename-changed",
|
||||
objectId: "bbbb",
|
||||
originalObjectId: "cccc",
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it("drops the folders Azure lists alongside the files that changed", () => {
|
||||
// A review shows files, and a folder has no content on either side to show for one.
|
||||
const page = expectSuccess(
|
||||
decodeIterationChangesJson(
|
||||
asJson({
|
||||
changeEntries: [
|
||||
{ changeType: "add", item: { path: "/docs", isFolder: true, gitObjectType: "tree" } },
|
||||
{ changeType: "add", item: { path: "/docs/page.md", objectId: "dddd" } },
|
||||
],
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
expect(page.changes.map((change) => change.path)).toEqual(["docs/page.md"]);
|
||||
});
|
||||
|
||||
it("carries where the next page of a long change starts", () => {
|
||||
const page = expectSuccess(
|
||||
decodeIterationChangesJson(
|
||||
asJson({
|
||||
changeEntries: [{ changeType: "add", item: { path: "/DEMO.md", objectId: "ec00" } }],
|
||||
nextSkip: 2000,
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
expect(page.nextSkip).toBe(2000);
|
||||
});
|
||||
|
||||
it("reads the last page, which names no page after it, as the end of the change", () => {
|
||||
const page = expectSuccess(
|
||||
decodeIterationChangesJson(
|
||||
asJson({
|
||||
changeEntries: [{ changeType: "add", item: { path: "/DEMO.md", objectId: "ec00" } }],
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
expect(page.nextSkip).toBeNull();
|
||||
});
|
||||
|
||||
it("reads where a rename came from out of either of the two places Azure names it", () => {
|
||||
// The iteration-changes route answers with `originalPath`; the commit routes answer with
|
||||
// `sourceServerItem`, and both are the same fact under two names.
|
||||
const page = expectSuccess(
|
||||
decodeIterationChangesJson(
|
||||
asJson({
|
||||
changeEntries: [
|
||||
{
|
||||
changeType: "rename",
|
||||
originalPath: "/docs/old.md",
|
||||
item: { path: "/docs/new.md", objectId: "aaaa", originalObjectId: "aaaa" },
|
||||
},
|
||||
],
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
expect(page.changes.at(0)?.oldPath).toBe("docs/old.md");
|
||||
});
|
||||
});
|
||||
|
||||
describe("decodeItemContentJson", () => {
|
||||
it("reads the file's text out of the envelope Azure wraps it in", () => {
|
||||
expect(
|
||||
expectSuccess(decodeItemContentJson(asJson({ path: "/a.md", content: "one\ntwo" }))),
|
||||
).toEqual({ contents: "one\ntwo", isBinary: false });
|
||||
});
|
||||
|
||||
it("reads an empty file as empty rather than as a failure to look", () => {
|
||||
expect(expectSuccess(decodeItemContentJson(asJson({ path: "/a.md" })))).toEqual({
|
||||
contents: "",
|
||||
isBinary: false,
|
||||
});
|
||||
});
|
||||
|
||||
it("keeps Azure's own word that a file is binary", () => {
|
||||
// Which it answers base64-encoded, so nothing in the text it sent would give it away.
|
||||
expect(
|
||||
expectSuccess(
|
||||
decodeItemContentJson(
|
||||
asJson({ path: "/logo.png", content: "b2xk", contentMetadata: { isBinary: true } }),
|
||||
),
|
||||
),
|
||||
).toEqual({ contents: "b2xk", isBinary: true });
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* Every other fixture in this file is written from Azure's published contract. These are the
|
||||
* shapes a real organisation answered with, read back through `az devops invoke` rather than
|
||||
* `az rest`: the extension hands over the route's own body with one key of its own added, and a
|
||||
* live repository leaves out fields the contract documents.
|
||||
*/
|
||||
describe("what az devops invoke answers with", () => {
|
||||
it("reads the envelope the extension adds its own continuation token to", () => {
|
||||
// Set on every JSON body it returns, from a response header these routes do not send, so it
|
||||
// arrives as null rather than not at all. Nothing reads it, and it must not fail the decode.
|
||||
expect(
|
||||
expectSuccess(decodeThreadsJson(asJson({ value: [], count: 0, continuation_token: null }))),
|
||||
).toEqual([]);
|
||||
|
||||
expect(
|
||||
expectSuccess(
|
||||
decodeIterationsJson(
|
||||
asJson({
|
||||
count: 1,
|
||||
continuation_token: null,
|
||||
value: [
|
||||
{
|
||||
id: 1,
|
||||
sourceRefCommit: { commitId: "f4031105213c68f197cd46ea303bb5e72acc889a" },
|
||||
commonRefCommit: { commitId: "acbc805382edd6c38b8d6de6ba9f9bba9da4ad35" },
|
||||
},
|
||||
],
|
||||
}),
|
||||
),
|
||||
),
|
||||
).toEqual([
|
||||
{
|
||||
id: 1,
|
||||
headCommit: "f4031105213c68f197cd46ea303bb5e72acc889a",
|
||||
mergeBaseCommit: "acbc805382edd6c38b8d6de6ba9f9bba9da4ad35",
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it("keeps the files of a change that names neither their object type nor their page after it", () => {
|
||||
// A live iteration states `changeType`, `item.path` and `item.objectId` and nothing else: no
|
||||
// `gitObjectType`, no `isFolder`, and no `nextSkip` on the only page. Each of those absences
|
||||
// is what the defaults in the decoder are for, and reading any of them as stated would drop
|
||||
// every file of every Azure change request.
|
||||
const page = expectSuccess(
|
||||
decodeIterationChangesJson(
|
||||
asJson({
|
||||
continuation_token: null,
|
||||
changeEntries: [
|
||||
{ changeType: "add", item: { path: "/DEMO.md", objectId: "EC005DB24" } },
|
||||
{ changeType: "edit", item: { path: "/README.md", objectId: "8F8047A49" } },
|
||||
],
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
expect(page.nextSkip).toBeNull();
|
||||
expect(page.changes).toEqual([
|
||||
{
|
||||
path: "DEMO.md",
|
||||
oldPath: "DEMO.md",
|
||||
changeKind: "new",
|
||||
objectId: "EC005DB24",
|
||||
originalObjectId: null,
|
||||
},
|
||||
{
|
||||
path: "README.md",
|
||||
oldPath: "README.md",
|
||||
changeKind: "change",
|
||||
objectId: "8F8047A49",
|
||||
originalObjectId: null,
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it("reads a text file whose content type Azure calls a stream", () => {
|
||||
// `contentMetadata` comes back for a markdown file with `application/octet-stream` on it and
|
||||
// no `isBinary` at all, so the content type is not the field to ask, and its absence is the
|
||||
// answer that the text is text.
|
||||
expect(
|
||||
expectSuccess(
|
||||
decodeItemContentJson(
|
||||
asJson({
|
||||
path: "/README.md",
|
||||
objectId: "8f8047a49",
|
||||
gitObjectType: "blob",
|
||||
content: "# T3Demo\n",
|
||||
contentMetadata: {
|
||||
contentType: "application/octet-stream",
|
||||
encoding: 65001,
|
||||
extension: "md",
|
||||
fileName: "README.md",
|
||||
},
|
||||
continuation_token: null,
|
||||
}),
|
||||
),
|
||||
),
|
||||
).toEqual({ contents: "# T3Demo\n", isBinary: false });
|
||||
});
|
||||
});
|
||||
@@ -1,574 +0,0 @@
|
||||
import * as Cause from "effect/Cause";
|
||||
import * as Exit from "effect/Exit";
|
||||
import * as Result from "effect/Result";
|
||||
import * as Schema from "effect/Schema";
|
||||
import type {
|
||||
PullRequestActor,
|
||||
PullRequestComment,
|
||||
PullRequestMergeMethod,
|
||||
PullRequestMergeability,
|
||||
PullRequestState,
|
||||
} from "@t3tools/contracts";
|
||||
import { TrimmedNonEmptyString } from "@t3tools/contracts";
|
||||
import { decodeJsonResult } from "@t3tools/shared/schemaJson";
|
||||
|
||||
import { azureDevOpsPullRequestWebUrl } from "../sourceControl/azureDevOpsPullRequests.ts";
|
||||
|
||||
/**
|
||||
* Azure's enums are decoded as plain strings and normalized here, in the same tolerant style as
|
||||
* the other hosts: a new merge status must not fail a whole payload. Every field beyond the
|
||||
* identity is optional, because `az repos pr` returns rather more or less of the REST object
|
||||
* depending on the command.
|
||||
*/
|
||||
const RawIdentitySchema = Schema.Struct({
|
||||
displayName: Schema.optional(Schema.NullOr(Schema.String)),
|
||||
/** An email or UPN, which is what `az account show` reports for the signed-in user. */
|
||||
uniqueName: Schema.optional(Schema.NullOr(Schema.String)),
|
||||
imageUrl: Schema.optional(Schema.NullOr(Schema.String)),
|
||||
});
|
||||
|
||||
const RawPullRequestSchema = Schema.Struct({
|
||||
pullRequestId: Schema.Int,
|
||||
title: Schema.String,
|
||||
description: Schema.optional(Schema.NullOr(Schema.String)),
|
||||
status: Schema.optional(Schema.NullOr(Schema.String)),
|
||||
isDraft: Schema.optional(Schema.NullOr(Schema.Boolean)),
|
||||
/**
|
||||
* Who armed auto-complete, which is the only thing Azure says about it: the field carries an
|
||||
* identity while the pull request is set to complete on its own, and Azure leaves it out
|
||||
* entirely once nobody has. So its presence is the answer, and there is no third state.
|
||||
*/
|
||||
autoCompleteSetBy: Schema.optional(Schema.NullOr(RawIdentitySchema)),
|
||||
completionOptions: Schema.optional(
|
||||
Schema.NullOr(
|
||||
Schema.Struct({
|
||||
mergeStrategy: Schema.optional(Schema.NullOr(Schema.String)),
|
||||
squashMerge: Schema.optional(Schema.NullOr(Schema.Boolean)),
|
||||
}),
|
||||
),
|
||||
),
|
||||
mergeStatus: Schema.optional(Schema.NullOr(Schema.String)),
|
||||
createdBy: Schema.optional(Schema.NullOr(RawIdentitySchema)),
|
||||
reviewers: Schema.optional(Schema.NullOr(Schema.Array(RawIdentitySchema))),
|
||||
// Required, and required to be non-empty: the wire contract will not carry a change request
|
||||
// without a branch or a created time, so a row missing one is skipped rather than breaking the
|
||||
// response it travels in.
|
||||
sourceRefName: TrimmedNonEmptyString,
|
||||
targetRefName: TrimmedNonEmptyString,
|
||||
creationDate: TrimmedNonEmptyString,
|
||||
closedDate: Schema.optional(Schema.NullOr(Schema.String)),
|
||||
url: Schema.optional(Schema.NullOr(Schema.String)),
|
||||
repository: Schema.optional(
|
||||
Schema.NullOr(
|
||||
Schema.Struct({
|
||||
name: Schema.optional(Schema.NullOr(Schema.String)),
|
||||
webUrl: Schema.optional(Schema.NullOr(Schema.String)),
|
||||
project: Schema.optional(
|
||||
Schema.NullOr(Schema.Struct({ name: Schema.optional(Schema.NullOr(Schema.String)) })),
|
||||
),
|
||||
}),
|
||||
),
|
||||
),
|
||||
_links: Schema.optional(
|
||||
Schema.NullOr(
|
||||
Schema.Struct({
|
||||
web: Schema.optional(
|
||||
Schema.NullOr(Schema.Struct({ href: Schema.optional(Schema.String) })),
|
||||
),
|
||||
}),
|
||||
),
|
||||
),
|
||||
});
|
||||
|
||||
/** A pull request thread, which is how Azure keeps its conversation. */
|
||||
const RawThreadSchema = Schema.Struct({
|
||||
id: Schema.Int,
|
||||
isDeleted: Schema.optional(Schema.NullOr(Schema.Boolean)),
|
||||
threadContext: Schema.optional(
|
||||
Schema.NullOr(Schema.Struct({ filePath: Schema.optional(Schema.NullOr(Schema.String)) })),
|
||||
),
|
||||
comments: Schema.optional(
|
||||
Schema.NullOr(
|
||||
Schema.Array(
|
||||
Schema.Struct({
|
||||
id: Schema.optional(Schema.NullOr(Schema.Int)),
|
||||
content: Schema.optional(Schema.NullOr(Schema.String)),
|
||||
author: Schema.optional(Schema.NullOr(RawIdentitySchema)),
|
||||
publishedDate: Schema.optional(Schema.NullOr(Schema.String)),
|
||||
isDeleted: Schema.optional(Schema.NullOr(Schema.Boolean)),
|
||||
/** `system` marks the notes Azure writes itself, which are events, not comments. */
|
||||
commentType: Schema.optional(Schema.NullOr(Schema.String)),
|
||||
}),
|
||||
),
|
||||
),
|
||||
),
|
||||
});
|
||||
|
||||
const RawThreadPageSchema = Schema.Struct({
|
||||
value: Schema.Array(Schema.Unknown),
|
||||
});
|
||||
|
||||
const RawViewerSchema = Schema.Struct({
|
||||
user: Schema.optional(
|
||||
Schema.NullOr(Schema.Struct({ name: Schema.optional(Schema.NullOr(Schema.String)) })),
|
||||
),
|
||||
});
|
||||
|
||||
/**
|
||||
* Where a repository lives, in the terms Azure's REST routes address it by. They take the project
|
||||
* and the repository as separate route parameters rather than as one path, so the pair travels
|
||||
* together rather than as a URL that would have to be taken apart again to use.
|
||||
*/
|
||||
export interface AzureDevOpsRepositoryLocation {
|
||||
readonly project: string;
|
||||
readonly repository: string;
|
||||
}
|
||||
|
||||
export interface AzureDevOpsPullRequest {
|
||||
readonly number: number;
|
||||
readonly title: string;
|
||||
readonly url: string;
|
||||
readonly author: PullRequestActor | null;
|
||||
readonly headBranch: string;
|
||||
readonly baseBranch: string;
|
||||
readonly state: PullRequestState;
|
||||
readonly isDraft: boolean;
|
||||
readonly mergeability: PullRequestMergeability;
|
||||
readonly createdAt: string;
|
||||
/**
|
||||
* Azure records no last-touched time on a pull request, so the closing time stands in where
|
||||
* there is one and the creation time otherwise. The same fallback the rest of the app uses.
|
||||
*/
|
||||
readonly updatedAt: string;
|
||||
readonly closedAt: string | null;
|
||||
readonly body: string;
|
||||
readonly reviewRequestLogins: ReadonlyArray<string>;
|
||||
readonly reviewers: ReadonlyArray<PullRequestActor>;
|
||||
/** Where this pull request lives, when Azure said enough to work it out. */
|
||||
readonly location: AzureDevOpsRepositoryLocation | null;
|
||||
/** Whether Azure is set to complete this on its own once its policies pass. */
|
||||
readonly autoMergeEnabled: boolean;
|
||||
/** The completion strategy Azure stored with auto-complete, where it reported one. */
|
||||
readonly autoMergeMethod?: PullRequestMergeMethod;
|
||||
}
|
||||
|
||||
function trimmed(value: string | null | undefined): string | null {
|
||||
const text = value?.trim() ?? "";
|
||||
return text.length > 0 ? text : null;
|
||||
}
|
||||
|
||||
function normalizeRefName(refName: string): string {
|
||||
return refName.trim().replace(/^refs\/heads\//, "");
|
||||
}
|
||||
|
||||
/** A login has to compare against `az account show`, which reports an email. */
|
||||
function toActor(raw: Schema.Schema.Type<typeof RawIdentitySchema> | null | undefined) {
|
||||
const login = trimmed(raw?.uniqueName) ?? trimmed(raw?.displayName);
|
||||
return login === null
|
||||
? null
|
||||
: { login, name: trimmed(raw?.displayName), avatarUrl: trimmed(raw?.imageUrl) };
|
||||
}
|
||||
|
||||
function toState(raw: Schema.Schema.Type<typeof RawPullRequestSchema>): PullRequestState {
|
||||
switch (raw.status?.trim().toLowerCase()) {
|
||||
case "completed":
|
||||
return "merged";
|
||||
case "abandoned":
|
||||
return "closed";
|
||||
default:
|
||||
return "open";
|
||||
}
|
||||
}
|
||||
|
||||
function toMergeability(value: string | null | undefined): PullRequestMergeability {
|
||||
switch (value?.trim().toLowerCase()) {
|
||||
case "succeeded":
|
||||
return "mergeable";
|
||||
case "conflicts":
|
||||
case "failure":
|
||||
case "rejectedbypolicy":
|
||||
return "conflicting";
|
||||
default:
|
||||
// `queued` and `notSet` mean Azure has not finished checking.
|
||||
return "unknown";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Where a pull request's own repository sits. Taken from what Azure returned rather than from the
|
||||
* local remote, whose shape differs between the modern, legacy and SSH forms.
|
||||
*/
|
||||
function toLocation(
|
||||
raw: Schema.Schema.Type<typeof RawPullRequestSchema>,
|
||||
): AzureDevOpsRepositoryLocation | null {
|
||||
const project = trimmed(raw.repository?.project?.name);
|
||||
const repository = trimmed(raw.repository?.name);
|
||||
if (project === null || repository === null) return null;
|
||||
return { project, repository };
|
||||
}
|
||||
|
||||
function toAutoMergeMethod(
|
||||
raw: Schema.Schema.Type<typeof RawPullRequestSchema>,
|
||||
): PullRequestMergeMethod | undefined {
|
||||
if (raw.autoCompleteSetBy == null) return undefined;
|
||||
switch (raw.completionOptions?.mergeStrategy?.trim().toLowerCase()) {
|
||||
case "squash":
|
||||
return "squash";
|
||||
case "rebase":
|
||||
case "rebasemerge":
|
||||
return "rebase";
|
||||
case "nofastforward":
|
||||
return "merge";
|
||||
default:
|
||||
return raw.completionOptions?.squashMerge === true ? "squash" : undefined;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Null when Azure said too little to place the pull request: a row with no browser url and no
|
||||
* branch left after its prefix is dropped cannot be rendered or opened, and the wire contract
|
||||
* refuses to carry it either.
|
||||
*/
|
||||
function toPullRequest(
|
||||
raw: Schema.Schema.Type<typeof RawPullRequestSchema>,
|
||||
): AzureDevOpsPullRequest | null {
|
||||
const autoMergeMethod = toAutoMergeMethod(raw);
|
||||
const reviewers = (raw.reviewers ?? []).flatMap((reviewer) => {
|
||||
const actor = toActor(reviewer);
|
||||
return actor === null ? [] : [actor];
|
||||
});
|
||||
const closedAt = trimmed(raw.closedDate);
|
||||
const url = trimmed(
|
||||
azureDevOpsPullRequestWebUrl({
|
||||
pullRequestId: raw.pullRequestId,
|
||||
webLink: raw._links?.web?.href,
|
||||
repositoryWebUrl: raw.repository?.webUrl,
|
||||
restApiUrl: raw.url,
|
||||
projectName: raw.repository?.project?.name,
|
||||
repositoryName: raw.repository?.name,
|
||||
}),
|
||||
);
|
||||
const headBranch = trimmed(normalizeRefName(raw.sourceRefName));
|
||||
const baseBranch = trimmed(normalizeRefName(raw.targetRefName));
|
||||
if (url === null || headBranch === null || baseBranch === null) return null;
|
||||
return {
|
||||
number: raw.pullRequestId,
|
||||
title: raw.title,
|
||||
url,
|
||||
author: toActor(raw.createdBy),
|
||||
headBranch,
|
||||
baseBranch,
|
||||
state: toState(raw),
|
||||
isDraft: raw.isDraft ?? false,
|
||||
mergeability: toMergeability(raw.mergeStatus),
|
||||
createdAt: raw.creationDate,
|
||||
updatedAt: closedAt ?? raw.creationDate,
|
||||
closedAt,
|
||||
body: raw.description ?? "",
|
||||
reviewRequestLogins: reviewers.map((reviewer) => reviewer.login),
|
||||
reviewers,
|
||||
location: toLocation(raw),
|
||||
autoMergeEnabled: (raw.autoCompleteSetBy ?? null) !== null,
|
||||
...(autoMergeMethod === undefined ? {} : { autoMergeMethod }),
|
||||
};
|
||||
}
|
||||
|
||||
const decodeUnknownList = decodeJsonResult(Schema.Array(Schema.Unknown));
|
||||
const decodePullRequestEntry = Schema.decodeUnknownExit(RawPullRequestSchema);
|
||||
const decodePullRequest = decodeJsonResult(RawPullRequestSchema);
|
||||
const decodeThreadPage = decodeJsonResult(RawThreadPageSchema);
|
||||
const decodeThreadEntry = Schema.decodeUnknownExit(RawThreadSchema);
|
||||
const decodeViewer = decodeJsonResult(RawViewerSchema);
|
||||
|
||||
type DecodeFailure = Cause.Cause<Schema.SchemaError>;
|
||||
|
||||
export interface AzureDevOpsPullRequestBatch {
|
||||
readonly items: ReadonlyArray<AzureDevOpsPullRequest>;
|
||||
/** Zero-based positions of the decoded items in Azure's raw page. */
|
||||
readonly rawIndexes: ReadonlyArray<number>;
|
||||
/** Rows Azure returned, counted before decoding, so a skipped row cannot hide a next page. */
|
||||
readonly rawCount: number;
|
||||
}
|
||||
|
||||
/** Malformed entries are skipped rather than failing the batch, as on the other hosts. */
|
||||
export function decodePullRequestListJson(
|
||||
raw: string,
|
||||
): Result.Result<AzureDevOpsPullRequestBatch, DecodeFailure> {
|
||||
const decoded = decodeUnknownList(raw);
|
||||
if (!Result.isSuccess(decoded)) {
|
||||
return Result.fail(decoded.failure);
|
||||
}
|
||||
const items: AzureDevOpsPullRequest[] = [];
|
||||
const rawIndexes: number[] = [];
|
||||
for (const [rawIndex, entry] of decoded.success.entries()) {
|
||||
const item = decodePullRequestEntry(entry);
|
||||
if (Exit.isFailure(item)) continue;
|
||||
const pullRequest = toPullRequest(item.value);
|
||||
if (pullRequest !== null) {
|
||||
items.push(pullRequest);
|
||||
rawIndexes.push(rawIndex);
|
||||
}
|
||||
}
|
||||
return Result.succeed({ items, rawIndexes, rawCount: decoded.success.length });
|
||||
}
|
||||
|
||||
/** Null carries "Azure answered, but with too little to use", which the caller reports. */
|
||||
export function decodePullRequestJson(
|
||||
raw: string,
|
||||
): Result.Result<AzureDevOpsPullRequest | null, DecodeFailure> {
|
||||
const decoded = decodePullRequest(raw);
|
||||
return Result.isSuccess(decoded)
|
||||
? Result.succeed(toPullRequest(decoded.success))
|
||||
: Result.fail(decoded.failure);
|
||||
}
|
||||
|
||||
/** `az account show --query user` reports the signed-in account, whose name is an email. */
|
||||
export function decodeViewerJson(raw: string): Result.Result<string | null, DecodeFailure> {
|
||||
const decoded = decodeViewer(raw);
|
||||
return Result.isSuccess(decoded)
|
||||
? Result.succeed(trimmed(decoded.success.user?.name))
|
||||
: Result.fail(decoded.failure);
|
||||
}
|
||||
|
||||
/**
|
||||
* Azure keeps its conversation as threads of comments, and every one of them is a remark
|
||||
* somebody wrote: a reply under a thread is as much of the conversation as the line that opened
|
||||
* it. A thread pinned to a file is a line-level review comment.
|
||||
*
|
||||
* Azure answers the whole thread collection in one response, with no cursor and no page to
|
||||
* follow, so what this returns is everything the host has.
|
||||
*/
|
||||
export function decodeThreadsJson(
|
||||
raw: string,
|
||||
): Result.Result<ReadonlyArray<PullRequestComment>, DecodeFailure> {
|
||||
const decoded = decodeThreadPage(raw);
|
||||
if (!Result.isSuccess(decoded)) {
|
||||
return Result.fail(decoded.failure);
|
||||
}
|
||||
const comments: PullRequestComment[] = [];
|
||||
for (const entry of decoded.success.value) {
|
||||
const decodedThread = decodeThreadEntry(entry);
|
||||
if (Exit.isFailure(decodedThread)) continue;
|
||||
const thread = decodedThread.value;
|
||||
if (thread.isDeleted === true) continue;
|
||||
const path = trimmed(thread.threadContext?.filePath);
|
||||
for (const comment of thread.comments ?? []) {
|
||||
const publishedDate = trimmed(comment.publishedDate);
|
||||
if (
|
||||
comment.isDeleted === true ||
|
||||
comment.commentType?.trim().toLowerCase() === "system" ||
|
||||
(comment.content ?? "").trim().length === 0 ||
|
||||
publishedDate === null
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
comments.push({
|
||||
id: `${thread.id}:${comment.id ?? 0}`,
|
||||
kind: path === null ? "issue-comment" : "review-comment",
|
||||
author: toActor(comment.author),
|
||||
body: comment.content ?? "",
|
||||
createdAt: publishedDate,
|
||||
url: null,
|
||||
path,
|
||||
reviewState: null,
|
||||
});
|
||||
}
|
||||
}
|
||||
return Result.succeed(
|
||||
comments.toSorted((left, right) => left.createdAt.localeCompare(right.createdAt)),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* One push's worth of a pull request. Azure records every push as an iteration and keys the whole
|
||||
* review off them: the changed files, and the marks a reader leaves on those files, both hang
|
||||
* from an iteration rather than from the pull request.
|
||||
*/
|
||||
const RawIterationSchema = Schema.Struct({
|
||||
id: Schema.Int,
|
||||
sourceRefCommit: Schema.optional(
|
||||
Schema.NullOr(Schema.Struct({ commitId: Schema.optional(Schema.NullOr(Schema.String)) })),
|
||||
),
|
||||
commonRefCommit: Schema.optional(
|
||||
Schema.NullOr(Schema.Struct({ commitId: Schema.optional(Schema.NullOr(Schema.String)) })),
|
||||
),
|
||||
});
|
||||
|
||||
const RawIterationPageSchema = Schema.Struct({ value: Schema.Array(Schema.Unknown) });
|
||||
|
||||
const RawChangeEntrySchema = Schema.Struct({
|
||||
changeType: Schema.optional(Schema.NullOr(Schema.String)),
|
||||
sourceServerItem: Schema.optional(Schema.NullOr(Schema.String)),
|
||||
/** Where a renamed file came from. Azure states it here on an iteration's changes. */
|
||||
originalPath: Schema.optional(Schema.NullOr(Schema.String)),
|
||||
item: Schema.optional(
|
||||
Schema.NullOr(
|
||||
Schema.Struct({
|
||||
path: Schema.optional(Schema.NullOr(Schema.String)),
|
||||
objectId: Schema.optional(Schema.NullOr(Schema.String)),
|
||||
originalObjectId: Schema.optional(Schema.NullOr(Schema.String)),
|
||||
/** Azure marks a directory this way; a review has nothing to show for one. */
|
||||
isFolder: Schema.optional(Schema.NullOr(Schema.Boolean)),
|
||||
gitObjectType: Schema.optional(Schema.NullOr(Schema.String)),
|
||||
}),
|
||||
),
|
||||
),
|
||||
});
|
||||
|
||||
const RawChangePageSchema = Schema.Struct({
|
||||
changeEntries: Schema.Array(Schema.Unknown),
|
||||
/** Where the page after this one starts. Azure leaves it out on the last page. */
|
||||
nextSkip: Schema.optional(Schema.NullOr(Schema.Number)),
|
||||
});
|
||||
|
||||
const RawItemContentSchema = Schema.Struct({
|
||||
content: Schema.optional(Schema.NullOr(Schema.String)),
|
||||
/** What Azure makes of the file it is handing over, which is where it says it is not text. */
|
||||
contentMetadata: Schema.optional(
|
||||
Schema.NullOr(Schema.Struct({ isBinary: Schema.optional(Schema.NullOr(Schema.Boolean)) })),
|
||||
),
|
||||
});
|
||||
|
||||
/** The head and the merge base of one iteration, which is the range its patch is taken over. */
|
||||
export interface AzureDevOpsIteration {
|
||||
readonly id: number;
|
||||
readonly headCommit: string;
|
||||
readonly mergeBaseCommit: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* What one file did across an iteration. `oldPath` differs from `path` only for a rename, which
|
||||
* Azure reports by naming the file's previous home rather than as a delete and an add.
|
||||
*/
|
||||
export interface AzureDevOpsChangeEntry {
|
||||
readonly path: string;
|
||||
readonly oldPath: string;
|
||||
readonly changeKind: "new" | "deleted" | "change" | "rename-pure" | "rename-changed";
|
||||
readonly objectId: string | null;
|
||||
readonly originalObjectId: string | null;
|
||||
}
|
||||
|
||||
/**
|
||||
* One page of what an iteration changed, and where the next one starts. Azure pages this route
|
||||
* rather than answering with the whole change, so a review large enough to be paged is followed
|
||||
* to its end instead of being cut off at the first page's worth.
|
||||
*/
|
||||
export interface AzureDevOpsChangePage {
|
||||
readonly changes: ReadonlyArray<AzureDevOpsChangeEntry>;
|
||||
readonly nextSkip: number | null;
|
||||
}
|
||||
|
||||
/** One file's text at one commit, and whether Azure says the text is text at all. */
|
||||
export interface AzureDevOpsItemContent {
|
||||
readonly contents: string;
|
||||
readonly isBinary: boolean;
|
||||
}
|
||||
|
||||
const decodeIterationPage = decodeJsonResult(RawIterationPageSchema);
|
||||
const decodeIterationEntry = Schema.decodeUnknownExit(RawIterationSchema);
|
||||
const decodeChangePage = decodeJsonResult(RawChangePageSchema);
|
||||
const decodeChangeEntry = Schema.decodeUnknownExit(RawChangeEntrySchema);
|
||||
const decodeItemContent = decodeJsonResult(RawItemContentSchema);
|
||||
|
||||
/**
|
||||
* Azure leads a path with a slash that every other host and every patch omits. Not trimmed like
|
||||
* the rest of this payload, since a leading/trailing space is a legal part of a file's name and
|
||||
* the patch and viewed mark are keyed by the untrimmed path.
|
||||
*/
|
||||
function toRepositoryPath(value: string | null | undefined): string | null {
|
||||
if (value === undefined || value === null) return null;
|
||||
const path = value.replace(/^\/+/, "");
|
||||
return path.length === 0 ? null : path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Azure names a change with one word or two, and a rename arrives either alone or alongside the
|
||||
* edit that came with it. Anything it has added since reads as a plain change, which shows the
|
||||
* file rather than dropping it from the review.
|
||||
*/
|
||||
function toChangeKind(
|
||||
raw: string | null | undefined,
|
||||
renamed: boolean,
|
||||
): AzureDevOpsChangeEntry["changeKind"] {
|
||||
const parts = new Set(
|
||||
(raw ?? "")
|
||||
.toLowerCase()
|
||||
.split(",")
|
||||
.map((part) => part.trim())
|
||||
.filter((part) => part.length > 0),
|
||||
);
|
||||
if (parts.has("delete")) return "deleted";
|
||||
if (renamed || parts.has("rename")) return parts.has("edit") ? "rename-changed" : "rename-pure";
|
||||
if (parts.has("add")) return "new";
|
||||
return "change";
|
||||
}
|
||||
|
||||
export function decodeIterationsJson(
|
||||
raw: string,
|
||||
): Result.Result<ReadonlyArray<AzureDevOpsIteration>, DecodeFailure> {
|
||||
const decoded = decodeIterationPage(raw);
|
||||
if (!Result.isSuccess(decoded)) return Result.fail(decoded.failure);
|
||||
const iterations: AzureDevOpsIteration[] = [];
|
||||
for (const entry of decoded.success.value) {
|
||||
const decodedIteration = decodeIterationEntry(entry);
|
||||
if (Exit.isFailure(decodedIteration)) continue;
|
||||
const iteration = decodedIteration.value;
|
||||
const headCommit = trimmed(iteration.sourceRefCommit?.commitId);
|
||||
const mergeBaseCommit = trimmed(iteration.commonRefCommit?.commitId);
|
||||
// An iteration Azure cannot place both ends of names no range, and a patch needs both.
|
||||
if (headCommit === null || mergeBaseCommit === null) continue;
|
||||
iterations.push({ id: iteration.id, headCommit, mergeBaseCommit });
|
||||
}
|
||||
return Result.succeed(iterations.toSorted((left, right) => left.id - right.id));
|
||||
}
|
||||
|
||||
export function decodeIterationChangesJson(
|
||||
raw: string,
|
||||
): Result.Result<AzureDevOpsChangePage, DecodeFailure> {
|
||||
const decoded = decodeChangePage(raw);
|
||||
if (!Result.isSuccess(decoded)) return Result.fail(decoded.failure);
|
||||
const changes: AzureDevOpsChangeEntry[] = [];
|
||||
for (const entry of decoded.success.changeEntries) {
|
||||
const decodedChange = decodeChangeEntry(entry);
|
||||
if (Exit.isFailure(decodedChange)) continue;
|
||||
const change = decodedChange.value;
|
||||
const path = toRepositoryPath(change.item?.path);
|
||||
if (path === null) continue;
|
||||
// Azure lists the folders a change touched alongside the files themselves. A review shows
|
||||
// files, and a folder has no content to show for either side of one.
|
||||
if (change.item?.isFolder === true) continue;
|
||||
if ((change.item?.gitObjectType ?? "blob").toLowerCase() !== "blob") continue;
|
||||
// Azure names where a renamed file came from in either of two places depending on the route
|
||||
// and the version, so both are read and the current path stands in when neither is there.
|
||||
const oldPath =
|
||||
toRepositoryPath(change.sourceServerItem) ?? toRepositoryPath(change.originalPath) ?? path;
|
||||
changes.push({
|
||||
path,
|
||||
oldPath,
|
||||
changeKind: toChangeKind(change.changeType, oldPath !== path),
|
||||
objectId: trimmed(change.item?.objectId),
|
||||
originalObjectId: trimmed(change.item?.originalObjectId),
|
||||
});
|
||||
}
|
||||
const nextSkip = decoded.success.nextSkip ?? null;
|
||||
return Result.succeed({
|
||||
changes,
|
||||
nextSkip: nextSkip !== null && Number.isSafeInteger(nextSkip) && nextSkip > 0 ? nextSkip : null,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Azure answers an absent file with an empty body rather than an error, which reads as empty.
|
||||
* Whether the bytes are text is Azure's own call, not this decoder's to guess from content.
|
||||
*/
|
||||
export function decodeItemContentJson(
|
||||
raw: string,
|
||||
): Result.Result<AzureDevOpsItemContent, DecodeFailure> {
|
||||
const decoded = decodeItemContent(raw);
|
||||
return Result.isSuccess(decoded)
|
||||
? Result.succeed({
|
||||
contents: decoded.success.content ?? "",
|
||||
isBinary: decoded.success.contentMetadata?.isBinary === true,
|
||||
})
|
||||
: Result.fail(decoded.failure);
|
||||
}
|
||||
Reference in new issue
Block a user