@eventcatalog/notifier
Version:
CLI tool to detect EventCatalog changes and send notifications
72 lines • 3.67 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.sendNotifications = sendNotifications;
const axios_1 = __importDefault(require("axios"));
const chalk_1 = __importDefault(require("chalk"));
const ConsumerAddedEvent_1 = require("./notifications/ConsumerAddedEvent");
const ConsumerRemovedEvent_1 = require("./notifications/ConsumerRemovedEvent");
const logger_1 = require("./utils/logger");
const SubscribedSchemaChangedEvent_1 = require("./notifications/SubscribedSchemaChangedEvent");
const logger = new logger_1.Logger();
async function sendNotifications(config, notifications, options) {
for (const notification of notifications) {
for (const configuredOwnerName in config.owners) {
const ownersConfiguration = config.owners[configuredOwnerName];
const ownersSubscribedEvents = ownersConfiguration.events;
// Check if owner is configured for this event type AND actually owns the resource
if (ownersSubscribedEvents.includes(notification.id)
// notification.resource.owners.some((resourceOwner) => resourceOwner.id === configuredOwnerName)
) {
for (const channel of ownersConfiguration.channels) {
if (channel.type === 'slack') {
logger.info(`Sending slack notification to ${channel.webhook}`);
logger.verbose(`Notification: ${JSON.stringify(notification, null, 2)}`);
await sendSlackNotification(config, notification, channel, options);
}
}
}
}
}
}
async function sendSlackNotification(config, notification, channel, options) {
let message = null;
switch (notification.id) {
case 'consumer-added':
message = ConsumerAddedEvent_1.ConsumerAddedEvent.getSlackMessage(config, notification, options.lifecycle, options.actionUrl);
break;
case 'consumer-removed':
message = ConsumerRemovedEvent_1.ConsumerRemovedEvent.getSlackMessage(config, notification, options.lifecycle, options.actionUrl);
break;
case 'subscribed-schema-changed':
message = SubscribedSchemaChangedEvent_1.SubscribedSchemaChangedEvent.getSlackMessage(config, notification, options.lifecycle, options.actionUrl);
break;
default:
message = null;
}
if (!message) {
console.log(chalk_1.default.red('✗'), `No message found for notification ${notification.id}`);
return;
}
const headers = channel.headers || {};
if (options.dryRun) {
console.log(chalk_1.default.yellow(`[DRY RUN]`), `Would send notification to ${chalk_1.default.cyan(channel.webhook)}:`);
console.log(chalk_1.default.gray(JSON.stringify(message, null, 2)));
if (Object.keys(headers).length > 0) {
console.log(chalk_1.default.yellow(`[DRY RUN]`), `Headers:`, chalk_1.default.gray(JSON.stringify(headers, null, 2)));
}
}
else {
try {
await axios_1.default.post(channel.webhook, message, { headers });
console.log(chalk_1.default.green('✓'), `Notification sent successfully to ${chalk_1.default.cyan(channel.webhook)}`);
}
catch (error) {
console.log(chalk_1.default.red('✗'), `Failed to send notification to ${chalk_1.default.cyan(channel.webhook)}:`, error);
throw error;
}
}
}
//# sourceMappingURL=notifier.js.map