Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check failed actions #62

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions check-failed-actions/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: Check Failed Actions
description: Check for any failed actions
runs:
using: node16
pre: '../setup.mjs'
main: ../build/check-failed-actions/index.js
20 changes: 15 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"@types/debug": "^4.1.7",
"@types/semver": "^7.3.13",
"@types/uuid": "^9.0.1",
"get-port": "^5.1.1",
"nock": "^13.3.0",
"rimraf": "^4.4.0",
"uuid": "^9.0.0"
Expand Down
65 changes: 65 additions & 0 deletions src/check-failed-actions/check-failed.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// check-failed-actions/check-failed.spec.ts

import { strict as assert } from 'node:assert';
import os from 'node:os';
import path from 'node:path';
import fs from 'node:fs/promises';
import http from 'node:http';
import { v4 as uuid } from 'uuid';
import getPort from 'get-port';

import checkFailed from './check-failed';

describe('github', () => {
const tmpFile = path.join(os.tmpdir(), uuid());
let server: http.Server;
let port: number;
let lastRequest: string;

function setupServer() {
return http
.createServer((req, res) => {
let requestBody = '';
req.on('data', (chunk) => {
requestBody += chunk;
});
req.on('end', () => {
lastRequest = requestBody;
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ message: 'Request body received successfully' }));
});
})
.listen(port, '::1');
}

beforeAll(async () => {
await fs.writeFile(tmpFile, JSON.stringify({ foo: 'bar' }));
port = await getPort();
server = setupServer();
});

afterAll(async () => {
await fs.rm(tmpFile);
server.close();
});

it('Error - slack notified', async () => {
process.env['GITHUB_EVENT_PATH'] = tmpFile;
process.env['GITHUB_REPOSITORY'] = 'owner/repo';
process.env['INPUT_FAILED'] = 'true';
process.env['SLACK_FAILED_URL'] = `http://[::1]:${port}`;

await checkFailed();
assert.equal(lastRequest.includes('owner/repo'), true);
assert.equal(lastRequest.includes('*owner/repo - action failure*'), true);
});

it('No error - slack not called', async () => {
process.env['GITHUB_EVENT_PATH'] = tmpFile;
process.env['GITHUB_REPOSITORY'] = 'owner/repo';
process.env['INPUT_FAILED'] = 'false';

await checkFailed();
assert.ok(true);
});
});
30 changes: 30 additions & 0 deletions src/check-failed-actions/check-failed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// check-failed-actions/check-failed.ts

import process from 'node:process';
import { debug } from 'debug';
import { getInput } from '@actions/core';

import { getPullRequestContext } from '../github-api';
import slackPost from './slack';

const log = debug('check-failed-action');
export default async function (): Promise<void | boolean> {
log('Action starting');

const githubContext = await getPullRequestContext();
if (!githubContext) {
log('Error - unable to get github context');
return;
}

const workFlowName = process.env['GITHUB_WORKFLOW'] ?? 'unknown';
log('GITHUB_WORKFLOW', workFlowName);

const branch = process.env['GITHUB_REF'] ?? 'unknown';

const failedJob = getInput('failed');
log('Status received', failedJob);
if (failedJob === 'true') {
await slackPost(`${githubContext.owner}/${githubContext.repo}`, branch, workFlowName);
}
}
17 changes: 17 additions & 0 deletions src/check-failed-actions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// check-failed-actions/index.ts

import main from './check-failed';

main()
.then(() => {
process.stdin.destroy();
// eslint-disable-next-line unicorn/no-process-exit
process.exit(0);
})
// eslint-disable-next-line unicorn/prefer-top-level-await
.catch((error) => {
// eslint-disable-next-line no-console
console.log('Action Error - exit 1 - error:', error);
// eslint-disable-next-line unicorn/no-process-exit
process.exit(1);
});
46 changes: 46 additions & 0 deletions src/check-failed-actions/slack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// check-failed-actions/slack.ts

import { strict as assert } from 'node:assert';
import debug from 'debug';
import { fetch } from 'undici';

const log = debug('check-failed-action:slack');

export interface SlackMessage {
text: string;
attachments: [
{
color: string;
text: string;
}
];
}

async function postSlackMessage(slackMessage: SlackMessage): Promise<void> {
try {
const slackUrl = process.env['SLACK_FAILED_URL'];
assert(slackUrl);
log('slack HTTP POST request options: ', JSON.stringify(slackMessage));
await fetch(slackUrl, {
method: 'POST',
body: JSON.stringify(slackMessage),
headers: { 'Content-Type': 'application/json' },
});
} catch (slackPostError) {
log(`slack HTTP POST Error: ${String(slackPostError)}`);
throw new Error(`slack HTTP POST Error: ${String(slackPostError)}`);
}
}

export default async function (repoName: string, branch: string, actionName: string): Promise<void> {
const slackMessage: SlackMessage = {
text: `*${repoName} - action failure*`,
attachments: [
{
color: 'danger',
text: `*Details* \n - Action ${actionName} in ${repoName} in ${branch} has failed*`,
},
],
};
await postSlackMessage(slackMessage);
}