From 291afb57afeb6597a457b9f616a0d18502525757 Mon Sep 17 00:00:00 2001 From: Vincent Briglia Date: Thu, 25 Mar 2021 18:18:08 +0100 Subject: [PATCH 01/10] Remove githubtoslack and use simple json serialization instead icrosoft Teams accepts markdown, as long as the content was escaped/serialized to a JSON string first --- plugins/microsoft-teams/package.json | 2 +- plugins/microsoft-teams/src/index.ts | 29 +++------------------------- yarn.lock | 5 +++++ 3 files changed, 9 insertions(+), 27 deletions(-) diff --git a/plugins/microsoft-teams/package.json b/plugins/microsoft-teams/package.json index 1dbb6358f..f8294eb0a 100644 --- a/plugins/microsoft-teams/package.json +++ b/plugins/microsoft-teams/package.json @@ -37,11 +37,11 @@ "test": "jest --maxWorkers=2 --config ../../package.json" }, "dependencies": { - "@atomist/slack-messages": "~1.2.0", "@auto-it/core": "link:../../packages/core", "fp-ts": "^2.5.3", "https-proxy-agent": "^5.0.0", "io-ts": "^2.1.2", + "jsesc": "^3.0.2", "node-fetch": "2.6.1", "tslib": "2.1.0" } diff --git a/plugins/microsoft-teams/src/index.ts b/plugins/microsoft-teams/src/index.ts index 9bb854d59..0127fee52 100644 --- a/plugins/microsoft-teams/src/index.ts +++ b/plugins/microsoft-teams/src/index.ts @@ -1,4 +1,4 @@ -import { githubToSlack } from "@atomist/slack-messages"; +import { jsesc } from "jsesc"; import createHttpsProxyAgent, { HttpsProxyAgent } from "https-proxy-agent"; import { @@ -10,31 +10,8 @@ import { import fetch from "node-fetch"; import * as t from "io-ts"; -const MARKDOWN_LANGUAGE = /^(```)(\S+)$/m; - -/** Transform markdown into slack friendly text */ -export const sanitizeMarkdown = (markdown: string) => - githubToSlack(markdown) - .split("\n") - .map((line) => { - // Strip out the ### prefix and replace it with ** to make it bold - if (line.startsWith("#")) { - return `*${line.replace(/^[#]+/, "")}*`; - } - - // Give extra padding to nested lists - if (line.match(/^\s+•/)) { - return line.replace(/^\s+•/, " •"); - } - - // Strip markdown code block type. Slack does not render them correctly. - if (line.match(MARKDOWN_LANGUAGE)) { - return line.replace(MARKDOWN_LANGUAGE, "`$2`:\n\n$1"); - } - - return line; - }) - .join("\n"); +/** Microsoft Teams accepts markdown, as long as the content was escaped/serialized to a JSON string first */ +export const sanitizeMarkdown = (markdown: string) => jsesc(markdown); const pluginOptions = t.partial({ /** URL of the mircosoft teams to post to */ diff --git a/yarn.lock b/yarn.lock index cf76d4a18..c9dbb6922 100644 --- a/yarn.lock +++ b/yarn.lock @@ -9627,6 +9627,11 @@ jsesc@^2.5.1: resolved "https://registry.yarnpkg.com/jsesc/-/jsesc-2.5.2.tgz#80564d2e483dacf6e8ef209650a67df3f0c283a4" integrity sha512-OYu7XEzjkCQ3C5Ps3QIZsQfNpqoJyZZA99wd9aWd05NCtC5pWOkShK2mkL6HXQR6/Cy2lbNdPlZBpuQHXE63gA== +jsesc@^3.0.2: + version "3.0.2" + resolved "https://registry.yarnpkg.com/jsesc/-/jsesc-3.0.2.tgz#bb8b09a6597ba426425f2e4a07245c3d00b9343e" + integrity sha512-xKqzzWXDttJuOcawBt4KnKHHIf5oQ/Cxax+0PWFG+DFDgHNAdi+TXECADI+RYiFUMmx8792xsMbbgXj4CwnP4g== + jsesc@~0.5.0: version "0.5.0" resolved "https://registry.yarnpkg.com/jsesc/-/jsesc-0.5.0.tgz#e7dee66e35d6fc16f710fe91d5cf69f70f08911d" From e26123f2f9d4f25ce7b5f09919fde273a4e01f43 Mon Sep 17 00:00:00 2001 From: Vincent Briglia Date: Thu, 25 Mar 2021 18:18:40 +0100 Subject: [PATCH 02/10] remove default "channel" as it gets checked further down below. Mentions don't actually work when posted through the teams/office api --- plugins/microsoft-teams/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/microsoft-teams/src/index.ts b/plugins/microsoft-teams/src/index.ts index 0127fee52..9712ebf1f 100644 --- a/plugins/microsoft-teams/src/index.ts +++ b/plugins/microsoft-teams/src/index.ts @@ -42,7 +42,7 @@ export default class MicrosoftTeamsPlugin { this.options = { ...options, url: process.env.MICROSOFT_TEAMS_WEBHOOK_URL || options.url || "", - atTarget: options.atTarget ? options.atTarget : "channel", + atTarget: options.atTarget, publishPreRelease: options.publishPreRelease ? options.publishPreRelease : false, From c636e956700e2118b13b35f2fac6fe654d96d148 Mon Sep 17 00:00:00 2001 From: Vincent Briglia Date: Thu, 25 Mar 2021 18:20:09 +0100 Subject: [PATCH 03/10] add an action button for people to click on instead of baking in the URL at the top --- plugins/microsoft-teams/src/index.ts | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/plugins/microsoft-teams/src/index.ts b/plugins/microsoft-teams/src/index.ts index 9712ebf1f..7b8b2ad66 100644 --- a/plugins/microsoft-teams/src/index.ts +++ b/plugins/microsoft-teams/src/index.ts @@ -154,7 +154,19 @@ export default class MicrosoftTeamsPlugin { body: JSON.stringify({ "@context": "http://schema.org/extensions", "@type": "MessageCard", - text: [`${atTarget}New release ${releaseUrl}`, releaseNotes].join("\n"), + text: releaseNotes, + potentialAction: [ + { + "@type": "OpenUri", + name: "Learn More", + targets: [ + { + os: "default", + uri: releaseUrl, + }, + ], + }, + ], }), headers: { "Content-Type": "application/json" }, agent, From 302383f7fa0bd507a9e96df90033696dbb60f532 Mon Sep 17 00:00:00 2001 From: Vincent Briglia Date: Thu, 25 Mar 2021 18:41:59 +0100 Subject: [PATCH 04/10] add @types/jsesc --- plugins/microsoft-teams/package.json | 3 +++ plugins/microsoft-teams/src/index.ts | 4 +++- yarn.lock | 20 ++++++++++---------- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/plugins/microsoft-teams/package.json b/plugins/microsoft-teams/package.json index f8294eb0a..9c8abc9c6 100644 --- a/plugins/microsoft-teams/package.json +++ b/plugins/microsoft-teams/package.json @@ -44,5 +44,8 @@ "jsesc": "^3.0.2", "node-fetch": "2.6.1", "tslib": "2.1.0" + }, + "devDependencies": { + "@types/jsesc": "^2.5.1" } } diff --git a/plugins/microsoft-teams/src/index.ts b/plugins/microsoft-teams/src/index.ts index 7b8b2ad66..31ee050c0 100644 --- a/plugins/microsoft-teams/src/index.ts +++ b/plugins/microsoft-teams/src/index.ts @@ -147,7 +147,9 @@ export default class MicrosoftTeamsPlugin { auto.logger.verbose.info("Posting release notes to microsoft teams."); - const atTarget = this.options.atTarget ? `@${this.options.atTarget}: ` : ""; + // @mentions don't work in teams - yet + // https://microsoftteams.uservoice.com/forums/555103-public/suggestions/17153099-webhook-needs-to-support-forced-notification-a-la + // const atTarget = this.options.atTarget ? `@${this.options.atTarget}: ` : ""; await fetch(`${this.options.url}`, { method: "POST", diff --git a/yarn.lock b/yarn.lock index c9dbb6922..ebe1a750d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -63,16 +63,11 @@ resolved "https://registry.yarnpkg.com/@atomist/slack-messages/-/slack-messages-1.2.2.tgz#782d31936a0363e4458272bcc8fbe4f7651292ee" integrity sha512-K1kQv1BZVtMXQqdpNZt9Pgh85KwamsWX9gYyq1xG4cpyb+EacfMiNfumrju16piFXanCUrCR0P1DowPjV2qV/A== -"@atomist/slack-messages@~1.2.0": - version "1.2.1" - resolved "https://registry.yarnpkg.com/@atomist/slack-messages/-/slack-messages-1.2.1.tgz#74e9499ae2b017cbb0e4f047033b9434cb42c1d7" - integrity sha512-TYiuOxy5Pf9ORn94X/ujl7PY9opIh+l6NzRAV8EBLpIv3IC9gmEoev4wmmyP7Q33J0/nGjqxAaZcq/n2SZrYaQ== - "@auto-it/bot-list@link:packages/bot-list": - version "10.21.2" + version "10.22.1" "@auto-it/core@link:packages/core": - version "10.21.2" + version "10.22.1" dependencies: "@auto-it/bot-list" "link:packages/bot-list" "@endemolshinegroup/cosmiconfig-typescript-loader" "^3.0.2" @@ -114,7 +109,7 @@ url-join "^4.0.0" "@auto-it/npm@link:plugins/npm": - version "10.21.2" + version "10.22.1" dependencies: "@auto-it/core" "link:packages/core" "@auto-it/package-json-utils" "link:packages/package-json-utils" @@ -132,13 +127,13 @@ user-home "^2.0.0" "@auto-it/package-json-utils@link:packages/package-json-utils": - version "10.21.2" + version "10.22.1" dependencies: parse-author "^2.0.0" parse-github-url "1.0.2" "@auto-it/released@link:plugins/released": - version "10.21.2" + version "10.22.1" dependencies: "@auto-it/bot-list" "link:packages/bot-list" "@auto-it/core" "link:packages/core" @@ -3123,6 +3118,11 @@ "@types/parse5" "*" "@types/tough-cookie" "*" +"@types/jsesc@^2.5.1": + version "2.5.1" + resolved "https://registry.yarnpkg.com/@types/jsesc/-/jsesc-2.5.1.tgz#c34defc608ec94b68dc6a12a581b440942c6d503" + integrity sha512-9VN+6yxLOPLOav+7PwjZbxiID2bVaeq0ED4qSQmdQTdjnXJSaCVKTR58t15oqH1H5t8Ng2ZX1SabJVoN9Q34bw== + "@types/json-schema@^7.0.3": version "7.0.3" resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.3.tgz#bdfd69d61e464dcc81b25159c270d75a73c1a636" From ecccebd1767c59ea1192276f7911339cf929a977 Mon Sep 17 00:00:00 2001 From: Vincent Briglia Date: Thu, 25 Mar 2021 18:51:25 +0100 Subject: [PATCH 05/10] correct import --- plugins/microsoft-teams/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/microsoft-teams/src/index.ts b/plugins/microsoft-teams/src/index.ts index 31ee050c0..6e05cea82 100644 --- a/plugins/microsoft-teams/src/index.ts +++ b/plugins/microsoft-teams/src/index.ts @@ -1,4 +1,4 @@ -import { jsesc } from "jsesc"; +import jsesc from "jsesc"; import createHttpsProxyAgent, { HttpsProxyAgent } from "https-proxy-agent"; import { From 8f20c0b7fb4587728d424253753eabfd78a11247 Mon Sep 17 00:00:00 2001 From: Vincent Briglia Date: Thu, 25 Mar 2021 19:07:34 +0100 Subject: [PATCH 06/10] fix snaps --- .../__snapshots__/index.test.ts.snap | 20 +++++++++---------- .../microsoft-teams.test.ts.snap | 4 ++-- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/plugins/microsoft-teams/__tests__/__snapshots__/index.test.ts.snap b/plugins/microsoft-teams/__tests__/__snapshots__/index.test.ts.snap index 10f172f0f..75d6b750d 100644 --- a/plugins/microsoft-teams/__tests__/__snapshots__/index.test.ts.snap +++ b/plugins/microsoft-teams/__tests__/__snapshots__/index.test.ts.snap @@ -1,21 +1,19 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`createPost should add more indents to nested lists - 2 spaces 1`] = `"{\\"@context\\":\\"http://schema.org/extensions\\",\\"@type\\":\\"MessageCard\\",\\"text\\":\\"@channel: New release **\\\\n*My Notes*\\\\n• PR \\\\n • Another note\\"}"`; +exports[`createPost should add more indents to nested lists - 2 spaces 1`] = `"{\\"@context\\":\\"http://schema.org/extensions\\",\\"@type\\":\\"MessageCard\\",\\"text\\":\\"# My Notes//n- PR [some link](google.com)//n - Another note\\",\\"potentialAction\\":[{\\"@type\\":\\"OpenUri\\",\\"name\\":\\"Learn More\\",\\"targets\\":[{\\"os\\":\\"default\\",\\"uri\\":\\"**\\"}]}]}"`; -exports[`createPost should add more indents to nested lists 1`] = `"{\\"@context\\":\\"http://schema.org/extensions\\",\\"@type\\":\\"MessageCard\\",\\"text\\":\\"@channel: New release **\\\\n*My Notes*\\\\n• PR \\\\n • Another note\\"}"`; +exports[`createPost should add more indents to nested lists 1`] = `"{\\"@context\\":\\"http://schema.org/extensions\\",\\"@type\\":\\"MessageCard\\",\\"text\\":\\"# My Notes//n- PR [some link](google.com)//n - Another note\\",\\"potentialAction\\":[{\\"@type\\":\\"OpenUri\\",\\"name\\":\\"Learn More\\",\\"targets\\":[{\\"os\\":\\"default\\",\\"uri\\":\\"**\\"}]}]}"`; -exports[`createPost should add title 1`] = `"{\\"@context\\":\\"http://schema.org/extensions\\",\\"@type\\":\\"MessageCard\\",\\"text\\":\\"@channel: New release **\\\\n*My Notes*\\\\n• PR \\"}"`; +exports[`createPost should add title 1`] = `"{\\"@context\\":\\"http://schema.org/extensions\\",\\"@type\\":\\"MessageCard\\",\\"text\\":\\"# My Notes//n- PR [some link](google.com)\\",\\"potentialAction\\":[{\\"@type\\":\\"OpenUri\\",\\"name\\":\\"Learn More\\",\\"targets\\":[{\\"os\\":\\"default\\",\\"uri\\":\\"**\\"}]}]}"`; -exports[`createPost should call slack api 1`] = `"{\\"@context\\":\\"http://schema.org/extensions\\",\\"@type\\":\\"MessageCard\\",\\"text\\":\\"@channel: New release **\\\\n*My Notes*\\\\n• PR \\"}"`; +exports[`createPost should call slack api 1`] = `"{\\"@context\\":\\"http://schema.org/extensions\\",\\"@type\\":\\"MessageCard\\",\\"text\\":\\"# My Notes//n- PR [some link](google.com)\\",\\"potentialAction\\":[{\\"@type\\":\\"OpenUri\\",\\"name\\":\\"Learn More\\",\\"targets\\":[{\\"os\\":\\"default\\",\\"uri\\":\\"**\\"}]}]}"`; -exports[`createPost should call slack api in env var 1`] = `"{\\"@context\\":\\"http://schema.org/extensions\\",\\"@type\\":\\"MessageCard\\",\\"text\\":\\"@channel: New release **\\\\n*My Notes*\\\\n• PR \\"}"`; +exports[`createPost should call slack api in env var 1`] = `"{\\"@context\\":\\"http://schema.org/extensions\\",\\"@type\\":\\"MessageCard\\",\\"text\\":\\"# My Notes//n- PR [some link](google.com)\\",\\"potentialAction\\":[{\\"@type\\":\\"OpenUri\\",\\"name\\":\\"Learn More\\",\\"targets\\":[{\\"os\\":\\"default\\",\\"uri\\":\\"**\\"}]}]}"`; -exports[`createPost should call slack api through http proxy 1`] = `"{\\"@context\\":\\"http://schema.org/extensions\\",\\"@type\\":\\"MessageCard\\",\\"text\\":\\"@channel: New release **\\\\n*My Notes*\\\\n• PR \\"}"`; +exports[`createPost should call slack api through http proxy 1`] = `"{\\"@context\\":\\"http://schema.org/extensions\\",\\"@type\\":\\"MessageCard\\",\\"text\\":\\"# My Notes//n- PR [some link](google.com)\\",\\"potentialAction\\":[{\\"@type\\":\\"OpenUri\\",\\"name\\":\\"Learn More\\",\\"targets\\":[{\\"os\\":\\"default\\",\\"uri\\":\\"**\\"}]}]}"`; -exports[`createPost should call slack api through https proxy 1`] = `"{\\"@context\\":\\"http://schema.org/extensions\\",\\"@type\\":\\"MessageCard\\",\\"text\\":\\"@channel: New release **\\\\n*My Notes*\\\\n• PR \\"}"`; +exports[`createPost should call slack api through https proxy 1`] = `"{\\"@context\\":\\"http://schema.org/extensions\\",\\"@type\\":\\"MessageCard\\",\\"text\\":\\"# My Notes//n- PR [some link](google.com)\\",\\"potentialAction\\":[{\\"@type\\":\\"OpenUri\\",\\"name\\":\\"Learn More\\",\\"targets\\":[{\\"os\\":\\"default\\",\\"uri\\":\\"**\\"}]}]}"`; -exports[`createPost should call slack api with custom atTarget 1`] = `"{\\"@context\\":\\"http://schema.org/extensions\\",\\"@type\\":\\"MessageCard\\",\\"text\\":\\"@here: New release **\\\\n*My Notes*\\\\n• PR \\"}"`; +exports[`createPost should call slack api with minimal config 1`] = `"{\\"@context\\":\\"http://schema.org/extensions\\",\\"@type\\":\\"MessageCard\\",\\"text\\":\\"# My Notes//n- PR [some link](google.com)\\",\\"potentialAction\\":[{\\"@type\\":\\"OpenUri\\",\\"name\\":\\"Learn More\\",\\"targets\\":[{\\"os\\":\\"default\\",\\"uri\\":\\"**\\"}]}]}"`; -exports[`createPost should call slack api with minimal config 1`] = `"{\\"@context\\":\\"http://schema.org/extensions\\",\\"@type\\":\\"MessageCard\\",\\"text\\":\\"@channel: New release **\\\\n*My Notes*\\\\n• PR \\"}"`; - -exports[`createPost should remove markdown code types from block 1`] = `"{\\"@context\\":\\"http://schema.org/extensions\\",\\"@type\\":\\"MessageCard\\",\\"text\\":\\"@channel: New release **\\\\n*My Notes*\\\\n\`json\`:\\\\n\\\\n\`\`\`\\\\n{ \\\\\\"foo\\\\\\": \\\\\\"bar\\\\\\" }\`\`\`\\\\n• PR \\"}"`; +exports[`createPost should remove markdown code types from block 1`] = `"{\\"@context\\":\\"http://schema.org/extensions\\",\\"@type\\":\\"MessageCard\\",\\"text\\":\\"# My Notes//n\`\`\`json//n{ /\\"foo/\\": /\\"bar/\\" }\`\`\`//n- PR [some link](google.com)\\",\\"potentialAction\\":[{\\"@type\\":\\"OpenUri\\",\\"name\\":\\"Learn More\\",\\"targets\\":[{\\"os\\":\\"default\\",\\"uri\\":\\"**\\"}]}]}"`; diff --git a/plugins/microsoft-teams/__tests__/__snapshots__/microsoft-teams.test.ts.snap b/plugins/microsoft-teams/__tests__/__snapshots__/microsoft-teams.test.ts.snap index b0907ed65..ef8a18e64 100644 --- a/plugins/microsoft-teams/__tests__/__snapshots__/microsoft-teams.test.ts.snap +++ b/plugins/microsoft-teams/__tests__/__snapshots__/microsoft-teams.test.ts.snap @@ -1,5 +1,5 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`postToMicrosoftTeams should call microsoft-teams api through http proxy 1`] = `"{\\"@context\\":\\"http://schema.org/extensions\\",\\"@type\\":\\"MessageCard\\",\\"text\\":\\"@channel: New release **\\\\n*My Notes*\\\\n• PR \\"}"`; +exports[`postToMicrosoftTeams should call microsoft-teams api through http proxy 1`] = `"{\\"@context\\":\\"http://schema.org/extensions\\",\\"@type\\":\\"MessageCard\\",\\"text\\":\\"*My Notes*\\\\n• PR \\",\\"potentialAction\\":[{\\"@type\\":\\"OpenUri\\",\\"name\\":\\"Learn More\\",\\"targets\\":[{\\"os\\":\\"default\\",\\"uri\\":\\"**\\"}]}]}"`; -exports[`postToMicrosoftTeams should call microsoft-teams api with minimal config 1`] = `"{\\"@context\\":\\"http://schema.org/extensions\\",\\"@type\\":\\"MessageCard\\",\\"text\\":\\"@channel: New release **\\\\n*My Notes*\\\\n• PR \\"}"`; +exports[`postToMicrosoftTeams should call microsoft-teams api with minimal config 1`] = `"{\\"@context\\":\\"http://schema.org/extensions\\",\\"@type\\":\\"MessageCard\\",\\"text\\":\\"*My Notes*\\\\n• PR \\",\\"potentialAction\\":[{\\"@type\\":\\"OpenUri\\",\\"name\\":\\"Learn More\\",\\"targets\\":[{\\"os\\":\\"default\\",\\"uri\\":\\"**\\"}]}]}"`; From 3db3145500fa5a8ad3a78a8111fadfe9d93fae9b Mon Sep 17 00:00:00 2001 From: Vincent Briglia Date: Fri, 26 Mar 2021 18:08:29 +0100 Subject: [PATCH 07/10] Update snapshots as well as commenting out atTarget test MS Teams might add this functionality later --- plugins/microsoft-teams/README.md | 2 +- .../__snapshots__/index.test.ts.snap | 10 ++-- .../microsoft-teams/__tests__/index.test.ts | 59 ++++++++++--------- plugins/microsoft-teams/src/index.ts | 7 ++- 4 files changed, 40 insertions(+), 38 deletions(-) diff --git a/plugins/microsoft-teams/README.md b/plugins/microsoft-teams/README.md index 4d55ef20b..2cbdc2ded 100644 --- a/plugins/microsoft-teams/README.md +++ b/plugins/microsoft-teams/README.md @@ -26,7 +26,7 @@ To generate incoming webhook in microsoft teams, checkout [this blog](https://me // or [ "microsoft-teams", - { "url": "https://url-to-your--hook.com", "atTarget": "username" } + { "url": "https://url-to-your--hook.com" } ] // Below: Uses microsoft-teams hook set in process.env.MICROSOFT_TEAMS_WEBHOOK_URL "microsoft-teams" diff --git a/plugins/microsoft-teams/__tests__/__snapshots__/index.test.ts.snap b/plugins/microsoft-teams/__tests__/__snapshots__/index.test.ts.snap index 75d6b750d..93e67e8e1 100644 --- a/plugins/microsoft-teams/__tests__/__snapshots__/index.test.ts.snap +++ b/plugins/microsoft-teams/__tests__/__snapshots__/index.test.ts.snap @@ -6,14 +6,14 @@ exports[`createPost should add more indents to nested lists 1`] = `"{\\"@context exports[`createPost should add title 1`] = `"{\\"@context\\":\\"http://schema.org/extensions\\",\\"@type\\":\\"MessageCard\\",\\"text\\":\\"# My Notes//n- PR [some link](google.com)\\",\\"potentialAction\\":[{\\"@type\\":\\"OpenUri\\",\\"name\\":\\"Learn More\\",\\"targets\\":[{\\"os\\":\\"default\\",\\"uri\\":\\"**\\"}]}]}"`; -exports[`createPost should call slack api 1`] = `"{\\"@context\\":\\"http://schema.org/extensions\\",\\"@type\\":\\"MessageCard\\",\\"text\\":\\"# My Notes//n- PR [some link](google.com)\\",\\"potentialAction\\":[{\\"@type\\":\\"OpenUri\\",\\"name\\":\\"Learn More\\",\\"targets\\":[{\\"os\\":\\"default\\",\\"uri\\":\\"**\\"}]}]}"`; +exports[`createPost should call Microsoft Office Teams api 1`] = `"{\\"@context\\":\\"http://schema.org/extensions\\",\\"@type\\":\\"MessageCard\\",\\"text\\":\\"# My Notes//n- PR [some link](google.com)\\",\\"potentialAction\\":[{\\"@type\\":\\"OpenUri\\",\\"name\\":\\"Learn More\\",\\"targets\\":[{\\"os\\":\\"default\\",\\"uri\\":\\"**\\"}]}]}"`; -exports[`createPost should call slack api in env var 1`] = `"{\\"@context\\":\\"http://schema.org/extensions\\",\\"@type\\":\\"MessageCard\\",\\"text\\":\\"# My Notes//n- PR [some link](google.com)\\",\\"potentialAction\\":[{\\"@type\\":\\"OpenUri\\",\\"name\\":\\"Learn More\\",\\"targets\\":[{\\"os\\":\\"default\\",\\"uri\\":\\"**\\"}]}]}"`; +exports[`createPost should call Microsoft Office Teams api in env var 1`] = `"{\\"@context\\":\\"http://schema.org/extensions\\",\\"@type\\":\\"MessageCard\\",\\"text\\":\\"# My Notes//n- PR [some link](google.com)\\",\\"potentialAction\\":[{\\"@type\\":\\"OpenUri\\",\\"name\\":\\"Learn More\\",\\"targets\\":[{\\"os\\":\\"default\\",\\"uri\\":\\"**\\"}]}]}"`; -exports[`createPost should call slack api through http proxy 1`] = `"{\\"@context\\":\\"http://schema.org/extensions\\",\\"@type\\":\\"MessageCard\\",\\"text\\":\\"# My Notes//n- PR [some link](google.com)\\",\\"potentialAction\\":[{\\"@type\\":\\"OpenUri\\",\\"name\\":\\"Learn More\\",\\"targets\\":[{\\"os\\":\\"default\\",\\"uri\\":\\"**\\"}]}]}"`; +exports[`createPost should call Microsoft Office Teams api through http proxy 1`] = `"{\\"@context\\":\\"http://schema.org/extensions\\",\\"@type\\":\\"MessageCard\\",\\"text\\":\\"# My Notes//n- PR [some link](google.com)\\",\\"potentialAction\\":[{\\"@type\\":\\"OpenUri\\",\\"name\\":\\"Learn More\\",\\"targets\\":[{\\"os\\":\\"default\\",\\"uri\\":\\"**\\"}]}]}"`; -exports[`createPost should call slack api through https proxy 1`] = `"{\\"@context\\":\\"http://schema.org/extensions\\",\\"@type\\":\\"MessageCard\\",\\"text\\":\\"# My Notes//n- PR [some link](google.com)\\",\\"potentialAction\\":[{\\"@type\\":\\"OpenUri\\",\\"name\\":\\"Learn More\\",\\"targets\\":[{\\"os\\":\\"default\\",\\"uri\\":\\"**\\"}]}]}"`; +exports[`createPost should call Microsoft Office Teams api through https proxy 1`] = `"{\\"@context\\":\\"http://schema.org/extensions\\",\\"@type\\":\\"MessageCard\\",\\"text\\":\\"# My Notes//n- PR [some link](google.com)\\",\\"potentialAction\\":[{\\"@type\\":\\"OpenUri\\",\\"name\\":\\"Learn More\\",\\"targets\\":[{\\"os\\":\\"default\\",\\"uri\\":\\"**\\"}]}]}"`; -exports[`createPost should call slack api with minimal config 1`] = `"{\\"@context\\":\\"http://schema.org/extensions\\",\\"@type\\":\\"MessageCard\\",\\"text\\":\\"# My Notes//n- PR [some link](google.com)\\",\\"potentialAction\\":[{\\"@type\\":\\"OpenUri\\",\\"name\\":\\"Learn More\\",\\"targets\\":[{\\"os\\":\\"default\\",\\"uri\\":\\"**\\"}]}]}"`; +exports[`createPost should call Microsoft Office Teams api with minimal config 1`] = `"{\\"@context\\":\\"http://schema.org/extensions\\",\\"@type\\":\\"MessageCard\\",\\"text\\":\\"# My Notes//n- PR [some link](google.com)\\",\\"potentialAction\\":[{\\"@type\\":\\"OpenUri\\",\\"name\\":\\"Learn More\\",\\"targets\\":[{\\"os\\":\\"default\\",\\"uri\\":\\"**\\"}]}]}"`; exports[`createPost should remove markdown code types from block 1`] = `"{\\"@context\\":\\"http://schema.org/extensions\\",\\"@type\\":\\"MessageCard\\",\\"text\\":\\"# My Notes//n\`\`\`json//n{ /\\"foo/\\": /\\"bar/\\" }\`\`\`//n- PR [some link](google.com)\\",\\"potentialAction\\":[{\\"@type\\":\\"OpenUri\\",\\"name\\":\\"Learn More\\",\\"targets\\":[{\\"os\\":\\"default\\",\\"uri\\":\\"**\\"}]}]}"`; diff --git a/plugins/microsoft-teams/__tests__/index.test.ts b/plugins/microsoft-teams/__tests__/index.test.ts index 23093f52e..d22c16ad1 100644 --- a/plugins/microsoft-teams/__tests__/index.test.ts +++ b/plugins/microsoft-teams/__tests__/index.test.ts @@ -199,7 +199,7 @@ describe("createPost", () => { expect(plugin.createPost).toHaveBeenCalledTimes(1); }); - test("should call slack api with minimal config", async () => { + test("should call Microsoft Office Teams api with minimal config", async () => { const plugin = new MicrosoftTeamsPlugin("https://custom-microsoft-url"); await plugin.createPost( @@ -257,7 +257,7 @@ describe("createPost", () => { expect(fetchSpy.mock.calls[0][1].body).toMatchSnapshot(); }); - test("should call slack api through http proxy", async () => { + test("should call Microsoft Office Teams api through http proxy", async () => { const plugin = new MicrosoftTeamsPlugin("https://custom-microsoft-url"); process.env.http_proxy = "http-proxy"; @@ -291,7 +291,7 @@ describe("createPost", () => { expect(fetchSpy.mock.calls[0][1].body).toMatchSnapshot(); }); - test("should call slack api through https proxy", async () => { + test("should call Microsoft Office Teams api through https proxy", async () => { const plugin = new MicrosoftTeamsPlugin("https://custom-microsoft-url"); process.env.https_proxy = "https-proxy"; @@ -310,7 +310,7 @@ describe("createPost", () => { expect(fetchSpy.mock.calls[0][1].body).toMatchSnapshot(); }); - test("should call slack api", async () => { + test("should call Microsoft Office Teams api", async () => { const plugin = new MicrosoftTeamsPlugin({ url: "https://custom-microsoft-url" }); const hooks = makeHooks(); plugin.apply({ hooks, options: {}, ...mockAuto } as Auto); @@ -331,32 +331,33 @@ describe("createPost", () => { expect(fetchSpy.mock.calls[0][1].body).toMatchSnapshot(); }); - test("should call slack api with custom atTarget", async () => { - const plugin = new MicrosoftTeamsPlugin({ - url: "https://custom-microsoft-url", - atTarget: "here", - }); - const hooks = makeHooks(); - 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][0]).toBe( - "https://custom-microsoft-url" - ); - expect(fetchSpy.mock.calls[0][1].body.includes("@here")).toBe(true); - expect(fetchSpy.mock.calls[0][1].body).toMatchSnapshot(); - }); - test("should call slack api in env var", async () => { + // test("should call Microsoft Office Teams api with custom atTarget", async () => { + // const plugin = new MicrosoftTeamsPlugin({ + // url: "https://custom-microsoft-url", + // atTarget: "here", + // }); + // const hooks = makeHooks(); + // 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][0]).toBe( + // "https://custom-microsoft-url" + // ); + // expect(fetchSpy.mock.calls[0][1].body.includes("@here")).toBe(true); + // expect(fetchSpy.mock.calls[0][1].body).toMatchSnapshot(); + // }); + + test("should call Microsoft Office Teams api in env var", async () => { process.env.MICROSOFT_TEAMS_WEBHOOK_URL = "https://foo.bar"; const plugin = new MicrosoftTeamsPlugin(); const hooks = makeHooks(); diff --git a/plugins/microsoft-teams/src/index.ts b/plugins/microsoft-teams/src/index.ts index 6e05cea82..e68dc6d2f 100644 --- a/plugins/microsoft-teams/src/index.ts +++ b/plugins/microsoft-teams/src/index.ts @@ -26,7 +26,7 @@ const pluginOptions = t.partial({ export type IMicrosoftTeamsPluginOptions = t.TypeOf; -/** Post your release notes to Slack during `auto release` */ +/** Post your release notes to Microsoft Teams during `auto release` */ export default class MicrosoftTeamsPlugin { /** The name of the plugin */ name = "microsoft-teams"; @@ -114,6 +114,7 @@ export default class MicrosoftTeamsPlugin { (Array.isArray(response) && response) || (response && [response]) || []; + const urls = releases.map( (release) => `*<${release.data.html_url}|${ @@ -134,7 +135,7 @@ export default class MicrosoftTeamsPlugin { ); } - /** Post the release notes to slack */ + /** Post the release notes to Microsoft Teams */ async createPost( auto: Auto, releaseNotes: string, @@ -147,7 +148,7 @@ export default class MicrosoftTeamsPlugin { auto.logger.verbose.info("Posting release notes to microsoft teams."); - // @mentions don't work in teams - yet + // TODO: @mentions don't work in teams - yet // https://microsoftteams.uservoice.com/forums/555103-public/suggestions/17153099-webhook-needs-to-support-forced-notification-a-la // const atTarget = this.options.atTarget ? `@${this.options.atTarget}: ` : ""; From 1e2301e7441a28e701a96e74ad210650d9855fa2 Mon Sep 17 00:00:00 2001 From: Vincent Briglia Date: Sat, 8 May 2021 14:08:43 +0200 Subject: [PATCH 08/10] chore: clamp rehype --- package.json | 2 +- yarn.lock | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index c6c1de45d..6624c3468 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ "postinstall": "patch-package" }, "devDependencies": { - "@atomictech/rehype-toc": "^3.1.2", + "@atomictech/rehype-toc": "3.1.2", "@fortawesome/fontawesome-svg-core": "^1.2.27", "@fortawesome/free-solid-svg-icons": "^5.12.1", "@fortawesome/react-fontawesome": "^0.1.9", diff --git a/yarn.lock b/yarn.lock index a2bc688e8..7b2ccbe59 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2,10 +2,10 @@ # yarn lockfile v1 -"@atomictech/rehype-toc@^3.1.2": - version "3.1.3" - resolved "https://registry.yarnpkg.com/@atomictech/rehype-toc/-/rehype-toc-3.1.3.tgz#3b3f88a69319cadc36e7d33ddae71dacfa5fe9a2" - integrity sha512-qiNUJgdORp8RctdULIVm15KwePHFCGJdY3NsrMUbIhKt/oyVXyB30Z6qPFhmZrNVluoDw5TPiFrr4j0yUxofdA== +"@atomictech/rehype-toc@3.1.2": + version "3.1.2" + resolved "https://registry.yarnpkg.com/@atomictech/rehype-toc/-/rehype-toc-3.1.2.tgz#62ec993528be26dbf304e1ec0db75ae074d459df" + integrity sha512-ZLqzgBo9WUgXmLpCF8e2rYeHZjJbvWk/3R+mmpKBMDmjYRTb6/Fk6SSGFv0x1aorPwg0OLfg6ZvjAaRbndRPcA== "@atomist/slack-messages@^1.2.2": version "1.2.2" From 9877b9ac5978a40753e3f0db67afdbe7454708b7 Mon Sep 17 00:00:00 2001 From: Vincent Briglia Date: Sat, 8 May 2021 14:27:08 +0200 Subject: [PATCH 09/10] fix: tapable issues --- package.json | 1 + packages/core/package.json | 2 +- packages/core/src/git.ts | 7 +++++-- packages/core/src/log-parse.ts | 2 +- yarn.lock | 11 +++++++++-- 5 files changed, 17 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 6624c3468..c99aebe88 100644 --- a/package.json +++ b/package.json @@ -47,6 +47,7 @@ "@types/jest": "^26.0.0", "@types/mock-fs": "^4.13.0", "@types/parse-github-url": "1.0.0", + "@types/tapable": "2.2.2", "@typescript-eslint/eslint-plugin": "^4.1.1", "@typescript-eslint/parser": "^4.1.1", "all-contributors-cli": "^6.4.0", diff --git a/packages/core/package.json b/packages/core/package.json index 02ca25709..30a9d5a33 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -68,7 +68,7 @@ "requireg": "^0.2.2", "semver": "^7.0.0", "signale": "^1.4.0", - "tapable": "^2.0.0-beta.2", + "tapable": "^2.2.0", "terminal-link": "^2.1.1", "tinycolor2": "^1.4.1", "ts-node": "^9.1.1", diff --git a/packages/core/src/git.ts b/packages/core/src/git.ts index 758b838cb..98c87921d 100644 --- a/packages/core/src/git.ts +++ b/packages/core/src/git.ts @@ -164,8 +164,11 @@ export default class Git { }, }); this.github.hook.error("request", (error) => { - if (error?.headers?.authorization) { - delete error.headers.authorization; + if (error) { + // narrow down the type + if ("headers" in error && error.headers.authorization) { + delete error.headers.authorization; + } } throw error; diff --git a/packages/core/src/log-parse.ts b/packages/core/src/log-parse.ts index 54d8b63d1..856e95e95 100644 --- a/packages/core/src/log-parse.ts +++ b/packages/core/src/log-parse.ts @@ -20,7 +20,7 @@ export interface IPullRequest { /** The base branch the pull request is on */ base?: string; /** The body of the PR (opening comment) */ - body?: string; + body?: string | null; } export interface ICommit { diff --git a/yarn.lock b/yarn.lock index 7b2ccbe59..b7baca67b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -49,7 +49,7 @@ requireg "^0.2.2" semver "^7.0.0" signale "^1.4.0" - tapable "^2.0.0-beta.2" + tapable "^2.2.0" terminal-link "^2.1.1" tinycolor2 "^1.4.1" ts-node "^9.1.1" @@ -3007,6 +3007,13 @@ resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-2.0.0.tgz#7036640b4e21cc2f259ae826ce843d277dad8cff" integrity sha512-RJJrrySY7A8havqpGObOB4W92QXKJo63/jFLLgpvOtsGUqbQZ9Sbgl35KMm1DjC6j7AvmmU2bIno+3IyEaemaw== +"@types/tapable@2.2.2": + version "2.2.2" + resolved "https://registry.yarnpkg.com/@types/tapable/-/tapable-2.2.2.tgz#1d324b524190954a5700d86b6328bfc57e1fda48" + integrity sha512-ujqOVJEeLcwpDVJPnp/k3u1UXmTKq5urJq9fO8aUKg8Vlel5RNOFbVKEfqfh6wGfF/M+HiTJlBJMLC1aDfyf0Q== + dependencies: + tapable "^2.2.0" + "@types/tapable@^1": version "1.0.7" resolved "https://registry.yarnpkg.com/@types/tapable/-/tapable-1.0.7.tgz#545158342f949e8fd3bfd813224971ecddc3fac4" @@ -13789,7 +13796,7 @@ table@^6.0.4: string-width "^4.2.0" strip-ansi "^6.0.0" -tapable@^2.0.0-beta.2: +tapable@^2.2.0: version "2.2.0" resolved "https://registry.yarnpkg.com/tapable/-/tapable-2.2.0.tgz#5c373d281d9c672848213d0e037d1c4165ab426b" integrity sha512-FBk4IesMV1rBxX2tfiK8RAmogtWn53puLOQlvO8XuwlgxcYbP4mVPS9Ph4aeamSyyVjOl24aYWAuc8U5kCVwMw== From 5fa091a7a290397022ae208294465ba88598c859 Mon Sep 17 00:00:00 2001 From: Andrew Lisowski Date: Fri, 21 May 2021 13:29:49 -0700 Subject: [PATCH 10/10] fix tests --- packages/core/src/__tests__/auto-env.test.ts | 1 + packages/core/src/__tests__/release.test.ts | 4 ++++ plugins/brew/__tests__/brew.test.ts | 4 ++++ plugins/crates/__tests__/crates.test.ts | 4 ++++ plugins/maven/__tests__/maven.test.ts | 4 ++++ plugins/npm/__tests__/monorepo-log.test.ts | 4 ++++ scripts/jest-setup.js | 2 +- 7 files changed, 22 insertions(+), 1 deletion(-) diff --git a/packages/core/src/__tests__/auto-env.test.ts b/packages/core/src/__tests__/auto-env.test.ts index c2d6f2828..f079977c6 100644 --- a/packages/core/src/__tests__/auto-env.test.ts +++ b/packages/core/src/__tests__/auto-env.test.ts @@ -1,6 +1,7 @@ import { Auto } from "../auto"; jest.mock("fs", () => ({ + read: () => undefined, readFileSync: () => 'FOO="test value"', closeSync: () => undefined, existsSync: () => true, diff --git a/packages/core/src/__tests__/release.test.ts b/packages/core/src/__tests__/release.test.ts index ad9077e7b..d7ea08b63 100644 --- a/packages/core/src/__tests__/release.test.ts +++ b/packages/core/src/__tests__/release.test.ts @@ -123,6 +123,10 @@ jest.mock("fs", () => ({ writeFile: (file, data, cb) => { cb(undefined, writeSpy(file, data)); }, + // @ts-ignore + read: (a, b, cb) => { + cb(undefined); + }, })); const logParse = new LogParse(); diff --git a/plugins/brew/__tests__/brew.test.ts b/plugins/brew/__tests__/brew.test.ts index 9f2fc0a2e..3f044fa78 100644 --- a/plugins/brew/__tests__/brew.test.ts +++ b/plugins/brew/__tests__/brew.test.ts @@ -12,6 +12,10 @@ const readFileSync = jest.fn(); const writeFileSync = jest.fn(); jest.mock("fs", () => ({ + // @ts-ignore + read: (a, b, cb) => { + cb(undefined); + }, // @ts-ignore existsSync: (...args) => existsSync(...args), // @ts-ignore diff --git a/plugins/crates/__tests__/crates.test.ts b/plugins/crates/__tests__/crates.test.ts index 160bebdac..3f766a8d4 100644 --- a/plugins/crates/__tests__/crates.test.ts +++ b/plugins/crates/__tests__/crates.test.ts @@ -51,6 +51,10 @@ jest.mock("fs", () => ({ // @ts-ignore existsSync: (...args) => existsSync(...args), // @ts-ignore + read: (a, b, cb) => { + cb(undefined, readResult); + }, + // @ts-ignore readFile: (a, b, cb) => { cb(undefined, readResult); }, diff --git a/plugins/maven/__tests__/maven.test.ts b/plugins/maven/__tests__/maven.test.ts index 610b50f33..ef947e5c7 100644 --- a/plugins/maven/__tests__/maven.test.ts +++ b/plugins/maven/__tests__/maven.test.ts @@ -30,6 +30,10 @@ jest.mock("fs", () => ({ WriteStream: function () {}, // @ts-ignore closeSync: () => undefined, + // @ts-ignore + read: (a, b, cb) => { + cb(undefined); + }, })); jest.mock("child_process"); diff --git a/plugins/npm/__tests__/monorepo-log.test.ts b/plugins/npm/__tests__/monorepo-log.test.ts index e50c2f3bd..20087d396 100644 --- a/plugins/npm/__tests__/monorepo-log.test.ts +++ b/plugins/npm/__tests__/monorepo-log.test.ts @@ -34,6 +34,10 @@ jest.mock("fs", () => ({ readFile: jest.fn(), // @ts-ignore readFileSync: () => readFileSync(), + // @ts-ignore + read: (a, b, cb) => { + cb(undefined); + }, ReadStream: function () {}, WriteStream: function () {}, // @ts-ignore diff --git a/scripts/jest-setup.js b/scripts/jest-setup.js index 7c37886b5..67af2c198 100644 --- a/scripts/jest-setup.js +++ b/scripts/jest-setup.js @@ -1,3 +1,3 @@ require("mock-fs"); -jest.mock('requireg', () => require); +jest.mock('requireg', () => require); \ No newline at end of file