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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
66 changed files
with
6,462 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
name: 'Slack errors' | ||
description: 'Send build errors summary message to Slack' | ||
inputs: | ||
slack-webhook-url: | ||
description: 'Slack webhook URL' | ||
required: true | ||
default: '' | ||
branch-name: | ||
description: 'Branch name' | ||
required: true | ||
default: '' | ||
commit-sha: | ||
description: 'Run commit SHA' | ||
required: true | ||
default: '' | ||
commit-owner: | ||
description: 'Owner of commit' | ||
required: true | ||
default: '' | ||
repo-name: | ||
description: 'Repository name' | ||
required: true | ||
default: '' | ||
run-id: | ||
description: 'Build run ID' | ||
required: true | ||
default: '' | ||
runs: | ||
using: 'node12' | ||
main: 'index.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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":`CI 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); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.