mirror of
https://github.com/getpaseo/paseo.git
synced 2026-11-19 23:05:21 +00:00
fix(server): include failing check links in PR watch updates
This commit is contained in:
@@ -64,7 +64,10 @@ function describeUpdate(group: WatchGroup, next: ChangeRequestSnapshot) {
|
||||
(check) =>
|
||||
check.state === "failed" && (next.headSha !== previous.headSha || !failed.has(check.id)),
|
||||
)
|
||||
.map((check) => `Check failed on ${next.headSha}: ${JSON.stringify(check.name)}`);
|
||||
.map(
|
||||
(check) =>
|
||||
`Check failed on ${next.headSha}: ${JSON.stringify(check.name)}${check.url ? ` (${check.url})` : ""}`,
|
||||
);
|
||||
if (
|
||||
next.checksState === "passed" &&
|
||||
(previous.checksState !== "passed" || previous.headSha !== next.headSha)
|
||||
@@ -416,26 +419,35 @@ export function createChangeRequestWatches(deps: Dependencies) {
|
||||
|
||||
return {
|
||||
async start() {
|
||||
let restored: WatchGroup[];
|
||||
const active: WatchGroup[] = [];
|
||||
let pruned = false;
|
||||
try {
|
||||
const stored = await readFile(file, "utf8").catch((error: NodeJS.ErrnoException) => {
|
||||
if (error.code === "ENOENT") return "[]";
|
||||
throw error;
|
||||
});
|
||||
restored = z.array(watchGroupSchema).parse(JSON.parse(stored));
|
||||
const restored = z.array(watchGroupSchema).parse(JSON.parse(stored));
|
||||
for (const group of restored) {
|
||||
const watchers = [];
|
||||
for (const watcher of group.watchers) {
|
||||
if (await deps.isActive(watcher.agentId)) watchers.push(watcher);
|
||||
}
|
||||
pruned ||= watchers.length !== group.watchers.length;
|
||||
if (watchers.length) active.push({ ...group, watchers });
|
||||
}
|
||||
} catch (error) {
|
||||
recoveryError = error;
|
||||
deps.logger.error({ err: error, file }, "Could not recover change request watches");
|
||||
return;
|
||||
}
|
||||
for (const group of restored) groups.set(group.url, group);
|
||||
for (const group of restored) {
|
||||
const watchers = [];
|
||||
for (const watcher of group.watchers) {
|
||||
if (await deps.isActive(watcher.agentId)) watchers.push(watcher);
|
||||
if (pruned) {
|
||||
try {
|
||||
await writeJsonFileAtomic(file, active);
|
||||
} catch (error) {
|
||||
deps.logger.warn({ err: error, file }, "Could not prune archived change request watches");
|
||||
}
|
||||
if (watchers.length !== group.watchers.length) await commit({ ...group, watchers });
|
||||
}
|
||||
for (const group of active) groups.set(group.url, group);
|
||||
for (const group of groups.values()) {
|
||||
if (group.snapshot.state === "open") observe(group.url);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { mkdir, mkdtemp, rm } from "node:fs/promises";
|
||||
import { mkdir, mkdtemp, readFile, rename, rm } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import pino from "pino";
|
||||
import { afterEach, expect, test, vi } from "vitest";
|
||||
@@ -71,7 +71,14 @@ test("shares one observation and restores a busy recipient's unsent digest", asy
|
||||
const failing = {
|
||||
...initial,
|
||||
checksState: "failed" as const,
|
||||
checks: [{ id: "1", name: "build", state: "failed" as const }],
|
||||
checks: [
|
||||
{
|
||||
id: "1",
|
||||
name: "build",
|
||||
state: "failed" as const,
|
||||
url: "https://github.com/owner/repo/actions/runs/1",
|
||||
},
|
||||
],
|
||||
};
|
||||
await listener!.snapshot(failing);
|
||||
await vi.advanceTimersByTimeAsync(15_000);
|
||||
@@ -87,6 +94,7 @@ test("shares one observation and restores a busy recipient's unsent digest", asy
|
||||
await watches.flush();
|
||||
expect(delivered).toEqual([["first", expect.stringContaining("build")]]);
|
||||
expect(delivered[0]?.[1]).toContain("#issuecomment-101");
|
||||
expect(delivered[0]?.[1]).toContain(failing.checks[0]!.url);
|
||||
|
||||
await watches.close();
|
||||
watches = create();
|
||||
@@ -337,3 +345,44 @@ test("sends all due digests in one turn when an agent becomes idle", async () =>
|
||||
await rm(home, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("recovers active watches even when pruning archived watchers cannot be written", async () => {
|
||||
const home = await createTestHome();
|
||||
const file = join(home, "change-request-watches.json");
|
||||
const backup = join(home, "saved-watches.json");
|
||||
const create = (isActive: (id: string) => Promise<boolean>) =>
|
||||
createChangeRequestWatches({
|
||||
source: {
|
||||
resolve: (input) => input,
|
||||
observe(_address, callbacks) {
|
||||
queueMicrotask(() => void callbacks.snapshot(initial));
|
||||
return () => {};
|
||||
},
|
||||
},
|
||||
paseoHome: home,
|
||||
isActive,
|
||||
deliver: async () => true,
|
||||
logger: pino({ enabled: false }),
|
||||
});
|
||||
let watches = create(async () => true);
|
||||
try {
|
||||
await watches.start();
|
||||
await watches.subscribe("archived", url);
|
||||
await watches.subscribe("active", url);
|
||||
await watches.close();
|
||||
const stored = await readFile(file, "utf8");
|
||||
watches = create(async (id) => {
|
||||
if (id !== "archived") return true;
|
||||
await rename(file, backup);
|
||||
await mkdir(file);
|
||||
return false;
|
||||
});
|
||||
await watches.start();
|
||||
await expect(watches.unsubscribe("archived", url)).resolves.toMatchObject({ removed: false });
|
||||
await expect(watches.subscribe("active", url)).resolves.toMatchObject({ subscribed: true });
|
||||
expect(await readFile(backup, "utf8")).toBe(stored);
|
||||
} finally {
|
||||
await watches.close();
|
||||
await rm(home, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
@@ -3,6 +3,7 @@ import { z } from "zod";
|
||||
const changeRequestCheckSchema = z.object({
|
||||
id: z.string(),
|
||||
name: z.string(),
|
||||
url: z.url({ protocol: /^https?$/ }).optional(),
|
||||
state: z.enum(["pending", "failed", "passed", "neutral"]),
|
||||
});
|
||||
export const changeRequestStatusSchema = z.object({
|
||||
|
||||
@@ -50,6 +50,7 @@ const failedStates = new Set([
|
||||
"ACTION_REQUIRED",
|
||||
"STARTUP_FAILURE",
|
||||
]);
|
||||
const checkUrlSchema = z.url({ protocol: /^https?$/ });
|
||||
const checkSchema = z
|
||||
.discriminatedUnion("__typename", [
|
||||
z.object({
|
||||
@@ -58,20 +59,32 @@ const checkSchema = z
|
||||
name: z.string(),
|
||||
status: z.string(),
|
||||
conclusion: z.string().nullable(),
|
||||
detailsUrl: z.string().nullish(),
|
||||
}),
|
||||
z.object({
|
||||
__typename: z.literal("StatusContext"),
|
||||
id: z.string(),
|
||||
context: z.string(),
|
||||
targetUrl: z.string().nullish(),
|
||||
state: z.string(),
|
||||
}),
|
||||
])
|
||||
.transform((check): ChangeRequestSnapshot["checks"][number] => {
|
||||
if (check.__typename === "CheckRun") {
|
||||
const status = check.status === "COMPLETED" ? check.conclusion : "PENDING";
|
||||
return { id: check.id, name: check.name, state: checkState(status) };
|
||||
return {
|
||||
id: check.id,
|
||||
name: check.name,
|
||||
state: checkState(status),
|
||||
url: checkUrlSchema.safeParse(check.detailsUrl).data,
|
||||
};
|
||||
}
|
||||
return { id: check.id, name: check.context, state: checkState(check.state) };
|
||||
return {
|
||||
id: check.id,
|
||||
name: check.context,
|
||||
state: checkState(check.state),
|
||||
url: checkUrlSchema.safeParse(check.targetUrl).data,
|
||||
};
|
||||
});
|
||||
|
||||
function checkState(status: string | null): ChangeRequestSnapshot["checksState"] {
|
||||
|
||||
@@ -5192,6 +5192,16 @@ it("batches co-due workspace discovery and exact PR reads with one check rollup"
|
||||
context: "ci",
|
||||
state: "SUCCESS",
|
||||
};
|
||||
const checkUrl = "https://github.com/acme/widgets/actions/runs/7";
|
||||
const check = {
|
||||
__typename: "CheckRun",
|
||||
id: "run",
|
||||
name: "build",
|
||||
status: "COMPLETED",
|
||||
conclusion: "SUCCESS",
|
||||
detailsUrl: checkUrl,
|
||||
};
|
||||
const targetUrl = "https://example.com/ci";
|
||||
const runner = createRunner([
|
||||
batchPollStatusJson({
|
||||
t0: batchPollRepositoryJson([
|
||||
@@ -5211,7 +5221,10 @@ it("batches co-due workspace discovery and exact PR reads with one check rollup"
|
||||
pullRequest: {
|
||||
state: "OPEN",
|
||||
headRefOid: "abc",
|
||||
statusCheckRollup: { state: "SUCCESS", contexts: { nodes: [rollup], pageInfo } },
|
||||
statusCheckRollup: {
|
||||
state: "SUCCESS",
|
||||
contexts: { nodes: [{ ...rollup, targetUrl }, check], pageInfo },
|
||||
},
|
||||
reviewDecision: null,
|
||||
comments: { nodes: [], pageInfo },
|
||||
reviews: { nodes: [], pageInfo },
|
||||
@@ -5253,7 +5266,16 @@ it("batches co-due workspace discovery and exact PR reads with one check rollup"
|
||||
expect(runner.calls[0]?.args.at(-1)).toContain('pullRequests(headRefName: "feature"');
|
||||
expect(runner.calls[0]?.args.at(-1)).toContain("pullRequest(number: 41)");
|
||||
expect(runner.calls[0]?.args.at(-1)?.match(/statusCheckRollup/g)).toHaveLength(1);
|
||||
expect(snapshots).toEqual([expect.objectContaining({ headSha: "abc", checksState: "passed" })]);
|
||||
expect(snapshots).toEqual([
|
||||
expect.objectContaining({
|
||||
headSha: "abc",
|
||||
checksState: "passed",
|
||||
checks: [
|
||||
expect.objectContaining({ id: "context", url: targetUrl }),
|
||||
expect.objectContaining({ id: "run", url: checkUrl }),
|
||||
],
|
||||
}),
|
||||
]);
|
||||
expect(statuses).toEqual([expect.objectContaining({ number: 41, checksStatus: "success" })]);
|
||||
} finally {
|
||||
service.dispose?.();
|
||||
|
||||
Reference in New Issue
Block a user