|
| 1 | +/** |
| 2 | + * ------------------------------------------------------------------------ |
| 3 | + * This file contains the notification services that are supported |
| 4 | + * This includes Slack,Email,Telegram |
| 5 | + * ------------------------------------------------------------------------ |
| 6 | + */ |
| 7 | +const config = require('../config/notifications') |
| 8 | +const https = require('https') |
| 9 | +const transport = require('./mail') |
| 10 | + |
| 11 | +class NotificationService { |
| 12 | + /** |
| 13 | + * Notification service is enabled |
| 14 | + */ |
| 15 | + notificationEnabled = Boolean |
| 16 | + /** |
| 17 | + * The Slack Webhook URL |
| 18 | + */ |
| 19 | + slackURL = String |
| 20 | + /** |
| 21 | + * Successful build notification |
| 22 | + */ |
| 23 | + notifyOnSuccess = Boolean |
| 24 | + /** |
| 25 | + * Error Notifications |
| 26 | + */ |
| 27 | + notifyOnError = Boolean |
| 28 | + /** |
| 29 | + * The email for notifications |
| 30 | + */ |
| 31 | + notificationEmail = String |
| 32 | + |
| 33 | + /** |
| 34 | + * The notification payload |
| 35 | + */ |
| 36 | + notificationData = String |
| 37 | + |
| 38 | + /** |
| 39 | + * The notification type |
| 40 | + */ |
| 41 | + notificationType = Boolean |
| 42 | + |
| 43 | + /** |
| 44 | + * initialise the service |
| 45 | + * @param {Array} data |
| 46 | + * @param {String} type |
| 47 | + */ |
| 48 | + constructor(data = {}, type = '') { |
| 49 | + this.notificationEnabled = config.allowNotifications |
| 50 | + this.slackURL = config.slackWebhookURL |
| 51 | + this.notifyOnError = config.notifyOnErrors |
| 52 | + this.notifyOnSuccess = config.notifyOnSucess |
| 53 | + this.notificationEmail = config.notificationEmail |
| 54 | + this.notificationData = data |
| 55 | + this.notificationType = type |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Send notifications |
| 60 | + * @param {Array} services |
| 61 | + * @returns void |
| 62 | + */ |
| 63 | + sendNotifications() { |
| 64 | + if (!this.notificationEnabled) { |
| 65 | + return |
| 66 | + } |
| 67 | + if (this.notificationType === 'success' && this.notifyOnSuccess === 'false') { |
| 68 | + return |
| 69 | + } |
| 70 | + if (this.notificationType === 'error' && this.notifyOnError === 'false') { |
| 71 | + return |
| 72 | + } |
| 73 | + const services = this.getSupportedServices() |
| 74 | + services.forEach((service) => { |
| 75 | + switch (service.name) { |
| 76 | + case 'Slack': |
| 77 | + this.sendSlackWebhook(service.value) |
| 78 | + break; |
| 79 | + case 'email': |
| 80 | + this.sendEmails() |
| 81 | + break; |
| 82 | + default: |
| 83 | + break; |
| 84 | + } |
| 85 | + }) |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Get the supported services |
| 90 | + * @returns Array [] |
| 91 | + */ |
| 92 | + getSupportedServices() { |
| 93 | + const supportedServices = [] |
| 94 | + if (this.slackURL) { |
| 95 | + supportedServices.push({ |
| 96 | + name: 'Slack', |
| 97 | + value: this.slackURL |
| 98 | + }) |
| 99 | + } |
| 100 | + if (this.notificationEmail) { |
| 101 | + supportedServices.push({ |
| 102 | + name: 'email', value: this.notificationEmail |
| 103 | + }) |
| 104 | + } |
| 105 | + return supportedServices |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Send slack webhook |
| 110 | + * @param {String} url |
| 111 | + * @returns Boolean |
| 112 | + */ |
| 113 | + sendSlackWebhook(url = '') { |
| 114 | + const fullURL = new URL(url) |
| 115 | + const payload = Buffer.from(JSON.stringify({ |
| 116 | + text: this.notificationData.slack, |
| 117 | + mrkdwn: true |
| 118 | + })) |
| 119 | + const request = https.request({ |
| 120 | + host: fullURL.host, |
| 121 | + path: fullURL.pathname, |
| 122 | + method: 'POST', |
| 123 | + port: 443, |
| 124 | + protocol: 'https:' |
| 125 | + }, (res) => { |
| 126 | + res.setEncoding('utf-8'); |
| 127 | + }) |
| 128 | + request.on('error', (err) => { |
| 129 | + throw err |
| 130 | + }) |
| 131 | + request.write(payload) |
| 132 | + request.end() |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Send Email(s) |
| 137 | + * @param {String} emails |
| 138 | + * @returns Boolean |
| 139 | + */ |
| 140 | + sendEmails() { |
| 141 | + transport.sendMail({ |
| 142 | + from: config.notificationFrom, |
| 143 | + to: config.notificationEmail, |
| 144 | + subject: this.notificationData.mail.subject, |
| 145 | + html: this.notificationData.mail.data, |
| 146 | + }); |
| 147 | + return true |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +module.exports = NotificationService |
0 commit comments