UNPKG

@auto-canary/microsoft-teams

Version:

Microsoft Teams plugin for auto

127 lines 5.66 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const slack_messages_1 = require("@atomist/slack-messages"); const https_proxy_agent_1 = tslib_1.__importDefault(require("https-proxy-agent")); const core_1 = require("@auto-it/core"); const node_fetch_1 = tslib_1.__importDefault(require("node-fetch")); const t = tslib_1.__importStar(require("io-ts")); const MARKDOWN_LANGUAGE = /^(```)(\S+)$/m; /** Transform markdown into microsoft teams friendly text */ const sanitizeMarkdown = (markdown) => slack_messages_1.githubToSlack(markdown) .split("\n") .map((line) => { // Strip out the ### prefix and replace it with *<word>* 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"); const pluginOptions = t.partial({ /** URL of the microsoft teams to post to */ url: t.string, /** Who to bother when posting to the channel */ atTarget: t.string, /** Allow users to opt into having prereleases posted to slack */ publishPreRelease: t.boolean, }); /** Post your release notes to Microsof during `auto release` */ class MicrosoftTeamsPlugin { /** Initialize the plugin with it's options */ constructor(options = {}) { /** The name of the plugin */ this.name = "microsoft-teams"; if (typeof options === "string") { this.options = { url: options, atTarget: "channel" }; } else { this.options = { url: process.env.MICROSOFT_TEAMS_WEBHOOK_URL || options.url || "", atTarget: options.atTarget ? options.atTarget : "channel", publishPreRelease: options.publishPreRelease ? options.publishPreRelease : false, }; } } /** Custom initialization for this plugin */ init(initializer) { initializer.hooks.createEnv.tapPromise(this.name, async (vars) => [ ...vars, { variable: "MICROSOFT_TEAMS_WEBHOOK_URL", message: "What is the incoming webhook url of your microsoft teams hook? ()", }, ]); } /** Tap into auto plugin points. */ apply(auto) { auto.hooks.validateConfig.tapPromise(this.name, async (name, options) => { // If it's a string thats valid config if (name === this.name && typeof options !== "string") { return core_1.validatePluginConfiguration(this.name, pluginOptions, options); } }); auto.hooks.afterRelease.tapPromise(this.name, async ({ newVersion, commits, releaseNotes, response }) => { var _a, _b, _c; // Avoid publishing on prerelease branches by default, but allow folks to opt in if they care to const currentBranch = core_1.getCurrentBranch(); if (currentBranch && ((_b = (_a = auto.config) === null || _a === void 0 ? void 0 : _a.prereleaseBranches) === null || _b === void 0 ? void 0 : _b.includes(currentBranch)) && !this.options.publishPreRelease) { return; } if (!newVersion) { return; } const head = commits[0]; if (!head) { return; } const skipReleaseLabels = (((_c = auto.config) === null || _c === void 0 ? void 0 : _c.labels.filter((l) => l.releaseType === "skip")) || []).map((l) => l.name); const isSkipped = head.labels.find((label) => skipReleaseLabels.includes(label)); if (isSkipped) { return; } if (!this.options.url) { throw new Error("Microsoft Teams url must be set to post a message to microsoft teams."); } await this.postToMicrosoftTeams(auto, newVersion, releaseNotes, (Array.isArray(response) && response) || (response && [response]) || []); }); } /** Post the release notes to microsoft teams */ async postToMicrosoftTeams(auto, newVersion, releaseNotes, releases) { if (!auto.git) { return; } auto.logger.verbose.info("Posting release notes to microsoft teams."); const body = sanitizeMarkdown(releaseNotes); const proxyUrl = process.env.https_proxy || process.env.http_proxy; const atTarget = this.options.atTarget; const urls = releases.map((release) => `*<[${release.data.name || release.data.tag_name}](${release.data.html_url})>*`); const releaseUrl = urls.length ? urls.join(", ") : newVersion; await node_fetch_1.default(`${this.options.url}`, { method: "POST", body: JSON.stringify({ "@context": "http://schema.org/extensions", "@type": "MessageCard", text: [`@${atTarget}: New release ${releaseUrl}`, body].join("\n"), }), headers: { "Content-Type": "application/json" }, agent: proxyUrl ? https_proxy_agent_1.default(proxyUrl) : undefined, }); auto.logger.verbose.info("Posted release notes to microsoft teams."); } } exports.default = MicrosoftTeamsPlugin; //# sourceMappingURL=index.js.map