From ad3ce64ab0891593c365b8f611aac29d4ab7206f Mon Sep 17 00:00:00 2001 From: Alex Fedotyev <61838744+alex-fedotyev@users.noreply.github.com> Date: Tue, 24 Nov 2026 03:56:28 +0000 Subject: [PATCH] feat(api): keep SLO alerts out of the external alerts API SLO alerts are changed through their SLO, so the external alerts API leaves them out: the list skips them, and getting, updating, deleting, silencing or unsilencing one answers 404, as for an alert that does not exist. Co-Authored-By: Claude Opus 5.5 --- .../api/__tests__/sloAlerts.int.test.ts | 34 +++++++++++++++++++ .../api/src/routers/external-api/v2/alerts.ts | 33 ++++++++++++++++-- 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/packages/api/src/routers/api/__tests__/sloAlerts.int.test.ts b/packages/api/src/routers/api/__tests__/sloAlerts.int.test.ts index eabb024df..ffb263ff5 100644 --- a/packages/api/src/routers/api/__tests__/sloAlerts.int.test.ts +++ b/packages/api/src/routers/api/__tests__/sloAlerts.int.test.ts @@ -17,12 +17,14 @@ import Alert, { AlertSource, AlertThresholdType } from '@/models/alert'; import Connection from '@/models/connection'; import Slo from '@/models/slo'; import { Source } from '@/models/source'; +import type { IUser } from '@/models/user'; import Webhook from '@/models/webhook'; describe('SLO alerts', () => { const server = getServer(); let agent: Awaited>['agent']; let team: Awaited>['team']; + let user: IUser; let sourceId: string; let webhookId: string; @@ -52,6 +54,7 @@ describe('SLO alerts', () => { const result = await getLoggedInAgent(server); agent = result.agent; team = result.team; + user = result.user; const connection = await Connection.create({ team: team._id, name: 'Default', @@ -224,4 +227,35 @@ describe('SLO alerts', () => { await deleteAlert(alertId, team._id); expect(await Alert.exists({ _id: alertId })).toBeNull(); }); + + it('leaves SLO alerts out of the external API', async () => { + const created = await agent + .post('/slos') + .send(definition(fastOnly())) + .expect(200); + const alertId = created.body.alertStates.fastBurn.id; + const auth = `Bearer ${user.accessKey}`; + + await agent + .get(`/api/v2/alerts/${alertId}`) + .set('Authorization', auth) + .expect(404); + await agent + .delete(`/api/v2/alerts/${alertId}`) + .set('Authorization', auth) + .expect(404); + await agent + .post(`/api/v2/alerts/${alertId}/silenced`) + .set('Authorization', auth) + .send({ mutedUntil: new Date(Date.now() + 3_600_000).toISOString() }) + .expect(404); + const list = await agent + .get('/api/v2/alerts') + .set('Authorization', auth) + .expect(200); + expect(list.body.data.map((a: { id: string }) => a.id)).not.toContain( + alertId, + ); + expect(await Alert.exists({ _id: alertId })).toBeTruthy(); + }); }); diff --git a/packages/api/src/routers/external-api/v2/alerts.ts b/packages/api/src/routers/external-api/v2/alerts.ts index f4abc4c2c..a43a9fc1a 100644 --- a/packages/api/src/routers/external-api/v2/alerts.ts +++ b/packages/api/src/routers/external-api/v2/alerts.ts @@ -8,6 +8,7 @@ import { deleteAlert, getAlertsWithDisplayRefs, getAlertWithDisplayRefs, + isSloAlert, populateAlertDisplayRefs, silenceAlert, unsilenceAlert, @@ -528,7 +529,8 @@ router.get( const alert = await getAlertWithDisplayRefs(req.params.id, teamId); - if (alert == null) { + // SLO alerts are managed through their SLO and are not part of this API. + if (alert == null || alert.source === AlertSource.SLO) { return res.status(404).json({ message: 'Alert not found' }); } @@ -685,7 +687,13 @@ router.get( const { limit, offset, ...filterParams } = listAlertsQuerySchema.parse( req.query ?? {}, ); - const filter = buildAlertsFilter(teamId, filterParams); + // SLO alerts are managed through their SLO and are not part of this API. + const filter = { + $and: [ + buildAlertsFilter(teamId, filterParams), + { source: { $ne: AlertSource.SLO } }, + ], + }; const [alerts, total] = await Promise.all([ getAlertsWithDisplayRefs(teamId, { limit, offset }, filter), countAlerts(teamId, filter), @@ -908,6 +916,12 @@ router.put( if (teamId == null) { return res.status(403).json({ message: 'Forbidden' }); } + + // SLO alerts are managed through their SLO and are not part of this API. + if (await isSloAlert(req.params.id, teamId)) { + return res.status(404).json({ message: 'Alert not found' }); + } + const { id } = req.params; const alertInput = toInternalAlertInput(req.body); @@ -992,6 +1006,11 @@ router.delete( return res.status(403).json({ message: 'Forbidden' }); } + // SLO alerts are managed through their SLO and are not part of this API. + if (await isSloAlert(req.params.id, teamId)) { + return res.status(404).json({ message: 'Alert not found' }); + } + const { deletedCount } = await deleteAlert(alertId, teamId); if (deletedCount === 0) { return res.status(404).json({ message: 'Alert not found' }); @@ -1083,6 +1102,11 @@ router.post( return res.status(403).json({ message: 'Forbidden' }); } + // SLO alerts are managed through their SLO and are not part of this API. + if (await isSloAlert(req.params.id, teamId)) { + return res.status(404).json({ message: 'Alert not found' }); + } + const updatedAlert = await silenceAlert( req.params.id, teamId, @@ -1165,6 +1189,11 @@ router.delete( return res.status(403).json({ message: 'Forbidden' }); } + // SLO alerts are managed through their SLO and are not part of this API. + if (await isSloAlert(req.params.id, teamId)) { + return res.status(404).json({ message: 'Alert not found' }); + } + const updatedAlert = await unsilenceAlert(req.params.id, teamId); if (!updatedAlert) { return res.status(404).json({ message: 'Alert not found' });