Skip to content

Commit da81b1d

Browse files
committed
fix(azure-devops): report build results to PR watches
1 parent 9e55228 commit da81b1d

7 files changed

Lines changed: 493 additions & 1 deletion

File tree

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
import { assert, it } from "@effect/vitest";
2+
import * as Effect from "effect/Effect";
3+
import * as Layer from "effect/Layer";
4+
import { ChildProcessSpawner } from "effect/process";
5+
import * as AzureDevOpsCli from "@t3tools/source-control-azure-devops/server/AzureDevOpsCli";
6+
import * as AzureDevOpsPullRequestCli from "@t3tools/source-control-azure-devops/server/AzureDevOpsPullRequestCli";
7+
import * as AzureDevOpsPullRequestProvider from "@t3tools/source-control-azure-devops/server/AzureDevOpsPullRequestProvider";
8+
9+
import { evaluatePullRequestWatch, pullRequestWatchMessage } from "./pullRequestWatch.ts";
10+
11+
it.effect("reports Azure build failures and completion, including reruns without a head SHA", () =>
12+
Effect.gen(function* () {
13+
let status = "queued";
14+
const provider = yield* AzureDevOpsPullRequestProvider.make.pipe(
15+
Effect.provide(
16+
AzureDevOpsPullRequestCli.layer.pipe(
17+
Layer.provide(
18+
Layer.mock(AzureDevOpsCli.AzureDevOpsCli)({
19+
execute: ({ args }) => {
20+
const response = args.includes("show")
21+
? {
22+
pullRequestId: 42,
23+
title: "Build validation",
24+
status: "active",
25+
sourceRefName: "refs/heads/fix/build",
26+
targetRefName: "refs/heads/main",
27+
creationDate: "2026-10-01T00:00:00Z",
28+
repository: { name: "web", project: { name: "platform" } },
29+
url: "https://dev.azure.com/example/platform/_apis/git/repositories/web/pullRequests/42",
30+
}
31+
: args.includes("policy")
32+
? [
33+
{
34+
status,
35+
configuration: {
36+
id: 1,
37+
isEnabled: true,
38+
isBlocking: true,
39+
type: { id: "0609b952-1397-4640-95ec-e00a01b2c241" },
40+
settings: { displayName: "Tests" },
41+
},
42+
},
43+
{
44+
status: "running",
45+
configuration: {
46+
id: 2,
47+
isEnabled: true,
48+
isBlocking: false,
49+
type: { id: "0609b952-1397-4640-95ec-e00a01b2c241" },
50+
settings: { displayName: "Advisory" },
51+
},
52+
},
53+
]
54+
: { value: [] };
55+
return Effect.succeed({
56+
exitCode: ChildProcessSpawner.ExitCode(0),
57+
stdout: JSON.stringify(response),
58+
stderr: "",
59+
stdoutTruncated: false,
60+
stderrTruncated: false,
61+
});
62+
},
63+
}),
64+
),
65+
),
66+
),
67+
);
68+
const read = provider
69+
.getChangeRequest({
70+
cwd: "/workspace",
71+
host: "dev.azure.com",
72+
repository: "example/platform/web",
73+
number: 42,
74+
})
75+
.pipe(Effect.map((detail) => ({ ...detail, headSha: detail.headSha ?? undefined })));
76+
const initial = yield* read;
77+
assert.isTrue(initial.checks.some((check) => check.status === "pending"));
78+
const waiting = evaluatePullRequestWatch(
79+
{
80+
startedAt: "2026-10-01T00:00:00Z",
81+
headSha: null,
82+
failedChecks: [],
83+
passed: false,
84+
passedChecks: [],
85+
remarksThrough: "2026-10-01T00:00:00Z",
86+
remarkIds: [],
87+
conflicting: false,
88+
wakes: 0,
89+
},
90+
initial,
91+
[],
92+
);
93+
assert.deepEqual(waiting.changes, []);
94+
95+
status = "rejected";
96+
const failedDetail = yield* read;
97+
const failed = evaluatePullRequestWatch(waiting.next, failedDetail, []);
98+
assert.deepEqual(failed.changes, [
99+
{ kind: "checks-failed", failed: failedDetail.checks.filter((check) => check.required) },
100+
]);
101+
const messageInput = {
102+
number: 42,
103+
url: failedDetail.url,
104+
baseBranch: "main",
105+
headSha: null,
106+
};
107+
assert.equal(
108+
pullRequestWatchMessage({ ...messageInput, report: failed }).notification.outcome,
109+
"failed",
110+
);
111+
assert.deepEqual(evaluatePullRequestWatch(failed.next, yield* read, []).changes, []);
112+
113+
status = "running";
114+
const rerun = evaluatePullRequestWatch(failed.next, yield* read, []);
115+
assert.deepEqual(rerun.changes, []);
116+
status = "rejected";
117+
const failedAgain = evaluatePullRequestWatch(rerun.next, yield* read, []);
118+
assert.equal(failedAgain.changes[0]?.kind, "checks-failed");
119+
120+
status = "approved";
121+
const passed = evaluatePullRequestWatch(failedAgain.next, yield* read, []);
122+
assert.deepEqual(passed.changes, [{ kind: "checks-passed", count: 1, required: true }]);
123+
assert.equal(
124+
pullRequestWatchMessage({ ...messageInput, report: passed }).notification.outcome,
125+
"completed",
126+
);
127+
assert.deepEqual(evaluatePullRequestWatch(passed.next, yield* read, []).changes, []);
128+
129+
status = "queued";
130+
const rebuilding = evaluatePullRequestWatch(passed.next, yield* read, []);
131+
assert.deepEqual(rebuilding.changes, []);
132+
status = "approved";
133+
assert.deepEqual(evaluatePullRequestWatch(rebuilding.next, yield* read, []).changes, [
134+
{ kind: "checks-passed", count: 1, required: true },
135+
]);
136+
}),
137+
);

‎packages/source-control-azure-devops/src/server/AzureDevOpsPullRequestCli.test.ts‎

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,17 @@ const oneIteration = {
6868
],
6969
};
7070

71+
const buildPolicy = {
72+
status: "rejected",
73+
configuration: {
74+
id: 12,
75+
isEnabled: true,
76+
isBlocking: true,
77+
type: { id: "0609b952-1397-4640-95ec-e00a01b2c241" },
78+
settings: { displayName: "Tests" },
79+
},
80+
};
81+
7182
function pullRequestRows(
7283
count: number,
7384
firstNumber: number,
@@ -428,6 +439,139 @@ layer("AzureDevOpsPullRequestCli.layer", (it) => {
428439
}),
429440
);
430441

442+
it.effect("reads build checks after a full page of unrelated policies", () =>
443+
Effect.gen(function* () {
444+
const unrelated = {
445+
...buildPolicy,
446+
configuration: { ...buildPolicy.configuration, type: { id: "reviewers" } },
447+
};
448+
mockedExecute
449+
.mockReturnValueOnce(
450+
Effect.succeed(
451+
output(JSON.stringify(Array.from({ length: 100 }, () => ({ ...unrelated })))),
452+
),
453+
)
454+
.mockReturnValueOnce(Effect.succeed(output(JSON.stringify([buildPolicy]))));
455+
const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
456+
const checks = yield* cli.listBuildChecks({ cwd: "/w", number: 42 });
457+
458+
assert.deepEqual(checks, [
459+
{
460+
name: "Tests (#12)",
461+
status: "failure",
462+
required: true,
463+
description: null,
464+
url: null,
465+
},
466+
]);
467+
assert.deepEqual(argsOfCall(0), [
468+
"repos",
469+
"pr",
470+
"policy",
471+
"list",
472+
"--detect",
473+
"true",
474+
"--id",
475+
"42",
476+
"--top",
477+
"100",
478+
"--skip",
479+
"0",
480+
"--only-show-errors",
481+
"--output",
482+
"json",
483+
]);
484+
expect(argsOfCall(1)).toContain("100");
485+
assert.equal(argsOfCall(1)[argsOfCall(1).indexOf("--skip") + 1], "100");
486+
}),
487+
);
488+
489+
it.effect("rejects incomplete policy reads instead of returning a passing subset", () =>
490+
Effect.gen(function* () {
491+
const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
492+
const passedPage = output(
493+
JSON.stringify(Array.from({ length: 100 }, () => ({ ...buildPolicy, status: "approved" }))),
494+
);
495+
for (const unreadable of [
496+
output("{}"),
497+
output(""),
498+
{ ...output("[]"), stdoutTruncated: true },
499+
]) {
500+
mockedExecute
501+
.mockReturnValueOnce(Effect.succeed(passedPage))
502+
.mockReturnValueOnce(Effect.succeed(unreadable));
503+
const error = yield* Effect.flip(cli.listBuildChecks({ cwd: "/w", number: 42 }));
504+
assert.equal(error._tag, "AzureDevOpsPullRequestReadError");
505+
}
506+
mockedExecute.mockReset();
507+
mockedExecute.mockReturnValue(Effect.succeed(passedPage));
508+
const bounded = yield* Effect.flip(cli.listBuildChecks({ cwd: "/w", number: 42 }));
509+
assert.equal(bounded._tag, "AzureDevOpsPullRequestReadError");
510+
assert.equal(mockedExecute.mock.calls.length, 10);
511+
}),
512+
);
513+
514+
it.effect(
515+
"propagates build-read failures through detail with their provider failure reason",
516+
() =>
517+
Effect.gen(function* () {
518+
const fields = {
519+
operation: "execute" as const,
520+
command: "az" as const,
521+
cwd: "/w",
522+
argumentCount: 16,
523+
cause: "Policy read failed",
524+
};
525+
const failures = [
526+
{
527+
error: new AzureDevOpsCli.AzureDevOpsCliAuthenticationError(fields),
528+
reason: "unauthenticated",
529+
},
530+
{
531+
error: new AzureDevOpsCli.AzureDevOpsCliRateLimitError(fields),
532+
reason: "rate-limited",
533+
},
534+
{
535+
error: new AzureDevOpsCli.AzureDevOpsCliUnavailableError(fields),
536+
reason: "missing-tool",
537+
},
538+
{ error: new AzureDevOpsCli.AzureDevOpsCommandFailedError(fields), reason: "failed" },
539+
{
540+
error: new AzureDevOpsCli.AzureDevOpsPullRequestNotFoundError(fields),
541+
reason: "not-found",
542+
},
543+
];
544+
const provider = yield* AzureDevOpsPullRequestProvider.make;
545+
for (const { error, reason } of failures) {
546+
mockedExecute
547+
.mockReturnValueOnce(Effect.succeed(output(json(pullRequestRow))))
548+
.mockReturnValueOnce(Effect.fail(error));
549+
const failure = yield* Effect.flip(
550+
provider.getChangeRequest({
551+
cwd: "/w",
552+
repository: "example/platform/web",
553+
host: "dev.azure.com",
554+
number: 42,
555+
}),
556+
);
557+
assert.equal(failure.reason, reason);
558+
assert.equal(failure.cause, error);
559+
}
560+
mockedExecute
561+
.mockReturnValueOnce(Effect.succeed(output(json(pullRequestRow))))
562+
.mockReturnValueOnce(Effect.succeed(output("{}")));
563+
const unreadable = yield* Effect.flip(
564+
provider.getChangeRequest({
565+
cwd: "/w",
566+
repository: "example/platform/web",
567+
host: "dev.azure.com",
568+
number: 42,
569+
}),
570+
);
571+
assert.equal(unreadable.reason, "failed");
572+
}),
573+
);
574+
431575
it.effect("reads the signed-in account, which az reports as a bare value", () =>
432576
Effect.gen(function* () {
433577
// `--query user` unwraps the object, so the wrapper has to put it back.

‎packages/source-control-azure-devops/src/server/AzureDevOpsPullRequestCli.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ import * as Result from "effect/Result";
55
import * as Schema from "effect/Schema";
66
import type {
77
PullRequestAction,
8+
PullRequestCheck,
89
PullRequestComment,
910
PullRequestInvolvement,
1011
PullRequestListState,
1112
PullRequestMergeMethod,
1213
} from "@t3tools/contracts";
1314

1415
import * as AzureDevOpsCli from "./AzureDevOpsCli.ts";
16+
import { decodeBuildPolicyChecksJson } from "./azureDevOpsPullRequestChecks.ts";
1517
import {
1618
decodeItemContentJson,
1719
decodeIterationChangesJson,
@@ -142,6 +144,8 @@ const REVIEW_HISTORY_MAX_OUTPUT_BYTES = 8 * 1024 * 1024;
142144

143145
/** Azure's own ceiling for one page of an iteration's changes. */
144146
const CHANGE_ENTRIES_PER_PAGE = 2000;
147+
const POLICY_EVALUATIONS_PER_PAGE = 100;
148+
const MAX_POLICY_EVALUATIONS = 1000;
145149

146150
/**
147151
* Where following the pages stops, counted in the entries Azure was asked to skip rather than in
@@ -192,6 +196,11 @@ export class AzureDevOpsPullRequestCli extends Context.Service<
192196
readonly number: number;
193197
}) => Effect.Effect<AzureDevOpsPullRequest, AzureDevOpsPullRequestCliError>;
194198

199+
readonly listBuildChecks: (input: {
200+
readonly cwd: string;
201+
readonly number: number;
202+
}) => Effect.Effect<ReadonlyArray<PullRequestCheck>, AzureDevOpsPullRequestCliError>;
203+
195204
/** Threads are not reachable through `az repos pr`, so they come from the REST API. */
196205
readonly listThreads: (input: {
197206
readonly cwd: string;
@@ -631,6 +640,47 @@ export const make = Effect.gen(function* () {
631640
),
632641
),
633642

643+
listBuildChecks: Effect.fnUntraced(function* (input) {
644+
const checks: PullRequestCheck[] = [];
645+
// The CLI resolves the PR's project ID and scopes evaluations to its CodeReview artifact.
646+
// Page all policies before filtering builds so a later required check cannot be missed.
647+
for (let skip = 0; skip < MAX_POLICY_EVALUATIONS; skip += POLICY_EVALUATIONS_PER_PAGE) {
648+
const result = yield* executeJson({
649+
cwd: input.cwd,
650+
args: [
651+
"repos",
652+
"pr",
653+
"policy",
654+
"list",
655+
...detectArgs,
656+
"--id",
657+
String(input.number),
658+
"--top",
659+
String(POLICY_EVALUATIONS_PER_PAGE),
660+
"--skip",
661+
String(skip),
662+
],
663+
});
664+
const decoded = decodeBuildPolicyChecksJson(result.stdout.trim());
665+
if (result.stdoutTruncated || Result.isFailure(decoded)) {
666+
return yield* new AzureDevOpsPullRequestReadError({
667+
command: "az",
668+
cwd: input.cwd,
669+
operation: "listBuildChecks",
670+
cause: Result.isFailure(decoded) ? decoded.failure : "Policy output was truncated",
671+
});
672+
}
673+
checks.push(...decoded.success.checks);
674+
if (decoded.success.rawCount < POLICY_EVALUATIONS_PER_PAGE) return checks;
675+
}
676+
return yield* new AzureDevOpsPullRequestReadError({
677+
command: "az",
678+
cwd: input.cwd,
679+
operation: "listBuildChecks",
680+
cause: "Policy evaluation pagination exceeded the read limit",
681+
});
682+
}),
683+
634684
listThreads: (input) =>
635685
invoke({
636686
cwd: input.cwd,

‎packages/source-control-azure-devops/src/server/AzureDevOpsPullRequestProvider.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ describe("getChangeRequest", () => {
175175
Effect.provide(
176176
Layer.mock(AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli)({
177177
getPullRequest: () => Effect.succeed(PULL_REQUEST),
178+
listBuildChecks: () => Effect.succeed([]),
178179
listIterations: () => Effect.succeed([ITERATION]),
179180
listIterationChanges: () =>
180181
Effect.succeed({ changes: [change("a.ts"), change("b.ts")], truncated: false }),

0 commit comments

Comments
 (0)