From ad3b0af3c4f2f1f9487651b7b8b3048dc4214b24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jim=20Br=C3=A4nnlund?= Date: Thu, 14 Nov 2019 01:42:53 -0800 Subject: [PATCH] fix: Truncate release notes on "onSuccessTemplate" (#23) Fixes: #21 --- lib/success.js | 28 +++++++++++++--------------- test/test_truncate.js | 6 ++++++ 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/lib/success.js b/lib/success.js index e24e458..6f083e9 100644 --- a/lib/success.js +++ b/lib/success.js @@ -25,21 +25,25 @@ module.exports = async (pluginConfig, context) => { logger.log('Sending slack notification on success') - let slackMessage = {} - const repoPath = options.repositoryUrl.indexOf('git@github.com') !== -1 ? options.repositoryUrl.split(':')[1].replace('.git', '') : undefined const repoURL = repoPath && `https://github.com/${repoPath}` - // Override default success message - if (pluginConfig.onSuccessTemplate) { + let releaseNotes = nextRelease.notes + + if (pluginConfig.markdownReleaseNotes) { // Creating slack format from the markdown notes. - let releaseNotes = nextRelease.notes - if (pluginConfig.markdownReleaseNotes) - releaseNotes = slackifyMarkdown(releaseNotes) + releaseNotes = slackifyMarkdown(releaseNotes) + } + // truncate long messages + releaseNotes = truncate(releaseNotes, MAX_LENGTH) + + let slackMessage = {} + // Override default success message + if (pluginConfig.onSuccessTemplate) { slackMessage = template(pluginConfig.onSuccessTemplate, { package_name, npm_package_version: nextRelease.version, @@ -60,18 +64,12 @@ module.exports = async (pluginConfig, context) => { } ] - if (nextRelease.notes !== '') { - // truncate long messages - let messageText = truncate(nextRelease.notes, MAX_LENGTH) - - if (pluginConfig.markdownReleaseNotes) - messageText = slackifyMarkdown(messageText) - + if (releaseNotes !== '') { messageBlocks.push({ type: 'section', text: { type: 'mrkdwn', - text: `${messageText}` + text: `${releaseNotes}` } }) } diff --git a/test/test_truncate.js b/test/test_truncate.js index 1c3bbb0..4316e24 100644 --- a/test/test_truncate.js +++ b/test/test_truncate.js @@ -2,6 +2,12 @@ const assert = require('assert') const truncate = require('../lib/truncate') describe('test truncate', () => { + it('should not fail with empty message', () => { + const expected = '' + const actual = truncate(expected, expected.length) + assert.equal(expected, actual) + }) + it('should not truncate when length <= maxLength', () => { const expected = 'hello world' const actual = truncate(expected, expected.length)