Open-source status page, incident response and uptime monitoring
TypeScript alertingincident-managementmcpmcp-servermonitoringmonitoring-as-codeobservabilityon-callopen-sourcesrestatus-pagestatuspagesynthetic-monitoringterraformtinybirdtursouptimeuptime-checkeruptime-monitor
Add incident update api (#456)
Thibault Le Ouayfd36f519f1e9
diff --git a/apps/server/src/v1/incident.test.ts b/apps/server/src/v1/incident.test.tsnew file mode 100644index 000000000..336cc9ab3--- /dev/null+++ b/apps/server/src/v1/incident.test.ts@@ -0,0 +1,39 @@+import { expect, test } from "bun:test";++import { api } from ".";++test("GET one incident update ", async () => {+ const res = await api.request("/incident/1", {+ headers: {+ "x-openstatus-key": "1",+ },+ });+ expect(res.status).toBe(200);+ expect(await res.json()).toMatchObject({+ id: 1,+ status: "investigating",+ title: "Test Incident",+ incident_updates: expect.any(Array),+ });+});++test("create one incident ", async () => {+ const res = await api.request("/incident", {+ method: "POST",+ headers: {+ "x-openstatus-key": "1",+ "content-type": "application/json",+ },+ body: JSON.stringify({+ status: "investigating",+ date: "2023-11-08T21:03:13.000Z",+ title: "Test Incident",+ }),+ });+ expect(res.status).toBe(200);+ expect(await res.json()).toMatchObject({+ id: expect.any(Number),+ status: "investigating",+ title: "Test Incident",+ });+});diff --git a/apps/server/src/v1/incident.ts b/apps/server/src/v1/incident.tsindex 1f7facdee..a78cc0330 100644--- a/apps/server/src/v1/incident.ts+++ b/apps/server/src/v1/incident.ts@@ -48,6 +48,10 @@ const incidentSchema = z.object({}),});+const incidentResultSchema = incidentSchema.extend({+ id: z.number().openapi({ description: "The id of the incident" }),+});+const incidentExtendedSchema = incidentSchema.extend({id: z.number().openapi({ description: "The id of the incident" }),incident_updates: z@@ -150,7 +154,6 @@ incidentApi.openapi(getRoute, async (c) => {});if (!_incident) return c.jsonT({ code: 404, message: "Not Found" });- console.log(_incident);const data = incidentExtendedSchema.parse({..._incident,incident_updates: _incident.incidentUpdates.map(@@ -180,7 +183,7 @@ const postRoute = createRoute({200: {content: {"application/json": {- schema: incidentSchema,+ schema: incidentExtendedSchema,},},description: "Incident created",@@ -209,7 +212,7 @@ incidentApi.openapi(postRoute, async (c) => {.returning().get();- const data = incidentSchema.parse(_newIncident);+ const data = incidentExtendedSchema.parse(_newIncident);return c.jsonT(data);});diff --git a/apps/server/src/v1/incidentUpdate.test.ts b/apps/server/src/v1/incidentUpdate.test.tsnew file mode 100644index 000000000..74ff1eafe--- /dev/null+++ b/apps/server/src/v1/incidentUpdate.test.ts@@ -0,0 +1,37 @@+import { expect, test } from "bun:test";++import { api } from ".";++test("GET one incident update ", async () => {+ const res = await api.request("/incident_update/1", {+ headers: {+ "x-openstatus-key": "1",+ },+ });+ expect(res.status).toBe(200);+ expect(await res.json()).toMatchObject({+ status: "investigating",+ message: "test",+ });+});++test("create one incident update ", async () => {+ const res = await api.request("/incident_update", {+ method: "POST",+ headers: {+ "x-openstatus-key": "1",+ "content-type": "application/json",+ },+ body: JSON.stringify({+ status: "investigating",+ date: "2023-11-08T21:03:13.000Z",+ message: "test",+ incident_id: 1,+ }),+ });+ expect(res.status).toBe(200);+ expect(await res.json()).toMatchObject({+ status: "investigating",+ message: "test",+ });+});diff --git a/apps/server/src/v1/incidentUpdate.ts b/apps/server/src/v1/incidentUpdate.tsindex 5b5b3f972..1fc2f61bb 100644--- a/apps/server/src/v1/incidentUpdate.ts+++ b/apps/server/src/v1/incidentUpdate.ts@@ -32,7 +32,7 @@ const incidentUpdateSchema = z.object({}),id: z.coerce.string().openapi({ description: "The id of the update" }),date: z- .preprocess((val) => String(val), z.string())+ .preprocess((val) => new Date(String(val)).toISOString(), z.string()).openapi({description: "The date of the update in ISO 8601 format",}),@@ -41,8 +41,20 @@ const incidentUpdateSchema = z.object({}),});-// const incidentUpdateApi = new OpenAPIHono<{ Variables: Variables }>();-+const createIncidentUpdateSchema = z.object({+ incident_id: z.number().openapi({+ description: "The id of the incident",+ }),+ status: z.enum(incidentStatus).openapi({+ description: "The status of the update",+ }),+ date: z.string().datetime().openapi({+ description: "The date of the update in ISO 8601 format",+ }),+ message: z.string().openapi({+ description: "The message of the update",+ }),+});const getUpdateRoute = createRoute({method: "get",tags: ["incident_update"],@@ -96,7 +108,73 @@ incidenUpdateApi.openapi(getUpdateRoute, async (c) => {return c.jsonT({ code: 401, message: "Not Authorized" });const data = incidentUpdateSchema.parse(update);++ return c.jsonT(data);+});++const createIncidentUpdate = createRoute({+ method: "post",+ tags: ["incident_update"],+ path: "/",+ request: {+ body: {+ description: "the incident update",+ content: {+ "application/json": {+ schema: createIncidentUpdateSchema,+ },+ },+ },+ },+ responses: {+ 200: {+ content: {+ "application/json": {+ schema: incidentUpdateSchema,+ },+ },+ description: "Get all incidents",+ },+ 400: {+ content: {+ "application/json": {+ schema: ErrorSchema,+ },+ },+ description: "Returns an error",+ },+ },+});++incidenUpdateApi.openapi(createIncidentUpdate, async (c) => {+ const workspaceId = Number(c.get("workspaceId"));+ const input = c.req.valid("json");++ const currentIncident = await db+ .select()+ .from(incident)+ .where(+ and(+ eq(incident.id, input.incident_id),+ eq(incident.workspaceId, workspaceId),+ ),+ )+ .get();+ if (!currentIncident)+ return c.jsonT({ code: 401, message: "Not Authorized" });++ const res = await db+ .insert(incidentUpdate)+ .values({+ ...input,+ date: new Date(input.date),+ incidentId: input.incident_id,+ })+ .returning()+ .get();++ const data = incidentUpdateSchema.parse(res);return c.jsonT(data);});-export { incidenUpdateApi as incidenUpdatetApi };+export { incidenUpdateApi };diff --git a/apps/server/src/v1/index.ts b/apps/server/src/v1/index.tsindex c2aede596..10cb90d7e 100644--- a/apps/server/src/v1/index.ts+++ b/apps/server/src/v1/index.ts@@ -4,7 +4,7 @@ import { logger } from "hono/logger";import type { Plan } from "@openstatus/plans";import { incidentApi } from "./incident";-import { incidenUpdatetApi } from "./incidentUpdate";+import { incidenUpdateApi } from "./incidentUpdate";import { middleware } from "./middleware";import { monitorApi } from "./monitor";@@ -29,6 +29,6 @@ api.doc("/openapi", {api.use("/*", middleware);api.use("/*", logger());api.route("/monitor", monitorApi);-api.route("/incident_update", incidenUpdatetApi);+api.route("/incident_update", incidenUpdateApi);api.route("/incident", incidentApi);diff --git a/apps/web/next.config.js b/apps/web/next.config.jsindex 7e080a81d..3c52645df 100644--- a/apps/web/next.config.js+++ b/apps/web/next.config.js@@ -47,10 +47,7 @@ module.exports = withSentryConfig(widenClientFileUpload: true,// Transpiles SDK to be compatible with IE11 (increases bundle size)- transpileClientSDK: true,-- // Routes browser requests to Sentry through a Next.js rewrite to circumvent ad-blockers (increases server load)- tunnelRoute: "/monitoring",+ transpileClientSDK: false,// Hides source maps from generated client bundleshideSourceMaps: true,diff --git a/apps/web/sentry.client.config.ts b/apps/web/sentry.client.config.tsindex 45bbf1bda..c71fa2945 100644--- a/apps/web/sentry.client.config.ts+++ b/apps/web/sentry.client.config.ts@@ -11,7 +11,7 @@ Sentry.init({dsn: env.NEXT_PUBLIC_SENTRY_DSN,// Adjust this value in production, or use tracesSampler for greater control- tracesSampleRate: 0.2,+ tracesSampleRate: 0.5,// Setting this option to true will print useful information to the console while you're setting up Sentry.debug: false,diff --git a/packages/db/src/seed.mts b/packages/db/src/seed.mtsindex b2c861a75..3c5c26b36 100644--- a/packages/db/src/seed.mts+++ b/packages/db/src/seed.mts@@ -5,6 +5,8 @@ import { drizzle } from "drizzle-orm/libsql";import { env } from "../env.mjs";import {+ incident,+ incidentUpdate,monitor,monitorsToPages,notification,@@ -118,6 +120,28 @@ async function main() {.insert(notificationsToMonitors).values({ monitorId: 1, notificationId: 1 }).run();++ await db+ .insert(incident)+ .values({+ id: 1,+ workspaceId: 1,+ title: "Test Incident",+ status: "investigating",+ updatedAt: new Date(),+ })+ .run();++ await db+ .insert(incidentUpdate)+ .values({+ id: 1,+ incidentId: 1,+ status: "investigating",+ message: "test",+ date: new Date(),+ })+ .run();process.exit(0);}