Skip to content

Commit

Permalink
fix: Allow setting minimized from within Job (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
jweingarten authored Nov 26, 2020
1 parent ad3e900 commit 89bfe87
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 1 deletion.
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
153 changes: 153 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down

0 comments on commit 89bfe87

Please sign in to comment.