Skip to content

Commit 8260622

Browse files
authored
Merge pull request #27 from ts33/feature/format-codepipeline-notifications
Feature/format codepipeline notifications
2 parents 7961738 + 5e9c067 commit 8260622

7 files changed

+156
-6
lines changed

Makefile

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,21 @@ deps:
1515
.PHONY: test
1616
test:
1717
@test -s $(CONFIG_FILE) || { echo "No lambda config file. Update deploy.env.example and copy it to deploy.env"; exit 1; }
18-
AWS_REGION=$(AWS_REGION) $(LAMBDA_TEST) --configFile=$(CONFIG_FILE) run -x test/context.json -j test/sns-cloudwatch-event.json
18+
AWS_REGION=$(AWS_REGION) $(LAMBDA_TEST) --configFile=$(CONFIG_FILE) run -x test/context.json -j test/sns-codedeploy-event.json
19+
20+
.PHONY: test-codepipeline
21+
test-codepipeline:
22+
@test -s $(CONFIG_FILE) || { echo "No lambda config file. Update deploy.env.example and copy it to deploy.env"; exit 1; }
23+
AWS_REGION=$(AWS_REGION) $(LAMBDA_TEST) --configFile=$(CONFIG_FILE) run -x test/context.json -j test/sns-codepipeline-event-pipeline-started.json
24+
AWS_REGION=$(AWS_REGION) $(LAMBDA_TEST) --configFile=$(CONFIG_FILE) run -x test/context.json -j test/sns-codepipeline-event-stage-started.json
25+
AWS_REGION=$(AWS_REGION) $(LAMBDA_TEST) --configFile=$(CONFIG_FILE) run -x test/context.json -j test/sns-codepipeline-event-stage-succeeded.json
26+
AWS_REGION=$(AWS_REGION) $(LAMBDA_TEST) --configFile=$(CONFIG_FILE) run -x test/context.json -j test/sns-codepipeline-event-stage-failed.json
27+
1928

2029
.PHONY: test-all
21-
test-all: test
30+
test-all: test test-codepipeline
31+
AWS_REGION=$(AWS_REGION) $(LAMBDA_TEST) --configFile=$(CONFIG_FILE) run -x test/context.json -j test/sns-cloudwatch-event.json
32+
AWS_REGION=$(AWS_REGION) $(LAMBDA_TEST) --configFile=$(CONFIG_FILE) run -x test/context.json -j test/sns-codepipeline-event.json
2233
AWS_REGION=$(AWS_REGION) $(LAMBDA_TEST) --configFile=$(CONFIG_FILE) run -x test/context.json -j test/sns-event.json
2334
AWS_REGION=$(AWS_REGION) $(LAMBDA_TEST) --configFile=$(CONFIG_FILE) run -x test/context.json -j test/sns-elastic-beanstalk-event.json
2435
AWS_REGION=$(AWS_REGION) $(LAMBDA_TEST) --configFile=$(CONFIG_FILE) run -x test/context.json -j test/sns-codedeploy-event.json
@@ -39,4 +50,4 @@ deploy:
3950
--secretKey $(AWS_ACCESS_KEY_SECRET) \
4051
--region $(AWS_REGION) \
4152
--configFile $(CONFIG_FILE) \
42-
--profile $(AWS_PROFILE)
53+
--profile $(AWS_PROFILE)

config.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ module.exports = {
1717
// text in the sns message or topicname to match on to process this service type
1818
match_text: "CloudWatchNotifications"
1919
},
20+
codepipeline: {
21+
// text in the sns message or topicname to match on to process this service type
22+
match_text: "CodePipelineNotifications"
23+
},
2024
codedeploy: {
2125
// text in the sns message or topicname to match on to process this service type
2226
match_text: "CodeDeploy"
@@ -31,4 +35,4 @@ module.exports = {
3135
}
3236
}
3337

34-
}
38+
}

index.js

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,65 @@ var handleCodeDeploy = function(event, context) {
144144
return _.merge(slackMessage, baseSlackMessage);
145145
};
146146

147+
var handleCodePipeline = function(event, context) {
148+
var subject = "AWS CodePipeline Notification";
149+
var timestamp = (new Date(event.Records[0].Sns.Timestamp)).getTime()/1000;
150+
var snsSubject = event.Records[0].Sns.Subject;
151+
var message;
152+
var fields = [];
153+
var color = "warning";
154+
var changeType = "";
155+
156+
try {
157+
message = JSON.parse(event.Records[0].Sns.Message);
158+
detailType = message['detail-type'];
159+
160+
if(detailType === "CodePipeline Pipeline Execution State Change"){
161+
changeType = "";
162+
} else if(detailType === "CodePipeline Stage Execution State Change"){
163+
changeType = "STAGE " + message.detail.stage;
164+
} else if(detailType === "CodePipeline Action Execution State Change"){
165+
changeType = "ACTION";
166+
}
167+
168+
if(message.detail.state === "SUCCEEDED"){
169+
color = "good";
170+
} else if(message.detail.state === "FAILED"){
171+
color = "danger";
172+
}
173+
header = message.detail.state + ": CodePipeline " + changeType;
174+
fields.push({ "title": "Message", "value": header, "short": false });
175+
fields.push({ "title": "Pipeline", "value": message.detail.pipeline, "short": true });
176+
fields.push({ "title": "Region", "value": message.region, "short": true });
177+
fields.push({
178+
"title": "Status Link",
179+
"value": "https://console.aws.amazon.com/codepipeline/home?region=" + message.region + "#/view/" + message.detail.pipeline,
180+
"short": false
181+
});
182+
}
183+
catch(e) {
184+
color = "good";
185+
message = event.Records[0].Sns.Message;
186+
header = message.detail.state + ": CodePipeline " + message.detail.pipeline;
187+
fields.push({ "title": "Message", "value": header, "short": false });
188+
fields.push({ "title": "Detail", "value": message, "short": false });
189+
}
190+
191+
192+
var slackMessage = {
193+
text: "*" + subject + "*",
194+
attachments: [
195+
{
196+
"color": color,
197+
"fields": fields,
198+
"ts": timestamp
199+
}
200+
]
201+
};
202+
203+
return _.merge(slackMessage, baseSlackMessage);
204+
};
205+
147206
var handleElasticache = function(event, context) {
148207
var subject = "AWS ElastiCache Notification"
149208
var message = JSON.parse(event.Records[0].Sns.Message);
@@ -311,7 +370,11 @@ var processEvent = function(event, context) {
311370
var eventSnsSubject = event.Records[0].Sns.Subject || 'no subject';
312371
var eventSnsMessage = event.Records[0].Sns.Message;
313372

314-
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){
373+
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){
374+
console.log("processing codepipeline notification");
375+
slackMessage = handleCodePipeline(event,context)
376+
}
377+
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){
315378
console.log("processing elasticbeanstalk notification");
316379
slackMessage = handleElasticBeanstalk(event,context)
317380
}
@@ -373,4 +436,4 @@ exports.handler = function(event, context) {
373436
} else {
374437
context.fail('hook url has not been set.');
375438
}
376-
};
439+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"Records": [
3+
{
4+
"EventSource": "aws:sns",
5+
"EventVersion": "1.0",
6+
"EventSubscriptionArn": "arn:aws:sns:us-east-1:123456789123:CodePipelineNotifications:00000000-0000-0000-0000-000000000000",
7+
"Sns": {
8+
"Type": "Notification",
9+
"MessageId": "00000000-0000-0000-0000-000000000000",
10+
"TopicArn": "arn:aws:sns:us-east-1:123456789:codepipeline-alerts",
11+
"Subject": null,
12+
"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}}",
13+
"Timestamp": "2018-02-14T07:14:27.208Z",
14+
"MessageAttributes": {}
15+
}
16+
}
17+
]
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"Records": [
3+
{
4+
"EventSource": "aws:sns",
5+
"EventVersion": "1.0",
6+
"EventSubscriptionArn": "arn:aws:sns:us-east-1:123456789123:CodePipelineNotifications:00000000-0000-0000-0000-000000000000",
7+
"Sns": {
8+
"Type": "Notification",
9+
"MessageId": "00000000-0000-0000-0000-000000000000",
10+
"TopicArn": "arn:aws:sns:us-east-1:123456789:codepipeline-alerts",
11+
"Subject": null,
12+
"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}}",
13+
"Timestamp": "2018-02-14T07:14:27.208Z",
14+
"MessageAttributes": {}
15+
}
16+
}
17+
]
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"Records": [
3+
{
4+
"EventSource": "aws:sns",
5+
"EventVersion": "1.0",
6+
"EventSubscriptionArn": "arn:aws:sns:us-east-1:123456789123:CodePipelineNotifications:00000000-0000-0000-0000-000000000000",
7+
"Sns": {
8+
"Type": "Notification",
9+
"MessageId": "00000000-0000-0000-0000-000000000000",
10+
"TopicArn": "arn:aws:sns:us-east-1:123456789:codepipeline-alerts",
11+
"Subject": null,
12+
"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}}",
13+
"Timestamp": "2018-02-14T07:14:27.208Z",
14+
"MessageAttributes": {}
15+
}
16+
}
17+
]
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"Records": [
3+
{
4+
"EventSource": "aws:sns",
5+
"EventVersion": "1.0",
6+
"EventSubscriptionArn": "arn:aws:sns:us-east-1:123456789123:CodePipelineNotifications:00000000-0000-0000-0000-000000000000",
7+
"Sns": {
8+
"Type": "Notification",
9+
"MessageId": "00000000-0000-0000-0000-000000000000",
10+
"TopicArn": "arn:aws:sns:us-east-1:123456789:codepipeline-alerts",
11+
"Subject": null,
12+
"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}}",
13+
"Timestamp": "2018-02-14T07:14:27.208Z",
14+
"MessageAttributes": {}
15+
}
16+
}
17+
]
18+
}

0 commit comments

Comments
 (0)