Skip to content

Open-source status page, incident response and uptime monitoring

Added Incidents (#604)

Kushagra Sharma0da38c3e312d
diff --git a/apps/docs/api-reference/incident/get-incident.mdx b/apps/docs/api-reference/incident/get-incident.mdx
new file mode 100644
index 000000000..3e943dcf9
--- /dev/null
+++ b/apps/docs/api-reference/incident/get-incident.mdx
@@ -0,0 +1,4 @@
+---
+title: Get all incidents
+openapi: get /incident/
+---
diff --git a/apps/docs/api-reference/incident/get-incident:id.mdx b/apps/docs/api-reference/incident/get-incident:id.mdx
new file mode 100644
index 000000000..aba29c3be
--- /dev/null
+++ b/apps/docs/api-reference/incident/get-incident:id.mdx
@@ -0,0 +1,4 @@
+---
+title: Get an incident
+openapi: get /incident/:id
+---
diff --git a/apps/docs/mint.json b/apps/docs/mint.json
index ac8fddc4d..d795514fd 100644
--- a/apps/docs/mint.json
+++ b/apps/docs/mint.json
@@ -74,12 +74,10 @@
},
{
"group": "Features",
-
"pages": [
"features/incident",
"features/heartbeat",
"features/monitor",
-
{
"group": "Notifications",
"pages": [
@@ -105,7 +103,6 @@
"group": "Developer Tools",
"pages": ["packages/react", "packages/status-widget"]
},
-
{
"group": "Contributor Guides",
"pages": [
@@ -120,6 +117,13 @@
"icon": "code",
"pages": [
"api-reference/auth",
+ {
+ "group": "Incident",
+ "pages": [
+ "api-reference/incident/get-incident:id",
+ "api-reference/incident/get-incident"
+ ]
+ },
{
"group": "Monitor",
"pages": [
diff --git a/apps/server/src/v1/incidents.test.ts b/apps/server/src/v1/incidents.test.ts
new file mode 100644
index 000000000..32a8ceb46
--- /dev/null
+++ b/apps/server/src/v1/incidents.test.ts
@@ -0,0 +1,17 @@
+import { expect, test } from "bun:test";
+
+import { api } from ".";
+
+test("GET one Incident", 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,
+ startedAt: expect.any(String),
+ monitorId: 1,
+ });
+});
diff --git a/apps/server/src/v1/incidents.ts b/apps/server/src/v1/incidents.ts
new file mode 100644
index 000000000..5e06bbe74
--- /dev/null
+++ b/apps/server/src/v1/incidents.ts
@@ -0,0 +1,162 @@
+import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";
+
+import { and, db, eq, isNotNull } from "@openstatus/db";
+import { incidentTable } from "@openstatus/db/src/schema/incidents";
+
+import type { Variables } from "./index";
+import { ErrorSchema } from "./shared";
+
+const incidentsApi = new OpenAPIHono<{ Variables: Variables }>();
+
+const ParamsSchema = z.object({
+ id: z
+ .string()
+ .min(1)
+ .openapi({
+ param: {
+ name: "id",
+ in: "path",
+ },
+ description: "The id of the Incident",
+ example: "1",
+ }),
+});
+
+const IncidentSchema = z.object({
+ id: z.number().openapi({
+ description: "The id of the incident",
+ example: 1,
+ }),
+ startedAt: z
+ .preprocess((val) => String(val), z.string())
+ .openapi({
+ description: "The date the incident started",
+ }),
+
+ monitorId: z
+ .number()
+ .openapi({
+ description: "The id of the monitor associated with the incident",
+ example: 1,
+ })
+ .nullable(),
+
+ acknowledgedAt: z
+ .preprocess((val) => String(val), z.string())
+ .openapi({
+ description: "The date the incident was acknowledged",
+ })
+ .optional(),
+
+ acknowledgedBy: z
+ .number()
+ .openapi({
+ description: "The user who acknowledged the incident",
+ })
+ .nullable(),
+
+ resolvedAt: z
+ .preprocess((val) => String(val), z.string())
+ .openapi({
+ description: "The date the incident was resolved",
+ })
+ .optional(),
+ resolvedBy: z
+ .number()
+ .openapi({
+ description: "The user who resolved the incident",
+ })
+ .nullable(),
+});
+
+const getAllRoute = createRoute({
+ method: "get",
+ tags: ["incident"],
+ description: "Get all incidents",
+ path: "/",
+ request: {},
+ responses: {
+ 200: {
+ content: {
+ "application/json": {
+ schema: z.array(IncidentSchema),
+ },
+ },
+ description: "Get all incidents",
+ },
+ 400: {
+ content: {
+ "application/json": {
+ schema: ErrorSchema,
+ },
+ },
+ description: "Returns an error",
+ },
+ },
+});
+
+incidentsApi.openapi(getAllRoute, async (c) => {
+ const workspaceId = Number(c.get("workspaceId"));
+ const result = await db
+ .select()
+ .from(incidentTable)
+ .where(eq(incidentTable.workspaceId, workspaceId))
+ .all();
+
+ if (!result) return c.jsonT({ code: 404, message: "Not Found" });
+
+ const data = z.array(IncidentSchema).parse(result);
+ return c.jsonT(data);
+});
+
+const getRoute = createRoute({
+ method: "get",
+ tags: ["incident"],
+ description: "Get an incident",
+ path: "/:id",
+ request: {
+ params: ParamsSchema,
+ },
+ responses: {
+ 200: {
+ content: {
+ "application/json": {
+ schema: IncidentSchema,
+ },
+ },
+ description: "Get an incident",
+ },
+ 400: {
+ content: {
+ "application/json": {
+ schema: ErrorSchema,
+ },
+ },
+ description: "Returns an error",
+ },
+ },
+});
+
+incidentsApi.openapi(getRoute, async (c) => {
+ const workspaceId = Number(c.get("workspaceId"));
+ const { id } = c.req.valid("param");
+
+ const incidentId = Number(id);
+ const result = await db
+ .select()
+ .from(incidentTable)
+ .where(
+ and(
+ eq(incidentTable.workspaceId, workspaceId),
+ eq(incidentTable.id, incidentId),
+ ),
+ )
+ .get();
+
+ if (!result) return c.jsonT({ code: 404, message: "Not Found" });
+ const data = IncidentSchema.parse(result);
+
+ return c.jsonT(data);
+});
+
+export { incidentsApi };
diff --git a/apps/server/src/v1/index.ts b/apps/server/src/v1/index.ts
index d0a833e1c..c4b3471b1 100644
--- a/apps/server/src/v1/index.ts
+++ b/apps/server/src/v1/index.ts
@@ -3,6 +3,7 @@ import { logger } from "hono/logger";
import type { Limits } from "@openstatus/plans/src/types";
+import { incidentsApi } from "./incidents";
import { middleware } from "./middleware";
import { monitorApi } from "./monitor";
import { statusReportApi } from "./statusReport";
@@ -35,5 +36,5 @@ api.use("/*", middleware);
api.use("/*", logger());
api.route("/monitor", monitorApi);
api.route("/status_report_update", statusReportUpdateApi);
-
+api.route("/incident", incidentsApi);
api.route("/status_report", statusReportApi);
diff --git a/packages/db/src/seed.mts b/packages/db/src/seed.mts
index 9739b93b5..d5027f908 100644
--- a/packages/db/src/seed.mts
+++ b/packages/db/src/seed.mts
@@ -11,6 +11,7 @@ import {
notificationsToMonitors,
page,
statusReport,
+ incidentTable,
statusReportUpdate,
user,
usersToWorkspaces,
@@ -132,6 +133,15 @@ async function main() {
})
.run();
+ await db
+ .insert(incidentTable)
+ .values({
+ id: 1,
+ workspaceId: 1,
+ monitorId: 1,
+ })
+ .run();
+
await db
.insert(statusReportUpdate)
.values({