|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const BbPromise = require("bluebird"), |
| 4 | + _ = require("lodash"), |
| 5 | + Slack = require("../slack"); |
| 6 | + |
| 7 | +class CodePipelineParser { |
| 8 | + |
| 9 | + parse(event) { |
| 10 | + //console.log("CodePipeline :: start..."); |
| 11 | + //console.log(_.get(event, "Records[0].Sns.Message", "{}")); |
| 12 | + return BbPromise.try(() => |
| 13 | + JSON.parse(_.get(event, "Records[0].Sns.Message", "{}"))) |
| 14 | + .catch(_.noop) // ignore JSON errors |
| 15 | + .then(message => { |
| 16 | + |
| 17 | + // Check that this is a CodePipeline message |
| 18 | + if (!_.has(message, "source") || message.source != "aws.codepipeline") { |
| 19 | + return BbPromise.resolve(false); |
| 20 | + } |
| 21 | + |
| 22 | + const source = message.source; |
| 23 | + |
| 24 | + // Check that this is NOT an approval message (there is a separate handler for those...) |
| 25 | + // NOTE: CodePipeline Action Execution State Changes that are APPROVALs are handled here, |
| 26 | + // only ignore the dedicated Approval request notifications |
| 27 | + if (_.has(message, "approval") && _.has(message, "consoleLink")) { |
| 28 | + return BbPromise.resolve(false); |
| 29 | + } |
| 30 | + |
| 31 | + const typeProvider = message.detail.type.provider; |
| 32 | + const typeCategory = message.detail.type.category; |
| 33 | + const pipeline = message.detail.pipeline; |
| 34 | + const stage = message.detail.stage; |
| 35 | + const action = message.detail.action; |
| 36 | + const state = message.detail.state; |
| 37 | + const time = new Date(message.time); |
| 38 | + |
| 39 | + // Compose the title based upon the best "one line" summary of the state |
| 40 | + var slackTitle = pipeline + " >> "; |
| 41 | + if(typeProvider == "Manual" && typeCategory == "Approval") { |
| 42 | + slackTitle += "APPROVAL REQUIRED for " + stage; |
| 43 | + } |
| 44 | + |
| 45 | + |
| 46 | + let color = Slack.COLORS.neutral; |
| 47 | + switch(state) { |
| 48 | + //case "RESUMED": |
| 49 | + //case "SUPERSEDED": |
| 50 | + case "STARTED": |
| 51 | + color = Slack.COLORS.accent; |
| 52 | + break; |
| 53 | + case "SUCCEEDED": |
| 54 | + color = Slack.COLORS.ok; |
| 55 | + break; |
| 56 | + case "FAILED": |
| 57 | + color = Slack.COLORS.critical; |
| 58 | + break; |
| 59 | + case "CANCELLED": |
| 60 | + color = Slack.COLORS.warning; |
| 61 | + break; |
| 62 | + } |
| 63 | + |
| 64 | + const slackMessage = { |
| 65 | + attachments: [{ |
| 66 | + fallback: `${pipeline} >> ${stage} is ${state}`, |
| 67 | + color: color, |
| 68 | + author_name: "AWS CodePipeline", |
| 69 | + title: slackTitle, |
| 70 | + text: message, |
| 71 | + fields: [{ |
| 72 | + title: "Stage", |
| 73 | + value: stage, |
| 74 | + short: true |
| 75 | + }, { |
| 76 | + title: "Action", |
| 77 | + value: action, |
| 78 | + short: true |
| 79 | + }, { |
| 80 | + title: "State", |
| 81 | + value: state, |
| 82 | + short: true |
| 83 | + }], |
| 84 | + ts: Slack.toEpochTime(time) |
| 85 | + }] |
| 86 | + }; |
| 87 | + |
| 88 | + return slackMessage; |
| 89 | + |
| 90 | + }); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +module.exports = CodePipelineParser; |
0 commit comments