|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const BbPromise = require("bluebird"), |
| 4 | + _ = require("lodash"), |
| 5 | + Slack = require("../slack"); |
| 6 | + |
| 7 | +class CodeDeployParser { |
| 8 | + |
| 9 | + parse(event) { |
| 10 | + return BbPromise.try(() => _.isObject(event) ? event : JSON.parse(event)) |
| 11 | + .catch(_.noop) // ignore JSON errors |
| 12 | + .then(event => { |
| 13 | + if (_.get(event, "source") !== "aws.codedeploy") { |
| 14 | + return BbPromise.resolve(false); |
| 15 | + } |
| 16 | + |
| 17 | + const time = new Date(_.get(event, "time")); |
| 18 | + const deployState = _.get(event, "detail.state"); |
| 19 | + const deploymentGroup = _.get(event, "detail.deploymentGroup"); |
| 20 | + const deploymentId = _.get(event, "detail.deploymentId"); |
| 21 | + const app = _.get(event, "detail.application"); |
| 22 | + const statusUrl = `https://console.aws.amazon.com/codedeploy/home?region=${event.region}#/deployments/${deploymentId}`; |
| 23 | + const fields = []; |
| 24 | + |
| 25 | + let color = Slack.COLORS.neutral; |
| 26 | + const baseTitle = `CodeDeploy Application ${app}`; |
| 27 | + let title = baseTitle; |
| 28 | + if (deployState === "SUCCESS") { |
| 29 | + title = `<${statusUrl}|${baseTitle}> has finished`; |
| 30 | + color = Slack.COLORS.ok; |
| 31 | + } |
| 32 | + else if (deployState === "STOP") { |
| 33 | + title = `<${statusUrl}|${baseTitle}> was stopped`; |
| 34 | + color = Slack.COLORS.warning; |
| 35 | + } |
| 36 | + else if (deployState === "FAILURE") { |
| 37 | + title = `<${statusUrl}|${baseTitle}> has failed`; |
| 38 | + color = Slack.COLORS.critical; |
| 39 | + } |
| 40 | + else if (deployState === "START") { |
| 41 | + title = `<${statusUrl}|${baseTitle}> has started deploying`; |
| 42 | + } |
| 43 | + |
| 44 | + if (deployState) { |
| 45 | + fields.push({ |
| 46 | + title: "Status", |
| 47 | + value: deployState, |
| 48 | + short: true |
| 49 | + }); |
| 50 | + } |
| 51 | + |
| 52 | + fields.push({ |
| 53 | + title: "DeploymentGroup", |
| 54 | + value: `${deploymentGroup}`, |
| 55 | + short: true |
| 56 | + }); |
| 57 | + |
| 58 | + const slackMessage = { |
| 59 | + attachments: [{ |
| 60 | + author_name: "AWS CodeDeploy Notification", |
| 61 | + fallback: `${baseTitle} ${deployState}`, |
| 62 | + color: color, |
| 63 | + title: title, |
| 64 | + fields: fields, |
| 65 | + mrkdwn_in: [ "title", "text" ], |
| 66 | + ts: Slack.toEpochTime(time) |
| 67 | + }] |
| 68 | + }; |
| 69 | + return BbPromise.resolve(slackMessage); |
| 70 | + }); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +module.exports = CodeDeployParser; |
0 commit comments