Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions packages/api/src/routers/api/__tests__/sloAlerts.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ReturnType<typeof getLoggedInAgent>>['agent'];
let team: Awaited<ReturnType<typeof getLoggedInAgent>>['team'];
let user: IUser;
let sourceId: string;
let webhookId: string;

Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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();
});
});
33 changes: 31 additions & 2 deletions packages/api/src/routers/external-api/v2/alerts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
deleteAlert,
getAlertsWithDisplayRefs,
getAlertWithDisplayRefs,
isSloAlert,
populateAlertDisplayRefs,
silenceAlert,
unsilenceAlert,
Expand Down Expand Up @@ -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' });
}

Expand Down Expand Up @@ -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 } },

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 P2 — The list endpoint still accepts source=slo though SLO alerts are now always excluded

alertsFilterShape.source is z.nativeEnum(AlertSource) (controllers/alertsPage.ts:36-43), so GET /api/v2/alerts?source=slo validates and quietly returns an empty list. The documented AlertSource enum is [saved_search, tile, inline], so this should be a 400. Narrow the external list schema's source to the non-SLO values.

Advisory. Fix if it is a small defect in code this PR changes; otherwise reply in the thread. Do not widen the PR or touch files it did not already change. How to respond

],
};
const [alerts, total] = await Promise.all([
getAlertsWithDisplayRefs(teamId, { limit, offset }, filter),
countAlerts(teamId, filter),
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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' });
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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' });
Expand Down
Loading