Skip to content

Commit

Permalink
Merge pull request #27 from ts33/feature/format-codepipeline-notifica…
Browse files Browse the repository at this point in the history
…tions

Feature/format codepipeline notifications
  • Loading branch information
creichert authored Jul 5, 2018
2 parents 7961738 + 5e9c067 commit 8260622
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 6 deletions.
17 changes: 14 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,21 @@ deps:
.PHONY: test
test:
@test -s $(CONFIG_FILE) || { echo "No lambda config file. Update deploy.env.example and copy it to deploy.env"; exit 1; }
AWS_REGION=$(AWS_REGION) $(LAMBDA_TEST) --configFile=$(CONFIG_FILE) run -x test/context.json -j test/sns-cloudwatch-event.json
AWS_REGION=$(AWS_REGION) $(LAMBDA_TEST) --configFile=$(CONFIG_FILE) run -x test/context.json -j test/sns-codedeploy-event.json

.PHONY: test-codepipeline
test-codepipeline:
@test -s $(CONFIG_FILE) || { echo "No lambda config file. Update deploy.env.example and copy it to deploy.env"; exit 1; }
AWS_REGION=$(AWS_REGION) $(LAMBDA_TEST) --configFile=$(CONFIG_FILE) run -x test/context.json -j test/sns-codepipeline-event-pipeline-started.json
AWS_REGION=$(AWS_REGION) $(LAMBDA_TEST) --configFile=$(CONFIG_FILE) run -x test/context.json -j test/sns-codepipeline-event-stage-started.json
AWS_REGION=$(AWS_REGION) $(LAMBDA_TEST) --configFile=$(CONFIG_FILE) run -x test/context.json -j test/sns-codepipeline-event-stage-succeeded.json
AWS_REGION=$(AWS_REGION) $(LAMBDA_TEST) --configFile=$(CONFIG_FILE) run -x test/context.json -j test/sns-codepipeline-event-stage-failed.json


.PHONY: test-all
test-all: test
test-all: test test-codepipeline
AWS_REGION=$(AWS_REGION) $(LAMBDA_TEST) --configFile=$(CONFIG_FILE) run -x test/context.json -j test/sns-cloudwatch-event.json
AWS_REGION=$(AWS_REGION) $(LAMBDA_TEST) --configFile=$(CONFIG_FILE) run -x test/context.json -j test/sns-codepipeline-event.json
AWS_REGION=$(AWS_REGION) $(LAMBDA_TEST) --configFile=$(CONFIG_FILE) run -x test/context.json -j test/sns-event.json
AWS_REGION=$(AWS_REGION) $(LAMBDA_TEST) --configFile=$(CONFIG_FILE) run -x test/context.json -j test/sns-elastic-beanstalk-event.json
AWS_REGION=$(AWS_REGION) $(LAMBDA_TEST) --configFile=$(CONFIG_FILE) run -x test/context.json -j test/sns-codedeploy-event.json
Expand All @@ -39,4 +50,4 @@ deploy:
--secretKey $(AWS_ACCESS_KEY_SECRET) \
--region $(AWS_REGION) \
--configFile $(CONFIG_FILE) \
--profile $(AWS_PROFILE)
--profile $(AWS_PROFILE)
6 changes: 5 additions & 1 deletion config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ module.exports = {
// text in the sns message or topicname to match on to process this service type
match_text: "CloudWatchNotifications"
},
codepipeline: {
// text in the sns message or topicname to match on to process this service type
match_text: "CodePipelineNotifications"
},
codedeploy: {
// text in the sns message or topicname to match on to process this service type
match_text: "CodeDeploy"
Expand All @@ -31,4 +35,4 @@ module.exports = {
}
}

}
}
67 changes: 65 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,65 @@ var handleCodeDeploy = function(event, context) {
return _.merge(slackMessage, baseSlackMessage);
};

var handleCodePipeline = function(event, context) {
var subject = "AWS CodePipeline Notification";
var timestamp = (new Date(event.Records[0].Sns.Timestamp)).getTime()/1000;
var snsSubject = event.Records[0].Sns.Subject;
var message;
var fields = [];
var color = "warning";
var changeType = "";

try {
message = JSON.parse(event.Records[0].Sns.Message);
detailType = message['detail-type'];

if(detailType === "CodePipeline Pipeline Execution State Change"){
changeType = "";
} else if(detailType === "CodePipeline Stage Execution State Change"){
changeType = "STAGE " + message.detail.stage;
} else if(detailType === "CodePipeline Action Execution State Change"){
changeType = "ACTION";
}

if(message.detail.state === "SUCCEEDED"){
color = "good";
} else if(message.detail.state === "FAILED"){
color = "danger";
}
header = message.detail.state + ": CodePipeline " + changeType;
fields.push({ "title": "Message", "value": header, "short": false });
fields.push({ "title": "Pipeline", "value": message.detail.pipeline, "short": true });
fields.push({ "title": "Region", "value": message.region, "short": true });
fields.push({
"title": "Status Link",
"value": "https://console.aws.amazon.com/codepipeline/home?region=" + message.region + "#/view/" + message.detail.pipeline,
"short": false
});
}
catch(e) {
color = "good";
message = event.Records[0].Sns.Message;
header = message.detail.state + ": CodePipeline " + message.detail.pipeline;
fields.push({ "title": "Message", "value": header, "short": false });
fields.push({ "title": "Detail", "value": message, "short": false });
}


var slackMessage = {
text: "*" + subject + "*",
attachments: [
{
"color": color,
"fields": fields,
"ts": timestamp
}
]
};

return _.merge(slackMessage, baseSlackMessage);
};

var handleElasticache = function(event, context) {
var subject = "AWS ElastiCache Notification"
var message = JSON.parse(event.Records[0].Sns.Message);
Expand Down Expand Up @@ -311,7 +370,11 @@ var processEvent = function(event, context) {
var eventSnsSubject = event.Records[0].Sns.Subject || 'no subject';
var eventSnsMessage = event.Records[0].Sns.Message;

if(eventSubscriptionArn.indexOf(config.services.elasticbeanstalk.match_text) > -1 || eventSnsSubject.indexOf(config.services.elasticbeanstalk.match_text) > -1 || eventSnsMessage.indexOf(config.services.elasticbeanstalk.match_text) > -1){
if(eventSubscriptionArn.indexOf(config.services.codepipeline.match_text) > -1 || eventSnsSubject.indexOf(config.services.codepipeline.match_text) > -1 || eventSnsMessage.indexOf(config.services.codepipeline.match_text) > -1){
console.log("processing codepipeline notification");
slackMessage = handleCodePipeline(event,context)
}
else if(eventSubscriptionArn.indexOf(config.services.elasticbeanstalk.match_text) > -1 || eventSnsSubject.indexOf(config.services.elasticbeanstalk.match_text) > -1 || eventSnsMessage.indexOf(config.services.elasticbeanstalk.match_text) > -1){
console.log("processing elasticbeanstalk notification");
slackMessage = handleElasticBeanstalk(event,context)
}
Expand Down Expand Up @@ -373,4 +436,4 @@ exports.handler = function(event, context) {
} else {
context.fail('hook url has not been set.');
}
};
};
18 changes: 18 additions & 0 deletions test/sns-codepipeline-event-pipeline-started.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"Records": [
{
"EventSource": "aws:sns",
"EventVersion": "1.0",
"EventSubscriptionArn": "arn:aws:sns:us-east-1:123456789123:CodePipelineNotifications:00000000-0000-0000-0000-000000000000",
"Sns": {
"Type": "Notification",
"MessageId": "00000000-0000-0000-0000-000000000000",
"TopicArn": "arn:aws:sns:us-east-1:123456789:codepipeline-alerts",
"Subject": null,
"Message": "{\"version\":\"0\",\"id\":\"00000000-0000-0000-0000-000000000000\",\"detail-type\":\"CodePipeline Pipeline Execution State Change\",\"source\":\"aws.codepipeline\",\"account\":\"123456789123\",\"time\":\"2018-02-14T07:14:14Z\",\"region\":\"us-east-1\",\"resources\":[\"arn:aws:codepipeline:us-east-1:123456789123:a-pipeline-project\"],\"detail\":{\"pipeline\":\"a-pipeline-project\",\"execution-id\":\"00000000-0000-0000-0000-000000000000\",\"state\":\"STARTED\",\"version\":1.0}}",
"Timestamp": "2018-02-14T07:14:27.208Z",
"MessageAttributes": {}
}
}
]
}
18 changes: 18 additions & 0 deletions test/sns-codepipeline-event-stage-failed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"Records": [
{
"EventSource": "aws:sns",
"EventVersion": "1.0",
"EventSubscriptionArn": "arn:aws:sns:us-east-1:123456789123:CodePipelineNotifications:00000000-0000-0000-0000-000000000000",
"Sns": {
"Type": "Notification",
"MessageId": "00000000-0000-0000-0000-000000000000",
"TopicArn": "arn:aws:sns:us-east-1:123456789:codepipeline-alerts",
"Subject": null,
"Message": "{\"version\":\"0\",\"id\":\"00000000-0000-0000-0000-000000000000\",\"detail-type\":\"CodePipeline Stage Execution State Change\",\"source\":\"aws.codepipeline\",\"account\":\"123456789123\",\"time\":\"2018-02-14T07:14:14Z\",\"region\":\"us-east-1\",\"resources\":[\"arn:aws:codepipeline:us-east-1:123456789123:a-pipeline-project\"],\"detail\":{\"pipeline\":\"a-pipeline-project\",\"execution-id\":\"00000000-0000-0000-0000-000000000000\",\"stage\":\"Build\",\"state\":\"FAILED\",\"version\":1.0}}",
"Timestamp": "2018-02-14T07:14:27.208Z",
"MessageAttributes": {}
}
}
]
}
18 changes: 18 additions & 0 deletions test/sns-codepipeline-event-stage-started.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"Records": [
{
"EventSource": "aws:sns",
"EventVersion": "1.0",
"EventSubscriptionArn": "arn:aws:sns:us-east-1:123456789123:CodePipelineNotifications:00000000-0000-0000-0000-000000000000",
"Sns": {
"Type": "Notification",
"MessageId": "00000000-0000-0000-0000-000000000000",
"TopicArn": "arn:aws:sns:us-east-1:123456789:codepipeline-alerts",
"Subject": null,
"Message": "{\"version\":\"0\",\"id\":\"00000000-0000-0000-0000-000000000000\",\"detail-type\":\"CodePipeline Stage Execution State Change\",\"source\":\"aws.codepipeline\",\"account\":\"123456789123\",\"time\":\"2018-02-14T07:14:14Z\",\"region\":\"us-east-1\",\"resources\":[\"arn:aws:codepipeline:us-east-1:123456789123:a-pipeline-project\"],\"detail\":{\"pipeline\":\"a-pipeline-project\",\"execution-id\":\"00000000-0000-0000-0000-000000000000\",\"stage\":\"Build\",\"state\":\"STARTED\",\"version\":1.0}}",
"Timestamp": "2018-02-14T07:14:27.208Z",
"MessageAttributes": {}
}
}
]
}
18 changes: 18 additions & 0 deletions test/sns-codepipeline-event-stage-succeeded.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"Records": [
{
"EventSource": "aws:sns",
"EventVersion": "1.0",
"EventSubscriptionArn": "arn:aws:sns:us-east-1:123456789123:CodePipelineNotifications:00000000-0000-0000-0000-000000000000",
"Sns": {
"Type": "Notification",
"MessageId": "00000000-0000-0000-0000-000000000000",
"TopicArn": "arn:aws:sns:us-east-1:123456789:codepipeline-alerts",
"Subject": null,
"Message": "{\"version\":\"0\",\"id\":\"00000000-0000-0000-0000-000000000000\",\"detail-type\":\"CodePipeline Stage Execution State Change\",\"source\":\"aws.codepipeline\",\"account\":\"123456789123\",\"time\":\"2018-02-14T07:14:14Z\",\"region\":\"us-east-1\",\"resources\":[\"arn:aws:codepipeline:us-east-1:123456789123:a-pipeline-project\"],\"detail\":{\"pipeline\":\"a-pipeline-project\",\"execution-id\":\"00000000-0000-0000-0000-000000000000\",\"stage\":\"Build\",\"state\":\"SUCCEEDED\",\"version\":1.0}}",
"Timestamp": "2018-02-14T07:14:27.208Z",
"MessageAttributes": {}
}
}
]
}

0 comments on commit 8260622

Please sign in to comment.