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";
|
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 type { Notification, NotificationMessage } from "@/domain/notifications/notification.type.js";
|
||||||
import { NotificationProvider } from "@/domain/notifications/providers/INotificationProvider.js";
|
import { NotificationProvider } from "@/domain/notifications/providers/INotificationProvider.js";
|
||||||
import { getTestMessage } from "@/domain/notifications/providers/utils.js";
|
import { getTestMessage } from "@/domain/notifications/providers/utils.js";
|
||||||
import got from "got";
|
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 {
|
export class RocketChatProvider extends NotificationProvider {
|
||||||
async sendTestAlert(notification: Partial<Notification>): Promise<boolean> {
|
async sendTestAlert(notification: Partial<Notification>): Promise<boolean> {
|
||||||
if (!notification.address) {
|
if (!notification.address) {
|
||||||
@@ -12,7 +23,7 @@ export class RocketChatProvider extends NotificationProvider {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
await got.post(notification.address, {
|
await got.post(notification.address, {
|
||||||
json: { text: getTestMessage() },
|
json: this.buildTestPayload(),
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"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> {
|
async sendMessage(notification: Notification, message: NotificationMessage): Promise<boolean> {
|
||||||
if (!notification.address) {
|
if (!notification.address) {
|
||||||
return false;
|
return false;
|
||||||
@@ -38,7 +65,7 @@ export class RocketChatProvider extends NotificationProvider {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
await got.post(notification.address, {
|
await got.post(notification.address, {
|
||||||
json: { text: this.buildText(message) },
|
json: this.buildPayload(message),
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
},
|
},
|
||||||
@@ -57,34 +84,88 @@ export class RocketChatProvider extends NotificationProvider {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private buildText(message: NotificationMessage): string {
|
private buildPayload(message: NotificationMessage): RocketChatPayload {
|
||||||
const lines = [
|
const monitorUrl = this.monitorUrl(message);
|
||||||
message.content.title,
|
const fields: RocketChatField[] = [
|
||||||
message.content.summary,
|
{ title: "Monitor", value: message.monitor.name, short: true },
|
||||||
"",
|
{ title: "Type", value: message.monitor.type, short: true },
|
||||||
`Monitor: ${message.monitor.name}`,
|
{ title: "Status", value: message.monitor.status, short: true },
|
||||||
`Type: ${message.monitor.type}`,
|
{ title: "URL", value: message.monitor.url, short: false },
|
||||||
`Status: ${message.monitor.status}`,
|
|
||||||
`URL: ${message.monitor.url}`,
|
|
||||||
];
|
];
|
||||||
|
|
||||||
if (message.content.details?.length) {
|
|
||||||
lines.push("", "Details:", ...message.content.details.map((detail) => `- ${detail}`));
|
|
||||||
}
|
|
||||||
if (message.content.thresholds?.length) {
|
if (message.content.thresholds?.length) {
|
||||||
lines.push(
|
fields.push(
|
||||||
"",
|
...message.content.thresholds.map((breach) => ({
|
||||||
"Thresholds:",
|
title: breach.metric.toUpperCase(),
|
||||||
...message.content.thresholds.map(
|
value: `${breach.formattedValue} (threshold: ${breach.threshold}${breach.unit})`,
|
||||||
(threshold) => `- ${threshold.metric.toUpperCase()}: ${threshold.formattedValue} (threshold: ${threshold.threshold}${threshold.unit})`
|
short: true,
|
||||||
)
|
}))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (message.content.incident) {
|
if (message.content.details?.length) {
|
||||||
lines.push("", `Incident: ${message.clientHost}/infrastructure/${message.monitor.id}`);
|
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 { 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 { createMockLogger } from "../../../helpers/createMockLogger.ts";
|
||||||
import { makeMessage, makeMessageWithIncident, makeMessageWithThresholds, makeNotification } from "../../../helpers/notificationMessage.ts";
|
import { makeMessage, makeMessageWithIncident, makeMessageWithThresholds, makeNotification } from "../../../helpers/notificationMessage.ts";
|
||||||
import { testNotificationProviderContract } from "../../../helpers/notificationProviderContract.ts";
|
import { testNotificationProviderContract } from "../../../helpers/notificationProviderContract.ts";
|
||||||
@@ -25,7 +27,7 @@ testNotificationProviderContract("RocketChatProvider", {
|
|||||||
describe("RocketChatProvider", () => {
|
describe("RocketChatProvider", () => {
|
||||||
beforeEach(() => mockGotPost.mockReset().mockResolvedValue({}));
|
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 { provider } = createProvider();
|
||||||
|
|
||||||
const result = await provider.sendTestAlert(makeNotification({ type: "rocket_chat" }));
|
const result = await provider.sendTestAlert(makeNotification({ type: "rocket_chat" }));
|
||||||
@@ -34,11 +36,29 @@ describe("RocketChatProvider", () => {
|
|||||||
expect(mockGotPost).toHaveBeenCalledWith(
|
expect(mockGotPost).toHaveBeenCalledWith(
|
||||||
"https://hooks.example.com/webhook",
|
"https://hooks.example.com/webhook",
|
||||||
expect.objectContaining({
|
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 },
|
timeout: { request: 30000 },
|
||||||
retry: { limit: 0 },
|
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 () => {
|
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 { provider } = createProvider();
|
||||||
|
|
||||||
const result = await provider.sendMessage(makeNotification({ type: "rocket_chat" }), makeMessage());
|
const result = await provider.sendMessage(makeNotification({ type: "rocket_chat" }), makeMessage());
|
||||||
@@ -78,22 +98,152 @@ describe("RocketChatProvider", () => {
|
|||||||
timeout: { request: 30000 },
|
timeout: { request: 30000 },
|
||||||
retry: { limit: 0 },
|
retry: { limit: 0 },
|
||||||
json: {
|
json: {
|
||||||
text: [
|
text: 'Monitor "Test Monitor" is currently down.',
|
||||||
"Monitor Down: Test Monitor",
|
attachments: [
|
||||||
'Monitor "Test Monitor" is currently down.',
|
{
|
||||||
"",
|
color: "#FF0000",
|
||||||
"Monitor: Test Monitor",
|
title: "Monitor Down: Test Monitor",
|
||||||
"Type: http",
|
title_link: "https://app.example.com/uptime/mon-1",
|
||||||
"Status: down",
|
fields: [
|
||||||
"URL: https://example.com",
|
{ title: "Monitor", value: "Test Monitor", short: true },
|
||||||
"",
|
{ title: "Type", value: "http", short: true },
|
||||||
"Details:",
|
{ title: "Status", value: "down", short: true },
|
||||||
"- URL: https://example.com",
|
{ title: "URL", value: "https://example.com", short: false },
|
||||||
"- Status: Down",
|
{
|
||||||
].join("\n"),
|
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 () => {
|
it("returns false when the message webhook URL is missing", async () => {
|
||||||
@@ -105,40 +255,6 @@ describe("RocketChatProvider", () => {
|
|||||||
expect(mockGotPost).not.toHaveBeenCalled();
|
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 () => {
|
it("returns false and logs when message delivery fails", async () => {
|
||||||
mockGotPost.mockRejectedValue(new Error("network failure"));
|
mockGotPost.mockRejectedValue(new Error("network failure"));
|
||||||
const { provider, logger } = createProvider();
|
const { provider, logger } = createProvider();
|
||||||
|
|||||||
Reference in new issue
Block a user