Skip to content

Commit

Permalink
Merge pull request #1570 from mukul13/master
Browse files Browse the repository at this point in the history
Microsoft Teams plugin for auto
  • Loading branch information
hipstersmoothie authored Oct 27, 2020
2 parents c9aa70d + 1ab9f78 commit 4926371
Show file tree
Hide file tree
Showing 10 changed files with 295 additions and 75 deletions.
35 changes: 35 additions & 0 deletions plugins/microsoft-teams/README.md
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"
]
}
```
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>\\"}"`;
55 changes: 55 additions & 0 deletions plugins/microsoft-teams/__tests__/microsoft-teams.test.ts
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();
});
});
46 changes: 46 additions & 0 deletions plugins/microsoft-teams/package.json
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"
}
}
63 changes: 63 additions & 0 deletions plugins/microsoft-teams/src/index.ts
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.");
}
}
16 changes: 16 additions & 0 deletions plugins/microsoft-teams/tsconfig.json
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"
}
]
}
Loading

0 comments on commit 4926371

Please sign in to comment.