Skip to content

Open-source status page, incident response and uptime monitoring

📟 pagerduty fix (#1161)

Thibault Le Ouay1f122887d6d4
diff --git a/apps/web/src/app/api/callback/pagerduty/route.ts b/apps/web/src/app/api/callback/pagerduty/route.ts
new file mode 100644
index 000000000..e11e21c2a
--- /dev/null
+++ b/apps/web/src/app/api/callback/pagerduty/route.ts
@@ -0,0 +1,12 @@
+import { redirect } from "next/navigation";
+
+export async function GET(request: Request) {
+ const { searchParams } = new URL(request.url);
+ const workspace = searchParams.get("workspace");
+ const url = `${
+ process.env.NODE_ENV === "development" // FIXME: This sucks
+ ? "http://localhost:3000"
+ : "https://www.openstatus.dev"
+ }/app/${workspace}/notifications/new/pagerduty?${searchParams}`;
+ redirect(url);
+}
diff --git a/apps/web/src/app/app/[workspaceSlug]/(dashboard)/notifications/(overview)/_components/channel-table.tsx b/apps/web/src/app/app/[workspaceSlug]/(dashboard)/notifications/(overview)/_components/channel-table.tsx
index 29b9b2095..3be127107 100644
--- a/apps/web/src/app/app/[workspaceSlug]/(dashboard)/notifications/(overview)/_components/channel-table.tsx
+++ b/apps/web/src/app/app/[workspaceSlug]/(dashboard)/notifications/(overview)/_components/channel-table.tsx
@@ -1,3 +1,4 @@
+import { env } from "@/env";
import type { Workspace } from "@openstatus/db/src/schema";
import { getLimit } from "@openstatus/db/src/schema/plan/utils";
@@ -34,11 +35,11 @@ export default function ChannelTable({ workspace, disabled }: ChannelTable) {
<Channel
title="PagerDuty"
description="Send notifications to PagerDuty."
- href={`https://app.pagerduty.com/install/integration?app_id=PN76M56&redirect_url=${
+ href={`https://app.pagerduty.com/install/integration?app_id=${env.PAGERDUTY_APP_ID}&redirect_url=${
process.env.NODE_ENV === "development" // FIXME: This sucks
? "http://localhost:3000"
: "https://www.openstatus.dev"
- }/app/${workspace.slug}/notifications/new/pagerduty&version=2`}
+ }/api/callback/pagerduty?workspace=${workspace.slug}&version=2`}
disabled={disabled || !workspace.limits.pagerduty}
/>
<Separator />
diff --git a/apps/web/src/components/forms/notification/provider/actions.ts b/apps/web/src/components/forms/notification/provider/actions.ts
index d2dae9927..ec0322c6f 100644
--- a/apps/web/src/components/forms/notification/provider/actions.ts
+++ b/apps/web/src/components/forms/notification/provider/actions.ts
@@ -1,11 +1,19 @@
"use server";
-import { sendTest } from "@openstatus/notification-opsgenie";
+import { sendTest as sendOpsGenieAlert } from "@openstatus/notification-opsgenie";
+import { sendTest as sendPagerDutyAlert } from "@openstatus/notification-pagerduty";
export async function sendOpsGenieTestAlert(
apiKey: string,
region: "us" | "eu",
) {
- const isSuccessfull = await sendTest({ apiKey, region });
+ const isSuccessfull = await sendOpsGenieAlert({ apiKey, region });
+ return isSuccessfull;
+}
+
+export async function sendPagerDutyTestAlert(integrationKey: string) {
+ const isSuccessfull = await sendPagerDutyAlert({
+ integrationKey: integrationKey,
+ });
return isSuccessfull;
}
diff --git a/apps/web/src/components/forms/notification/provider/section-pagerduty.tsx b/apps/web/src/components/forms/notification/provider/section-pagerduty.tsx
index 8319be3f9..2ba4020f9 100644
--- a/apps/web/src/components/forms/notification/provider/section-pagerduty.tsx
+++ b/apps/web/src/components/forms/notification/provider/section-pagerduty.tsx
@@ -2,10 +2,20 @@
import type { UseFormReturn } from "react-hook-form";
-import type {
- InsertNotificationWithData,
- WorkspacePlan,
+import { LoadingAnimation } from "@/components/loading-animation";
+import { toastAction } from "@/lib/toast";
+import {
+ type InsertNotificationWithData,
+ InsertNotificationWithDataSchema,
+ NotificationDataSchema,
+ type WorkspacePlan,
+ selectNotificationSchema,
} from "@openstatus/db/src/schema";
+import { PagerDutySchema } from "@openstatus/notification-pagerduty";
+import { Button } from "@openstatus/ui";
+import { useSearchParams } from "next/navigation";
+import { useTransition } from "react";
+import { sendPagerDutyTestAlert } from "./actions";
interface Props {
form: UseFormReturn<InsertNotificationWithData>;
@@ -13,5 +23,42 @@ interface Props {
}
export function SectionPagerDuty({ form }: Props) {
- return null;
+ const [isTestPending, startTestTransition] = useTransition();
+ const searchParams = useSearchParams();
+
+ const config = searchParams.get("config");
+
+ const result = PagerDutySchema.safeParse(JSON.parse(config || ""));
+ // We should fix that but that's not working for editing pagerduty notifications
+ if (result.success === false) {
+ return null;
+ }
+
+ const data = result.data;
+
+ async function sendTestAlert() {
+ startTestTransition(async () => {
+ const isSuccessfull = await sendPagerDutyTestAlert(
+ data.integration_keys[0].integration_key,
+ );
+ if (isSuccessfull) {
+ toastAction("test-success");
+ } else {
+ toastAction("test-error");
+ }
+ });
+ }
+
+ return (
+ <div className="col-span-full text-right">
+ <Button
+ type="button"
+ variant="secondary"
+ className="w-full sm:w-auto"
+ onClick={sendTestAlert}
+ >
+ {!isTestPending ? "Test Alert" : <LoadingAnimation variant="inverse" />}
+ </Button>
+ </div>
+ );
}
diff --git a/apps/web/src/middleware.ts b/apps/web/src/middleware.ts
index 96bfedc26..26513a852 100644
--- a/apps/web/src/middleware.ts
+++ b/apps/web/src/middleware.ts
@@ -155,6 +155,5 @@ export const config = {
// "/node_modules/function-bind/**",
// "**/node_modules/.pnpm/**/function-bind/**",
// "../../packages/analytics/src/index.ts",
- "**/node_modules/.pnpm/@jitsu*/**",
],
};
diff --git a/packages/notifications/pagerduty/src/index.ts b/packages/notifications/pagerduty/src/index.ts
index ff023f4a3..b1f689f4e 100644
--- a/packages/notifications/pagerduty/src/index.ts
+++ b/packages/notifications/pagerduty/src/index.ts
@@ -24,27 +24,25 @@ export const sendAlert = async ({
const notificationData = PagerDutySchema.parse(JSON.parse(notification.data));
const { name } = monitor;
- const event = triggerEventPayloadSchema.parse({
- rounting_key: notificationData.integration_keys[0].integration_key,
- dedup_key: `${monitor.id}}-${incidentId}`,
- event_action: "trigger",
- payload: {
- summary: `${name} is down`,
- source: "Open Status",
- severity: "error",
- timestamp: new Date(cronTimestamp).toISOString(),
- custom_details: {
- statusCode,
- message,
- },
- },
- });
-
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}}-${incidentId}`,
+ event_action: "trigger",
+ payload: {
+ summary: `${name} is down`,
+ source: "Open Status",
+ severity: "error",
+ timestamp: new Date(cronTimestamp).toISOString(),
+ custom_details: {
+ statusCode,
+ message,
+ },
+ },
+ });
await fetch("https://events.pagerduty.com/v2/enqueue", {
method: "POST",
body: JSON.stringify(event),
@@ -73,7 +71,7 @@ export const sendDegraded = async ({
const { name } = monitor;
const event = triggerEventPayloadSchema.parse({
- rounting_key: notificationData.integration_keys[0].integration_key,
+ routing_key: notificationData.integration_keys[0].integration_key,
dedup_key: `${monitor.id}}`,
event_action: "trigger",
payload: {
@@ -125,7 +123,7 @@ export const sendRecovery = async ({
try {
for await (const integrationKey of notificationData.integration_keys) {
const event = resolveEventPayloadSchema.parse({
- rounting_key: integrationKey.integration_key,
+ routing_key: integrationKey.integration_key,
dedup_key: `${monitor.id}}-${incidentId}`,
event_action: "resolve",
});
@@ -140,4 +138,38 @@ export const sendRecovery = async ({
}
};
+export const sendTest = async ({
+ integrationKey,
+}: {
+ integrationKey: string;
+}) => {
+ console.log("Sending test alert to PagerDuty");
+ try {
+ // biome-ignore lint/correctness/noUnusedVariables: <explanation>
+ const event = triggerEventPayloadSchema.parse({
+ routing_key: integrationKey,
+ dedup_key: "openstatus-test",
+ event_action: "trigger",
+ payload: {
+ summary: "This is a test from OpenStatus",
+ source: "Open Status",
+ severity: "error",
+ timestamp: new Date().toISOString(),
+ custom_details: {
+ statusCode: 418,
+ message: 'I"m a teapot',
+ },
+ },
+ });
+
+ const res = await fetch("https://events.pagerduty.com/v2/enqueue", {
+ method: "POST",
+ body: JSON.stringify(event),
+ });
+ } catch (err) {
+ console.log(err);
+ return false;
+ }
+ return true;
+};
export { PagerDutySchema };
diff --git a/packages/notifications/pagerduty/src/schema/config.ts b/packages/notifications/pagerduty/src/schema/config.ts
index 5bd35cdae..1ea2964d8 100644
--- a/packages/notifications/pagerduty/src/schema/config.ts
+++ b/packages/notifications/pagerduty/src/schema/config.ts
@@ -37,7 +37,7 @@ export const linkSchema = z.object({
});
const baseEventPayloadSchema = z.object({
- rounting_key: z.string(),
+ routing_key: z.string(),
dedup_key: z.string(),
});