Skip to content

Commit

Permalink
✨ Handle slack post message errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mickaelalibert authored and emeric-martineau committed Sep 15, 2023
1 parent 90e4d6d commit 010ab4a
Show file tree
Hide file tree
Showing 3 changed files with 241 additions and 103 deletions.
15 changes: 10 additions & 5 deletions build/services/slack/view-submissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ module.exports = {
const releaseType = payload.view.state.values['publish-release-type']['release-type-option'].selected_option.value;
const { hasConfigFileChanged, latestTag } = await githubService.hasConfigFileChangedSinceLatestRelease();
if (hasConfigFileChanged) {
await slackPostMessageService.postMessage(
'Changements détéctés sur le fichier config.js dans la release : liens des commits',
undefined,
'#tech-releases',
);
try {
await slackPostMessageService.postMessage(
'Changements détéctés sur le fichier config.js dans la release : liens des commits',
undefined,
'#tech-releases',
);
} catch (error) {
// We really don't want to break everything if the slack message can't be send
// Maybe we should not throw an error in slack post message in the first place ?
}
}
return openModalReleasePublicationConfirmation({ releaseType, hasConfigFileChanged, latestTag });
},
Expand Down
22 changes: 19 additions & 3 deletions common/services/slack/surfaces/messages/post-message.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,25 @@ module.exports = {
},
};

const response = await axios(options);
if (!response.data.ok) {
logger.error({ event: 'post-message', message: response.data });
try {
const response = await axios(options);
} catch (error) {
if (error.response) {
logger.error({ event: 'slack-post-message', message: error.response.data });
} else if (error.request) {
logger.error({ event: 'slack-post-message', message: error.request });
} else {
logger.error({ event: 'slack-post-message', message: error.message });
}
throw new Error('Slack error received');
}

const slackResponseSuccess = response.data.ok;

if (!slackResponseSuccess) {
const slackErrorDetail = response.data.error;

logger.error({ event: 'slack-post-message', message: slackErrorDetail });
throw new Error('Slack error received');
}

Expand Down
307 changes: 212 additions & 95 deletions test/acceptance/build/slack_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,123 +187,240 @@ describe('Acceptance | Build | Slack', function () {
},
});
});

it('returns the confirmation modal with a warning and sends a slack message to tech-releases', async function () {
// given
const tagNock = nock('https://api.github.com')
.get('/repos/github-owner/github-repository/tags')
.twice()
.reply(200, [
{
commit: {
url: 'https://api.github.com/repos/github-owner/github-repository/commits/1234',
describe('when config file has been changed', function () {
it('returns the confirmation modal with a warning and sends a slack message to tech-releases', async function () {
// given
const tagNock = nock('https://api.github.com')
.get('/repos/github-owner/github-repository/tags')
.twice()
.reply(200, [
{
commit: {
url: 'https://api.github.com/repos/github-owner/github-repository/commits/1234',
},
name: 'v6.6.6',
},
name: 'v6.6.6',
},
{
{
commit: {
url: 'https://api.github.com/repos/github-owner/github-repository/commits/456',
},
name: 'v6.6.5',
},
]);
const firstCommitNock = nock('https://api.github.com')
.get('/repos/github-owner/github-repository/commits/1234')
.reply(200, {
commit: {
url: 'https://api.github.com/repos/github-owner/github-repository/commits/456',
committer: {
date: '2021-04-14T12:40:50.326Z',
},
},
name: 'v6.6.5',
},
]);
});
const commitsNock = nock('https://api.github.com')
.filteringPath(
/since=\d{4}-\d{2}-\d{2}T\d{2}%3A\d{2}%3A\d{2}.\d{3}Z&until=\d{4}-\d{2}-\d{2}T\d{2}%3A\d{2}%3A\d{2}.\d{3}Z/g,
'since=XXXX&until=XXXX',
)
.get('/repos/github-owner/github-repository/commits?since=XXXX&until=XXXX&path=api%2Flib%2Fconfig.js')
.reply(200, [{}]);

const slackMessageNock = nock('https://slack.com')
.post('/api/chat.postMessage', {
channel: '#tech-releases',
text: 'Changements détéctés sur le fichier config.js dans la release : liens des commits',
})
.reply(200, {
ok: true,
});

const firstCommitNock = nock('https://api.github.com')
.get('/repos/github-owner/github-repository/commits/1234')
.reply(200, {
commit: {
committer: {
date: '2021-04-14T12:40:50.326Z',
const body = {
type: 'view_submission',
view: {
callback_id: 'release-type-selection',
state: {
values: {
'publish-release-type': {
'release-type-option': {
selected_option: {
value: 'major',
},
},
},
},
},
},
};

// when
const res = await server.inject({
method: 'POST',
url: '/build/slack/interactive-endpoint',
headers: createSlackWebhookSignatureHeaders(JSON.stringify(body)),
payload: body,
});

const commitsNock = nock('https://api.github.com')
.filteringPath(
/since=\d{4}-\d{2}-\d{2}T\d{2}%3A\d{2}%3A\d{2}.\d{3}Z&until=\d{4}-\d{2}-\d{2}T\d{2}%3A\d{2}%3A\d{2}.\d{3}Z/g,
'since=XXXX&until=XXXX',
)
.get('/repos/github-owner/github-repository/commits?since=XXXX&until=XXXX&path=api%2Flib%2Fconfig.js')
.reply(200, [{}]);
//then

expect(tagNock.isDone()).to.be.true;
expect(firstCommitNock.isDone()).to.be.true;
expect(commitsNock.isDone()).to.be.true;
expect(slackMessageNock.isDone()).to.be.true;

const slackMessageNock = nock('https://slack.com')
.post('/api/chat.postMessage', {
channel: '#tech-releases',
text: 'Changements détéctés sur le fichier config.js dans la release : liens des commits',
})
.reply(200, {
ok: true,
expect(res.statusCode).to.equal(200);
expect(JSON.parse(res.payload)).to.deep.equal({
response_action: 'push',
view: {
type: 'modal',
callback_id: 'release-publication-confirmation',
private_metadata: 'major',
title: {
type: 'plain_text',
text: 'Confirmation',
},
submit: {
type: 'plain_text',
text: '🚀 Go !',
},
close: {
type: 'plain_text',
text: 'Annuler',
},
blocks: [
{
type: 'section',
text: {
type: 'mrkdwn',
text: ":warning: Il y a eu des ajout(s)/suppression(s) dans le fichier <https://github.com/1024pix/pix/compare/v6.6.6...dev|*config.js*>. Pensez à vérifier que toutes les variables d'environnement sont bien à jour sur *Scalingo RECETTE*.",
},
},
{
type: 'section',
text: {
type: 'mrkdwn',
text: 'Vous vous apprêtez à publier une version *major* et la déployer en recette. Êtes-vous sûr de vous ?',
},
},
],
},
});
});

const body = {
type: 'view_submission',
view: {
callback_id: 'release-type-selection',
state: {
values: {
'publish-release-type': {
'release-type-option': {
selected_option: {
value: 'major',
it('returns the confirmation modal with a warning and ignores slack error', async function () {
// given
const tagNock = nock('https://api.github.com')
.get('/repos/github-owner/github-repository/tags')
.twice()
.reply(200, [
{
commit: {
url: 'https://api.github.com/repos/github-owner/github-repository/commits/1234',
},
name: 'v6.6.6',
},
{
commit: {
url: 'https://api.github.com/repos/github-owner/github-repository/commits/456',
},
name: 'v6.6.5',
},
]);
const firstCommitNock = nock('https://api.github.com')
.get('/repos/github-owner/github-repository/commits/1234')
.reply(200, {
commit: {
committer: {
date: '2021-04-14T12:40:50.326Z',
},
},
});
const commitsNock = nock('https://api.github.com')
.filteringPath(
/since=\d{4}-\d{2}-\d{2}T\d{2}%3A\d{2}%3A\d{2}.\d{3}Z&until=\d{4}-\d{2}-\d{2}T\d{2}%3A\d{2}%3A\d{2}.\d{3}Z/g,
'since=XXXX&until=XXXX',
)
.get('/repos/github-owner/github-repository/commits?since=XXXX&until=XXXX&path=api%2Flib%2Fconfig.js')
.reply(200, [{}]);

const slackMessageNock = nock('https://slack.com')
.post('/api/chat.postMessage', {
channel: '#tech-releases',
text: 'Changements détéctés sur le fichier config.js dans la release : liens des commits',
})
.reply(200, {
ok: false,
error: 'no_text',
});

const body = {
type: 'view_submission',
view: {
callback_id: 'release-type-selection',
state: {
values: {
'publish-release-type': {
'release-type-option': {
selected_option: {
value: 'major',
},
},
},
},
},
},
},
};
};

// when
const res = await server.inject({
method: 'POST',
url: '/build/slack/interactive-endpoint',
headers: createSlackWebhookSignatureHeaders(JSON.stringify(body)),
payload: body,
});
// when
const res = await server.inject({
method: 'POST',
url: '/build/slack/interactive-endpoint',
headers: createSlackWebhookSignatureHeaders(JSON.stringify(body)),
payload: body,
});

//then
//then

expect(tagNock.isDone()).to.be.true;
expect(firstCommitNock.isDone()).to.be.true;
expect(commitsNock.isDone()).to.be.true;
expect(slackMessageNock.isDone()).to.be.true;
expect(tagNock.isDone()).to.be.true;
expect(firstCommitNock.isDone()).to.be.true;
expect(commitsNock.isDone()).to.be.true;
expect(slackMessageNock.isDone()).to.be.true;

expect(res.statusCode).to.equal(200);
expect(JSON.parse(res.payload)).to.deep.equal({
response_action: 'push',
view: {
type: 'modal',
callback_id: 'release-publication-confirmation',
private_metadata: 'major',
title: {
type: 'plain_text',
text: 'Confirmation',
},
submit: {
type: 'plain_text',
text: '🚀 Go !',
},
close: {
type: 'plain_text',
text: 'Annuler',
},
blocks: [
{
type: 'section',
text: {
type: 'mrkdwn',
text: ":warning: Il y a eu des ajout(s)/suppression(s) dans le fichier <https://github.com/1024pix/pix/compare/v6.6.6...dev|*config.js*>. Pensez à vérifier que toutes les variables d'environnement sont bien à jour sur *Scalingo RECETTE*.",
},
expect(res.statusCode).to.equal(200);
expect(JSON.parse(res.payload)).to.deep.equal({
response_action: 'push',
view: {
type: 'modal',
callback_id: 'release-publication-confirmation',
private_metadata: 'major',
title: {
type: 'plain_text',
text: 'Confirmation',
},
{
type: 'section',
text: {
type: 'mrkdwn',
text: 'Vous vous apprêtez à publier une version *major* et la déployer en recette. Êtes-vous sûr de vous ?',
},
submit: {
type: 'plain_text',
text: '🚀 Go !',
},
],
},
close: {
type: 'plain_text',
text: 'Annuler',
},
blocks: [
{
type: 'section',
text: {
type: 'mrkdwn',
text: ":warning: Il y a eu des ajout(s)/suppression(s) dans le fichier <https://github.com/1024pix/pix/compare/v6.6.6...dev|*config.js*>. Pensez à vérifier que toutes les variables d'environnement sont bien à jour sur *Scalingo RECETTE*.",
},
},
{
type: 'section',
text: {
type: 'mrkdwn',
text: 'Vous vous apprêtez à publier une version *major* et la déployer en recette. Êtes-vous sûr de vous ?',
},
},
],
},
});
});
});
});
Expand Down

0 comments on commit 010ab4a

Please sign in to comment.