This commit is contained in:
Joel Male
2021-02-26 14:00:44 +10:00
parent 9380ee26ff
commit 6f6298309b
14 changed files with 284 additions and 4038 deletions
+25 -13
View File
@@ -1,35 +1,47 @@
const fetch = require('node-fetch');
var https = require('https');
const fetch = require('node-fetch')
var https = require('https')
class Http {
make(url: string, headers: string | null, body: string | null, ignoreCertificate: boolean | null): Promise<any> {
make(
url: string,
headers: string | null,
body: string | null,
ignoreCertificate: boolean | null
): Promise<any> {
return new Promise((resolve, reject) => {
fetch(url, this.getOptions('post', headers, body, ignoreCertificate))
.then((res: Response) => resolve(res));
});
fetch(
url,
this.getOptions('post', headers, body, ignoreCertificate)
).then((res: Response) => resolve(res))
})
}
getOptions(method: string, headers: string | null, body: string | null, ignoreCertificate: boolean | null) {
getOptions(
method: string,
headers: string | null,
body: string | null,
ignoreCertificate: boolean | null
) {
const options: any = {
headers: headers ? JSON.parse(headers) : {},
method
};
}
if (body) {
// parse the body
options.body = body;
options.body = body
}
if (ignoreCertificate) {
// ignore the certificate by not rejecting authorized servers
options.agent = new https.Agent({ rejectUnauthorized: false });
options.agent = new https.Agent({rejectUnauthorized: false})
}
// set these headers
options.headers['content-type'] = 'application/json';
options.headers['content-type'] = 'application/json'
return options;
return options
}
}
export const http = new Http();
export const http = new Http()
+56 -39
View File
@@ -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()