Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,13 @@ layer("AzureDevOpsPullRequestCli.layer", (it) => {
mockedExecute
.mockReturnValueOnce(
Effect.succeed(
output(json({ ...pullRequestRow, lastMergeSourceCommit: { commitId: previousHead } })),
output(
json({
...pullRequestRow,
lastMergeSourceCommit: { commitId: previousHead },
reviewers: [{ uniqueName: "reviewer@acme.dev", vote: 10 }],
}),
),
),
)
.mockReturnValueOnce(
Expand Down Expand Up @@ -205,6 +211,7 @@ layer("AzureDevOpsPullRequestCli.layer", (it) => {
expect(argsOfCall(2)).toContain("iterationId=2");
expect(argsOfCall(4)).toContain("pullRequestLabels");
expect(detail.labels).toEqual([{ name: "release-1.0", color: null }]);
expect(detail.reviewDecision).toBe("approved");
expect(mockedExecute).toHaveBeenCalledTimes(5);
}),
);
Expand Down Expand Up @@ -320,6 +327,7 @@ layer("AzureDevOpsPullRequestCli.layer", (it) => {
mockedExecute.mockReturnValueOnce(
Effect.succeed(output(JSON.stringify({ name: "bilal@acme.dev", type: "user" }))),
);

const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;

const viewer = yield* cli.getViewer({ cwd: "/w" });
Expand All @@ -337,6 +345,157 @@ layer("AzureDevOpsPullRequestCli.layer", (it) => {
}),
);

it.effect("pages Reviewing over voted and malformed rows without losing group requests", () =>
Effect.gen(function* () {
const votes = [10, 0, 0, null, -10, 20, 5, undefined, -5];
const rows = pullRequestRows(votes.length, 1).map((row, index) =>
index === 1
? { pullRequestId: "malformed" }
: {
...row,
reviewers:
index === 3
? [{ displayName: "[web]\\Reviewers", uniqueName: "web-reviewers" }]
: [{ uniqueName: "Bilal@acme.dev", vote: votes[index] }],
},
);
mockedExecute.mockImplementation((input) => {
const skipIndex = input.args.indexOf("--skip");
const skip = skipIndex === -1 ? 0 : Number(input.args[skipIndex + 1]);
const top = Number(input.args[input.args.indexOf("--top") + 1]);
return Effect.succeed(output(JSON.stringify(rows.slice(skip, skip + top))));
});
const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
const input = {
cwd: "/w",
repository: "web",
state: "open" as const,
involvement: "reviewing" as const,
viewer: "bilal@acme.dev",
limit: 2,
};
const first = yield* cli.listPullRequests(input);
const second = yield* cli.listPullRequests({
...input,
cursor: { delivered: first.cursorAdvance, updatedBefore: "2026-07-02T00:00:00Z" },
});
expect(first.items.map((item) => item.number)).toEqual([3, 4]);
expect(second.items.map((item) => item.number)).toEqual([6, 8]);
expect([first.cursorAdvance, second.cursorAdvance]).toEqual([4, 4]);
expect([first.truncated, second.truncated]).toEqual([true, true]);
const third = yield* cli.listPullRequests({
...input,
cursor: {
delivered: first.cursorAdvance + second.cursorAdvance,
updatedBefore: "2026-07-02T00:00:00Z",
},
});
expect(third).toEqual({ items: [], truncated: false, cursorAdvance: 1 });
}),
);

it.effect.each([true, false])(
"bounds Reviewing reads only after a resumable row exists (early row: %s)",
(earlyRow) =>
Effect.gen(function* () {
let reads = 0;
mockedExecute.mockImplementation((input) => {
reads += 1;
const skipIndex = input.args.indexOf("--skip");
const skip = skipIndex === -1 ? 0 : Number(input.args[skipIndex + 1]);
const top = Number(input.args[input.args.indexOf("--top") + 1]);
return Effect.succeed(
output(
JSON.stringify(
pullRequestRows(top, skip + 1).map((row, index) => ({
...row,
reviewers: [
{
uniqueName: "bilal@acme.dev",
vote: index === 0 && reads === (earlyRow ? 1 : 4) ? 0 : 10,
},
],
})),
),
),
);
});
const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
const batch = yield* cli.listPullRequests({
cwd: "/w",
repository: "web",
state: "all",
involvement: "reviewing",
viewer: "bilal@acme.dev",
limit: 2,
});
expect(reads).toBe(earlyRow ? 3 : 4);
expect(batch.items.map((item) => item.number)).toEqual([earlyRow ? 1 : 10]);
expect(batch.truncated).toBe(true);
expect(batch.cursorAdvance).toBe(earlyRow ? 7 : 12);
}),
);

it.effect("carries decisions and dictionary-attributed vote activity through the provider", () =>
Effect.gen(function* () {
const reviewer = { id: "d6245f20-2af8-44f4-9451-8107cb2767db", uniqueName: "jane@acme.dev" };
mockedExecute.mockImplementation((input) =>
Effect.succeed(
output(
input.args.includes("pullRequestThreads")
? json({
value: [
{
id: 2,
properties: {
CodeReviewThreadType: { $value: "VoteUpdate" },
CodeReviewVoteResult: { $value: "10" },
CodeReviewVotedByIdentity: { $value: "1" },
},
identities: { "1": reviewer },
comments: [
{
id: 1,
content: "Vote.",
commentType: "system",
publishedDate: "2026-07-02T00:00:00Z",
},
],
},
],
})
: input.args.includes("policy")
? "[]"
: input.args.includes("list")
? JSON.stringify([{ ...pullRequestRow, reviewers: [{ ...reviewer, vote: 10 }] }])
: json({ ...pullRequestRow, reviewers: [{ ...reviewer, vote: 10 }] }),
),
),
);
const provider = yield* AzureDevOpsPullRequestProvider.make;
const target = { cwd: "/w", repository: "platform/web", host: "dev.azure.com" };
const page = yield* provider.listChangeRequests({
...target,
state: "open",
involvement: "all",
viewer: "me@acme.dev",
limit: 10,
});
const summary = yield* provider.getChangeRequestSummary!({ ...target, number: 42 });
const activity = yield* provider.getChangeRequestActivity({ ...target, number: 42 });
expect(page.items[0]?.reviewDecision).toBe("approved");
expect(summary.reviewDecision).toBe("approved");
expect(activity.comments).toMatchObject([
{
id: "2:1",
kind: "review",
reviewState: "APPROVED",
author: { login: "jane@acme.dev" },
},
]);
}),
);

it.effect("names a token sign-in by the viewer's own pull request", () =>
Effect.gen(function* () {
const notLoggedIn = new AzureDevOpsCli.AzureDevOpsCliAuthenticationError({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
type AzureDevOpsItemContent,
type AzureDevOpsIteration,
type AzureDevOpsPullRequest,
type AzureDevOpsPullRequestBatch,
type AzureDevOpsRepositoryLocation,
} from "./azureDevOpsPullRequestJson.ts";
import type { ProviderListCursor } from "@t3tools/source-control-core/server/PullRequestProvider";
Expand Down Expand Up @@ -112,6 +113,11 @@ export type AzureDevOpsPullRequestCliError =

const REST_API_VERSION = "7.1";
const PULL_REQUEST_LIST_MAX_OUTPUT_BYTES = 16 * 1024 * 1024;
/**
* Reviewing drops voted rows; bound reads once a row can carry the list service's resume cursor.
* An empty page has no cursor, so it must keep reading until a row or the end.
*/
const PULL_REQUEST_LIST_MAX_READS = 3;
/** A 2,000-file page exceeds the default 1 MiB output budget. */
const CHANGE_ENTRIES_MAX_OUTPUT_BYTES = 8 * 1024 * 1024;
/** JSON escaping and base64 can exceed the decoded file's size. */
Expand Down Expand Up @@ -186,6 +192,7 @@ export class AzureDevOpsPullRequestCli extends Context.Service<
readonly cwd: string;
readonly location: AzureDevOpsRepositoryLocation;
readonly number: number;
readonly reviewersById: AzureDevOpsPullRequest["reviewersById"];
}) => Effect.Effect<ReadonlyArray<PullRequestComment>, AzureDevOpsPullRequestCliError>;

/** `az repos pr show` returns null labels, so the detail reads them here. */
Expand Down Expand Up @@ -279,6 +286,26 @@ function involvementArgs(input: {
}
}

/** Azure's `--reviewer` also returns requests the viewer has already voted on. */
function withoutVotedByViewer(
batch: AzureDevOpsPullRequestBatch,
viewer: string,
): AzureDevOpsPullRequestBatch {
const login = viewer.toLowerCase();
const isViewer = (candidate: string) => candidate.toLowerCase() === login;
const kept = batch.items.flatMap((item, index) =>
item.reviewers.some((reviewer) => isViewer(reviewer.login)) &&
!item.reviewRequestLogins.some(isViewer)
? []
: [{ item, rawIndex: batch.rawIndexes[index] ?? 0 }],
);
return {
items: kept.map((entry) => entry.item),
rawIndexes: kept.map((entry) => entry.rawIndex),
rawCount: batch.rawCount,
};
}

function actionArgs(
action: PullRequestAction,
mergeMethod: PullRequestMergeMethod | undefined,
Expand Down Expand Up @@ -412,6 +439,7 @@ export const make = Effect.gen(function* () {
readonly limit: number;
readonly skip: number;
readonly cursorAdvance: number;
readonly reads: number;
readonly items: ReadonlyArray<AzureDevOpsPullRequest>;
}): Effect.Effect<
{
Expand Down Expand Up @@ -465,28 +493,36 @@ export const make = Effect.gen(function* () {
);
}

const lastItemIndex = decoded.success.rawIndexes[remaining - 1];
const batch =
input.involvement === "reviewing"
? withoutVotedByViewer(decoded.success, input.viewer)
: decoded.success;
const lastItemIndex = batch.rawIndexes[remaining - 1];
if (lastItemIndex !== undefined) {
const consumed = lastItemIndex + 1;
return Effect.succeed({
items: [...input.items, ...decoded.success.items.slice(0, remaining)],
truncated: consumed < decoded.success.rawCount || decoded.success.rawCount === top,
items: [...input.items, ...batch.items.slice(0, remaining)],
truncated: consumed < batch.rawCount || batch.rawCount === top,
cursorAdvance: input.cursorAdvance + consumed,
});
}

const items = [...input.items, ...decoded.success.items];
if (decoded.success.rawCount < top) {
const items = [...input.items, ...batch.items];
if (
batch.rawCount < top ||
(items.length > 0 && input.reads + 1 >= PULL_REQUEST_LIST_MAX_READS)
) {
return Effect.succeed({
items,
truncated: false,
cursorAdvance: input.cursorAdvance + decoded.success.rawCount,
truncated: batch.rawCount === top,
cursorAdvance: input.cursorAdvance + batch.rawCount,
});
}
return listPullRequestPage({
...input,
skip: input.skip + decoded.success.rawCount,
cursorAdvance: input.cursorAdvance + decoded.success.rawCount,
skip: input.skip + batch.rawCount,
cursorAdvance: input.cursorAdvance + batch.rawCount,
reads: input.reads + 1,
items,
});
}),
Expand Down Expand Up @@ -558,6 +594,7 @@ export const make = Effect.gen(function* () {
limit: input.limit,
skip: input.cursor?.delivered ?? 0,
cursorAdvance: 0,
reads: 0,
items: [],
});
}),
Expand Down Expand Up @@ -636,7 +673,7 @@ export const make = Effect.gen(function* () {
resource: "pullRequestThreads",
routeParameters: pullRequestRoute(input),
maxOutputBytes: REVIEW_HISTORY_MAX_OUTPUT_BYTES,
decode: decodeThreadsJson,
decode: (raw) => decodeThreadsJson(raw, input.reviewersById),
});
}),

Expand Down
Loading
Loading