83 lines
5.0 KiB
TypeScript
83 lines
5.0 KiB
TypeScript
// PEP-127: stories in review whose «готова к ревью» message is held back (the PR can't be merged
|
|
// yet) or went stale (the main branch moved on and the PR got into a conflict). Once a minute:
|
|
// green and no conflict → the message goes to the PM; a conflict → the agent rebases (once per
|
|
// head commit) and a message the PM already has is taken back; red checks go to the agent by
|
|
// CI feedback as before.
|
|
import { isProjectPaused, answerQuestion, clearReviewHold, getPendingAccept, giteaForBot, listReviewHolds, listSessions, listTasks, pendingQuestions, prGate, messageDetails, REVIEW_REVOKED_ANSWER, sendReviewCard, setReviewHold, taskPrNumber, type GiteaClient, type ReviewHold, type TaskRef } from "@peppa/core";
|
|
import { projects, type Db } from "@peppa/db";
|
|
import { formatTaskKey } from "@peppa/shared";
|
|
import { eq } from "drizzle-orm";
|
|
import { conflictStep } from "./conflict-step.ts";
|
|
import { resolveTaskById } from "./sessions.ts";
|
|
|
|
export interface ReviewCardTickOptions {
|
|
gitea?: GiteaClient;
|
|
log?: (m: string) => void;
|
|
}
|
|
|
|
export async function reviewCardTick(db: Db, opts: ReviewCardTickOptions = {}): Promise<void> {
|
|
const log = opts.log ?? (() => {});
|
|
for (const [taskId, hold] of await listReviewHolds(db)) {
|
|
try {
|
|
await releaseHeld(db, taskId, hold, opts.gitea, log);
|
|
} catch (e) {
|
|
log(`review hold #${taskId}: ${(e as Error).message}`);
|
|
}
|
|
}
|
|
await revokeStale(db, opts.gitea, log).catch((e) => log(`stale review messages: ${(e as Error).message}`));
|
|
}
|
|
|
|
const reviewCards = async (db: Db, ref: TaskRef) => (await pendingQuestions(db, ref.project.id)).filter((q) => q.taskId === ref.task.id && q.kind === "attention");
|
|
|
|
async function releaseHeld(db: Db, taskId: number, hold: ReviewHold, client: GiteaClient | undefined, log: (m: string) => void) {
|
|
const ref = await resolveTaskById(db, taskId).catch(() => null);
|
|
if (!ref || ref.task.archivedAt || ["backlog", "done", "blocked"].includes(ref.task.status)) return clearReviewHold(db, taskId);
|
|
// PEP-174: on pause the message waits (it is sent after «Продолжить» if still due).
|
|
if (await isProjectPaused(db, ref.project.id)) return;
|
|
// Back at work (rebase, red checks): the hold keeps the rebased head until the story returns.
|
|
if (ref.task.status !== "review") return;
|
|
// Accepted already (carried by pendingAcceptTick) or the PM has a message: nothing to send.
|
|
if ((await getPendingAccept(db, taskId)) || (await reviewCards(db, ref)).length) return clearReviewHold(db, taskId);
|
|
const gitea = client ?? giteaForBot();
|
|
const gate = await prGate(ref, gitea).catch(() => null);
|
|
if (!gate) return; // Gitea unreachable: closed, the next minute asks again
|
|
if (gate.ok) {
|
|
const session = (await listSessions(db, { active: true, taskId }))[0] ?? null;
|
|
if (await sendReviewCard(db, ref, { text: hold.text, details: hold.details ?? null }, session, gitea)) log(`${ref.key}: checks green, no conflict — ready-for-review sent`);
|
|
return;
|
|
}
|
|
const pull = gate.pull;
|
|
if (pull && gate.behind) {
|
|
// The main branch moved on: Peppa updates the PR branch itself (once per head); the PM hears after its checks.
|
|
if (hold.updateSha === pull.head.sha) return;
|
|
await setReviewHold(db, taskId, { ...hold, updateSha: pull.head.sha });
|
|
await gitea.updatePullBranch(ref.project.giteaOwner, ref.project.giteaRepo, pull.number);
|
|
log(`${ref.key}: in review, behind the main branch — PR branch updated`);
|
|
return;
|
|
}
|
|
if (pull && pull.state === "open" && pull.mergeable === false) {
|
|
// PEP-269: one step for the conflict — one request to the agent, then a person.
|
|
if (hold.rebaseSha !== pull.head.sha) await setReviewHold(db, taskId, { ...hold, rebaseSha: pull.head.sha });
|
|
await conflictStep(db, ref, pull, { log });
|
|
}
|
|
}
|
|
|
|
/** A message the PM already has, and the PR got into a conflict since: taken back, the agent rebases. */
|
|
async function revokeStale(db: Db, client: GiteaClient | undefined, log: (m: string) => void) {
|
|
for (const task of await listTasks(db, { statuses: ["review"] })) {
|
|
const [project] = await db.select().from(projects).where(eq(projects.id, task.projectId));
|
|
if (!project || !taskPrNumber(project, task.prUrl) || (await isProjectPaused(db, project.id))) continue;
|
|
const ref: TaskRef = { project, task, key: formatTaskKey(project.key, task.number) };
|
|
const cards = await reviewCards(db, ref);
|
|
if (!cards.length) continue;
|
|
const gitea = client ?? giteaForBot();
|
|
const pull = (await prGate(ref, gitea).catch(() => null))?.pull;
|
|
if (!pull || pull.merged || pull.state !== "open" || pull.mergeable !== false) continue;
|
|
for (const q of cards) await answerQuestion(db, q.id, REVIEW_REVOKED_ANSWER, "system").catch(() => {});
|
|
const last = cards[cards.length - 1]!;
|
|
await setReviewHold(db, task.id, { text: last.text, details: (await messageDetails(db, [last.id])).get(last.id) ?? null, at: Date.now(), rebaseSha: pull.head.sha });
|
|
await conflictStep(db, ref, pull, { log });
|
|
log(`${ref.key}: the PR got into a conflict — ready-for-review taken back`);
|
|
}
|
|
}
|