prettier-playwright-msteams-report
Version:
A modified version of the Playwright MS Teams Messager
156 lines (155 loc) • 6.19 kB
JavaScript
"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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.processResults = void 0;
const utils_1 = require("./utils");
const constants_1 = require("./constants");
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 totalTests = suite.allTests().length;
const isSuccess = totalStatus.failed === 0;
if (isSuccess && !options.notifyOnSuccess) {
if (!options.quiet) {
console.log("No failed tests, skipping notification");
}
return;
}
// Populate the table with test results
table.rows.push((0, utils_1.createTableRow)("Type", "Total"));
table.rows.push((0, utils_1.createTableRow)("✅ Passed", totalStatus.passed, { style: "good" }));
if (totalStatus.flaky) {
table.rows.push((0, utils_1.createTableRow)("⚠️ Flaky", totalStatus.flaky, { style: "warning" }));
}
table.rows.push((0, utils_1.createTableRow)("❌ Failed", totalStatus.failed, { style: "attention" }));
table.rows.push((0, utils_1.createTableRow)("⏭️ Skipped", totalStatus.skipped, { style: "accent" }));
table.rows.push((0, utils_1.createTableRow)("Total tests", totalTests, { isSubtle: true, weight: "Bolder" }));
// Add the table to the card
adaptiveCard.body.push({
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) {
adaptiveCard.body.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 action buttons
if (options.linkToResultsUrl) {
const linkToResultsUrl = typeof options.linkToResultsUrl === "string" ? options.linkToResultsUrl : options.linkToResultsUrl();
if (linkToResultsUrl) {
adaptiveCard.actions.push({
type: "Action.OpenUrl",
title: options.linkToResultsText,
url: linkToResultsUrl,
});
}
}
if (!isSuccess && options.linkTextOnFailure && options.linkUrlOnFailure) {
const linkUrlOnFailure = typeof options.linkUrlOnFailure === "string" ? options.linkUrlOnFailure : options.linkUrlOnFailure();
if (linkUrlOnFailure) {
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 response = yield fetch(options.webhookUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body,
});
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;