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
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export async function handleRequest(request: Request, env: Env, context?: Webhoo
if (context) {
context.waitUntil(
processing.catch((error) => {
console.error(error);
console.error(error instanceof Error ? error.message : error);
}),
);
return new Response(null, { status: 202 });
Expand All @@ -32,7 +32,7 @@ export async function handleRequest(request: Request, env: Env, context?: Webhoo
await processing;
return new Response(null, { status: 202 });
} catch (error) {
console.error(error);
console.error(error instanceof Error ? error.message : error);
return new Response(error instanceof Error ? error.message : "Webhook processing failed", { status: 500 });
}
}
Expand Down
23 changes: 22 additions & 1 deletion tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createHmac } from "node:crypto";

import { describe, expect, test, vi } from "vitest";
import { afterEach, describe, expect, test, vi } from "vitest";

import { handleRequest } from "../src/index";

Expand All @@ -22,6 +22,10 @@ function webhook(body: string, event: string, signature?: string): Request {
});
}

afterEach(() => {
vi.restoreAllMocks();
});

describe("worker", () => {
test.each([
["/", "Merge when GitHub says it is ready."],
Expand Down Expand Up @@ -69,6 +73,23 @@ describe("worker", () => {
await context.waitUntil.mock.calls[0][0];
});

test("logs asynchronous webhook error messages", async () => {
const errorMock = vi.spyOn(console, "error").mockImplementation(() => undefined);
const body = JSON.stringify({
action: "synchronize",
installation: { id: 1 },
repository: { id: 1, full_name: "owner/repository" },
pull_request: { number: 1, labels: [{ name: "automerge" }] },
});
const context = { waitUntil: vi.fn() };

const response = await handleRequest(webhook(body, "pull_request"), env, context);
await context.waitUntil.mock.calls[0][0];

expect(response.status).toBe(202);
expect(errorMock).toHaveBeenCalledWith("GitHub App private key must be an RSA PEM private key");
});

test("reports webhook processing errors", async () => {
const body = JSON.stringify({
action: "synchronize",
Expand Down