diff --git a/src/watch.ts b/src/watch.ts index 205a9e3..8970b9d 100644 --- a/src/watch.ts +++ b/src/watch.ts @@ -150,9 +150,13 @@ export async function checkBuilds(env: Env, at = new Date()): Promise<{ failed: try { scripts = (await cf(env, `/accounts/${account}/workers/scripts`)).result || []; if (!scripts.length) return { failed: 0 }; - const q = new URLSearchParams({ external_script_ids: scripts.map((s) => s.tag).join(",") }); - const r = (await cf(env, `/accounts/${account}/builds/builds/latest?${q}`)).result || {}; - latest = r.builds || r; + // The endpoint takes at most 20 script ids per request. + latest = {}; + for (let i = 0; i < scripts.length; i += 20) { + const q = new URLSearchParams({ external_script_ids: scripts.slice(i, i + 20).map((s) => s.tag).join(",") }); + const r = (await cf(env, `/accounts/${account}/builds/builds/latest?${q}`)).result || {}; + Object.assign(latest, r.builds || r); + } } catch (e) { if (!/answered 403/.test(String((e as Error)?.message))) throw e; // The token lacks Workers Scripts Read or Workers CI Read: say so once instead of failing every run. diff --git a/test/watch.test.ts b/test/watch.test.ts index 510a62a..3f40e76 100644 --- a/test/watch.test.ts +++ b/test/watch.test.ts @@ -29,10 +29,13 @@ before(async () => { return Response.json({ success: true, errors: [], result, result_info: {} }); } if (u.pathname === "/client/v4/accounts/acc/workers/scripts") { - return Response.json({ success: true, errors: [], result: [{ id: "app", tag: "t-app" }, { id: "other", tag: "t-other" }] }); + const filler = Array.from({ length: 20 }, (_, i) => ({ id: `w${i}`, tag: `t-w${i}` })); + return Response.json({ success: true, errors: [], result: [...filler, { id: "app", tag: "t-app" }, { id: "other", tag: "t-other" }] }); } if (u.pathname === "/client/v4/accounts/acc/builds/builds/latest") { - assert.equal(u.searchParams.get("external_script_ids"), "t-app,t-other"); + const ids = u.searchParams.get("external_script_ids").split(","); + assert.ok(ids.length <= 20, "at most 20 script ids per request"); + if (!ids.includes("t-app")) return Response.json({ success: true, errors: [], result: { builds: {} } }); const uuid = buildOutcome === "fail" ? "b-fail" : "b-ok"; return Response.json({ success: true, errors: [], result: { builds: { "t-app": build(uuid, buildOutcome, "main", ["main"]),