- Convert project to Javascript/Typescript
- Allow custom headers to be passed in (optional)
- Allow body to be optional
This commit is contained in:
Joel Male
2020-08-26 10:52:47 +10:00
committed by GitHub
parent 749e2dcbc6
commit 1ada95e04a
5740 changed files with 1689439 additions and 50 deletions
+29
View File
@@ -0,0 +1,29 @@
const fetch = require('node-fetch');
class Http {
make(url: string, headers: string, body: string): Promise<any> {
return new Promise((resolve, reject) => {
fetch(url, this.getOptions('post', headers, body))
.then((res: Response) => resolve(res));
});
}
getOptions(method: string, headers: string, body: string) {
const options: any = {
headers: headers ? JSON.parse(headers) : {},
method
};
if (body) {
// parse the body
options.body = body;
}
// set these headers
options.headers['content-type'] = 'application/json';
return options;
}
}
export const http = new Http();
+31
View File
@@ -0,0 +1,31 @@
import * as core from '@actions/core';
import { http } from './http';
async function run() {
const url = core.getInput('url');
const headers = core.getInput('headers') ?? null;
const body = core.getInput('body') ?? null;
// 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
// make the request
http.make(url, headers, body)
.then((res) => {
// 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) => {
// set the action to failed
core.setFailed(`Received status code: ${err.status}`);
});
}
run();