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
|
|
|
|
2023-02-16 00:43:53 +01:00
|
|
|
const WEBHOOK_URL = process.env.WEBHOOK_URL
|
2022-05-18 20:55:41 +02:00
|
|
|
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
|
2023-03-30 11:55:04 +02:00
|
|
|
const REPORT_PATH = path.resolve(__dirname, "..", "testResults.json")
|
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
|
|
|
|
2023-06-09 21:39:14 +02:00
|
|
|
const env = process.argv.slice(2)[0]
|
|
|
|
|
|
|
|
if (!env) {
|
|
|
|
throw new Error("environment argument is required")
|
|
|
|
}
|
|
|
|
|
2023-02-16 00:38:59 +01:00
|
|
|
async function discordResultsNotification(report) {
|
2022-05-20 02:04:52 +02:00
|
|
|
const {
|
2023-02-16 00:38:59 +01:00
|
|
|
numTotalTestSuites,
|
|
|
|
numTotalTests,
|
|
|
|
numPassedTests,
|
|
|
|
numPendingTests,
|
|
|
|
numFailedTests,
|
|
|
|
success,
|
|
|
|
startTime,
|
|
|
|
endTime,
|
|
|
|
} = report
|
2022-05-18 20:55:41 +02:00
|
|
|
|
2023-02-16 00:38:59 +01:00
|
|
|
const OUTCOME = success ? "success" : "failure"
|
2022-08-22 14:15:03 +02:00
|
|
|
|
2022-05-18 20:55:41 +02:00
|
|
|
const options = {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
Accept: "application/json",
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
2023-07-20 16:31:19 +02:00
|
|
|
content: `**Tests Status**: ${OUTCOME}`,
|
2022-05-18 20:55:41 +02:00
|
|
|
embeds: [
|
|
|
|
{
|
2023-06-09 21:39:14 +02:00
|
|
|
title: `Budi QA Bot - ${env}`,
|
|
|
|
description: `API Integration Tests`,
|
2022-05-18 20:55:41 +02:00
|
|
|
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: "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",
|
2023-02-16 00:38:59 +01:00
|
|
|
value: numTotalTestSuites,
|
2022-05-18 20:55:41 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Tests",
|
2023-02-16 00:38:59 +01:00
|
|
|
value: numTotalTests,
|
2022-05-18 20:55:41 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Passed",
|
2023-02-16 00:38:59 +01:00
|
|
|
value: numPassedTests,
|
2022-05-18 20:55:41 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Pending",
|
2023-02-16 00:38:59 +01:00
|
|
|
value: numPendingTests,
|
2022-05-18 20:55:41 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Failures",
|
2023-02-16 00:38:59 +01:00
|
|
|
value: numFailedTests,
|
2022-05-18 20:55:41 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Duration",
|
2023-02-16 00:38:59 +01:00
|
|
|
value: endTime
|
|
|
|
? `${(endTime - startTime) / 1000} Seconds`
|
|
|
|
: "DNF",
|
2022-05-18 20:55:41 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Pass Percentage",
|
2023-02-16 00:38:59 +01:00
|
|
|
value: Math.floor((numPassedTests / numTotalTests) * 100),
|
2022-05-18 20:55:41 +02:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
}
|
2023-02-16 00:38:59 +01:00
|
|
|
|
|
|
|
// Only post in discord when tests fail
|
|
|
|
if (success) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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 14:15:03 +02:00
|
|
|
const report = await generateReport()
|
2023-02-16 00:38:59 +01:00
|
|
|
await discordResultsNotification(report)
|
2022-05-18 20:55:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
run()
|