diff --git a/config/default.json b/config/default.json index 081c20a..ee8df93 100644 --- a/config/default.json +++ b/config/default.json @@ -19,7 +19,8 @@ }, "slack": { "token": "", - "signingSecret": "" + "signingSecret": "", + "defaultChannel": "C0712GGAN94" }, "jira": { "url": "" diff --git a/src/handlers/AgentHandler.js b/src/handlers/AgentHandler.js index 3589a73..d8e2212 100644 --- a/src/handlers/AgentHandler.js +++ b/src/handlers/AgentHandler.js @@ -2,7 +2,9 @@ const Handler = require("./Handler"); const _ = require("lodash"); const chalk = require("chalk"); const dayjs = require("dayjs"); +const config = require("config"); const util = require("util"); +const AgentUpdatedNotification = require("../templates/AgentUpdatedNotification"); class AgentHandler extends Handler { static shouldHandle(request) { @@ -18,6 +20,7 @@ class AgentHandler extends Handler { const { host_name, agent_config_state, agent_state, build_state } = body; const now = dayjs().format("YYYY-MM-DD HH:mm"); if (!isDeployAgent) { + process.stdout.write("."); return; } @@ -30,10 +33,14 @@ class AgentHandler extends Handler { if (agent_state === "LostContact") { AgentHandler.log(body); console.log(chalk.bgRed.white(`[${now}] Deployment Agent lost contact`)); + this.doNotify(body, `Agent **${host_name}** in ${agent_state} state`); } else if (agent_state === "Idle" && build_state === "Idle") { AgentHandler.log(body); console.log(chalk.bgGreen.white(`[${now}] Deployment Agent came online?`)); + this.doNotify(body, `**${host_name}** has come back online`); } + } else { + this.doNotify(body, "Testing"); } console.log(`[${now}] ${agentName} Status: ${configState} State: ${agentState} • Build State: ${buildState}`); @@ -42,6 +49,23 @@ class AgentHandler extends Handler { } } + async doNotify(agent, text) { + const notification = new AgentUpdatedNotification(agent)?.toJSON(text); + try { + const response = await this.app.client.chat.postMessage({ + text, + token: config.get("slack.token"), + channel: config.get("slack.defaultChannel"), + ...notification, + }); + console.log("Slack message sent", response?.ok, response?.message.text ?? response); + } catch (error) { + console.log("Error sending slack message", chalk.red(error.message)); + console.log(notification); + } + process.exit(0); + } + static log(data, options = {}) { console.log( util.inspect(data, { diff --git a/src/templates/AgentUpdatedNotification.js b/src/templates/AgentUpdatedNotification.js new file mode 100644 index 0000000..dc74ae9 --- /dev/null +++ b/src/templates/AgentUpdatedNotification.js @@ -0,0 +1,39 @@ +class AgentUpdatedNotification { + constructor(agent) { + this.agent = agent; + } + + getColor() { + if (this.agent.agent_state === "LostContact") { + return "#ff5a5a"; // Yellow + } + + if (this.agent.agent_state === "Idle" && this.agent.build_state === "Idle") { + return "#00FF7F"; // Green + } + + if (this.agent.agent_state === "Building" || this.agent.build_state === "Building") { + return "#27ce70"; // Another green + } + + return "#1352c6"; // Blue + } + + toJSON(text) { + const { agent } = this; + const payload = { + attachments: [{ + mrkdwn_in: ["text", "pretext"], + color: this.getColor(), + title: text ?? `Something changed with agent: ${agent.host_name}`, + text: `Agent State: ${agent.agent_state} Build State: ${agent.build_state}`, + title_link: `https://sage.ci.xola.com/go/agents/${agent.uuid}/job_run_history`, + footer: `Agent UUID: ${agent.uuid}` + }] + } + + return payload; + } +} + +module.exports = AgentUpdatedNotification; diff --git a/src/templates/PipelineFailedNotification.js b/src/templates/PipelineFailedNotification.js index b1dc5e7..584d931 100644 --- a/src/templates/PipelineFailedNotification.js +++ b/src/templates/PipelineFailedNotification.js @@ -73,9 +73,9 @@ class PipelineFailedNotification { fields.push({ title: `Failed Jobs (${failedJobs.length})`, value: message }); } - const emails = [`Commited By: ${pipeline.getCommitterEmail()}`]; + const emails = [`Committed By: ${pipeline.getCommitterEmail()}`]; if (pipeline.getApprovedByEmail() && pipeline.getApprovedByEmail() !== pipeline.getCommitterEmail()) { - emails.push(`Trigerred by: ${pipeline.getApprovedByEmail()}`); + emails.push(`Triggered by: ${pipeline.getApprovedByEmail()}`); } let footer = `Status: ${pipeline.get("stage.result")}, well done`;