Skip to content

Open-source status page, incident response and uptime monitoring

Fix pager duty (#1556)

Thibault Le Ouay9b47fa623db1
diff --git a/packages/notifications/pagerduty/package.json b/packages/notifications/pagerduty/package.json
index 4c17a3c76..e4d0b8e18 100644
--- a/packages/notifications/pagerduty/package.json
+++ b/packages/notifications/pagerduty/package.json
@@ -1,6 +1,9 @@
{
"name": "@openstatus/notification-pagerduty",
"version": "0.0.0",
+ "scripts": {
+ "test": "bun test"
+ },
"main": "src/index.ts",
"dependencies": {
"@openstatus/db": "workspace:*",
diff --git a/packages/notifications/pagerduty/src/index.test.ts b/packages/notifications/pagerduty/src/index.test.ts
new file mode 100644
index 000000000..abaee1ee3
--- /dev/null
+++ b/packages/notifications/pagerduty/src/index.test.ts
@@ -0,0 +1,131 @@
+import {
+ afterEach,
+ beforeEach,
+ describe,
+ expect,
+ jest,
+ spyOn,
+ test,
+} from "bun:test";
+import { selectNotificationSchema } from "@openstatus/db/src/schema";
+import { sendAlert, sendDegraded, sendRecovery } from "./index";
+
+describe("PagerDuty Notifications", () => {
+ // biome-ignore lint/suspicious/noExplicitAny: <explanation>
+ let fetchMock: any = undefined;
+
+ beforeEach(() => {
+ fetchMock = spyOn(global, "fetch").mockImplementation(() =>
+ Promise.resolve(new Response(null, { status: 200 })),
+ );
+ });
+
+ afterEach(() => {
+ jest.resetAllMocks();
+ });
+
+ test("Send degraded", async () => {
+ const monitor = {
+ id: "monitor-1",
+ name: "API Health Check",
+ url: "https://api.example.com/health",
+ jobType: "http" as const,
+ periodicity: "5m" as const,
+ status: "active" as const, // or "down", "degraded"
+ createdAt: new Date(),
+ updatedAt: new Date(),
+ region: "us-east-1",
+ };
+
+ const a = {
+ id: 1,
+ name: "PagerDuty Notification",
+ provider: "pagerduty",
+ workspaceId: 1,
+ createdAt: new Date(),
+ updatedAt: new Date(),
+ data: '{"pagerduty":"{\\"integration_keys\\":[{\\"integration_key\\":\\"my_key\\",\\"name\\":\\"Default Service\\",\\"id\\":\\"ABCD\\",\\"type\\":\\"service\\"}],\\"account\\":{\\"subdomain\\":\\"test\\",\\"name\\":\\"test\\"}}"}',
+ };
+
+ const n = selectNotificationSchema.parse(a);
+ await sendDegraded({
+ // @ts-expect-error
+ monitor,
+ notification: n,
+ statusCode: 500,
+ message: "Something went wrong",
+ cronTimestamp: Date.now(),
+ });
+ expect(fetchMock).toHaveBeenCalled();
+ });
+
+ test("Send Recovered", async () => {
+ const monitor = {
+ id: "monitor-1",
+ name: "API Health Check",
+ url: "https://api.example.com/health",
+ jobType: "http" as const,
+ periodicity: "5m" as const,
+ status: "active" as const, // or "down", "degraded"
+ createdAt: new Date(),
+ updatedAt: new Date(),
+ region: "us-east-1",
+ };
+
+ const a = {
+ id: 1,
+ name: "PagerDuty Notification",
+ provider: "pagerduty",
+ workspaceId: 1,
+ createdAt: new Date(),
+ updatedAt: new Date(),
+ data: '{"pagerduty":"{\\"integration_keys\\":[{\\"integration_key\\":\\"my_key\\",\\"name\\":\\"Default Service\\",\\"id\\":\\"ABCD\\",\\"type\\":\\"service\\"}],\\"account\\":{\\"subdomain\\":\\"test\\",\\"name\\":\\"test\\"}}"}',
+ };
+
+ const n = selectNotificationSchema.parse(a);
+ await sendRecovery({
+ // @ts-expect-error
+ monitor,
+ notification: n,
+ statusCode: 500,
+ message: "Something went wrong",
+ cronTimestamp: Date.now(),
+ });
+ expect(fetchMock).toHaveBeenCalled();
+ });
+
+ test("Send Alert", async () => {
+ const monitor = {
+ id: "monitor-1",
+ name: "API Health Check",
+ url: "https://api.example.com/health",
+ jobType: "http" as const,
+ periodicity: "5m" as const,
+ status: "active" as const, // or "down", "degraded"
+ createdAt: new Date(),
+ updatedAt: new Date(),
+ region: "us-east-1",
+ };
+ const a = {
+ id: 1,
+ name: "PagerDuty Notification",
+ provider: "pagerduty",
+ workspaceId: 1,
+ createdAt: new Date(),
+ updatedAt: new Date(),
+ data: '{"pagerduty":"{\\"integration_keys\\":[{\\"integration_key\\":\\"my_key\\",\\"name\\":\\"Default Service\\",\\"id\\":\\"ABCD\\",\\"type\\":\\"service\\"}],\\"account\\":{\\"subdomain\\":\\"test\\",\\"name\\":\\"test\\"}}"}',
+ };
+
+ const n = selectNotificationSchema.parse(a);
+
+ await sendAlert({
+ // @ts-expect-error
+ monitor,
+ notification: n,
+ statusCode: 500,
+ message: "Something went wrong",
+ cronTimestamp: Date.now(),
+ });
+ expect(fetchMock).toHaveBeenCalled();
+ });
+});
diff --git a/packages/notifications/pagerduty/src/index.ts b/packages/notifications/pagerduty/src/index.ts
index cb2ab576c..d92375fa9 100644
--- a/packages/notifications/pagerduty/src/index.ts
+++ b/packages/notifications/pagerduty/src/index.ts
@@ -1,4 +1,8 @@
-import type { Monitor, Notification } from "@openstatus/db/src/schema";
+import {
+ type Monitor,
+ type Notification,
+ pagerdutyDataSchema,
+} from "@openstatus/db/src/schema";
import type { Region } from "@openstatus/db/src/schema/constants";
import {
@@ -24,7 +28,10 @@ export const sendAlert = async ({
latency?: number;
region?: Region;
}) => {
- const notificationData = PagerDutySchema.parse(JSON.parse(notification.data));
+ const data = pagerdutyDataSchema.parse(JSON.parse(notification.data));
+
+ const notificationData = PagerDutySchema.parse(JSON.parse(data.pagerduty));
+
const { name } = monitor;
try {
@@ -72,30 +79,32 @@ export const sendDegraded = async ({
latency?: number;
region?: Region;
}) => {
- const notificationData = PagerDutySchema.parse(JSON.parse(notification.data));
- const { name } = monitor;
+ const data = pagerdutyDataSchema.parse(JSON.parse(notification.data));
- const event = triggerEventPayloadSchema.parse({
- routing_key: notificationData.integration_keys[0].integration_key,
- dedup_key: `${monitor.id}}`,
- event_action: "trigger",
- payload: {
- summary: `${name} is degraded`,
- source: "Open Status",
- severity: "warning",
- timestamp: new Date().toISOString(),
- custom_details: {
- statusCode,
- message,
- },
- },
- });
+ const notificationData = PagerDutySchema.parse(JSON.parse(data.pagerduty));
+ const { name } = monitor;
try {
for await (const integrationKey of notificationData.integration_keys) {
// biome-ignore lint/correctness/noUnusedVariables: <explanation>
const { integration_key, type } = integrationKey;
+ const event = triggerEventPayloadSchema.parse({
+ routing_key: integration_key,
+ dedup_key: `${monitor.id}}`,
+ event_action: "trigger",
+ payload: {
+ summary: `${name} is degraded`,
+ source: "Open Status",
+ severity: "warning",
+ timestamp: new Date().toISOString(),
+ custom_details: {
+ statusCode,
+ message,
+ },
+ },
+ });
+
await fetch("https://events.pagerduty.com/v2/enqueue", {
method: "POST",
body: JSON.stringify(event),
@@ -125,7 +134,9 @@ export const sendRecovery = async ({
latency?: number;
region?: Region;
}) => {
- const notificationData = PagerDutySchema.parse(JSON.parse(notification.data));
+ const data = pagerdutyDataSchema.parse(JSON.parse(notification.data));
+
+ const notificationData = PagerDutySchema.parse(JSON.parse(data.pagerduty));
try {
for await (const integrationKey of notificationData.integration_keys) {