From 53c3442ba0e66e65c99ba3c40f741a3170be0a39 Mon Sep 17 00:00:00 2001 From: y-oksaku <43719835+y-oksaku@users.noreply.github.com> Date: Wed, 18 Dec 2024 15:51:57 +0900 Subject: [PATCH] feat: add message field to change default slack message (#51) --- index.js | 16 ++-- test/index.test.js | 200 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 210 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 60d1b1d..47b1352 100644 --- a/index.js +++ b/index.js @@ -45,6 +45,7 @@ const SCHEMA_SLACK_SETTINGS = Joi.object() Joi.object().keys({ channels: SCHEMA_SLACK_CHANNELS, statuses: SCHEMA_STATUSES, + message: Joi.string(), minimized: Joi.boolean() }), SCHEMA_SLACK_CHANNELS, @@ -113,6 +114,7 @@ function buildStatus(buildData, config) { buildData.settings.slack = { channels: buildData.settings.slack, statuses: DEFAULT_STATUSES, + message: '', minimized: false }; } @@ -171,15 +173,17 @@ function buildStatus(buildData, config) { message = `${message}\n*Source Directory:* ${rootDir}`; } + const defaultMessage = buildData.settings.slack.message; const metaMessage = hoek.reach(buildData, 'build.meta.notification.slack.message', { default: false }); - const metaVar = `build.meta.notification.slack.${buildData.jobName}.message`; - const buildMessage = hoek.reach(buildData, metaVar, { default: false }); + const buildMessage = hoek.reach(buildData, `build.meta.notification.slack.${buildData.jobName}.message`, { + default: false + }); // Use job specific message over generic message. - if (buildMessage) { - message = `${message}\n${buildMessage}`; - } else if (metaMessage) { - message = `${message}\n${metaMessage}`; + const specificMessage = buildMessage || metaMessage || defaultMessage || ''; + + if (specificMessage) { + message = `${message}\n${specificMessage}`; } const attachments = isMinimized diff --git a/test/index.test.js b/test/index.test.js index b9de00f..25678d1 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -1005,6 +1005,206 @@ describe('index', () => { }); }); + it('default message data overwrite.', done => { + const buildDataMockArray = { + settings: { + slack: { + channels: ['meeseeks'], + message: 'Default additional message' + } + }, + 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', + url: 'http://scmtest/org/repo/commit/123456' + }, + sha: '1234567890abcdeffedcba098765432100000000' + }, + buildLink: 'http://thisisaSDtest.com/pipelines/12/builds/1234' + }; + + const postMessagePayloadData = { + channel: 'meeseeks', + text: '*FAILURE* :umbrella: \nDefault additional message', + as_user: true, + attachments: [ + { + fallback: '', + color: 'danger', + title: '#1234', + title_link: 'http://thisisaSDtest.com/pipelines/12/builds/1234', + text: + 'fixing a bug ()\n' + + 'Merge pull request #26 from screwdriver-cd/notifications' + } + ] + }; + + serverMock.event(eventMock); + serverMock.events.on(eventMock, data => notifier.notify(eventMock, data)); + serverMock.events.emit(eventMock, buildDataMockArray); + + process.nextTick(() => { + assert.calledOnce(WebClientMock.chat.postMessage); + assert.calledWith(WebClientMock.chat.postMessage, postMessagePayloadData); + done(); + }); + }); + + it('message meta data overwrite.', done => { + const buildDataMockArray = { + settings: { + slack: { + channels: ['meeseeks'], + message: 'Default additional message' + } + }, + status: 'FAILURE', + pipeline: { + id: '123', + scmRepo: { + name: 'screwdriver-cd/notifications' + } + }, + jobName: 'publish', + build: { + id: '1234', + meta: { + notification: { + slack: { + publish: { + message: 'Additional meta message' + } + } + } + } + }, + event: { + id: '12345', + causeMessage: 'Merge pull request #26 from screwdriver-cd/notifications', + creator: { username: 'foo' }, + commit: { + author: { name: 'foo' }, + message: 'fixing a bug', + url: 'http://scmtest/org/repo/commit/123456' + }, + sha: '1234567890abcdeffedcba098765432100000000' + }, + buildLink: 'http://thisisaSDtest.com/pipelines/12/builds/1234' + }; + + const postMessagePayloadData = { + channel: 'meeseeks', + text: '*FAILURE* :umbrella: \nAdditional meta message', + as_user: true, + attachments: [ + { + fallback: '', + color: 'danger', + title: '#1234', + title_link: 'http://thisisaSDtest.com/pipelines/12/builds/1234', + text: + 'fixing a bug ()\n' + + 'Merge pull request #26 from screwdriver-cd/notifications' + } + ] + }; + + serverMock.event(eventMock); + serverMock.events.on(eventMock, data => notifier.notify(eventMock, data)); + serverMock.events.emit(eventMock, buildDataMockArray); + + process.nextTick(() => { + assert.calledOnce(WebClientMock.chat.postMessage); + assert.calledWith(WebClientMock.chat.postMessage, postMessagePayloadData); + done(); + }); + }); + + it('message meta data overwrite. should not overwrite. wrong job name', done => { + const buildDataMockArray = { + settings: { + slack: { + channels: ['meeseeks'] + } + }, + status: 'FAILURE', + pipeline: { + id: '123', + scmRepo: { + name: 'screwdriver-cd/notifications' + } + }, + jobName: 'publish', + build: { + id: '1234', + meta: { + notification: { + slack: { + wrong: { + message: 'Additional meta message' + } + } + } + } + }, + event: { + id: '12345', + causeMessage: 'Merge pull request #26 from screwdriver-cd/notifications', + creator: { username: 'foo' }, + commit: { + author: { name: 'foo' }, + message: 'fixing a bug', + url: 'http://scmtest/org/repo/commit/123456' + }, + sha: '1234567890abcdeffedcba098765432100000000' + }, + buildLink: 'http://thisisaSDtest.com/pipelines/12/builds/1234' + }; + + const postMessagePayloadData = { + channel: 'meeseeks', + text: '*FAILURE* :umbrella: ', + as_user: true, + attachments: [ + { + fallback: '', + color: 'danger', + title: '#1234', + title_link: 'http://thisisaSDtest.com/pipelines/12/builds/1234', + text: + 'fixing a bug ()\n' + + 'Merge pull request #26 from screwdriver-cd/notifications' + } + ] + }; + + serverMock.event(eventMock); + serverMock.events.on(eventMock, data => notifier.notify(eventMock, data)); + serverMock.events.emit(eventMock, buildDataMockArray); + + process.nextTick(() => { + assert.calledOnce(WebClientMock.chat.postMessage); + assert.calledWith(WebClientMock.chat.postMessage, postMessagePayloadData); + done(); + }); + }); + it('allows additional notifications plugins in buildData.settings', done => { buildDataMock.settings.hipchat = { awesome: 'sauce',