UNPKG

playwright-msteams-reporter-conditional

Version:

Microsoft Teams reporter for Playwright which allows you to send notifications about the status of your E2E tests.

187 lines (186 loc) 7.46 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.processResults = void 0; const utils_1 = require("./utils"); const constants_1 = require("./constants"); const node_fetch_1 = __importDefault(require("node-fetch")); const https_proxy_agent_1 = require("https-proxy-agent"); const processResults = (suite, options) => __awaiter(void 0, void 0, void 0, function* () { if (!options.webhookUrl) { console.error("No webhook URL provided"); return; } if (!(0, utils_1.validateWebhookUrl)(options.webhookUrl, options.webhookType)) { console.error("Invalid webhook URL"); return; } if (!suite) { console.error("No test suite found"); return; } if (options.shouldRun && !(options === null || options === void 0 ? void 0 : options.shouldRun(suite))) return; // Clone the base adaptive card and table const adaptiveCard = structuredClone(constants_1.BaseAdaptiveCard); const table = structuredClone(constants_1.BaseTable); const totalStatus = (0, utils_1.getTotalStatus)(suite.suites); const failedTests = (0, utils_1.getFailedTests)(suite.suites); const totalTests = suite.allTests().length; const isSuccess = totalStatus.failed === 0; if (isSuccess && !options.notifyOnSuccess) { if (!options.quiet) { console.log("No failed tests, skipping notification"); } return; } table.rows.push((0, utils_1.createTableRow)("Type", "Total")); table.rows.push((0, utils_1.createTableRow)(`${options.enableEmoji ? "✅ " : ""}Passed`, totalStatus.passed, { style: "good" })); if (totalStatus.flaky) { table.rows.push((0, utils_1.createTableRow)(`${options.enableEmoji ? "⚠️ " : ""}Flaky`, totalStatus.flaky, { style: "warning" })); } table.rows.push((0, utils_1.createTableRow)(`${options.enableEmoji ? "❌ " : ""}Failed`, totalStatus.failed, { style: "attention" })); if (failedTests.length > 20) { table.rows.push((0, utils_1.createTableRow)("Failed more than 20 tests, see Jenkins pipeline for further details", '', { style: "attention" })); } else { for (const failedTest of failedTests) { table.rows.push((0, utils_1.createTableRow)(failedTest, '', { style: "attention" })); } } table.rows.push((0, utils_1.createTableRow)(`${options.enableEmoji ? "⏭️ " : ""}Skipped`, totalStatus.skipped, { style: "accent" })); table.rows.push((0, utils_1.createTableRow)("Total tests", totalTests, { isSubtle: true, weight: "Bolder", })); const container = { type: "Container", items: [ { type: "TextBlock", size: "ExtraLarge", weight: "Bolder", text: options.title, }, { type: "TextBlock", size: "Large", weight: "Bolder", text: (0, utils_1.getNotificationTitle)(totalStatus), color: (0, utils_1.getNotificationColor)(totalStatus), }, table, ], bleed: true, backgroundImage: { url: (0, utils_1.getNotificationBackground)(totalStatus), fillMode: "RepeatHorizontally", }, }; // Check if we should ping on failure if (!isSuccess) { const mentionData = (0, utils_1.getMentions)(options.mentionOnFailure, options.mentionOnFailureText); if ((mentionData === null || mentionData === void 0 ? void 0 : mentionData.message) && mentionData.mentions.length > 0) { container.items.push({ type: "TextBlock", size: "Medium", text: mentionData.message, wrap: true, }); adaptiveCard.msteams.entities = mentionData.mentions.map((mention) => ({ type: "mention", text: `<at>${mention.email}</at>`, mentioned: { id: mention.email, name: mention.name, }, })); } } // Add the container to the body adaptiveCard.body.push(container); // Get the github actions run URL if (options.linkToResultsUrl) { let linkToResultsUrl; if (typeof options.linkToResultsUrl === "string") { linkToResultsUrl = options.linkToResultsUrl; } else { linkToResultsUrl = options.linkToResultsUrl(); } if (linkToResultsUrl && typeof linkToResultsUrl === "string") { adaptiveCard.actions.push({ type: "Action.OpenUrl", title: options.linkToResultsText, url: linkToResultsUrl, }); } } if (!isSuccess && options.linkTextOnFailure && options.linkUrlOnFailure) { let linkUrlOnFailure; if (typeof options.linkUrlOnFailure === "string") { linkUrlOnFailure = options.linkUrlOnFailure; } else { linkUrlOnFailure = options.linkUrlOnFailure(); } if (linkUrlOnFailure && typeof linkUrlOnFailure === "string") { adaptiveCard.actions.push({ type: "Action.OpenUrl", title: options.linkTextOnFailure, url: linkUrlOnFailure, }); } } if (options.webhookType === "powerautomate") { adaptiveCard.version = "1.4"; } const body = JSON.stringify({ type: "message", attachments: [ { contentType: "application/vnd.microsoft.card.adaptive", contentUrl: null, content: adaptiveCard, }, ], }); if (options.debug) { console.log("Sending the following message:"); console.log(body); } const agent = new https_proxy_agent_1.HttpsProxyAgent('http://enc-proxy-mi.enc.local:8080'); const response = yield (0, node_fetch_1.default)(options.webhookUrl, { method: "POST", headers: { "Content-Type": "application/json", }, body, agent, }); if (response.ok) { if (!options.quiet) { console.log("Message sent successfully"); const responseText = yield response.text(); if (responseText !== "1") { console.log(responseText); } } } else { console.error("Failed to send message"); console.error(yield response.text()); } }); exports.processResults = processResults;