From ac5f4888db3f16218832683d6b79bf0299391d8c Mon Sep 17 00:00:00 2001 From: Andrew Lisowski Date: Thu, 4 Feb 2021 13:12:17 -0800 Subject: [PATCH] add title option --- plugins/slack/README.md | 30 +++++++---- .../__snapshots__/slack.test.ts.snap | 2 + plugins/slack/__tests__/slack.test.ts | 50 ++++++++++++++----- plugins/slack/src/index.ts | 7 ++- 4 files changed, 65 insertions(+), 24 deletions(-) diff --git a/plugins/slack/README.md b/plugins/slack/README.md index 032e95a22..2e8bb8068 100644 --- a/plugins/slack/README.md +++ b/plugins/slack/README.md @@ -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 { @@ -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" } + ], ] } ``` diff --git a/plugins/slack/__tests__/__snapshots__/slack.test.ts.snap b/plugins/slack/__tests__/__snapshots__/slack.test.ts.snap index c8b4adc03..f35294b95 100644 --- a/plugins/slack/__tests__/__snapshots__/slack.test.ts.snap +++ b/plugins/slack/__tests__/__snapshots__/slack.test.ts.snap @@ -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 **\\\\n*My Notes*\\\\n• PR \\\\n • Another note\\",\\"link_names\\":1}"`; +exports[`postToSlack should add title 1`] = `"{\\"text\\":\\"My Cool Project\\\\n\\\\n@channel: New release **\\\\n*My Notes*\\\\n• PR \\",\\"link_names\\":1}"`; + exports[`postToSlack should call slack api 1`] = `"{\\"text\\":\\"@channel: New release **\\\\n*My Notes*\\\\n• PR \\",\\"link_names\\":1}"`; exports[`postToSlack should call slack api in env var 1`] = `"{\\"text\\":\\"@channel: New release **\\\\n*My Notes*\\\\n• PR \\",\\"link_names\\":1}"`; diff --git a/plugins/slack/__tests__/slack.test.ts b/plugins/slack/__tests__/slack.test.ts index 738578f28..4637d4293 100644 --- a/plugins/slack/__tests__/slack.test.ts +++ b/plugins/slack/__tests__/slack.test.ts @@ -208,7 +208,7 @@ describe("postToSlack", () => { await plugin.createPost( { ...mockAuto, logger } as Auto, sanitizeMarkdown("# My Notes\n- PR [some link](google.com)"), - '', + "", undefined ); @@ -222,7 +222,7 @@ describe("postToSlack", () => { await plugin.createPost( mockAuto, sanitizeMarkdown("# My Notes\n- PR [some link](google.com)"), - '**', + "**", undefined ); @@ -240,8 +240,10 @@ describe("postToSlack", () => { await plugin.createPost( mockAuto, - sanitizeMarkdown("# My Notes\n- PR [some link](google.com)\n - Another note"), - '**', + sanitizeMarkdown( + "# My Notes\n- PR [some link](google.com)\n - Another note" + ), + "**", undefined ); @@ -259,8 +261,10 @@ describe("postToSlack", () => { await plugin.createPost( mockAuto, - sanitizeMarkdown("# My Notes\n- PR [some link](google.com)\n - Another note"), - '**', + sanitizeMarkdown( + "# My Notes\n- PR [some link](google.com)\n - Another note" + ), + "**", undefined ); @@ -280,8 +284,8 @@ describe("postToSlack", () => { await plugin.createPost( mockAuto, sanitizeMarkdown("# My Notes\n- PR [some link](google.com)"), - '**', - createHttpsProxyAgent('mock-url') + "**", + createHttpsProxyAgent("mock-url") ); expect(fetchSpy).toHaveBeenCalled(); @@ -298,8 +302,10 @@ describe("postToSlack", () => { await plugin.createPost( mockAuto, - sanitizeMarkdown(`# My Notes\n\`\`\`json\n{ "foo": "bar" }\`\`\`\n- PR [some link](google.com)`), - '**', + sanitizeMarkdown( + `# My Notes\n\`\`\`json\n{ "foo": "bar" }\`\`\`\n- PR [some link](google.com)` + ), + "**", undefined ); @@ -314,8 +320,8 @@ describe("postToSlack", () => { await plugin.createPost( mockAuto, sanitizeMarkdown("# My Notes\n- PR [some link](google.com)"), - '**', - createHttpsProxyAgent('mock-url') + "**", + createHttpsProxyAgent("mock-url") ); expect(fetchSpy).toHaveBeenCalled(); @@ -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(); + }); }); diff --git a/plugins/slack/src/index.ts b/plugins/slack/src/index.ts index d426afcf6..426c6d338 100644 --- a/plugins/slack/src/index.ts +++ b/plugins/slack/src/index.ts @@ -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; @@ -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 @@ -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,