Skip to content

Commit

Permalink
Merge pull request #1777 from intuit/slack-title
Browse files Browse the repository at this point in the history
add title option
  • Loading branch information
hipstersmoothie authored Feb 4, 2021
2 parents 354318b + ac5f488 commit 9722438
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 24 deletions.
30 changes: 19 additions & 11 deletions plugins/slack/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,13 @@ To use the plugin include it in your `.autorc`.

This URL should be to you webhook. Store it in `SLACK_WEBHOOK_URL` for more security. If you require a token to post to a slack hook, make sure you have a `SLACK_TOKEN` variable available on your environment. This token will be added to eh URL as a query string parameter.

### Next
## Options

If you are using a `prerelease` branch like `next`, Slack will not post a message by default. This is done to avoid spamming your consumers every time you make a preview release. However, if you would like to configure it such that Slack _does_ post on prerelease, you can add the `publishPreRelease` to your `.autorc` like so:
### publishPreRelease

If you are using a `prerelease` branch like `next`, Slack will not post a message by default.
This is done to avoid spamming your consumers every time you make a preview release.
However, if you would like to configure it such that Slack _does_ post on prerelease, you can add the `publishPreRelease` to your `.autorc` like so:

```json
{
Expand All @@ -47,17 +51,21 @@ If you are using a `prerelease` branch like `next`, Slack will not post a messag
"slack",
{ "url": "https://url-to-your-slack-hook.com", "publishPreRelease": true }
],
// or
["slack", "https://url-to-your-slack-hook.com"],
// or
]
}
```

### title

Additional Title to add at the start of the slack message.

```json
{
"plugins": [
[
"slack",
{
"url": "https://url-to-your-slack-hook.com",
"atTarget": "here",
"publishPreRelease": true
}
]
{ "url": "https://url-to-your-slack-hook.com", "title": "My Cool Project" }
],
]
}
```
2 changes: 2 additions & 0 deletions plugins/slack/__tests__/__snapshots__/slack.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ exports[`postToSlack should add more indents to nested lists - 2 spaces 1`] = `"

exports[`postToSlack should add more indents to nested lists 1`] = `"{\\"text\\":\\"@channel: New release *<https://git.hub/some/project/releases/v1.0.0|v1.0.0>*\\\\n*My Notes*\\\\n• PR <google.com|some link>\\\\n • Another note\\",\\"link_names\\":1}"`;

exports[`postToSlack should add title 1`] = `"{\\"text\\":\\"My Cool Project\\\\n\\\\n@channel: New release *<https://git.hub/some/project/releases/v1.0.0|v1.0.0>*\\\\n*My Notes*\\\\n• PR <google.com|some link>\\",\\"link_names\\":1}"`;

exports[`postToSlack should call slack api 1`] = `"{\\"text\\":\\"@channel: New release *<https://git.hub/some/project/releases/v1.0.0|v1.0.0>*\\\\n*My Notes*\\\\n• PR <google.com|some link>\\",\\"link_names\\":1}"`;

exports[`postToSlack should call slack api in env var 1`] = `"{\\"text\\":\\"@channel: New release *<https://git.hub/some/project/releases/v1.0.0|v1.0.0>*\\\\n*My Notes*\\\\n• PR <google.com|some link>\\",\\"link_names\\":1}"`;
Expand Down
50 changes: 38 additions & 12 deletions plugins/slack/__tests__/slack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ describe("postToSlack", () => {
await plugin.createPost(
{ ...mockAuto, logger } as Auto,
sanitizeMarkdown("# My Notes\n- PR [some link](google.com)"),
'',
"",
undefined
);

Expand All @@ -222,7 +222,7 @@ describe("postToSlack", () => {
await plugin.createPost(
mockAuto,
sanitizeMarkdown("# My Notes\n- PR [some link](google.com)"),
'*<https://git.hub/some/project/releases/v1.0.0|v1.0.0>*',
"*<https://git.hub/some/project/releases/v1.0.0|v1.0.0>*",
undefined
);

Expand All @@ -240,8 +240,10 @@ describe("postToSlack", () => {

await plugin.createPost(
mockAuto,
sanitizeMarkdown("# My Notes\n- PR [some link](google.com)\n - Another note"),
'*<https://git.hub/some/project/releases/v1.0.0|v1.0.0>*',
sanitizeMarkdown(
"# My Notes\n- PR [some link](google.com)\n - Another note"
),
"*<https://git.hub/some/project/releases/v1.0.0|v1.0.0>*",
undefined
);

Expand All @@ -259,8 +261,10 @@ describe("postToSlack", () => {

await plugin.createPost(
mockAuto,
sanitizeMarkdown("# My Notes\n- PR [some link](google.com)\n - Another note"),
'*<https://git.hub/some/project/releases/v1.0.0|v1.0.0>*',
sanitizeMarkdown(
"# My Notes\n- PR [some link](google.com)\n - Another note"
),
"*<https://git.hub/some/project/releases/v1.0.0|v1.0.0>*",
undefined
);

Expand All @@ -280,8 +284,8 @@ describe("postToSlack", () => {
await plugin.createPost(
mockAuto,
sanitizeMarkdown("# My Notes\n- PR [some link](google.com)"),
'*<https://git.hub/some/project/releases/v1.0.0|v1.0.0>*',
createHttpsProxyAgent('mock-url')
"*<https://git.hub/some/project/releases/v1.0.0|v1.0.0>*",
createHttpsProxyAgent("mock-url")
);

expect(fetchSpy).toHaveBeenCalled();
Expand All @@ -298,8 +302,10 @@ describe("postToSlack", () => {

await plugin.createPost(
mockAuto,
sanitizeMarkdown(`# My Notes\n\`\`\`json\n{ "foo": "bar" }\`\`\`\n- PR [some link](google.com)`),
'*<https://git.hub/some/project/releases/v1.0.0|v1.0.0>*',
sanitizeMarkdown(
`# My Notes\n\`\`\`json\n{ "foo": "bar" }\`\`\`\n- PR [some link](google.com)`
),
"*<https://git.hub/some/project/releases/v1.0.0|v1.0.0>*",
undefined
);

Expand All @@ -314,8 +320,8 @@ describe("postToSlack", () => {
await plugin.createPost(
mockAuto,
sanitizeMarkdown("# My Notes\n- PR [some link](google.com)"),
'*<https://git.hub/some/project/releases/v1.0.0|v1.0.0>*',
createHttpsProxyAgent('mock-url')
"*<https://git.hub/some/project/releases/v1.0.0|v1.0.0>*",
createHttpsProxyAgent("mock-url")
);

expect(fetchSpy).toHaveBeenCalled();
Expand Down Expand Up @@ -394,4 +400,24 @@ describe("postToSlack", () => {
expect(fetchSpy.mock.calls[0][0]).toBe("https://foo.bar?token=MY_TOKEN");
expect(fetchSpy.mock.calls[0][1].body).toMatchSnapshot();
});

test("should add title", async () => {
process.env.SLACK_WEBHOOK_URL = "https://foo.bar";
const plugin = new SlackPlugin({ title: "My Cool Project" });
const hooks = makeHooks();
process.env.SLACK_TOKEN = "MY_TOKEN";
plugin.apply({ hooks, options: {}, ...mockAuto } as Auto);

await hooks.afterRelease.promise({
newVersion: "1.0.0",
lastRelease: "0.1.0",
commits: [makeCommitFromMsg("a patch")],
releaseNotes: "# My Notes\n- PR [some link](google.com)",
// @ts-ignore
response: mockResponse,
});

expect(fetchSpy).toHaveBeenCalled();
expect(fetchSpy.mock.calls[0][1].body).toMatchSnapshot();
});
});
7 changes: 6 additions & 1 deletion plugins/slack/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ const pluginOptions = t.partial({
atTarget: t.string,
/** Allow users to opt into having prereleases posted to slack */
publishPreRelease: t.boolean,
/** Additional Title to add at the start of the slack message */
title: t.string,
});

export type ISlackPluginOptions = t.TypeOf<typeof pluginOptions>;
Expand All @@ -62,6 +64,7 @@ export default class SlackPlugin implements IPlugin {
this.options = { url: options, atTarget: "channel" };
} else {
this.options = {
...options,
url: process.env.SLACK_WEBHOOK_URL || options.url || "",
atTarget: options.atTarget ? options.atTarget : "channel",
publishPreRelease: options.publishPreRelease
Expand Down Expand Up @@ -178,7 +181,9 @@ export default class SlackPlugin implements IPlugin {
method: "POST",
body: JSON.stringify({
text: [
`@${this.options.atTarget}: New release ${releaseUrl}`,
`${this.options.title ? `${this.options.title}\n\n` : ""}@${
this.options.atTarget
}: New release ${releaseUrl}`,
releaseNotes,
].join("\n"),
link_names: 1,
Expand Down

0 comments on commit 9722438

Please sign in to comment.