webhook-action/src/main.ts

51 lines
1.6 KiB
TypeScript
Raw Normal View History

import * as core from '@actions/core';
import { http } from './http';
async function run() {
2020-08-27 08:25:12 +00:00
const url = core.getInput('url') ? core.getInput('url') : (process.env.WEBHOOK_URL ? process.env.WEBHOOK_URL : '');
2020-08-27 08:23:33 +00:00
const headers = core.getInput('headers') ? core.getInput('headers') : (process.env.headers ? process.env.headers : null);
2020-08-27 08:25:12 +00:00
const body = core.getInput('body') ? core.getInput('body') : (process.env.data ? process.env.data : null);
2020-08-27 08:23:33 +00:00
if (!url) {
// validate a url
core.setFailed('A url is required to run this action.');
2020-08-29 01:10:10 +00:00
// error
throw new Error('A url is required to run this action.');
2020-08-27 08:23:33 +00:00
}
// initial info
2020-08-26 00:25:43 +00:00
core.info(`Sending webhook request to ${url}`);
// debug start
core.debug((new Date()).toTimeString()); // debug is only output if you set the secret `ACTIONS_RUNNER_DEBUG` to true
// make the request
http.make(url, headers, body)
2020-08-26 00:25:43 +00:00
.then((res) => {
2020-08-29 01:10:10 +00:00
// if the status code is not 2xx
if (res.status >= 400) {
// throw an error
error(res.status);
}
2020-08-26 00:25:43 +00:00
// output the status
core.setOutput('statusCode', res.status);
// report on the status code
core.info(`Received status code: ${res.status}`);
2020-08-26 00:38:11 +00:00
// debug end
core.info((new Date()).toTimeString());
})
.catch((err) => {
2020-08-29 01:10:10 +00:00
error(err.status);
2020-08-26 00:25:43 +00:00
});
}
2020-08-29 01:10:10 +00:00
function error(statusCode) {
// set the action to failed
core.setFailed(`Received status code: ${statusCode}`);
// throw an error
throw new Error(`Request failed with status code: ${statusCode}`);
}
run();