Skip to content

Commit

Permalink
Merge pull request #292 from adierkens/slack-formatting
Browse files Browse the repository at this point in the history
Fix the formatting for slack messages
  • Loading branch information
hipstersmoothie committed Feb 15, 2019
2 parents 2e7efcb + e361532 commit 97c03e4
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -36,6 +36,7 @@
"docs:publish": "./scripts/publish-docs.sh"
},
"dependencies": {
"@atomist/slack-messages": "~1.1.0",
"@hutson/set-npm-auth-token-for-ci": "^3.0.2",
"@octokit/plugin-enterprise-compatibility": "^1.0.0",
"@octokit/rest": "^16.13.1",
Expand Down
2 changes: 1 addition & 1 deletion src/utils/__tests__/__snapshots__/slack.test.ts.snap
@@ -1,3 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`postToSlack should call slack api 1`] = `"{\\"text\\":\\"@channel: New release *1.0.0*\\\\nhttps://github.custom.com/Adam Dierkens/test/releases/tag/1.0.0\\\\n\\\\n\`\`\`# My Notes\\\\n- PR some link\`\`\`\\",\\"link_names\\":1}"`;
exports[`postToSlack should call slack api 1`] = `"{\\"text\\":\\"@channel: New release *<https://github.custom.com/Adam Dierkens/test/releases/tag/1.0.0|1.0.0>*\\\\n* My Notes*\\\\n PR <google.com|some link>\\",\\"link_names\\":1}"`;
23 changes: 15 additions & 8 deletions src/utils/slack.ts
@@ -1,12 +1,17 @@
import { githubToSlack } from '@atomist/slack-messages';
import fetch from 'node-fetch';
import join from 'url-join';

const sanitizeMarkdown = (markdown: string) =>
markdown
githubToSlack(markdown)
.split('\n')
.map(line =>
line.replace(/\[([\s\S]+)\]\([\S\s]+\)/, (_, label: string) => label)
)
.map(line => {
// Strip out the ### prefix and replace it with *<word>* to make it bold
if (line.startsWith('#')) {
return `*${line.replace(/^[#]+/, '')}*`;
}
return line;
})
.join('\n');

interface IPostToSlackOptions {
Expand All @@ -21,15 +26,15 @@ export default async function postToSlack(
releaseNotes: string,
options: IPostToSlackOptions
) {
const body = `\`\`\`${sanitizeMarkdown(releaseNotes)}\`\`\``;
const body = sanitizeMarkdown(releaseNotes);
const token = process.env.SLACK_TOKEN;
const url = `${join(
const url = join(
options.baseUrl,
options.owner,
options.repo,
'releases/tag',
options.tag
)}\n`;
);

if (!token) {
return Promise.reject(new Error('Slack needs a token to send a message'));
Expand All @@ -38,7 +43,9 @@ export default async function postToSlack(
return fetch(`${options.slackUrl}?token=${token}`, {
method: 'POST',
body: JSON.stringify({
text: [`@channel: New release *${options.tag}*`, url, body].join('\n'),
text: [`@channel: New release *<${url}|${options.tag}>*`, body].join(
'\n'
),
// eslint-disable-next-line camelcase
link_names: 1
}),
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Expand Up @@ -2,6 +2,11 @@
# yarn lockfile v1


"@atomist/slack-messages@~1.1.0":
version "1.1.0"
resolved "https://registry.npmjs.org/@atomist/slack-messages/-/slack-messages-1.1.0.tgz#82f6b0dc91498726d0df2541ba01dab4a6c5775f"
integrity sha512-sai3gxAyFXVBa7A7AADdVcO8Nw0gV+bXHY9T4Ki447gqbMpAyyVj3V2qxY6wmdI9j5EcwPl0fOaBpFN3xEJTkw==

"@babel/cli@7.2.0":
version "7.2.0"
resolved "https://registry.yarnpkg.com/@babel/cli/-/cli-7.2.0.tgz#505ed8d351daee6a88918da02c046c18c8c5a24f"
Expand Down

0 comments on commit 97c03e4

Please sign in to comment.