Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions packages/web/src/lib/webhookHandlers/handlePush.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
// 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";

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 = [
Expand Down
80 changes: 80 additions & 0 deletions packages/web/tests/unit/webhook-push-enqueues-sync-only.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
Loading