From 7a0b5c524432a7cc6f079cab7b940c1d6906011a Mon Sep 17 00:00:00 2001 From: Alajmah <55954113+Alajmah@users.noreply.github.com> Date: Sun, 22 Nov 2026 22:53:19 +0300 Subject: [PATCH] fix(webhook): stop enqueueing validate-configs on push Every push enqueued a `validate-configs` job next to `sync-repo`. No worker acts on that job name: the webhook worker handles sync-installation and sync-repo, and for any other name it only logs "Generic event logged" at debug level. Each push therefore added a job that was stored and never did anything. handlePush now enqueues sync-repo only, with the same payload and priority, and still invalidates the cached .gitwire.yml when a push changes it. Validating .gitwire.yml on push is not an active capability. Validation runs on request, through the policy preview API and the dashboard's playground. The jobs already stored in Redis are left untouched. They belong to the separate Redis cleanup decision. tests/unit/webhook-push-enqueues-sync-only.test.js covers the enqueue, the absence of validate-configs, and the cache invalidation. It fails on the previous handler and passes on this one. Nineteen deliberate mutations of the handler each make it fail. Signed-off-by: Alajmah <55954113+Alajmah@users.noreply.github.com> --- .../web/src/lib/webhookHandlers/handlePush.js | 9 ++- .../webhook-push-enqueues-sync-only.test.js | 80 +++++++++++++++++++ 2 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 packages/web/tests/unit/webhook-push-enqueues-sync-only.test.js diff --git a/packages/web/src/lib/webhookHandlers/handlePush.js b/packages/web/src/lib/webhookHandlers/handlePush.js index b5679741..9830593e 100644 --- a/packages/web/src/lib/webhookHandlers/handlePush.js +++ b/packages/web/src/lib/webhookHandlers/handlePush.js @@ -1,6 +1,12 @@ // src/lib/webhookHandlers/handlePush.js // Handler for "push" webhook events. -// Triggers incremental repo sync, config validation, and cache invalidation. +// Triggers incremental repo sync and config cache invalidation. +// +// Nothing is enqueued to validate .gitwire.yml on push. No worker consumes +// such a job (startWebhookWorker handles sync-installation and sync-repo +// only), so an enqueue would only add a job that is stored and never +// processed. Validation runs on request instead: the policy preview API +// and the dashboard's playground. import { invalidateConfigCache } from "../../services/configService.js"; @@ -8,7 +14,6 @@ export async function handlePush(payload, deliveryId, ctx) { const jobData = { eventName: "push", payload, deliveryId, receivedAt: Date.now() }; await ctx.webhookQueue.add("sync-repo", jobData, { priority: 3 }); - await ctx.webhookQueue.add("validate-configs", jobData, { priority: 2 }); // Invalidate .gitwire.yml cache if it changed in this push const changed = [ diff --git a/packages/web/tests/unit/webhook-push-enqueues-sync-only.test.js b/packages/web/tests/unit/webhook-push-enqueues-sync-only.test.js new file mode 100644 index 00000000..64f9f0f8 --- /dev/null +++ b/packages/web/tests/unit/webhook-push-enqueues-sync-only.test.js @@ -0,0 +1,80 @@ +// tests/unit/webhook-push-enqueues-sync-only.test.js +// A push enqueues the repository sync and nothing else. It used to enqueue a +// second job named `validate-configs` as well, and no worker ever consumed +// that name: every push added a job that was stored and never processed. +// Validation of `.gitwire.yml` runs on request (policy preview API and the +// dashboard's playground), not on push. + +import { jest } from "@jest/globals"; + +const mockInvalidateConfigCache = jest.fn(async () => undefined); +jest.unstable_mockModule("../../src/services/configService.js", () => ({ + invalidateConfigCache: mockInvalidateConfigCache, +})); + +const { handlePush } = await import("../../src/lib/webhookHandlers/handlePush.js"); + +const mockWebhookQueueAdd = jest.fn(async () => undefined); +const ctx = () => ({ webhookQueue: { add: mockWebhookQueueAdd } }); + +function pushPayload({ added = [], modified = [] } = {}) { + return { + repository: { id: 7, full_name: "acme/widgets" }, + head_commit: { added, modified }, + sender: { login: "alice" }, + }; +} + +beforeEach(() => { + jest.clearAllMocks(); +}); + +describe("handlePush — jobs enqueued", () => { + it("enqueues the repository sync once, with the push payload and priority 3", async () => { + const payload = pushPayload({ modified: ["src/index.js"] }); + await handlePush(payload, "delivery-1", ctx()); + + expect(mockWebhookQueueAdd).toHaveBeenCalledTimes(1); + const [name, jobData, opts] = mockWebhookQueueAdd.mock.calls[0]; + expect(name).toBe("sync-repo"); + expect(jobData.eventName).toBe("push"); + expect(jobData.payload).toBe(payload); + expect(jobData.deliveryId).toBe("delivery-1"); + expect(jobData.receivedAt).toEqual(expect.any(Number)); + expect(opts).toEqual({ priority: 3 }); + }); + + it("does not enqueue a validate-configs job", async () => { + await handlePush(pushPayload({ modified: [".gitwire.yml"] }), "delivery-2", ctx()); + + const jobNames = mockWebhookQueueAdd.mock.calls.map((call) => call[0]); + expect(jobNames).not.toContain("validate-configs"); + expect(jobNames).toEqual(["sync-repo"]); + }); +}); + +describe("handlePush — config cache", () => { + it.each([ + ["modifies the root config", { modified: [".gitwire.yml"] }], + ["adds the root config", { added: [".gitwire.yml"] }], + ["modifies the config under .github", { modified: [".github/.gitwire.yml"] }], + ])("invalidates the config cache when the push %s", async (_label, changed) => { + await handlePush(pushPayload(changed), "delivery-3", ctx()); + + expect(mockInvalidateConfigCache).toHaveBeenCalledTimes(1); + expect(mockInvalidateConfigCache).toHaveBeenCalledWith("acme/widgets"); + expect(mockWebhookQueueAdd).toHaveBeenCalledTimes(1); + }); + + it.each([ + ["other files", { modified: ["src/index.js"], added: ["src/new.js"] }], + ["a file of the same name in another directory", { modified: ["sub/.gitwire.yml"] }], + ["no head commit", null], + ])("leaves the config cache alone when the push changes %s", async (_label, changed) => { + const payload = changed ? pushPayload(changed) : { repository: { id: 7, full_name: "acme/widgets" } }; + await handlePush(payload, "delivery-4", ctx()); + + expect(mockInvalidateConfigCache).not.toHaveBeenCalled(); + expect(mockWebhookQueueAdd).toHaveBeenCalledTimes(1); + }); +});