/**
* @param webhook the webhook object
* @param webhook.method destination method. Allowed values: "POST", "PUT"
* @param webhook.url current destination address
* @param webhook.eventType current webhook Event Type
* @param webhook.payload JSON payload
* @param webhook.cancel whether to cancel dispatch of the given webhook
*/
function handler(webhook) {
// See https://learn.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/connectors-using#send-adaptive-cards-using-an-incoming-webhook
webhook.payload = summarizeTestCase(webhook.payload);
return webhook;
}
function summarizeTestCase(payload) {
if (!payload || typeof payload !== 'object' || !payload.test_case) {
return {
type: "message",
attachments: [{
contentType: "application/vnd.microsoft.card.adaptive",
contentUrl: null,
content: {
$schema: "http://adaptivecards.io/schemas/adaptive-card.json",
type: "AdaptiveCard",
version: "1.2",
body: [{
type: "TextBlock",
text: "Error: Invalid or missing payload received by Trunk Flaky Test Webhook Transformation.",
color: "attention"
}]
}
}]
};
}
const {
previous_status = "Unknown",
new_status = "Unknown",
timestamp,
repository = {},
test_case: {
name = "N/A",
classname = "",
file_path = "",
quarantined = false,
codeowners = [],
html_url = "N/A"
}
} = payload;
const statusTimestamp = timestamp
? new Date(timestamp).toLocaleString()
: "Unknown";
const subtitle = file_path || classname || "";
return {
type: "message",
attachments: [{
contentType: "application/vnd.microsoft.card.adaptive",
contentUrl: null,
content: {
$schema: "http://adaptivecards.io/schemas/adaptive-card.json",
type: "AdaptiveCard",
version: "1.2",
body: [
{
type: "TextBlock",
text: name,
size: "large",
weight: "bolder"
},
{
type: "TextBlock",
text: subtitle,
isSubtle: true,
spacing: "none"
},
{
type: "FactSet",
facts: [
{
title: "Status",
value: `${previous_status} → ${new_status}`
},
{
title: "Last Updated",
value: statusTimestamp
},
{
title: "Quarantine Status",
value: quarantined ? "Quarantined" : "Not Quarantined"
},
{
title: "Codeowners",
value: codeowners.join(", ") || "None"
}
]
},
{
type: "ActionSet",
actions: [
{
type: "Action.OpenUrl",
title: "View Repository",
url: repository.html_url || "#"
},
{
type: "Action.OpenUrl",
title: "View Test Details",
url: html_url || "#"
}
]
}
]
}
}]
};
}