-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsanity-report-dev11.js
74 lines (59 loc) · 2.08 KB
/
sanity-report-dev11.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const dotenv = require("dotenv");
const fs = require("fs");
dotenv.config();
const user1 = process.env.USER1;
const user2 = process.env.USER2;
const user3 = process.env.USER3;
const user4 = process.env.USER4;
const mochawesomeJsonOutput = fs.readFileSync(
"./mochawesome-report/mochawesome.json",
"utf-8"
);
const mochawesomeReport = JSON.parse(mochawesomeJsonOutput);
const totalTests = mochawesomeReport.stats.tests;
const passedTests = mochawesomeReport.stats.passes;
const failedTests = mochawesomeReport.stats.failures;
let durationInSeconds = Math.floor(mochawesomeReport.stats.duration / 1000);
const durationInMinutes = Math.floor(durationInSeconds / 60);
durationInSeconds %= 60;
const resultMessage =
passedTests === totalTests
? `:white_check_mark: Success (${passedTests} / ${totalTests} Passed)`
: `:x: Failure (${passedTests} / ${totalTests} Passed)`;
const pipelineName = process.env.GO_PIPELINE_NAME;
const pipelineCounter = process.env.GO_PIPELINE_COUNTER;
const goCdServer = process.env.GOCD_SERVER;
const reportUrl = `http://${goCdServer}/go/files/${pipelineName}/${pipelineCounter}/sanity/1/sanity/test-results/mochawesome-report/sanity-report.html`;
let tagUsers = ``;
if (failedTests > 0) {
tagUsers = `<@${user1}> <@${user2}> <@${user3}> <@${user4}>`;
}
const slackMessage = {
text: `Dev11, SDK-Marketplace Sanity
*Result:* ${resultMessage}. ${durationInMinutes}m ${durationInSeconds}s
*Failed Tests:* ${failedTests}
<${reportUrl}|View Report>
${tagUsers}`,
};
const slackWebhookUrl = process.env.SLACK_WEBHOOK_URL;
const sendSlackMessage = async (message) => {
const payload = {
text: message,
};
try {
const response = await fetch(slackWebhookUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
});
if (!response.ok) {
throw new Error(`Error sending message to Slack: ${response.statusText}`);
}
console.log("Message sent to Slack successfully");
} catch (error) {
console.error("Error:", error);
}
};
sendSlackMessage(slackMessage.text);