Skip to content

Commit 807e1fe

Browse files
committed
Make pull request checks watches unbounded by default
1 parent a522dac commit 807e1fe

14 files changed

Lines changed: 286 additions & 130 deletions

File tree

‎packages/cli/src/documents/sidekick/ci.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ Sidekick responses and the REPL entries they submit.
88

99
## Instructions
1010

11-
Use the pull request URL and choose a time budget. If you need the props or return shape, look up `PullRequest.Checks` with `REPL.Syntax`. Submit the watch in one entry and read its outcome before filtering for failures. An empty list can mean checks are still pending.
11+
Use the pull request URL. If you need the props or return shape, look up `PullRequest.Checks` with `REPL.Syntax`. Submit the watch in one entry and read its outcome before filtering for failures. An empty list can mean checks are still pending.

‎packages/cli/src/repl/repositories/deno/pull-request-reads.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ function admitRetainedWatch(owner: string, event: Extract<DurableEvent, { type:
239239
const type = event.description.type;
240240
const valid =
241241
type === PULL_REQUEST_CHECKS_WATCH_DEADLINE
242-
? parseChecksWatchDeadline(value) !== undefined
242+
? parseChecksWatchDeadline(value, question.timeoutSeconds) !== undefined
243243
: type === PULL_REQUEST_CHECKS_WATCH_HEAD
244244
? parseChecksWatchHead(value) !== undefined
245245
: parseCheckWatchResult(value) !== undefined;
@@ -374,7 +374,7 @@ function* watched(
374374
/**
375375
* What an admitted cycle raised, kept apart from a refusal made before admission.
376376
*
377-
* A failure after admission is a read the watch may retry; one before it is a
377+
* A failure after admission is classified by the watch; one before it is a
378378
* question this host will not ask, which waiting does not change.
379379
*/
380380
class WatchReadFailure extends Error {

‎packages/cli/tests/repl-pull-request-checks-watch.test.ts‎

Lines changed: 74 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -500,9 +500,10 @@ function pullRequest(fixture: Fixture) {
500500
}
501501

502502
/** The watch every row runs unless it says otherwise. */
503-
function watching(timeoutSeconds = 60, after: readonly string[] = []): string {
503+
function watching(timeoutSeconds: number | null = 60, after: readonly string[] = []): string {
504+
const timeout = timeoutSeconds === null ? "" : ` timeoutSeconds={${timeoutSeconds}}`;
504505
return [
505-
`<PullRequest.Checks url=${JSON.stringify(URL_1)} watch timeoutSeconds={${timeoutSeconds}} as="checkResult" />`,
506+
`<PullRequest.Checks url=${JSON.stringify(URL_1)} watch${timeout} as="checkResult" />`,
506507
"",
507508
"<Json value={checkResult} />",
508509
"",
@@ -842,8 +843,9 @@ describe("a watch's lifetime", () => {
842843
yield* suspend();
843844
};
844845
const stream = new InMemoryStream();
845-
const task = yield* started(fixture, watching(), stream);
846+
const task = yield* started(fixture, watching(null), stream);
846847
yield* reached.operation;
848+
expect(fixture.clock.waiters).toEqual([]);
847849
yield* task.halt();
848850
const events = yield* stream.readAll();
849851
expect(results(events)).toEqual([]);
@@ -906,7 +908,7 @@ describe("a watch's lifetime", () => {
906908
"",
907909
"after {answer.choice}",
908910
];
909-
const source = watching(60, question);
911+
const source = watching(null, question);
910912
const first = new InMemoryStream();
911913
const reached = withResolvers<void>();
912914
const task = yield* started(fixture, source, first, { holdQuestion: () => reached.resolve() });
@@ -915,7 +917,7 @@ describe("a watch's lifetime", () => {
915917
yield* task.halt();
916918
expect(results(prefix)).toHaveLength(1);
917919

918-
const before = checkRequests(fixture);
920+
const before = fixture.store.requests.length;
919921
fixture.ci.refuses = 500;
920922
const continued = yield* run(fixture, source, new InMemoryStream([...prefix]), {
921923
answer: { choice: "continue" },
@@ -925,7 +927,8 @@ describe("a watch's lifetime", () => {
925927
expect(Reflect.get(Object(rendered(continued.output.split("after")[0] ?? "")), "outcome")).toBe(
926928
"success",
927929
);
928-
expect(checkRequests(fixture)).toBe(before);
930+
expect(fixture.store.requests).toHaveLength(before);
931+
expect(fixture.clock.waiters).toEqual([]);
929932
expect(progress(continued.live)).toEqual([]);
930933
});
931934
});
@@ -936,7 +939,6 @@ describe("what a watch refuses before contacting anything", () => {
936939
const element = `<PullRequest.Checks url=${JSON.stringify(URL_1)}`;
937940
const cases: [string, string][] = [
938941
[`${element} timeoutSeconds={60} as="checks" />`, "`timeoutSeconds` bounds a watch"],
939-
[`${element} watch as="checks" />`, "a watch needs `timeoutSeconds`"],
940942
[`${element} watch timeoutSeconds={0} as="checks" />`, "Prop validation failed"],
941943
[`${element} watch timeoutSeconds={1.5} as="checks" />`, "Prop validation failed"],
942944
[`${element} watch timeoutSeconds={60} />`, "must be invoked with `as`"],
@@ -1143,8 +1145,9 @@ describe("the head a watch observed before its collection failed", () => {
11431145
fixture.ci.runs = [checkRun(31, "build", "completed", "success")];
11441146
fixture.ci.partial = true;
11451147
const stream = new InMemoryStream();
1146-
const task = yield* started(fixture, watching(), stream);
1148+
const task = yield* started(fixture, watching(null), stream);
11471149
const retry = yield* fixture.clock.waitFor((at) => at === START + CHECK_WATCH_RETRY_MS);
1150+
expect(fixture.clock.waiters.map(({ at }) => at)).toEqual([retry]);
11481151
delete fixture.ci.partial;
11491152
fixture.clock.advanceTo(retry);
11501153
const ran = yield* finished(task);
@@ -1166,7 +1169,7 @@ describe("the head a watch observed before its collection failed", () => {
11661169
fixture.ci.runs = [checkRun(31, "build", "completed", "success")];
11671170
fixture.ci.partial = true;
11681171
const first = new InMemoryStream();
1169-
const task = yield* started(fixture, watching(), first);
1172+
const task = yield* started(fixture, watching(null), first);
11701173
yield* fixture.clock.waitFor((at) => at === START + CHECK_WATCH_RETRY_MS);
11711174
yield* task.halt();
11721175
const prefix = [...(yield* first.readAll())];
@@ -1175,18 +1178,77 @@ describe("the head a watch observed before its collection failed", () => {
11751178
]);
11761179
expect(results(prefix)).toEqual([]);
11771180

1178-
fixture.clock.now = START + 20_000;
1181+
fixture.clock.now = START + 86_400_000;
11791182
pullRequest(fixture).headSha = MOVED;
11801183
delete fixture.ci.partial;
11811184
fixture.ci.statuses = [commitStatus(42, "green", "success")];
1182-
const again = yield* run(fixture, watching(), new InMemoryStream([...prefix]));
1185+
const again = yield* run(fixture, watching(null), new InMemoryStream([...prefix]));
11831186
expect(again.refusal).toBe(undefined);
11841187
expect(results(again.events)).toEqual([
11851188
{ outcome: "incomplete", reason: "head-changed", headSha: HEAD, checks: [] },
11861189
]);
11871190
expect(retained(again.events, PULL_REQUEST_CHECKS_WATCH_DEADLINE)).toEqual([
1188-
{ deadline: START + 60_000 },
1191+
{ deadline: null },
11891192
]);
11901193
expect(pagedAt(fixture, MOVED)).toBe(false);
11911194
});
11921195
});
1196+
1197+
describe("read failures without a deadline", () => {
1198+
it("W23: a terminal error before pinning binds read-error without a retry", function* () {
1199+
const fixture = yield* useFixture();
1200+
fixture.ci.pullRefuses = 401;
1201+
let requests = 0;
1202+
// deno-lint-ignore require-yield
1203+
fixture.ci.before = function* () {
1204+
requests += 1;
1205+
};
1206+
const task = yield* started(fixture, watching(null), new InMemoryStream());
1207+
const ran = yield* finished(task);
1208+
expect(results(ran.events)).toEqual([
1209+
{ outcome: "incomplete", reason: "read-error", headSha: null, checks: [] },
1210+
]);
1211+
expect(requests).toBe(1);
1212+
expect(progress(ran.live)).toEqual([]);
1213+
});
1214+
1215+
it("W24: invalid evidence stops an unbounded watch with its last accepted checks", function* () {
1216+
const fixture = yield* useFixture();
1217+
fixture.ci.runs = [checkRun(31, "build", "in_progress", null)];
1218+
const task = yield* started(fixture, watching(null), new InMemoryStream());
1219+
const poll = yield* pollAt(fixture);
1220+
fixture.ci.runs = [{ ...checkRun(32, "other", "completed", "success"), head_sha: MOVED }];
1221+
fixture.clock.advanceTo(poll);
1222+
const ran = yield* finished(task);
1223+
expect(results(ran.events)).toEqual([
1224+
{
1225+
outcome: "incomplete",
1226+
reason: "read-error",
1227+
headSha: HEAD,
1228+
checks: [runRecord(31, "build", "in_progress", null)],
1229+
},
1230+
]);
1231+
expect(progress(ran.live)).toEqual(["Checks at aaaaaaa: 0 passed, 0 failed, 1 pending"]);
1232+
});
1233+
1234+
it("W25: a long explicit timeout keeps polling across a maximum timer arm", function* () {
1235+
const fixture = yield* useFixture();
1236+
fixture.ci.statuses = [commitStatus(42, "green", "pending")];
1237+
const seconds = Number.MAX_SAFE_INTEGER;
1238+
const task = yield* started(fixture, watching(seconds), new InMemoryStream());
1239+
const arm = yield* fixture.clock.waitFor((at) => at === START + 2_147_483_647);
1240+
yield* pollAt(fixture);
1241+
fixture.clock.advanceTo(arm);
1242+
yield* fixture.clock.waitFor((at) => at === arm + 2_147_483_647);
1243+
const next = yield* fixture.clock.waitFor((at) => at === arm + CHECK_WATCH_POLL_MS);
1244+
fixture.ci.statuses = [commitStatus(42, "green", "success")];
1245+
fixture.clock.advanceTo(next);
1246+
const ran = yield* finished(task);
1247+
expect(results(ran.events)).toEqual([
1248+
{ outcome: "success", headSha: HEAD, checks: [statusRecord(42, "green", "success")] },
1249+
]);
1250+
expect(retained(ran.events, PULL_REQUEST_CHECKS_WATCH_DEADLINE)).toEqual([
1251+
{ deadline: String(BigInt(START) + BigInt(seconds) * 1000n) },
1252+
]);
1253+
});
1254+
});

‎packages/core/tests/fixtures/syntax-generated-base.json‎

Lines changed: 2 additions & 2 deletions
Large diffs are not rendered by default.

‎packages/git/src/composition/components.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,12 +298,12 @@ proceed once CI is green — read the checks, then decide, rather than merging a
298298
hoping.
299299

300300
```mdx
301-
<PullRequest.Checks url={pullRequest.url} watch timeoutSeconds={600} as="checkResult" />
301+
<PullRequest.Checks url={pullRequest.url} watch as="checkResult" />
302302
```
303303

304-
In a REPL entry, `watch` waits instead of reading once: it pins the head it first
305-
sees and polls until every reported check has settled or `timeoutSeconds` runs
306-
out, showing progress live. It binds `{ outcome, reason?, headSha, checks }`.
304+
In a REPL entry, `watch` pins the first head it sees and polls for settled
305+
checks, showing progress live. Supply `timeoutSeconds` to set a deadline.
306+
The watch binds `{ outcome, reason?, headSha, checks }`.
307307
`outcome` is `success` or `failure` once every check has settled — skipped and
308308
neutral pass, and one failure fails — or `incomplete` with a `reason` of
309309
`timeout`, `head-changed` or `read-error`. Branch on `outcome` before filtering

‎packages/git/src/composition/components/PullRequestReads.ts‎

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -78,22 +78,22 @@ const checksProps: PropsSchema = {
7878
type: "string",
7979
minLength: 1,
8080
description:
81-
"The name of the only provider that may answer, for a URL another provider would " +
82-
"also recognize, such as a self-hosted deployment: lower-case letters, digits " +
83-
"and hyphens, starting with a letter.",
81+
"Selects the provider, including for URLs automatic matching may not recognize, " +
82+
"such as a self-hosted deployment. Use lower-case letters, digits and hyphens, " +
83+
"starting with a letter.",
8484
},
8585
watch: {
8686
type: "boolean",
8787
description:
8888
"Wait until every check reported for the pinned head settles, instead of reading " +
89-
"once, and bind one watch result. Needs `timeoutSeconds`; only a REPL entry watches.",
89+
"once, and bind one watch result. Only a REPL entry watches.",
9090
},
9191
timeoutSeconds: {
9292
type: "integer",
9393
minimum: 1,
9494
description:
95-
"How many seconds a watch waits before it ends `incomplete`: a positive whole " +
96-
"number, required with `watch` and refused without it.",
95+
"Optional positive safe integer in seconds. Omit it to watch without a deadline. " +
96+
"Requires `watch`.",
9797
},
9898
},
9999
required: ["url"],
@@ -255,7 +255,7 @@ function read(
255255
props: Record<string, Json>,
256256
operations: PullRequestReadOperations,
257257
): Operation<Json> {
258-
const watching = kind === "checks" ? watchBudget(props, element) : undefined;
258+
const timeoutSeconds = kind === "checks" ? watchTimeout(props, element) : undefined;
259259
// Canonicalized before any provider sees it, so the component, the durable
260260
// request and every adapter read one answer.
261261
const url = canonicalPullRequestUrl(props.url);
@@ -277,8 +277,8 @@ function read(
277277
);
278278
}
279279

280-
if (watching !== undefined) {
281-
return yield* watched(operations, { url, provider, timeoutSeconds: watching }, element);
280+
if (kind === "checks" && props.watch === true) {
281+
return yield* watched(operations, { url, provider, timeoutSeconds }, element);
282282
}
283283

284284
const result = yield* operations.read({ url, kind, provider });
@@ -309,28 +309,28 @@ function read(
309309
}
310310

311311
/**
312-
* The budget a checks read watches for, or `undefined` for a snapshot.
312+
* Validate an explicit timeout before a provider sees the request.
313313
*
314314
* Decided before the URL is read, so an incomplete watch is refused before any
315315
* provider, credential or request is involved.
316316
*/
317-
function watchBudget(written: Record<string, Json>, element: string): number | undefined {
317+
function watchTimeout(written: Record<string, Json>, element: string): number | undefined {
318318
const budget = written.timeoutSeconds;
319-
if (written.watch !== true) {
320-
if (budget !== undefined) {
321-
throw new PullRequestReadError(
322-
"invalid-watch",
323-
element,
324-
"`timeoutSeconds` bounds a watch, so it is written with `watch`.",
325-
);
326-
}
319+
if (budget === undefined) {
327320
return undefined;
328321
}
322+
if (written.watch !== true) {
323+
throw new PullRequestReadError(
324+
"invalid-watch",
325+
element,
326+
"`timeoutSeconds` bounds a watch, so it is written with `watch`.",
327+
);
328+
}
329329
if (typeof budget !== "number" || !Number.isSafeInteger(budget) || budget < 1) {
330330
throw new PullRequestReadError(
331331
"invalid-watch",
332332
element,
333-
"a watch needs `timeoutSeconds`, a positive whole number of seconds to wait.",
333+
"`timeoutSeconds` must be a positive safe integer in seconds.",
334334
);
335335
}
336336
return budget;
@@ -339,7 +339,7 @@ function watchBudget(written: Record<string, Json>, element: string): number | u
339339
/** One watch, with its final answer read whole before it binds. */
340340
function* watched(
341341
operations: PullRequestReadOperations,
342-
invocation: { url: string; provider: string | undefined; timeoutSeconds: number },
342+
invocation: { url: string; provider: string | undefined; timeoutSeconds: number | undefined },
343343
element: string,
344344
): Operation<Json> {
345345
const result = parseCheckWatchResult(yield* operations.watch(invocation));
@@ -397,7 +397,7 @@ const DOCUMENTATION: Readonly<Record<PullRequestReadKind, FirstPartyDocumentatio
397397
description:
398398
`Read pull request checks. \`<PullRequest.Checks url={pullRequest.url} as="checks" />\` ` +
399399
`reads check runs and commit statuses for its head. In a REPL entry, add ` +
400-
`\`watch timeoutSeconds={600}\` to wait until every reported check settles.`,
400+
`\`watch\` to wait for checks to settle. Supply \`timeoutSeconds\` to set a deadline.`,
401401
as:
402402
"Required. Each check's kind, name, head commit and outcome; with `watch`, one " +
403403
"result with the outcome, reason, pinned head and those checks.",

‎packages/git/src/composition/errors.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,12 @@ export class PullRequestReadError extends StaleInputError {
332332

333333
readonly reason: PullRequestReadReason;
334334

335-
constructor(reason: PullRequestReadReason, element: string, sentence: string) {
335+
constructor(
336+
reason: PullRequestReadReason,
337+
element: string,
338+
sentence: string,
339+
readonly retryable = reason === "unavailable",
340+
) {
336341
super(`${element} could not read the evidence it names: ${sentence}`);
337342
this.reason = reason;
338343
}

0 commit comments

Comments
 (0)