From b8df59fe519a6160821f9e5e6e10b765a779453c Mon Sep 17 00:00:00 2001 From: Andrew Lisowski Date: Tue, 2 Mar 2021 13:35:40 -0800 Subject: [PATCH 1/2] expose username, iconUrl, and iconEmoji --- plugins/slack/README.md | 56 ++++++++++++++++++++++++++++++++++++++ plugins/slack/src/index.ts | 20 ++++++++++++++ 2 files changed, 76 insertions(+) diff --git a/plugins/slack/README.md b/plugins/slack/README.md index ddb535d3f..9074d85cf 100644 --- a/plugins/slack/README.md +++ b/plugins/slack/README.md @@ -135,6 +135,62 @@ Additional Title to add at the start of the slack message. } ``` +### username + +Username to post the message as. + +```json +{ + "plugins": [ + [ + "slack", + { + "url": "https://url-to-your-slack-hook.com", + "username": "My Project" + } + ] + ] +} +``` + +### iconUrl + +Image url to use as the message's avatar. + +```json +{ + "plugins": [ + [ + "slack", + { + "url": "https://url-to-your-slack-hook.com", + "iconUrl": "http://lorempixel.com/48/48" + } + ] + ] +} +``` + +> NOTE: If both `iconUrl` and `iconEmoji` are specified only `iconUrl` will be respected + +### iconEmoji + +Emoji code to use as the message's avatar. + +```json +{ + "plugins": [ + [ + "slack", + { + "url": "https://url-to-your-slack-hook.com", + "iconEmoji": ":chart_with_upwards_trend:" + } + ] + ] +} +``` + ### channels (App Auth Only) Channel, private group, or IM channel to send message to. diff --git a/plugins/slack/src/index.ts b/plugins/slack/src/index.ts index 509ef6606..efd858c69 100644 --- a/plugins/slack/src/index.ts +++ b/plugins/slack/src/index.ts @@ -150,6 +150,12 @@ const basePluginOptions = t.partial({ publishPreRelease: t.boolean, /** Additional Title to add at the start of the slack message */ title: t.string, + /** Username to post the message as */ + username: t.string, + /** Image url to use as the message's avatar */ + iconUrl: t.string, + /** Emoji code to use as the message's avatar */ + iconEmoji: t.string, }); const appPluginOptions = t.intersection([ @@ -319,6 +325,18 @@ export default class SlackPlugin implements IPlugin { messages[0].unshift(createSectionBlock(this.options.title)); } + const userPostMessageOptions: Record = {}; + + if (this.options.username) { + userPostMessageOptions.username = this.options.username; + } + + if (this.options.iconUrl) { + userPostMessageOptions.icon_url = this.options.iconUrl; + } else if (this.options.iconEmoji) { + userPostMessageOptions.icon_emoji = this.options.iconEmoji; + } + if ("auth" in this.options) { const channels = this.options.channels; @@ -331,6 +349,7 @@ export default class SlackPlugin implements IPlugin { await fetch("https://slack.com/api/chat.postMessage", { method: "POST", body: JSON.stringify({ + ...userPostMessageOptions, channel, blocks: message, link_names: true, @@ -366,6 +385,7 @@ export default class SlackPlugin implements IPlugin { await fetch(`${this.options.url}${token ? `?token=${token}` : ""}`, { method: "POST", body: JSON.stringify({ + ...userPostMessageOptions, link_names: true, // If not in app auth only one message is constructed blocks: messages[0], From 5c271f448c3e7d8bcbd2ab6e65a1e3a2dfcbb5f0 Mon Sep 17 00:00:00 2001 From: Andrew Lisowski Date: Tue, 2 Mar 2021 14:19:18 -0800 Subject: [PATCH 2/2] render lists in one markdown section --- .../__snapshots__/slack.test.ts.snap | 40 ++++--------------- plugins/slack/src/index.ts | 13 ++++++ 2 files changed, 21 insertions(+), 32 deletions(-) diff --git a/plugins/slack/__tests__/__snapshots__/slack.test.ts.snap b/plugins/slack/__tests__/__snapshots__/slack.test.ts.snap index e37de859a..afd0d24a9 100644 --- a/plugins/slack/__tests__/__snapshots__/slack.test.ts.snap +++ b/plugins/slack/__tests__/__snapshots__/slack.test.ts.snap @@ -43,14 +43,8 @@ Array [ }, Object { "text": Object { - "text": "• \`@auto-it/upload-assets\`", - "type": "mrkdwn", - }, - "type": "section", - }, - Object { - "text": Object { - "text": " • Don't create \\"Canary Release Assets\\" during non-canary builds + Change that releases tag to valid semver ()", + "text": "• \`@auto-it/upload-assets\` + • Don't create \\"Canary Release Assets\\" during non-canary builds + Change that releases tag to valid semver ()", "type": "mrkdwn", }, "type": "section", @@ -141,14 +135,8 @@ Array [ }, Object { "text": Object { - "text": "• \`@auto-it/upload-assets\`", - "type": "mrkdwn", - }, - "type": "section", - }, - Object { - "text": Object { - "text": " • Don't create \\"Canary Release Assets\\" during non-canary builds + Change that releases tag to valid semver ()", + "text": "• \`@auto-it/upload-assets\` + • Don't create \\"Canary Release Assets\\" during non-canary builds + Change that releases tag to valid semver ()", "type": "mrkdwn", }, "type": "section", @@ -263,14 +251,8 @@ Object { }, Object { "text": Object { - "text": "• PR ", - "type": "mrkdwn", - }, - "type": "section", - }, - Object { - "text": Object { - "text": " • Another note", + "text": "• PR + • Another note", "type": "mrkdwn", }, "type": "section", @@ -309,14 +291,8 @@ Object { }, Object { "text": Object { - "text": "• PR ", - "type": "mrkdwn", - }, - "type": "section", - }, - Object { - "text": Object { - "text": " • Another note", + "text": "• PR + • Another note", "type": "mrkdwn", }, "type": "section", diff --git a/plugins/slack/src/index.ts b/plugins/slack/src/index.ts index efd858c69..9065d0a56 100644 --- a/plugins/slack/src/index.ts +++ b/plugins/slack/src/index.ts @@ -79,6 +79,7 @@ interface Block { [params: string]: unknown; } +const CHANGELOG_LINE = /^\s*•/; type Messages = [Block[], ...Array]; /** Convert the sanitized markdown to slack blocks */ @@ -131,6 +132,18 @@ export function convertToBlocks( currentMessage.push(createContextBlock(authorLine)); } } + } else if (line.match(CHANGELOG_LINE)) { + const lines: string[] = [line]; + + for (const changelogLine of lineIterator) { + if (!changelogLine.match(CHANGELOG_LINE)) { + break; + } + + lines.push(changelogLine); + } + + currentMessage.push(createSectionBlock(lines.join("\n"))); } else if (line) { currentMessage.push(createSectionBlock(line)); }