Skip to content

Commit

Permalink
Merge pull request #1846 from intuit/slack-validation-bug
Browse files Browse the repository at this point in the history
Expose username, iconUrl, and iconEmoji validation bug + collapse markdown lists into 1 section
  • Loading branch information
hipstersmoothie committed Mar 2, 2021
2 parents 9c92942 + 5c271f4 commit 169be95
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 32 deletions.
56 changes: 56 additions & 0 deletions plugins/slack/README.md
Expand Up @@ -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.
Expand Down
40 changes: 8 additions & 32 deletions plugins/slack/__tests__/__snapshots__/slack.test.ts.snap
Expand Up @@ -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 <https://github.com/intuit/auto/pull/1802|#1802> (<https://github.com/hipstersmoothie|@hipstersmoothie>)",
"text": "\`@auto-it/upload-assets\`
• Don't create \\"Canary Release Assets\\" during non-canary builds + Change that releases tag to valid semver <https://github.com/intuit/auto/pull/1802|#1802> (<https://github.com/hipstersmoothie|@hipstersmoothie>)",
"type": "mrkdwn",
},
"type": "section",
Expand Down Expand Up @@ -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 <https://github.com/intuit/auto/pull/1802|#1802> (<https://github.com/hipstersmoothie|@hipstersmoothie>)",
"text": "\`@auto-it/upload-assets\`
• Don't create \\"Canary Release Assets\\" during non-canary builds + Change that releases tag to valid semver <https://github.com/intuit/auto/pull/1802|#1802> (<https://github.com/hipstersmoothie|@hipstersmoothie>)",
"type": "mrkdwn",
},
"type": "section",
Expand Down Expand Up @@ -263,14 +251,8 @@ Object {
},
Object {
"text": Object {
"text": "• PR <google.com|some link>",
"type": "mrkdwn",
},
"type": "section",
},
Object {
"text": Object {
"text": " • Another note",
"text": "• PR <google.com|some link>
Another note",
"type": "mrkdwn",
},
"type": "section",
Expand Down Expand Up @@ -309,14 +291,8 @@ Object {
},
Object {
"text": Object {
"text": "• PR <google.com|some link>",
"type": "mrkdwn",
},
"type": "section",
},
Object {
"text": Object {
"text": " • Another note",
"text": "• PR <google.com|some link>
Another note",
"type": "mrkdwn",
},
"type": "section",
Expand Down
33 changes: 33 additions & 0 deletions plugins/slack/src/index.ts
Expand Up @@ -79,6 +79,7 @@ interface Block {
[params: string]: unknown;
}

const CHANGELOG_LINE = /^\s*•/;
type Messages = [Block[], ...Array<Block[] | FileUpload>];

/** Convert the sanitized markdown to slack blocks */
Expand Down Expand Up @@ -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));
}
Expand All @@ -150,6 +163,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([
Expand Down Expand Up @@ -319,6 +338,18 @@ export default class SlackPlugin implements IPlugin {
messages[0].unshift(createSectionBlock(this.options.title));
}

const userPostMessageOptions: Record<string, string> = {};

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;

Expand All @@ -331,6 +362,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,
Expand Down Expand Up @@ -366,6 +398,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],
Expand Down

0 comments on commit 169be95

Please sign in to comment.