Skip to content

Commit e3234da

Browse files
Tabonxclaude
andcommitted
fix(azure-devops): pull requests report build validation as checks
The Azure DevOps provider returned no checks, so build validation never appeared in the app and PR watches never woke on a result. Read build and status policy evaluations with `az repos pr policy list` and report them through detail, the checks poll, and the summary. Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent df70f50 commit e3234da

10 files changed

Lines changed: 533 additions & 25 deletions

File tree

‎apps/server/src/pullRequest/PullRequestService.test.ts‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3055,18 +3055,26 @@ it.effect("deduplicates routed PR reports without dropping draft changes", () =>
30553055
}),
30563056
);
30573057

3058-
it.effect(
3059-
"reuses fresh detail checks without extending their refresh interval on detail cache hits",
3060-
() =>
3058+
it.effect.each(["github", "azure-devops"])(
3059+
"reuses fresh %s detail checks without extending their refresh interval on detail cache hits",
3060+
(provider) =>
30613061
Effect.gen(function* () {
30623062
let reads = 0;
3063-
const reference = { projectId: "p1" as ProjectId, repository: "acme/web", number: 1 };
3063+
const repository = provider === "azure-devops" ? "web" : "acme/web";
3064+
const reference = { projectId: "p1" as ProjectId, repository, number: 1 };
30643065
const service = yield* makeService({
30653066
projects: [
3066-
project({ id: "p1", title: "web", workspaceRoot: "/a", repository: "acme/web" }),
3067+
project({
3068+
id: "p1",
3069+
title: "web",
3070+
workspaceRoot: "/a",
3071+
repository,
3072+
provider,
3073+
...(provider === "azure-devops" ? { host: "dev.azure.com" } : {}),
3074+
}),
30673075
],
30683076
providers: [
3069-
fakePullRequestProvider(SourceControlProviderKind.make("github"), {
3077+
fakePullRequestProvider(SourceControlProviderKind.make(provider), {
30703078
getChangeRequest: () => Effect.succeed(hostedChangeRequest("Description")),
30713079
getChangeRequestChecks: () =>
30723080
Effect.sync(() => {

‎apps/server/src/pullRequest/PullRequestService.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3100,9 +3100,9 @@ export const make = Effect.gen(function* () {
31003100
return detailUncached(refOfCacheKey(key)).pipe(
31013101
Effect.tap(
31023102
Effect.fn("PullRequestService.recordDetailStats")(function* (value: PullRequestDetail) {
3103-
// GitHub detail includes the same checks; the checks TTL counts from their observation.
3103+
// GitHub and Azure detail include the same checks; the checks TTL counts from their observation.
31043104
if (
3105-
value.provider === "github" &&
3105+
(value.provider === "github" || value.provider === "azure-devops") &&
31063106
value.observedAt !== undefined &&
31073107
!(yield* Cache.has(checksCache, key))
31083108
) {

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

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ layer("AzureDevOpsPullRequestCli.layer", (it) => {
136136
),
137137
),
138138
)
139-
.mockReturnValueOnce(Effect.succeed(output(json({ changeEntries: [] }))));
139+
.mockReturnValueOnce(Effect.succeed(output(json({ changeEntries: [] }))))
140+
.mockReturnValueOnce(Effect.succeed(output("[]")));
140141
const provider = yield* AzureDevOpsPullRequestProvider.make;
141142
const detail = yield* provider.getChangeRequest({
142143
cwd: "/w",
@@ -147,7 +148,7 @@ layer("AzureDevOpsPullRequestCli.layer", (it) => {
147148

148149
expect(detail.headSha).toBe(latestHead);
149150
expect(argsOfCall(2)).toContain("iterationId=2");
150-
expect(mockedExecute).toHaveBeenCalledTimes(3);
151+
expect(mockedExecute).toHaveBeenCalledTimes(4);
151152
}),
152153
);
153154

@@ -326,6 +327,59 @@ layer("AzureDevOpsPullRequestCli.layer", (it) => {
326327
}),
327328
);
328329

330+
const policyRow = (name: string, status: string) => ({
331+
status,
332+
configuration: {
333+
url: "https://dev.azure.com/acme/platform/_apis/policy/configurations/54",
334+
isBlocking: true,
335+
type: { id: "0609b952-1397-4640-95ec-e00a01b2c241" },
336+
settings: { displayName: name },
337+
},
338+
});
339+
340+
it.effect("follows every page of policies and puts failures first", () =>
341+
Effect.gen(function* () {
342+
const fullPage = Array.from({ length: 100 }, () => policyRow("Lint", "approved"));
343+
mockedExecute
344+
.mockReturnValueOnce(Effect.succeed(output(JSON.stringify(fullPage))))
345+
.mockReturnValueOnce(
346+
Effect.succeed(output(JSON.stringify([policyRow("Tests", "rejected")]))),
347+
);
348+
const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
349+
const checks = yield* cli.listPolicyChecks({ cwd: "/w", number: 42, isDraft: false });
350+
assert.deepStrictEqual(argsOfCall(1).slice(0, 12), [
351+
"repos",
352+
"pr",
353+
"policy",
354+
"list",
355+
"--detect",
356+
"true",
357+
"--id",
358+
"42",
359+
"--top",
360+
"100",
361+
"--skip",
362+
"100",
363+
]);
364+
assert.strictEqual(checks.length, 101);
365+
assert.strictEqual(checks[0]?.name, "Tests");
366+
}),
367+
);
368+
369+
it.effect.each([
370+
{ name: "cut off", result: { ...output("[]"), stdoutTruncated: true } },
371+
{ name: "unreadable", result: output(JSON.stringify([{ status: 7 }])) },
372+
])("fails rather than report a $name policy page as complete", ({ result }) =>
373+
Effect.gen(function* () {
374+
mockedExecute.mockReturnValueOnce(Effect.succeed(result));
375+
const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
376+
const error = yield* Effect.flip(
377+
cli.listPolicyChecks({ cwd: "/w", number: 42, isDraft: false }),
378+
);
379+
assert.strictEqual(error._tag, "AzureDevOpsPullRequestReadError");
380+
}),
381+
);
382+
329383
it.effect("sends a description that starts with a dash as one value, not as a flag", () =>
330384
Effect.gen(function* () {
331385
mockedExecute.mockReturnValue(Effect.succeed(output("{}")));

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,21 @@ 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

15+
import { sortChecks } from "@t3tools/source-control-core/server/pullRequestChecks";
16+
1417
import * as AzureDevOpsCli from "./AzureDevOpsCli.ts";
1518
import {
1619
decodeItemContentJson,
1720
decodeIterationChangesJson,
1821
decodeIterationsJson,
22+
decodePolicyChecksJson,
1923
decodePullRequestJson,
2024
decodePullRequestListJson,
2125
decodeThreadsJson,
@@ -111,6 +115,9 @@ const ITEM_CONTENT_MAX_OUTPUT_BYTES = 4 * 1024 * 1024;
111115
/** Review history is unpaged and independent of diff size. */
112116
const REVIEW_HISTORY_MAX_OUTPUT_BYTES = 8 * 1024 * 1024;
113117

118+
const POLICY_EVALUATIONS_PER_PAGE = 100;
119+
const MAX_POLICY_EVALUATIONS = 1000;
120+
114121
/** Azure's iteration-change page limit. */
115122
const CHANGE_ENTRIES_PER_PAGE = 2000;
116123

@@ -157,6 +164,13 @@ export class AzureDevOpsPullRequestCli extends Context.Service<
157164
readonly number: number;
158165
}) => Effect.Effect<AzureDevOpsPullRequest, AzureDevOpsPullRequestCliError>;
159166

167+
readonly listPolicyChecks: (input: {
168+
readonly cwd: string;
169+
readonly number: number;
170+
/** Azure holds builds back on a draft, which reads the same as a build about to start. */
171+
readonly isDraft: boolean;
172+
}) => Effect.Effect<ReadonlyArray<PullRequestCheck>, AzureDevOpsPullRequestCliError>;
173+
160174
readonly listThreads: (input: {
161175
readonly cwd: string;
162176
readonly location: AzureDevOpsRepositoryLocation;
@@ -545,6 +559,41 @@ export const make = Effect.gen(function* () {
545559
);
546560
}),
547561

562+
listPolicyChecks: Effect.fn("AzureDevOpsPullRequestCli.listPolicyChecks")(function* (input) {
563+
const unreadable = (cause: unknown) =>
564+
new AzureDevOpsPullRequestReadError({
565+
command: "az",
566+
cwd: input.cwd,
567+
operation: "listPolicyChecks",
568+
cause,
569+
});
570+
const checks: PullRequestCheck[] = [];
571+
for (let skip = 0; skip < MAX_POLICY_EVALUATIONS; skip += POLICY_EVALUATIONS_PER_PAGE) {
572+
const result = yield* executeJson({
573+
cwd: input.cwd,
574+
args: [
575+
"repos",
576+
"pr",
577+
"policy",
578+
"list",
579+
...detectArgs,
580+
"--id",
581+
String(input.number),
582+
"--top",
583+
String(POLICY_EVALUATIONS_PER_PAGE),
584+
"--skip",
585+
String(skip),
586+
],
587+
});
588+
if (result.stdoutTruncated) return yield* unreadable("Policy output was truncated");
589+
const page = decodePolicyChecksJson(result.stdout.trim(), { isDraft: input.isDraft });
590+
if (!Result.isSuccess(page)) return yield* unreadable(page.failure);
591+
checks.push(...page.success.checks);
592+
if (page.success.rawCount < POLICY_EVALUATIONS_PER_PAGE) return sortChecks(checks);
593+
}
594+
return yield* unreadable("Policy evaluations exceeded the read limit");
595+
}),
596+
548597
listThreads: Effect.fn("AzureDevOpsPullRequestCli.listThreads")(function (input) {
549598
return invoke({
550599
cwd: input.cwd,

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

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { describe, expect, it } from "@effect/vitest";
22
import * as Effect from "effect/Effect";
33
import * as Layer from "effect/Layer";
4+
import type { PullRequestCheck, PullRequestCheckStatus } from "@t3tools/contracts";
45

56
import * as AzureDevOpsPullRequestCli from "./AzureDevOpsPullRequestCli.ts";
67
import {
@@ -129,6 +130,7 @@ describe("getChangeRequest", () => {
129130
Effect.provide(
130131
Layer.mock(AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli)({
131132
getPullRequest: () => Effect.succeed(PULL_REQUEST),
133+
listPolicyChecks: () => Effect.succeed([]),
132134
listIterations: () =>
133135
Effect.succeed([ITERATION, { ...ITERATION, id: 4, headCommit: "new-head" }]),
134136
listIterationChanges: () =>
@@ -163,6 +165,7 @@ describe("getChangeRequest", () => {
163165
Effect.provide(
164166
Layer.mock(AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli)({
165167
getPullRequest: () => Effect.succeed(PULL_REQUEST),
168+
listPolicyChecks: () => Effect.succeed([]),
166169
listIterations: () =>
167170
state === "missing"
168171
? Effect.succeed([])
@@ -189,6 +192,107 @@ describe("getChangeRequest", () => {
189192
);
190193
});
191194

195+
describe("policy checks", () => {
196+
const REPOSITORY = {
197+
cwd: "/w",
198+
repository: "acme/web",
199+
host: "dev.azure.com",
200+
number: 7,
201+
};
202+
const check = (status: PullRequestCheckStatus): PullRequestCheck => ({
203+
name: "PR validation",
204+
status,
205+
description: null,
206+
url: null,
207+
required: true,
208+
});
209+
const readFailure = new AzureDevOpsPullRequestCli.AzureDevOpsPullRequestReadError({
210+
command: "az",
211+
cwd: "/w",
212+
operation: "listPolicyChecks",
213+
cause: "unavailable",
214+
});
215+
216+
const provide = (
217+
listPolicyChecks: AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli["Service"]["listPolicyChecks"],
218+
pullRequest = PULL_REQUEST,
219+
) =>
220+
make.pipe(
221+
Effect.provide(
222+
Layer.mock(AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli)({
223+
getPullRequest: () => Effect.succeed(pullRequest),
224+
listIterations: () => Effect.succeed([ITERATION]),
225+
listIterationChanges: () => Effect.succeed({ changes: [], truncated: false }),
226+
listPolicyChecks,
227+
}),
228+
),
229+
);
230+
231+
it.effect("puts the pipeline on the detail, so a watch hears about it", () =>
232+
Effect.gen(function* () {
233+
const provider = yield* provide(() => Effect.succeed([check("failure")]));
234+
const detail = yield* provider.getChangeRequest(REPOSITORY);
235+
expect(detail.checks).toEqual([check("failure")]);
236+
}),
237+
);
238+
239+
it.effect("keeps the detail readable when the policies are not", () =>
240+
Effect.gen(function* () {
241+
const provider = yield* provide(() => Effect.fail(readFailure));
242+
const detail = yield* provider.getChangeRequest(REPOSITORY);
243+
expect(detail.checks).toEqual([]);
244+
}),
245+
);
246+
247+
it.effect("answers a checks poll with the pull request's state", () =>
248+
Effect.gen(function* () {
249+
const provider = yield* provide(() => Effect.succeed([check("pending")]));
250+
expect(yield* provider.getChangeRequestChecks!(REPOSITORY)).toEqual({
251+
state: "open",
252+
checks: [check("pending")],
253+
});
254+
}),
255+
);
256+
257+
it.effect.each([
258+
{ statuses: ["success"], expected: "passing" },
259+
{ statuses: ["success", "pending"], expected: "pending" },
260+
{ statuses: ["pending", "failure"], expected: "failing" },
261+
{ statuses: ["action-required"], expected: "pending" },
262+
{ statuses: ["neutral"], expected: null },
263+
{ statuses: [], expected: null },
264+
] as const)("summarizes $statuses as $expected", ({ statuses, expected }) =>
265+
Effect.gen(function* () {
266+
const provider = yield* provide(() => Effect.succeed(statuses.map(check)));
267+
const summary = yield* provider.getChangeRequestSummary!(REPOSITORY);
268+
expect(summary.checksState).toBe(expected);
269+
}),
270+
);
271+
272+
it.effect("leaves the last known state alone when the policies cannot be read", () =>
273+
Effect.gen(function* () {
274+
const provider = yield* provide(() => Effect.fail(readFailure));
275+
const summary = yield* provider.getChangeRequestSummary!(REPOSITORY);
276+
expect(summary.checksState).toBeUndefined();
277+
}),
278+
);
279+
280+
it.effect("tells the policy read whether the pull request is a draft", () =>
281+
Effect.gen(function* () {
282+
const drafts: Array<boolean> = [];
283+
const provider = yield* provide(
284+
(input) => {
285+
drafts.push(input.isDraft);
286+
return Effect.succeed([]);
287+
},
288+
{ ...PULL_REQUEST, isDraft: true },
289+
);
290+
yield* provider.getChangeRequestSummary!(REPOSITORY);
291+
expect(drafts).toEqual([true]);
292+
}),
293+
);
294+
});
295+
192296
describe("getDiff reads", () => {
193297
it.effect("holds every reader together to one request's worth of processes", () =>
194298
Effect.gen(function* () {

0 commit comments

Comments
 (0)