// PEP-218: actions on a Bugsink error from Peppa's error browser (web, MCP). «Завести задачу» // goes the same way as an error from the webhook (PEP-58): the issue is recorded as an incident // (`ingestIssue`, idempotent) and the PM's decision «backlog» makes the bug task — a second // press, or the policy having been faster, returns the task that already exists. import { issueFromEvent } from "@peppa/bugsink"; import { clearErrorsCache, errorForAction, getIncident, ingestIssue, PeppaError, resolveTask, setErrorState, type ErrorScope, type ErrorsOptions, type ErrorState, type TaskRef, } from "@peppa/core"; import { incidents, projects, tasks, type Db } from "@peppa/db"; import { formatTaskKey } from "@peppa/shared"; import { m } from "@peppa/shared/i18n"; import { eq } from "drizzle-orm"; import { decideIncident, resolveIncident } from "./incidents.ts"; async function taskOf(db: Db, taskId: number | null): Promise { if (!taskId) return null; const [t] = await db.select({ key: projects.key, number: tasks.number }).from(tasks).innerJoin(projects, eq(projects.id, tasks.projectId)).where(eq(tasks.id, taskId)); return t ? resolveTask(db, formatTaskKey(t.key, t.number)) : null; } /** «Завести задачу»: the bug task of the error (existing or new, in the backlog). */ export async function fileErrorTask(db: Db, scope: ErrorScope, issueId: string, who: string, o: ErrorsOptions = {}): Promise<{ task: TaskRef; created: boolean }> { const { client, issue, project } = await errorForAction(db, scope, issueId, o); if (project.projectId == null) throw new PeppaError(m("Этот проект учёта ошибок не подключён ни к одному проекту — задачу завести некуда.")); const event = await client.latestEvent(issue.id).catch(() => null); const r = await ingestIssue(db, issueFromEvent({ ...issue, url: client.issueUrl(issue.id) }, event, "POLL")); if (!r) throw new PeppaError(m("Этот проект учёта ошибок не подключён ни к одному проекту — задачу завести некуда.")); const existing = await taskOf(db, r.incident.taskId); if (existing) return { task: existing, created: false }; try { const d = await decideIncident(db, r.incident.id, "backlog", who); clearErrorsCache(); if (d.task) return { task: d.task, created: true }; } catch (e) { // The policy (or another click) decided first: its task, if it made one, is the answer. const again = await taskOf(db, (await getIncident(db, r.incident.id)).taskId); if (again) return { task: again, created: false }; throw e; } throw new PeppaError(m("Задачу завести не удалось.")); } /** «Решено»: through Peppa's incident when there is one (its task gets a note), else in Bugsink. */ export async function resolveError(db: Db, scope: ErrorScope, issueId: string, who: string, o: ErrorsOptions = {}): Promise { const { client, issue } = await errorForAction(db, scope, issueId, o); const [inc] = await db.select().from(incidents).where(eq(incidents.bugsinkIssueId, issue.id)); if (inc && inc.status !== "resolved") { const r = await resolveIncident(db, inc.id, { who, client }); clearErrorsCache(); if (!r.bugsinkResolved && !issue.is_resolved) throw new PeppaError(m("Учёт ошибок не отвечает. Попробуйте через минуту.")); return "resolved"; } return setErrorState(db, scope, issueId, "resolve", o); }