Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion notification-code/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,16 @@ module.exports = {
const EventMessage = `${TypeMessage} ${DeploymentId} for ${Application} (${ThisEvent.State})`;
const EventMessageMarkdown = `${Type.charAt(0).toUpperCase() + Type.slice(1)} ${DeploymentId} for ${Application} (${ThisEvent.State})`;

const headerEmoji = Type === 'deployment' ? (ThisEvent.State === 'completed' ? ':white_check_mark:' : ':hourglass_flowing_sand:') : ':no_entry_sign:';
const invalidationHeaderEmoji = ThisEvent.State === 'completed'
? ':white_check_mark:'
: ThisEvent.State === 'failed'
? ':no_entry_sign:'
: ':hourglass_flowing_sand:';
const headerEmoji = Type === 'invalidation'
? invalidationHeaderEmoji
: Type === 'deployment'
? (ThisEvent.State === 'completed' ? ':white_check_mark:' : ':hourglass_flowing_sand:')
: ':no_entry_sign:';

const MessageBlocks = [
{
Expand Down
4 changes: 3 additions & 1 deletion notification-code/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ const handleCodeDeployEvent = async (event) => {
const Record = event.Records[index];
const RecordMessage = JSON.parse(Record.Sns.Message);
let DeploymentId = RecordMessage.deploymentId;
let Type = 'deployment';
let Type = RecordMessage.eventType === 'CLOUDFRONT_INVALIDATION'
? 'invalidation'
: 'deployment';
let Application = process.env.APPLICATION_NAME;

const isRollback = RecordMessage.hasOwnProperty('rollbackInformation') &&
Expand Down
45 changes: 41 additions & 4 deletions notification-code/main.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const AWSMock = require('aws-sdk-mock');
const {handler} = require("./main");
const {successfulDeployment, failedDeployment} = require("./test-cases");
const {getSlackBotToken} = require("./helpers");
const {successfulDeployment, failedDeployment, cloudfrontInvalidation} = require("./test-cases");
const {getSlackBotToken, sendMessage} = require("./helpers");

process.env.PARAM_SLACK_BOT_TOKEN = '/test-app/notifications/slack-token';
process.env.SLACK_CHANNEL = 'dl-developers';
Expand All @@ -17,10 +17,16 @@ jest.mock('./helpers', () => ({
mockDeploymentDetails[DeploymentId] = {DeploymentId, Events}
console.log(DeploymentId, Events);
}),
sendMessage: jest.fn().mockResolvedValue(undefined),
}));

beforeEach(() => {
mockDeploymentDetails = {};
jest.clearAllMocks();
});

describe('a successful deployment', function () {
beforeAll(async () => {
beforeEach(async () => {
for (let index in successfulDeployment) {
await handler(successfulDeployment[index])
}
Expand All @@ -29,10 +35,20 @@ describe('a successful deployment', function () {
test('calls getSlackBotToken', () => {
expect(getSlackBotToken).toHaveBeenCalled();
});

test('keeps existing CodeDeploy events classified as deployment events', () => {
expect(sendMessage).toHaveBeenLastCalledWith(
'TEST_SLACK_BOT_TOKEN',
'status',
'd-MainDeployment',
expect.arrayContaining([expect.objectContaining({Type: 'deployment', State: 'completed'})]),
'deployment',
);
});
});

describe('an unsuccessful deployment', function () {
beforeAll(async () => {
beforeEach(async () => {
for (let index in failedDeployment) {
await handler(failedDeployment[index])
}
Expand All @@ -42,3 +58,24 @@ describe('an unsuccessful deployment', function () {
expect(getSlackBotToken).toHaveBeenCalled();
});
});

describe('a CloudFront invalidation', function () {
beforeEach(async () => {
for (let index in cloudfrontInvalidation) {
await handler(cloudfrontInvalidation[index]);
}
});

test('adds invalidation events to the existing deployment notification', () => {
expect(sendMessage).toHaveBeenLastCalledWith(
'TEST_SLACK_BOT_TOKEN',
'status',
'd-MainDeployment',
[
expect.objectContaining({Type: 'invalidation', State: 'started'}),
expect.objectContaining({Type: 'invalidation', State: 'completed'}),
],
'invalidation',
);
});
});
23 changes: 23 additions & 0 deletions notification-code/test-cases.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
module.exports = {
cloudfrontInvalidation: [{
"Records": [
{
"EventSource": "aws:sns",
"EventVersion": "1.0",
"Sns": {
"Type": "Notification",
"Message": "{\"eventType\":\"CLOUDFRONT_INVALIDATION\",\"deploymentId\":\"d-MainDeployment\",\"region\":\"eu-west-2\",\"status\":\"CREATED\"}"
}
}
]
}, {
"Records": [
{
"EventSource": "aws:sns",
"EventVersion": "1.0",
"Sns": {
"Type": "Notification",
"Message": "{\"eventType\":\"CLOUDFRONT_INVALIDATION\",\"deploymentId\":\"d-MainDeployment\",\"region\":\"eu-west-2\",\"status\":\"SUCCEEDED\"}"
}
}
]
}],
successfulDeployment: [{
"Records": [
{
Expand Down