|
| 1 | +const NotificationProvider = require("./notification-provider"); |
| 2 | +const axios = require("axios"); |
| 3 | +const { UP, DOWN } = require("../../src/util"); |
| 4 | + |
| 5 | +const okMsg = "Sent Successfully."; |
| 6 | + |
| 7 | +class JiraServiceManagement extends NotificationProvider { |
| 8 | + name = "JiraServiceManagement"; |
| 9 | + |
| 10 | + /** |
| 11 | + * @inheritdoc |
| 12 | + */ |
| 13 | + async send(notification, msg, monitorJSON = null, heartbeatJSON = null) { |
| 14 | + const priority = notification.jsmPriority || 3; |
| 15 | + const baseUrl = `https://api.atlassian.com/jsm/ops/api/${notification.jsmCloudId}/v1`; |
| 16 | + const textMsg = "Uptime Kuma Alert"; |
| 17 | + |
| 18 | + try { |
| 19 | + if (heartbeatJSON == null) { |
| 20 | + // Test notification |
| 21 | + let notificationTestAlias = "uptime-kuma-notification-test"; |
| 22 | + let data = { |
| 23 | + message: msg, |
| 24 | + alias: notificationTestAlias, |
| 25 | + source: "Uptime Kuma", |
| 26 | + priority: "P5", |
| 27 | + tags: ["Uptime Kuma"], |
| 28 | + }; |
| 29 | + |
| 30 | + return this.post(notification, `${baseUrl}/alerts`, data); |
| 31 | + } |
| 32 | + |
| 33 | + if (heartbeatJSON.status === DOWN) { |
| 34 | + let data = { |
| 35 | + message: monitorJSON ? `${textMsg}: ${monitorJSON.name}` : textMsg, |
| 36 | + alias: monitorJSON.name, |
| 37 | + description: msg, |
| 38 | + source: "Uptime Kuma", |
| 39 | + priority: `P${priority}`, |
| 40 | + tags: ["Uptime Kuma"], |
| 41 | + }; |
| 42 | + |
| 43 | + return this.post(notification, `${baseUrl}/alerts`, data); |
| 44 | + } |
| 45 | + |
| 46 | + if (heartbeatJSON.status === UP) { |
| 47 | + // JSM requires getting the alert ID first, then closing by ID |
| 48 | + const getUrl = `${baseUrl}/alerts/alias?alias=${encodeURIComponent(monitorJSON.name)}`; |
| 49 | + const config = this.getConfig(notification); |
| 50 | + |
| 51 | + let alertResponse = await axios.get(getUrl, config); |
| 52 | + const alertId = alertResponse.data.id; |
| 53 | + |
| 54 | + const closeUrl = `${baseUrl}/alerts/${alertId}/close`; |
| 55 | + let data = { |
| 56 | + source: "Uptime Kuma", |
| 57 | + }; |
| 58 | + |
| 59 | + return this.post(notification, closeUrl, data); |
| 60 | + } |
| 61 | + } catch (error) { |
| 62 | + this.throwGeneralAxiosError(error); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Get axios config with Basic Auth for JSM |
| 68 | + * @param {BeanModel} notification Notification details |
| 69 | + * @returns {object} Axios config object |
| 70 | + */ |
| 71 | + getConfig(notification) { |
| 72 | + const authToken = Buffer.from(`${notification.jsmEmail}:${notification.jsmApiToken}`).toString("base64"); |
| 73 | + let config = { |
| 74 | + headers: { |
| 75 | + "Content-Type": "application/json", |
| 76 | + Accept: "application/json", |
| 77 | + Authorization: `Basic ${authToken}`, |
| 78 | + }, |
| 79 | + }; |
| 80 | + return this.getAxiosConfigWithProxy(config); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Make POST request to Jira Service Management |
| 85 | + * @param {BeanModel} notification Notification to send |
| 86 | + * @param {string} url Request url |
| 87 | + * @param {object} data Request body |
| 88 | + * @returns {Promise<string>} Success message |
| 89 | + */ |
| 90 | + async post(notification, url, data) { |
| 91 | + let config = this.getConfig(notification); |
| 92 | + |
| 93 | + let res = await axios.post(url, data, config); |
| 94 | + if (res.status == null) { |
| 95 | + return "Jira Service Management notification failed with invalid response!"; |
| 96 | + } |
| 97 | + if (res.status < 200 || res.status >= 300) { |
| 98 | + return `Jira Service Management notification failed with status code ${res.status}`; |
| 99 | + } |
| 100 | + |
| 101 | + return okMsg; |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +module.exports = JiraServiceManagement; |
0 commit comments