-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1570 from mukul13/master
Microsoft Teams plugin for auto
- Loading branch information
Showing
10 changed files
with
295 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Microsoft-Teams Plugin | ||
|
||
Microsoft Teams plugin for auto | ||
|
||
## Installation | ||
|
||
This plugin is not included with the `auto` CLI installed via NPM. To install: | ||
|
||
```bash | ||
npm i --save-dev @auto-it/microsoft-teams | ||
# or | ||
yarn add -D @auto-it/microsoft-teams | ||
``` | ||
|
||
## Usage | ||
|
||
To use the plugin include it in your `.autorc`. | ||
To generate incoming webhook in microsoft teams, checkout [this blog](https://medium.com/@ankush.kumar133/get-started-with-microsoft-team-connectors-incoming-webhook-a330657993e7). | ||
|
||
```json | ||
{ | ||
"plugins": [ | ||
["microsoft-teams", { "url": "https://url-to-your-hook.com" }], | ||
// or | ||
["microsoft-teams", "https://url-to-your-hook.com"], | ||
// or | ||
[ | ||
"microsoft-teams", | ||
{ "url": "https://url-to-your--hook.com", "atTarget": "username" } | ||
] | ||
// Below: Uses microsoft-teams hook set in process.env.MICROSOFT_TEAMS_WEBHOOK_URL | ||
"microsoft-teams" | ||
] | ||
} | ||
``` |
5 changes: 5 additions & 0 deletions
5
plugins/microsoft-teams/__tests__/__snapshots__/microsoft-teams.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +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 *<https://git.hub/some/project/releases/v1.0.0|v1.0.0>*\\\\n* My Notes*\\\\n• PR <google.com|some link>\\"}"`; | ||
|
||
exports[`postToMicrosoftTeams should call microsoft-teams api with minimal config 1`] = `"{\\"@context\\":\\"http://schema.org/extensions\\",\\"@type\\":\\"MessageCard\\",\\"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>\\"}"`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { dummyLog } from "@auto-it/core/src/utils/logger"; | ||
import { sanitizeMarkdown } from "@auto-it/slack"; | ||
import createHttpsProxyAgent from "https-proxy-agent"; | ||
|
||
import MicrosoftTeamsPlugin from "../src"; | ||
|
||
const fetchSpy = jest.fn(); | ||
// @ts-ignore | ||
jest.mock("node-fetch", () => (...args) => { | ||
fetchSpy(...args); | ||
}); | ||
|
||
beforeEach(() => { | ||
fetchSpy.mockClear(); | ||
}); | ||
|
||
const mockAuto = { | ||
git: {}, | ||
logger: dummyLog(), | ||
} as any; | ||
|
||
describe("postToMicrosoftTeams", () => { | ||
test("should call microsoft-teams api with minimal config", async () => { | ||
const plugin = new MicrosoftTeamsPlugin("https://microsoft-teams-url"); | ||
|
||
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>*", | ||
undefined | ||
); | ||
|
||
expect(fetchSpy).toHaveBeenCalled(); | ||
expect(fetchSpy.mock.calls[0][0]).toBe("https://microsoft-teams-url"); | ||
expect(fetchSpy.mock.calls[0][1].agent).toBeUndefined(); | ||
expect(fetchSpy.mock.calls[0][1].body).toMatchSnapshot(); | ||
}); | ||
|
||
test("should call microsoft-teams api through http proxy", async () => { | ||
const plugin = new MicrosoftTeamsPlugin("https://microsoft-teams-url"); | ||
process.env.http_proxy = "http-proxy"; | ||
|
||
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") | ||
); | ||
|
||
expect(fetchSpy).toHaveBeenCalled(); | ||
expect(fetchSpy.mock.calls[0][0]).toBe("https://microsoft-teams-url"); | ||
expect(fetchSpy.mock.calls[0][1].agent).not.toBeUndefined(); | ||
expect(fetchSpy.mock.calls[0][1].body).toMatchSnapshot(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
{ | ||
"name": "@auto-it/microsoft-teams", | ||
"version": "9.54.3", | ||
"main": "dist/index.js", | ||
"description": "Microsoft Teams plugin for auto", | ||
"license": "MIT", | ||
"author": { | ||
"name": "Mukul Chaware", | ||
"email": "mukul.chaware13@gmail.com" | ||
}, | ||
"publishConfig": { | ||
"registry": "https://registry.npmjs.org/", | ||
"access": "public" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/intuit/auto" | ||
}, | ||
"files": [ | ||
"dist" | ||
], | ||
"keywords": [ | ||
"automation", | ||
"semantic", | ||
"release", | ||
"github", | ||
"labels", | ||
"automated", | ||
"continuos integration", | ||
"changelog", | ||
"microsoft-teams" | ||
], | ||
"scripts": { | ||
"build": "tsc -b", | ||
"start": "npm run build -- -w", | ||
"lint": "eslint src --ext .ts", | ||
"test": "jest --maxWorkers=2 --config ../../package.json" | ||
}, | ||
"dependencies": { | ||
"@auto-it/core": "link:../../packages/core", | ||
"@auto-it/slack": "link:../slack", | ||
"https-proxy-agent": "^5.0.0", | ||
"node-fetch": "2.6.1", | ||
"tslib": "2.0.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { HttpsProxyAgent } from "https-proxy-agent"; | ||
|
||
import { Auto, InteractiveInit } from "@auto-it/core"; | ||
import SlackPlugin, { ISlackPluginOptions } from "@auto-it/slack"; | ||
import fetch from "node-fetch"; | ||
|
||
/** Post your release notes to Slack during `auto release` */ | ||
export default class MicrosoftTeamsPlugin extends SlackPlugin { | ||
/** The name of the plugin */ | ||
name = "microsoft-teams"; | ||
|
||
/** Initialize the plugin with it's options */ | ||
constructor(options: ISlackPluginOptions | string = {}) { | ||
super( | ||
typeof options === "string" | ||
? options | ||
: { | ||
...options, | ||
url: process.env.MICROSOFT_TEAMS_WEBHOOK_URL || options.url || "", | ||
} | ||
); | ||
} | ||
|
||
/** Custom initialization for this plugin */ | ||
init(initializer: InteractiveInit) { | ||
initializer.hooks.createEnv.tapPromise(this.name, async (vars) => [ | ||
...vars, | ||
{ | ||
variable: "MICROSOFT_TEAMS_WEBHOOK_URL", | ||
message: "What is the root url of your slack hook? ()", | ||
}, | ||
]); | ||
} | ||
|
||
/** Post the release notes to slack */ | ||
async createPost( | ||
auto: Auto, | ||
releaseNotes: string, | ||
releaseUrl: string, | ||
agent: HttpsProxyAgent | undefined | ||
) { | ||
if (!auto.git) { | ||
return; | ||
} | ||
|
||
auto.logger.verbose.info("Posting release notes to microsoft teams."); | ||
|
||
const atTarget = this.options.atTarget ? `@${this.options.atTarget}: ` : ""; | ||
|
||
await fetch(`${this.options.url}`, { | ||
method: "POST", | ||
body: JSON.stringify({ | ||
"@context": "http://schema.org/extensions", | ||
"@type": "MessageCard", | ||
text: [`${atTarget}New release ${releaseUrl}`, releaseNotes].join("\n"), | ||
}), | ||
headers: { "Content-Type": "application/json" }, | ||
agent, | ||
}); | ||
|
||
auto.logger.verbose.info("Posted release notes to microsoft teams."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"include": ["src/**/*", "../../typings/**/*"], | ||
|
||
"compilerOptions": { | ||
"outDir": "./dist", | ||
"rootDir": "./src", | ||
"composite": true | ||
}, | ||
|
||
"references": [ | ||
{ | ||
"path": "../../packages/core" | ||
} | ||
] | ||
} |
Oops, something went wrong.