This repository has been archived by the owner on Jul 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
113 lines (100 loc) · 2.84 KB
/
index.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
const core = require('@actions/core');
const fs = require('fs');
const glob = require("glob");
const https = require('https');
const branchName = core.getInput('branch-name');
const commitOwner = core.getInput('commit-owner');
const commitSha = core.getInput('commit-sha');
const repoName = core.getInput('repo-name');
const runId = core.getInput('run-id');
const slackWebhookUrl = core.getInput('slack-webhook-url');
function prepareErrorOutput() {
return new Promise((resolve, reject) => {
glob("job-*.txt", {nonull: true}, function (err, files) {
if (err) { reject("Unable to parse files") };
// If there is only one matching file, it is the 'initiate-error-tracking' file
// No other matching files indicates the build ran without issue
if (files.length === 1) { resolve("") };
fullErrors = ""
for (let i=0; i<files.length; i++) {
try {
fullErrors += fs.readFileSync(files[i], 'utf8')
} catch (err) {
reject("Failed to read", err);
}
}
resolve(fullErrors);
})
})
}
function errorNotification (errors) {
return {"blocks":[
{
"type":"header",
"text":{
"type":"plain_text",
"text":`Build failed for commit ${commitSha.substring(0,9)} on ${branchName.replace("refs/heads/", "")}`
}
},
{
"type":"section",
"text":{
"type":"mrkdwn",
"text":`— _Author: ${commitOwner}_ <https://github.com/${repoName}/actions/runs/${runId}|View build>`
}
},
{
"type":"section",
"text":{
"type":"mrkdwn",
"text":`\n ${errors}`
}
}
]}
};
async function makePostRequest(webhookUrl, messageBody) {
return new Promise((resolve, reject) => {
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
}
const req = https.request(webhookUrl, options, res => {
let response = '';
res.on('data', d => {
response += d;
});
res.on('end', () => {
resolve(response);
});
});
req.on('error', err => {
reject(err)
})
req.write(JSON.stringify(messageBody));
req.end();
});
};
async function sendSlackNotification(message) {
try {
await makePostRequest(slackWebhookUrl, message);
console.log("Errors successfully reported to Slack")
} catch (err) {
console.error('There was a error reportig to Slack', err);
}
};
async function prepareAndSendNotification() {
const buildErrors = await prepareErrorOutput()
if (buildErrors === "") {
console.log("Slack message not sent, no build errors to report")
} else {
const slackMessage = errorNotification(buildErrors)
await sendSlackNotification(slackMessage);
}
}
try {
prepareAndSendNotification();
} catch (err) {
core.setFailed(err.message);
}