Skip to content

Commit 53c3442

Browse files
authored
feat: add message field to change default slack message (#51)
1 parent 6166b32 commit 53c3442

File tree

2 files changed

+210
-6
lines changed

2 files changed

+210
-6
lines changed

index.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ const SCHEMA_SLACK_SETTINGS = Joi.object()
4545
Joi.object().keys({
4646
channels: SCHEMA_SLACK_CHANNELS,
4747
statuses: SCHEMA_STATUSES,
48+
message: Joi.string(),
4849
minimized: Joi.boolean()
4950
}),
5051
SCHEMA_SLACK_CHANNELS,
@@ -113,6 +114,7 @@ function buildStatus(buildData, config) {
113114
buildData.settings.slack = {
114115
channels: buildData.settings.slack,
115116
statuses: DEFAULT_STATUSES,
117+
message: '',
116118
minimized: false
117119
};
118120
}
@@ -171,15 +173,17 @@ function buildStatus(buildData, config) {
171173
message = `${message}\n*Source Directory:* ${rootDir}`;
172174
}
173175

176+
const defaultMessage = buildData.settings.slack.message;
174177
const metaMessage = hoek.reach(buildData, 'build.meta.notification.slack.message', { default: false });
175-
const metaVar = `build.meta.notification.slack.${buildData.jobName}.message`;
176-
const buildMessage = hoek.reach(buildData, metaVar, { default: false });
178+
const buildMessage = hoek.reach(buildData, `build.meta.notification.slack.${buildData.jobName}.message`, {
179+
default: false
180+
});
177181

178182
// Use job specific message over generic message.
179-
if (buildMessage) {
180-
message = `${message}\n${buildMessage}`;
181-
} else if (metaMessage) {
182-
message = `${message}\n${metaMessage}`;
183+
const specificMessage = buildMessage || metaMessage || defaultMessage || '';
184+
185+
if (specificMessage) {
186+
message = `${message}\n${specificMessage}`;
183187
}
184188

185189
const attachments = isMinimized

test/index.test.js

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,206 @@ describe('index', () => {
10051005
});
10061006
});
10071007

1008+
it('default message data overwrite.', done => {
1009+
const buildDataMockArray = {
1010+
settings: {
1011+
slack: {
1012+
channels: ['meeseeks'],
1013+
message: 'Default additional message'
1014+
}
1015+
},
1016+
status: 'FAILURE',
1017+
pipeline: {
1018+
id: '123',
1019+
scmRepo: {
1020+
name: 'screwdriver-cd/notifications'
1021+
}
1022+
},
1023+
jobName: 'publish',
1024+
build: {
1025+
id: '1234'
1026+
},
1027+
event: {
1028+
id: '12345',
1029+
causeMessage: 'Merge pull request #26 from screwdriver-cd/notifications',
1030+
creator: { username: 'foo' },
1031+
commit: {
1032+
author: { name: 'foo' },
1033+
message: 'fixing a bug',
1034+
url: 'http://scmtest/org/repo/commit/123456'
1035+
},
1036+
sha: '1234567890abcdeffedcba098765432100000000'
1037+
},
1038+
buildLink: 'http://thisisaSDtest.com/pipelines/12/builds/1234'
1039+
};
1040+
1041+
const postMessagePayloadData = {
1042+
channel: 'meeseeks',
1043+
text: '*FAILURE* :umbrella: <http://thisisaSDtest.com/pipelines/12|screwdriver-cd/notifications publish>\nDefault additional message',
1044+
as_user: true,
1045+
attachments: [
1046+
{
1047+
fallback: '',
1048+
color: 'danger',
1049+
title: '#1234',
1050+
title_link: 'http://thisisaSDtest.com/pipelines/12/builds/1234',
1051+
text:
1052+
'fixing a bug (<http://scmtest/org/repo/commit/123456|123456>)\n' +
1053+
'Merge pull request #26 from screwdriver-cd/notifications'
1054+
}
1055+
]
1056+
};
1057+
1058+
serverMock.event(eventMock);
1059+
serverMock.events.on(eventMock, data => notifier.notify(eventMock, data));
1060+
serverMock.events.emit(eventMock, buildDataMockArray);
1061+
1062+
process.nextTick(() => {
1063+
assert.calledOnce(WebClientMock.chat.postMessage);
1064+
assert.calledWith(WebClientMock.chat.postMessage, postMessagePayloadData);
1065+
done();
1066+
});
1067+
});
1068+
1069+
it('message meta data overwrite.', done => {
1070+
const buildDataMockArray = {
1071+
settings: {
1072+
slack: {
1073+
channels: ['meeseeks'],
1074+
message: 'Default additional message'
1075+
}
1076+
},
1077+
status: 'FAILURE',
1078+
pipeline: {
1079+
id: '123',
1080+
scmRepo: {
1081+
name: 'screwdriver-cd/notifications'
1082+
}
1083+
},
1084+
jobName: 'publish',
1085+
build: {
1086+
id: '1234',
1087+
meta: {
1088+
notification: {
1089+
slack: {
1090+
publish: {
1091+
message: 'Additional meta message'
1092+
}
1093+
}
1094+
}
1095+
}
1096+
},
1097+
event: {
1098+
id: '12345',
1099+
causeMessage: 'Merge pull request #26 from screwdriver-cd/notifications',
1100+
creator: { username: 'foo' },
1101+
commit: {
1102+
author: { name: 'foo' },
1103+
message: 'fixing a bug',
1104+
url: 'http://scmtest/org/repo/commit/123456'
1105+
},
1106+
sha: '1234567890abcdeffedcba098765432100000000'
1107+
},
1108+
buildLink: 'http://thisisaSDtest.com/pipelines/12/builds/1234'
1109+
};
1110+
1111+
const postMessagePayloadData = {
1112+
channel: 'meeseeks',
1113+
text: '*FAILURE* :umbrella: <http://thisisaSDtest.com/pipelines/12|screwdriver-cd/notifications publish>\nAdditional meta message',
1114+
as_user: true,
1115+
attachments: [
1116+
{
1117+
fallback: '',
1118+
color: 'danger',
1119+
title: '#1234',
1120+
title_link: 'http://thisisaSDtest.com/pipelines/12/builds/1234',
1121+
text:
1122+
'fixing a bug (<http://scmtest/org/repo/commit/123456|123456>)\n' +
1123+
'Merge pull request #26 from screwdriver-cd/notifications'
1124+
}
1125+
]
1126+
};
1127+
1128+
serverMock.event(eventMock);
1129+
serverMock.events.on(eventMock, data => notifier.notify(eventMock, data));
1130+
serverMock.events.emit(eventMock, buildDataMockArray);
1131+
1132+
process.nextTick(() => {
1133+
assert.calledOnce(WebClientMock.chat.postMessage);
1134+
assert.calledWith(WebClientMock.chat.postMessage, postMessagePayloadData);
1135+
done();
1136+
});
1137+
});
1138+
1139+
it('message meta data overwrite. should not overwrite. wrong job name', done => {
1140+
const buildDataMockArray = {
1141+
settings: {
1142+
slack: {
1143+
channels: ['meeseeks']
1144+
}
1145+
},
1146+
status: 'FAILURE',
1147+
pipeline: {
1148+
id: '123',
1149+
scmRepo: {
1150+
name: 'screwdriver-cd/notifications'
1151+
}
1152+
},
1153+
jobName: 'publish',
1154+
build: {
1155+
id: '1234',
1156+
meta: {
1157+
notification: {
1158+
slack: {
1159+
wrong: {
1160+
message: 'Additional meta message'
1161+
}
1162+
}
1163+
}
1164+
}
1165+
},
1166+
event: {
1167+
id: '12345',
1168+
causeMessage: 'Merge pull request #26 from screwdriver-cd/notifications',
1169+
creator: { username: 'foo' },
1170+
commit: {
1171+
author: { name: 'foo' },
1172+
message: 'fixing a bug',
1173+
url: 'http://scmtest/org/repo/commit/123456'
1174+
},
1175+
sha: '1234567890abcdeffedcba098765432100000000'
1176+
},
1177+
buildLink: 'http://thisisaSDtest.com/pipelines/12/builds/1234'
1178+
};
1179+
1180+
const postMessagePayloadData = {
1181+
channel: 'meeseeks',
1182+
text: '*FAILURE* :umbrella: <http://thisisaSDtest.com/pipelines/12|screwdriver-cd/notifications publish>',
1183+
as_user: true,
1184+
attachments: [
1185+
{
1186+
fallback: '',
1187+
color: 'danger',
1188+
title: '#1234',
1189+
title_link: 'http://thisisaSDtest.com/pipelines/12/builds/1234',
1190+
text:
1191+
'fixing a bug (<http://scmtest/org/repo/commit/123456|123456>)\n' +
1192+
'Merge pull request #26 from screwdriver-cd/notifications'
1193+
}
1194+
]
1195+
};
1196+
1197+
serverMock.event(eventMock);
1198+
serverMock.events.on(eventMock, data => notifier.notify(eventMock, data));
1199+
serverMock.events.emit(eventMock, buildDataMockArray);
1200+
1201+
process.nextTick(() => {
1202+
assert.calledOnce(WebClientMock.chat.postMessage);
1203+
assert.calledWith(WebClientMock.chat.postMessage, postMessagePayloadData);
1204+
done();
1205+
});
1206+
});
1207+
10081208
it('allows additional notifications plugins in buildData.settings', done => {
10091209
buildDataMock.settings.hipchat = {
10101210
awesome: 'sauce',

0 commit comments

Comments
 (0)