mirror of
https://github.com/joelwmale/webhook-action.git
synced 2026-09-01 23:10:44 +08:00
v2.1.0
This commit is contained in:
+56
-39
@@ -1,53 +1,70 @@
|
||||
import * as core from '@actions/core';
|
||||
import { http } from './http';
|
||||
import * as core from '@actions/core'
|
||||
import {http} from './http'
|
||||
|
||||
async function run() {
|
||||
const url = core.getInput('url') ? core.getInput('url') : (process.env.WEBHOOK_URL ? process.env.WEBHOOK_URL : '');
|
||||
const headers = core.getInput('headers') ? core.getInput('headers') : (process.env.headers ? process.env.headers : null);
|
||||
const body = core.getInput('body') ? core.getInput('body') : (process.env.data ? process.env.data : null);
|
||||
const insecure = core.getInput('insecure') ? core.getInput('insecure') == 'true' : (process.env.insecure ? process.env.insecure == 'true' : false);
|
||||
const url = core.getInput('url')
|
||||
? core.getInput('url')
|
||||
: process.env.WEBHOOK_URL
|
||||
? process.env.WEBHOOK_URL
|
||||
: ''
|
||||
const headers = core.getInput('headers')
|
||||
? core.getInput('headers')
|
||||
: process.env.headers
|
||||
? process.env.headers
|
||||
: null
|
||||
const body = core.getInput('body')
|
||||
? core.getInput('body')
|
||||
: process.env.data
|
||||
? process.env.data
|
||||
: null
|
||||
const insecure = core.getInput('insecure')
|
||||
? core.getInput('insecure') == 'true'
|
||||
: process.env.insecure
|
||||
? process.env.insecure == 'true'
|
||||
: false
|
||||
|
||||
if (!url) {
|
||||
// validate a url
|
||||
core.setFailed('A url is required to run this action.');
|
||||
// error
|
||||
throw new Error('A url is required to run this action.');
|
||||
}
|
||||
if (!url) {
|
||||
// validate a url
|
||||
core.setFailed('A url is required to run this action.')
|
||||
// error
|
||||
throw new Error('A url is required to run this action.')
|
||||
}
|
||||
|
||||
// initial info
|
||||
core.info(`Sending webhook request to ${url}`);
|
||||
// initial info
|
||||
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
|
||||
// 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, insecure)
|
||||
.then((res) => {
|
||||
// if the status code is not 2xx
|
||||
if (res.status >= 400) {
|
||||
// throw an error
|
||||
error(res.status);
|
||||
return;
|
||||
}
|
||||
// make the request
|
||||
http
|
||||
.make(url, headers, body, insecure)
|
||||
.then(res => {
|
||||
// if the status code is not 2xx
|
||||
if (res.status >= 400) {
|
||||
// throw an error
|
||||
error(res.status)
|
||||
return
|
||||
}
|
||||
|
||||
// output the status
|
||||
core.setOutput('statusCode', res.status);
|
||||
// report on the status code
|
||||
core.info(`Received status code: ${res.status}`);
|
||||
// debug end
|
||||
core.info((new Date()).toTimeString());
|
||||
})
|
||||
.catch((err) => {
|
||||
error(err.status);
|
||||
return;
|
||||
});
|
||||
// output the status
|
||||
core.setOutput('statusCode', res.status)
|
||||
// report on the status code
|
||||
core.info(`Received status code: ${res.status}`)
|
||||
// debug end
|
||||
core.info(new Date().toTimeString())
|
||||
})
|
||||
.catch(err => {
|
||||
error(err.status)
|
||||
return
|
||||
})
|
||||
}
|
||||
|
||||
function error(statusCode) {
|
||||
// set the action to failed
|
||||
core.setFailed(`Received status code: ${statusCode}`);
|
||||
core.setFailed(`Received status code: ${statusCode}`)
|
||||
// throw an error
|
||||
throw new Error(`Request failed with status code: ${statusCode}`);
|
||||
throw new Error(`Request failed with status code: ${statusCode}`)
|
||||
}
|
||||
|
||||
run();
|
||||
run()
|
||||
|
||||
Reference in New Issue
Block a user