Merge pull request #3831 from mdiniz97/feat/rocket-chat-rich-notifications
feat: enrich Rocket.Chat notifications
This commit is contained in:
2 files changed
+270
-73
No files matched your search
@@ -1,9 +1,20 @@
|
||||
const SERVICE_NAME = "RocketChatProvider";
|
||||
import { supportsUptimeDetails, type MonitorType } from "@/domain/monitors/monitor.type.js";
|
||||
import type { Notification, NotificationMessage } from "@/domain/notifications/notification.type.js";
|
||||
import { NotificationProvider } from "@/domain/notifications/providers/INotificationProvider.js";
|
||||
import { getTestMessage } from "@/domain/notifications/providers/utils.js";
|
||||
import got from "got";
|
||||
|
||||
type RocketChatField = { title: string; value: string; short: boolean };
|
||||
type RocketChatAttachment = {
|
||||
color: string;
|
||||
title: string;
|
||||
title_link?: string;
|
||||
fields?: RocketChatField[];
|
||||
ts?: string;
|
||||
};
|
||||
type RocketChatPayload = { text: string; attachments: [RocketChatAttachment] };
|
||||
|
||||
export class RocketChatProvider extends NotificationProvider {
|
||||
async sendTestAlert(notification: Partial<Notification>): Promise<boolean> {
|
||||
if (!notification.address) {
|
||||
@@ -12,7 +23,7 @@ export class RocketChatProvider extends NotificationProvider {
|
||||
|
||||
try {
|
||||
await got.post(notification.address, {
|
||||
json: { text: getTestMessage() },
|
||||
json: this.buildTestPayload(),
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
@@ -31,6 +42,22 @@ export class RocketChatProvider extends NotificationProvider {
|
||||
}
|
||||
}
|
||||
|
||||
private buildTestPayload(): RocketChatPayload {
|
||||
return {
|
||||
text: getTestMessage(),
|
||||
attachments: [
|
||||
{
|
||||
color: "#0000FF",
|
||||
title: "Checkmate test notification",
|
||||
fields: [
|
||||
{ title: "Channel", value: "Rocket.Chat", short: true },
|
||||
{ title: "Status", value: "Test notification", short: true },
|
||||
],
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
async sendMessage(notification: Notification, message: NotificationMessage): Promise<boolean> {
|
||||
if (!notification.address) {
|
||||
return false;
|
||||
@@ -38,7 +65,7 @@ export class RocketChatProvider extends NotificationProvider {
|
||||
|
||||
try {
|
||||
await got.post(notification.address, {
|
||||
json: { text: this.buildText(message) },
|
||||
json: this.buildPayload(message),
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
@@ -57,34 +84,88 @@ export class RocketChatProvider extends NotificationProvider {
|
||||
}
|
||||
}
|
||||
|
||||
private buildText(message: NotificationMessage): string {
|
||||
const lines = [
|
||||
message.content.title,
|
||||
message.content.summary,
|
||||
"",
|
||||
`Monitor: ${message.monitor.name}`,
|
||||
`Type: ${message.monitor.type}`,
|
||||
`Status: ${message.monitor.status}`,
|
||||
`URL: ${message.monitor.url}`,
|
||||
private buildPayload(message: NotificationMessage): RocketChatPayload {
|
||||
const monitorUrl = this.monitorUrl(message);
|
||||
const fields: RocketChatField[] = [
|
||||
{ title: "Monitor", value: message.monitor.name, short: true },
|
||||
{ title: "Type", value: message.monitor.type, short: true },
|
||||
{ title: "Status", value: message.monitor.status, short: true },
|
||||
{ title: "URL", value: message.monitor.url, short: false },
|
||||
];
|
||||
|
||||
if (message.content.details?.length) {
|
||||
lines.push("", "Details:", ...message.content.details.map((detail) => `- ${detail}`));
|
||||
}
|
||||
if (message.content.thresholds?.length) {
|
||||
lines.push(
|
||||
"",
|
||||
"Thresholds:",
|
||||
...message.content.thresholds.map(
|
||||
(threshold) => `- ${threshold.metric.toUpperCase()}: ${threshold.formattedValue} (threshold: ${threshold.threshold}${threshold.unit})`
|
||||
)
|
||||
fields.push(
|
||||
...message.content.thresholds.map((breach) => ({
|
||||
title: breach.metric.toUpperCase(),
|
||||
value: `${breach.formattedValue} (threshold: ${breach.threshold}${breach.unit})`,
|
||||
short: true,
|
||||
}))
|
||||
);
|
||||
}
|
||||
|
||||
if (message.content.incident) {
|
||||
lines.push("", `Incident: ${message.clientHost}/infrastructure/${message.monitor.id}`);
|
||||
if (message.content.details?.length) {
|
||||
fields.push({
|
||||
title: "Details",
|
||||
value: message.content.details.map((detail) => `- ${detail}`).join("\n"),
|
||||
short: false,
|
||||
});
|
||||
}
|
||||
|
||||
return lines.join("\n");
|
||||
if (message.content.incident?.url.trim()) {
|
||||
fields.push({
|
||||
title: "Incident",
|
||||
value: message.content.incident.url,
|
||||
short: false,
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
text: message.content.summary,
|
||||
attachments: [
|
||||
{
|
||||
color: this.severityColor(message.severity),
|
||||
title: message.content.title,
|
||||
...(monitorUrl ? { title_link: monitorUrl } : {}),
|
||||
fields,
|
||||
ts: message.content.timestamp.toISOString(),
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
private monitorUrl(message: NotificationMessage): string | undefined {
|
||||
const clientHost = message.clientHost?.trim();
|
||||
if (!clientHost || clientHost === "Host not defined") {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (message.monitor.type === "hardware") {
|
||||
return `${clientHost}/infrastructure/${message.monitor.id}`;
|
||||
}
|
||||
|
||||
if (message.monitor.type === "pagespeed") {
|
||||
return `${clientHost}/pagespeed/${message.monitor.id}`;
|
||||
}
|
||||
|
||||
if (supportsUptimeDetails(message.monitor.type as MonitorType)) {
|
||||
return `${clientHost}/uptime/${message.monitor.id}`;
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
private severityColor(severity: NotificationMessage["severity"]): string {
|
||||
switch (severity) {
|
||||
case "critical":
|
||||
return "#FF0000";
|
||||
case "warning":
|
||||
return "#FFA500";
|
||||
case "success":
|
||||
return "#00FF00";
|
||||
case "info":
|
||||
return "#0000FF";
|
||||
default:
|
||||
return "#808080";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
import { describe, expect, it, jest } from "@jest/globals";
|
||||
import type { MonitorType } from "../../../../src/domain/monitors/monitor.type.ts";
|
||||
import type { NotificationSeverity } from "../../../../src/domain/notifications/notification.type.ts";
|
||||
import { createMockLogger } from "../../../helpers/createMockLogger.ts";
|
||||
import { makeMessage, makeMessageWithIncident, makeMessageWithThresholds, makeNotification } from "../../../helpers/notificationMessage.ts";
|
||||
import { testNotificationProviderContract } from "../../../helpers/notificationProviderContract.ts";
|
||||
@@ -25,7 +27,7 @@ testNotificationProviderContract("RocketChatProvider", {
|
||||
describe("RocketChatProvider", () => {
|
||||
beforeEach(() => mockGotPost.mockReset().mockResolvedValue({}));
|
||||
|
||||
it("sends a text-only test alert", async () => {
|
||||
it("sends a rich test alert without webhook overrides", async () => {
|
||||
const { provider } = createProvider();
|
||||
|
||||
const result = await provider.sendTestAlert(makeNotification({ type: "rocket_chat" }));
|
||||
@@ -34,11 +36,29 @@ describe("RocketChatProvider", () => {
|
||||
expect(mockGotPost).toHaveBeenCalledWith(
|
||||
"https://hooks.example.com/webhook",
|
||||
expect.objectContaining({
|
||||
json: { text: "This is a test notification from Checkmate" },
|
||||
json: {
|
||||
text: "This is a test notification from Checkmate",
|
||||
attachments: [
|
||||
{
|
||||
color: "#0000FF",
|
||||
title: "Checkmate test notification",
|
||||
fields: [
|
||||
{ title: "Channel", value: "Rocket.Chat", short: true },
|
||||
{ title: "Status", value: "Test notification", short: true },
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
timeout: { request: 30000 },
|
||||
retry: { limit: 0 },
|
||||
})
|
||||
);
|
||||
|
||||
const payload = mockGotPost.mock.calls[0][1].json;
|
||||
expect(payload).not.toHaveProperty("alias");
|
||||
expect(payload).not.toHaveProperty("avatar");
|
||||
expect(payload).not.toHaveProperty("emoji");
|
||||
expect(payload).not.toHaveProperty("channel");
|
||||
});
|
||||
|
||||
it("returns false when the test webhook URL is missing", async () => {
|
||||
@@ -66,7 +86,7 @@ describe("RocketChatProvider", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it("sends monitor details as a text-only message", async () => {
|
||||
it("sends a rich monitor attachment without webhook overrides", async () => {
|
||||
const { provider } = createProvider();
|
||||
|
||||
const result = await provider.sendMessage(makeNotification({ type: "rocket_chat" }), makeMessage());
|
||||
@@ -78,22 +98,152 @@ describe("RocketChatProvider", () => {
|
||||
timeout: { request: 30000 },
|
||||
retry: { limit: 0 },
|
||||
json: {
|
||||
text: [
|
||||
"Monitor Down: Test Monitor",
|
||||
'Monitor "Test Monitor" is currently down.',
|
||||
"",
|
||||
"Monitor: Test Monitor",
|
||||
"Type: http",
|
||||
"Status: down",
|
||||
"URL: https://example.com",
|
||||
"",
|
||||
"Details:",
|
||||
"- URL: https://example.com",
|
||||
"- Status: Down",
|
||||
].join("\n"),
|
||||
text: 'Monitor "Test Monitor" is currently down.',
|
||||
attachments: [
|
||||
{
|
||||
color: "#FF0000",
|
||||
title: "Monitor Down: Test Monitor",
|
||||
title_link: "https://app.example.com/uptime/mon-1",
|
||||
fields: [
|
||||
{ title: "Monitor", value: "Test Monitor", short: true },
|
||||
{ title: "Type", value: "http", short: true },
|
||||
{ title: "Status", value: "down", short: true },
|
||||
{ title: "URL", value: "https://example.com", short: false },
|
||||
{
|
||||
title: "Details",
|
||||
value: "- URL: https://example.com\n- Status: Down",
|
||||
short: false,
|
||||
},
|
||||
],
|
||||
ts: "2025-01-01T00:00:00.000Z",
|
||||
},
|
||||
],
|
||||
},
|
||||
})
|
||||
);
|
||||
|
||||
const payload = mockGotPost.mock.calls[0][1].json;
|
||||
expect(payload).not.toHaveProperty("alias");
|
||||
expect(payload).not.toHaveProperty("avatar");
|
||||
expect(payload).not.toHaveProperty("emoji");
|
||||
expect(payload).not.toHaveProperty("channel");
|
||||
});
|
||||
|
||||
it.each([
|
||||
["http", "https://app.example.com/uptime/mon-1"],
|
||||
["ping", "https://app.example.com/uptime/mon-1"],
|
||||
["pagespeed", "https://app.example.com/pagespeed/mon-1"],
|
||||
["hardware", "https://app.example.com/infrastructure/mon-1"],
|
||||
])("links %s monitors to %s", async (type, expectedLink) => {
|
||||
const { provider } = createProvider();
|
||||
|
||||
await provider.sendMessage(
|
||||
makeNotification({ type: "rocket_chat" }),
|
||||
makeMessage({ monitor: { id: "mon-1", name: "Test Monitor", url: "https://example.com", type: type as MonitorType, status: "down" } })
|
||||
);
|
||||
|
||||
const payload = mockGotPost.mock.calls[0][1].json;
|
||||
expect(payload.attachments[0].title_link).toBe(expectedLink);
|
||||
});
|
||||
|
||||
it.each([["Host not defined"], [""], [" "]])("omits title_link when clientHost is %j", async (clientHost) => {
|
||||
const { provider } = createProvider();
|
||||
|
||||
await provider.sendMessage(makeNotification({ type: "rocket_chat" }), makeMessage({ clientHost }));
|
||||
|
||||
const payload = mockGotPost.mock.calls[0][1].json;
|
||||
expect(payload.attachments[0]).not.toHaveProperty("title_link");
|
||||
});
|
||||
|
||||
it.each([
|
||||
["critical", "#FF0000"],
|
||||
["warning", "#FFA500"],
|
||||
["success", "#00FF00"],
|
||||
["info", "#0000FF"],
|
||||
["unknown", "#808080"],
|
||||
])("maps %s severity to %s", async (severity, color) => {
|
||||
const { provider } = createProvider();
|
||||
|
||||
await provider.sendMessage(makeNotification({ type: "rocket_chat" }), makeMessage({ severity: severity as NotificationSeverity }));
|
||||
|
||||
const payload = mockGotPost.mock.calls[0][1].json;
|
||||
expect(payload.attachments[0].color).toBe(color);
|
||||
});
|
||||
|
||||
it("appends threshold fields before details", async () => {
|
||||
const { provider } = createProvider();
|
||||
|
||||
await provider.sendMessage(makeNotification({ type: "rocket_chat" }), makeMessageWithThresholds());
|
||||
|
||||
const fields = mockGotPost.mock.calls[0][1].json.attachments[0].fields;
|
||||
expect(fields).toEqual([
|
||||
{ title: "Monitor", value: "Infra Server", short: true },
|
||||
{ title: "Type", value: "hardware", short: true },
|
||||
{ title: "Status", value: "up", short: true },
|
||||
{ title: "URL", value: "https://infra.example.com", short: false },
|
||||
{ title: "CPU", value: "90.0% (threshold: 80%)", short: true },
|
||||
{ title: "MEMORY", value: "85.0% (threshold: 70%)", short: true },
|
||||
{ title: "Details", value: "- URL: https://infra.example.com", short: false },
|
||||
]);
|
||||
});
|
||||
|
||||
it("appends the incident URL after details", async () => {
|
||||
const { provider } = createProvider();
|
||||
|
||||
await provider.sendMessage(makeNotification({ type: "rocket_chat" }), makeMessageWithIncident());
|
||||
|
||||
const fields = mockGotPost.mock.calls[0][1].json.attachments[0].fields;
|
||||
expect(fields).toEqual([
|
||||
{ title: "Monitor", value: "Test Monitor", short: true },
|
||||
{ title: "Type", value: "http", short: true },
|
||||
{ title: "Status", value: "down", short: true },
|
||||
{ title: "URL", value: "https://example.com", short: false },
|
||||
{ title: "Details", value: "- URL: https://example.com\n- Status: Down", short: false },
|
||||
{ title: "Incident", value: "https://app.example.com/incidents/inc-1", short: false },
|
||||
]);
|
||||
});
|
||||
|
||||
it("omits empty optional fields", async () => {
|
||||
const { provider } = createProvider();
|
||||
const message = makeMessage();
|
||||
|
||||
await provider.sendMessage(
|
||||
makeNotification({ type: "rocket_chat" }),
|
||||
makeMessage({
|
||||
content: {
|
||||
...message.content,
|
||||
details: [],
|
||||
thresholds: [],
|
||||
incident: undefined,
|
||||
},
|
||||
})
|
||||
);
|
||||
|
||||
const fields = mockGotPost.mock.calls[0][1].json.attachments[0].fields;
|
||||
expect(fields).toEqual([
|
||||
{ title: "Monitor", value: "Test Monitor", short: true },
|
||||
{ title: "Type", value: "http", short: true },
|
||||
{ title: "Status", value: "down", short: true },
|
||||
{ title: "URL", value: "https://example.com", short: false },
|
||||
]);
|
||||
});
|
||||
|
||||
it("omits the incident field when its URL is empty", async () => {
|
||||
const { provider } = createProvider();
|
||||
const message = makeMessageWithIncident();
|
||||
|
||||
await provider.sendMessage(
|
||||
makeNotification({ type: "rocket_chat" }),
|
||||
makeMessage({
|
||||
content: {
|
||||
...message.content,
|
||||
incident: { ...message.content.incident!, url: "" },
|
||||
},
|
||||
})
|
||||
);
|
||||
|
||||
const fields = mockGotPost.mock.calls[0][1].json.attachments[0].fields;
|
||||
expect(fields).not.toContainEqual(expect.objectContaining({ title: "Incident" }));
|
||||
});
|
||||
|
||||
it("returns false when the message webhook URL is missing", async () => {
|
||||
@@ -105,40 +255,6 @@ describe("RocketChatProvider", () => {
|
||||
expect(mockGotPost).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("includes threshold breaches in the text", async () => {
|
||||
const { provider } = createProvider();
|
||||
|
||||
await provider.sendMessage(makeNotification({ type: "rocket_chat" }), makeMessageWithThresholds());
|
||||
|
||||
const payload = mockGotPost.mock.calls[0][1].json;
|
||||
expect(payload.text).toContain("Thresholds:\n- CPU: 90.0% (threshold: 80%)\n- MEMORY: 85.0% (threshold: 70%)");
|
||||
});
|
||||
|
||||
it("includes a monitor-scoped link when incident data is present", async () => {
|
||||
const { provider } = createProvider();
|
||||
|
||||
await provider.sendMessage(makeNotification({ type: "rocket_chat" }), makeMessageWithIncident());
|
||||
|
||||
const payload = mockGotPost.mock.calls[0][1].json;
|
||||
expect(payload.text).toContain("Incident: https://app.example.com/infrastructure/mon-1");
|
||||
});
|
||||
|
||||
it("omits the incident link when incident data is absent", async () => {
|
||||
const { provider } = createProvider();
|
||||
|
||||
await provider.sendMessage(
|
||||
makeNotification({ type: "rocket_chat" }),
|
||||
makeMessage({
|
||||
type: "monitor_up",
|
||||
severity: "success",
|
||||
monitor: { id: "mon-1", name: "Test Monitor", url: "https://example.com", type: "http", status: "up" },
|
||||
})
|
||||
);
|
||||
|
||||
const payload = mockGotPost.mock.calls[0][1].json;
|
||||
expect(payload.text).not.toContain("Incident:");
|
||||
});
|
||||
|
||||
it("returns false and logs when message delivery fails", async () => {
|
||||
mockGotPost.mockRejectedValue(new Error("network failure"));
|
||||
const { provider, logger } = createProvider();
|
||||
|
||||
Reference in new issue
Block a user