35 lines
2.0 KiB
TypeScript
35 lines
2.0 KiB
TypeScript
// PEP-175: «Готово» = смержено. A story in «Готово» whose PR is still open is a hole in the
|
||
// product; once an hour the watchdog tells the superadmins (not the owner) in one message.
|
||
import { alertAdmins, giteaForBot, getSetting, openUnmergedPull, setSetting, type GiteaClient, type TaskRef } from "@peppa/core";
|
||
import { projects, tasks, type Db } from "@peppa/db";
|
||
import { asIs, m } from "@peppa/shared/i18n";
|
||
import { formatTaskKey } from "@peppa/shared";
|
||
import { and, eq, isNull, isNotNull } from "drizzle-orm";
|
||
|
||
const EVERY_MS = 3_600_000;
|
||
|
||
export async function doneOpenPrTick(db: Db, opts: { now?: Date; gitea?: GiteaClient; log?: (m: string) => void } = {}): Promise<string[]> {
|
||
const now = (opts.now ?? new Date()).getTime();
|
||
const last = (await getSetting<number>(db, "done.openpr.checked")) ?? 0;
|
||
if (now - last < EVERY_MS) return [];
|
||
await setSetting(db, "done.openpr.checked", now);
|
||
const rows = await db
|
||
.select({ task: tasks, project: projects })
|
||
.from(tasks)
|
||
.innerJoin(projects, eq(tasks.projectId, projects.id))
|
||
.where(and(eq(tasks.status, "done"), isNull(tasks.archivedAt), isNotNull(tasks.prUrl)));
|
||
const found: string[] = [];
|
||
for (const r of rows) {
|
||
const ref: TaskRef = { project: r.project, task: r.task, key: formatTaskKey(r.project.key, r.task.number) };
|
||
const open = await openUnmergedPull(ref, opts.gitea ?? giteaForBot()).catch(() => null);
|
||
if (open) found.push(`${ref.key} «${r.task.title}»`);
|
||
}
|
||
if (!found.length) return found;
|
||
const list = found.join(", ");
|
||
// One message per distinct list: the same set is not repeated every hour.
|
||
if ((await getSetting<string>(db, "done.openpr.reported")) === list) return found;
|
||
await setSetting(db, "done.openpr.reported", list);
|
||
await alertAdmins(db, `done-open-pr:${now}`, m("⚠️ В «Готово» лежат задачи, изменения которых не приняты в код проекта: {list}", { list: asIs(list) }), opts.log ?? (() => {}));
|
||
return found;
|
||
}
|