diff --git a/index.js b/index.js index 0e8bb9d..58961b2 100644 --- a/index.js +++ b/index.js @@ -147,7 +147,12 @@ class SlackNotifier extends NotificationBase { const commitMessage = buildData.event.commit.message.length > cutOff ? `${buildData.event.commit.message.substring(0, cutOff)}...` : buildData.event.commit.message; - const isMinimized = buildData.settings.slack.minimized; + + // Slack channel overwrite from meta data. Job specific only. + const metaMinimizedReplaceVar = + `build.meta.notification.slack.${buildData.jobName}.minimized`; + const isMinimized = hoek.reach(buildData, metaMinimizedReplaceVar, + { default: buildData.settings.slack.minimized }); let message = isMinimized ? // eslint-disable-next-line max-len diff --git a/test/index.test.js b/test/index.test.js index 8b410f8..694a56b 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -447,6 +447,159 @@ describe('index', () => { }); }); + it('verifies the default minimized setting is false.', (done) => { + const buildDataMockSimple = { + settings: { + slack: 'meeseeks' + }, + status: 'FAILURE', + pipeline: { + id: '123', + scmRepo: { + name: 'screwdriver-cd/notifications' + } + }, + jobName: 'publish', + build: { + id: '1234' + }, + event: { + id: '12345', + causeMessage: 'Merge pull request #26 from screwdriver-cd/notifications', + creator: { username: 'foo' }, + commit: { + author: { name: 'foo' }, + message: 'fixing a bug' + }, + sha: '1234567890abcdeffedcba098765432100000000' + }, + buildLink: 'http://thisisaSDtest.com/pipelines/12/builds/1234' + }; + + serverMock.event(eventMock); + serverMock.events.on(eventMock, data => notifier.notify(data)); + serverMock.events.emit(eventMock, buildDataMockSimple); + + process.nextTick(() => { + const resp = WebClientMock.chat.postMessage.firstCall.lastArg; + + assert.isNotNull(resp); + assert.isNotNull(resp.attachments); + assert.isArray(resp.attachments); + assert.isAtLeast(resp.attachments.length, 1); + assert.match(resp.attachments[0].text, 'Merge pull request #26'); + done(); + }); + }); + + it('verifies minimized is set to false for a specific job.', (done) => { + const buildDataMockSimple = { + settings: { + slack: 'meeseeks' + }, + status: 'FAILURE', + pipeline: { + id: '123', + scmRepo: { + name: 'screwdriver-cd/notifications' + } + }, + jobName: 'publish', + build: { + id: '1234', + meta: { + notification: { + slack: { + publish: { + minimized: false + } + } + } + } + }, + event: { + id: '12345', + causeMessage: 'Merge pull request #26 from screwdriver-cd/notifications', + creator: { username: 'foo' }, + commit: { + author: { name: 'foo' }, + message: 'fixing a bug' + }, + sha: '1234567890abcdeffedcba098765432100000000' + }, + buildLink: 'http://thisisaSDtest.com/pipelines/12/builds/1234' + }; + + serverMock.event(eventMock); + serverMock.events.on(eventMock, data => notifier.notify(data)); + serverMock.events.emit(eventMock, buildDataMockSimple); + + process.nextTick(() => { + const resp = WebClientMock.chat.postMessage.firstCall.lastArg; + + assert.isNotNull(resp); + assert.isNotNull(resp.attachments); + assert.isArray(resp.attachments); + assert.isAtLeast(resp.attachments.length, 1); + assert.match(resp.attachments[0].text, 'Merge pull request #26'); + done(); + }); + }); + + it('verifies minimized is set to true for a specific job.', (done) => { + const buildDataMockSimple = { + settings: { + slack: 'meeseeks' + }, + status: 'FAILURE', + pipeline: { + id: '123', + scmRepo: { + name: 'screwdriver-cd/notifications' + } + }, + jobName: 'publish', + build: { + id: '1234', + meta: { + notification: { + slack: { + publish: { + minimized: true + } + } + } + } + }, + event: { + id: '12345', + causeMessage: 'Merge pull request #26 from screwdriver-cd/notifications', + creator: { username: 'foo' }, + commit: { + author: { name: 'foo' }, + message: 'fixing a bug' + }, + sha: '1234567890abcdeffedcba098765432100000000' + }, + buildLink: 'http://thisisaSDtest.com/pipelines/12/builds/1234' + }; + + serverMock.event(eventMock); + serverMock.events.on(eventMock, data => notifier.notify(data)); + serverMock.events.emit(eventMock, buildDataMockSimple); + + process.nextTick(() => { + const resp = WebClientMock.chat.postMessage.firstCall.lastArg; + + assert.isNotNull(resp); + assert.isNotNull(resp.attachments); + assert.isArray(resp.attachments); + assert.isAtLeast(resp.attachments.length, 1); + assert.isNaN(resp.attachments[0].text); + done(); + }); + }); + it('channel meta data overwrite. 2 down to 1 channel', (done) => { const buildDataMockArray = { settings: {