Skip to content

Commit

Permalink
feat: add message field to change default slack message (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
y-oksaku authored Dec 18, 2024
1 parent 6166b32 commit 53c3442
Show file tree
Hide file tree
Showing 2 changed files with 210 additions and 6 deletions.
16 changes: 10 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -113,6 +114,7 @@ function buildStatus(buildData, config) {
buildData.settings.slack = {
channels: buildData.settings.slack,
statuses: DEFAULT_STATUSES,
message: '',
minimized: false
};
}
Expand Down Expand Up @@ -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
Expand Down
200 changes: 200 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: <http://thisisaSDtest.com/pipelines/12|screwdriver-cd/notifications publish>\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 (<http://scmtest/org/repo/commit/123456|123456>)\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: <http://thisisaSDtest.com/pipelines/12|screwdriver-cd/notifications publish>\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 (<http://scmtest/org/repo/commit/123456|123456>)\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: <http://thisisaSDtest.com/pipelines/12|screwdriver-cd/notifications publish>',
as_user: true,
attachments: [
{
fallback: '',
color: 'danger',
title: '#1234',
title_link: 'http://thisisaSDtest.com/pipelines/12/builds/1234',
text:
'fixing a bug (<http://scmtest/org/repo/commit/123456|123456>)\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',
Expand Down

0 comments on commit 53c3442

Please sign in to comment.