diff --git a/index.js b/index.js index a1c6ba0..421495a 100644 --- a/index.js +++ b/index.js @@ -134,7 +134,9 @@ function buildStatus(buildData, config) { return; } - const pipelineLink = buildData.buildLink.split('/builds')[0]; + const pipelineLink = /^PR-/.test(buildData.jobName) + ? `${buildData.buildLink.split('/builds')[0]}/pulls` + : buildData.buildLink.split('/builds')[0]; const truncatedSha = buildData.event.sha.slice(0, 6); const cutOff = 150; const commitMessage = diff --git a/test/index.test.js b/test/index.test.js index 9bd11d6..64426b3 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -250,19 +250,94 @@ describe('index', () => { creator: { username: 'foo' }, commit: { author: { name: 'foo' }, - message: 'fixing a bug' + 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, buildDataMockSimple); process.nextTick(() => { - assert.calledOnce(WebClientMock.chat.postMessage); + assert.calledWith(WebClientMock.chat.postMessage, postMessagePayloadData); + done(); + }); + }); + + it('sets channels and statuses for simple slack string name for PR builds.', done => { + const buildDataMockSimple = { + settings: { + slack: 'meeseeks' + }, + status: 'FAILURE', + pipeline: { + id: '123', + scmRepo: { + name: 'screwdriver-cd/notifications' + } + }, + jobName: 'PR-1: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: ', + 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, buildDataMockSimple); + + process.nextTick(() => { + assert.calledWith(WebClientMock.chat.postMessage, postMessagePayloadData); done(); }); });