2022-05-18 20:55:41 +02:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
const fetch = require("node-fetch")
|
|
|
|
const path = require("path")
|
2022-05-20 17:47:09 +02:00
|
|
|
const fs = require("fs")
|
2022-05-18 20:55:41 +02:00
|
|
|
|
|
|
|
const WEBHOOK_URL = process.env.CYPRESS_WEBHOOK_URL
|
|
|
|
const OUTCOME = process.env.CYPRESS_OUTCOME
|
|
|
|
const DASHBOARD_URL = process.env.CYPRESS_DASHBOARD_URL
|
|
|
|
const GIT_SHA = process.env.GITHUB_SHA
|
|
|
|
const GITHUB_ACTIONS_RUN_URL = process.env.GITHUB_ACTIONS_RUN_URL
|
|
|
|
|
2022-05-20 02:04:52 +02:00
|
|
|
async function generateReport() {
|
|
|
|
// read the report file
|
|
|
|
const REPORT_PATH = path.resolve(
|
|
|
|
__dirname,
|
|
|
|
"..",
|
|
|
|
"cypress",
|
|
|
|
"reports",
|
2022-05-20 17:47:09 +02:00
|
|
|
"testReport.json"
|
2022-05-20 02:04:52 +02:00
|
|
|
)
|
2022-05-20 17:47:09 +02:00
|
|
|
const report = fs.readFileSync(REPORT_PATH, "utf-8")
|
|
|
|
return JSON.parse(report)
|
2022-05-20 02:04:52 +02:00
|
|
|
}
|
2022-05-18 20:55:41 +02:00
|
|
|
|
2022-05-20 02:04:52 +02:00
|
|
|
async function discordCypressResultsNotification(report) {
|
|
|
|
const {
|
|
|
|
suites,
|
|
|
|
tests,
|
|
|
|
passes,
|
|
|
|
pending,
|
|
|
|
failures,
|
|
|
|
duration,
|
|
|
|
passPercent,
|
|
|
|
skipped,
|
|
|
|
} = report.stats
|
2022-05-18 20:55:41 +02:00
|
|
|
|
|
|
|
const options = {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
Accept: "application/json",
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
content: `**Nightly Tests Status**: ${OUTCOME}`,
|
|
|
|
embeds: [
|
|
|
|
{
|
|
|
|
title: "Budi QA Bot",
|
|
|
|
description: `Nightly Tests`,
|
|
|
|
url: GITHUB_ACTIONS_RUN_URL,
|
|
|
|
color: OUTCOME === "success" ? 3066993 : 15548997,
|
|
|
|
timestamp: new Date(),
|
|
|
|
footer: {
|
|
|
|
icon_url: "http://bbui.budibase.com/budibase-logo.png",
|
|
|
|
text: "Budibase QA Bot",
|
|
|
|
},
|
|
|
|
thumbnail: {
|
|
|
|
url: "http://bbui.budibase.com/budibase-logo.png",
|
|
|
|
},
|
|
|
|
author: {
|
|
|
|
name: "Budibase QA Bot",
|
|
|
|
url: "https://discordapp.com",
|
|
|
|
icon_url: "http://bbui.budibase.com/budibase-logo.png",
|
|
|
|
},
|
|
|
|
fields: [
|
|
|
|
{
|
|
|
|
name: "Commit",
|
2022-05-20 17:47:09 +02:00
|
|
|
value: `https://github.com/Budibase/budibase/commit/${GIT_SHA}`,
|
2022-05-18 20:55:41 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Cypress Dashboard URL",
|
|
|
|
value: DASHBOARD_URL || "None Supplied",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Github Actions Run URL",
|
2022-05-20 17:47:09 +02:00
|
|
|
value: GITHUB_ACTIONS_RUN_URL || "None Supplied",
|
2022-05-18 20:55:41 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test Suites",
|
|
|
|
value: suites,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Tests",
|
|
|
|
value: tests,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Passed",
|
|
|
|
value: passes,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Pending",
|
|
|
|
value: pending,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Skipped",
|
|
|
|
value: skipped,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Failures",
|
|
|
|
value: failures,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Duration",
|
|
|
|
value: `${duration / 1000} Seconds`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Pass Percentage",
|
2022-05-20 17:47:09 +02:00
|
|
|
value: Math.floor(passPercent),
|
2022-05-18 20:55:41 +02:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
const response = await fetch(WEBHOOK_URL, options)
|
|
|
|
|
2022-08-22 13:34:40 +02:00
|
|
|
if (response.status >= 201) {
|
2022-05-18 20:55:41 +02:00
|
|
|
const text = await response.text()
|
|
|
|
console.error(
|
|
|
|
`Error sending discord webhook. \nStatus: ${response.status}. \nResponse Body: ${text}. \nRequest Body: ${options.body}`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function run() {
|
2022-08-22 13:34:40 +02:00
|
|
|
try {
|
|
|
|
const report = await generateReport()
|
|
|
|
await discordCypressResultsNotification(report)
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
|
|
|
}
|
2022-05-18 20:55:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
run()
|