| 38 | } |
| 39 | |
| 40 | async tick(now: number, wait?: Call, source: "timer" | "post-tool" = "timer"): Promise<void> { |
| 41 | if (this.done) return; |
| 42 | const { fixture } = this; |
| 43 | if (!fixture.pr) { |
| 44 | const pulls = await this.github.api<Array<{ number: number }>>( |
| 45 | "GET", `/repos/${fixture.repo}/pulls?state=all&base=main&per_page=100`, |
| 46 | ); |
| 47 | if (!pulls.length) return; |
| 48 | if (pulls.length !== 1 || !pulls[0]) throw new Error("Expected exactly one fixture PR"); |
| 49 | fixture.pr = await this.github.pull(fixture.repo, pulls[0].number); |
| 50 | } |
| 51 | const pr = await this.github.pull(fixture.repo, fixture.pr.number); |
| 52 | fixture.pr = pr; |
| 53 | if (pr.state !== "open") { |
| 54 | this.done = true; |
| 55 | return; |
| 56 | } |
| 57 | if (this.step.kind === "merge" && pr.mergeable !== true) { |
| 58 | if (pr.mergeable === false) throw new Error("The fixture PR has an unexpected merge conflict"); |
| 59 | return; |
| 60 | } |
| 61 | const expectedFix = this.step.kind === "approval" ? this.step.greeting |
| 62 | : this.step.kind === "review" ? this.step.afterFix : undefined; |
| 63 | if (expectedFix !== undefined && this.verifiedHead !== pr.head.sha) { |
| 64 | if (pr.head.sha === this.phaseHead) return; |
| 65 | if (await this.github.greeting(fixture.repo, pr.head.sha) !== expectedFix) return; |
| 66 | this.actions.push({ kind: "fix", at: now, pr: pr.number, head: pr.head.sha, trigger: "verification" }); |
| 67 | this.verifiedHead = pr.head.sha; |
| 68 | this.fallbackStartedAt = now; |
| 69 | } |
| 70 | // A listener started before this phase only counts if it keeps listening after an event: a one-shot listener |
| 71 | // from an earlier phase may be about to exit with that phase's event. |
| 72 | const phaseWait = wait && wait.endedAt === undefined |
| 73 | && wait.startedAt <= now |
| 74 | && (wait.startedAt >= this.phaseStartedAt || persistentWait(wait) |
| 75 | || this.actions.some(action => action.callId === wait.id)); |
| 76 | const afterTool = this.step.kind === "review" && this.step.delivery === "after-tool" && source === "post-tool"; |
| 77 | // The fix may only become visible after the push's post-tool tick, and an agent that then blocks on a listener |
| 78 | // completes no further tool, so an attached listener can take the approval once the fix is verified. |
| 79 | const afterFix = this.step.kind === "approval" && this.step.delivery === "after-fix" |
| 80 | && (source === "post-tool" || Boolean(phaseWait)); |
| 81 | const requiresPostTool = (this.step.kind === "review" && this.step.delivery === "after-tool") |
| 82 | || (this.step.kind === "approval" && this.step.delivery === "after-fix"); |
| 83 | const duringWait = !requiresPostTool && phaseWait; |
| 84 | if (!afterTool && !afterFix && !duringWait && now - this.fallbackStartedAt < this.fallbackMs) return; |
| 85 | const viaListener = duringWait || (afterFix && source !== "post-tool"); |
| 86 | const action: Action = { |
| 87 | kind: this.phase, at: now, pr: pr.number, head: pr.head.sha, |
| 88 | trigger: afterTool ? "after-tool" : afterFix ? "after-fix" : duringWait ? "wait" : "fallback", |
| 89 | ...(viaListener && wait ? { callId: wait.id } : {}), |
| 90 | }; |
| 91 | const reviewId = await this.perform(this.step, pr); |
| 92 | if (reviewId !== undefined) action.reviewId = reviewId; |
| 93 | this.actions.push(action); |
| 94 | const next = this.remaining.shift(); |
| 95 | if (next) { |
| 96 | this.step = next; |
| 97 | this.phaseStartedAt = action.at; |