mirror of
https://github.com/joelwmale/webhook-action.git
synced 2026-08-24 00:49:00 +08:00
Convert action to ts
- Add optional header support - Add optional body support
This commit is contained in:
+26
@@ -0,0 +1,26 @@
|
||||
class Http {
|
||||
async make(url: string, headers: string, body: string): Promise<any> {
|
||||
return new Promise((resolve, reject) => {
|
||||
fetch(url, this.getOptions('post', headers, body))
|
||||
.then((res) => resolve(res.body))
|
||||
.catch((res) => reject(res.body));
|
||||
});
|
||||
}
|
||||
|
||||
getOptions(method: string, headers: string, body: string) {
|
||||
const options: any = {
|
||||
headers: JSON.parse(headers),
|
||||
method
|
||||
};
|
||||
|
||||
// stringify the body
|
||||
options.body = JSON.stringify(body);
|
||||
|
||||
// set these headers
|
||||
options.headers['content-type'] = 'application/json';
|
||||
|
||||
return options;
|
||||
}
|
||||
}
|
||||
|
||||
export const http = new Http();
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
import * as core from '@actions/core';
|
||||
import { http } from './http';
|
||||
|
||||
// most @actions toolkit packages have async methods
|
||||
async function run() {
|
||||
try {
|
||||
const url = core.getInput('url');
|
||||
const headers = core.getInput('headers') ?? '';
|
||||
const body = core.getInput('body') ?? '';
|
||||
|
||||
// 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) => console.log('hi'));
|
||||
|
||||
// debug end
|
||||
core.info((new Date()).toTimeString());
|
||||
|
||||
// output the time it took
|
||||
core.setOutput('time', new Date().toTimeString());
|
||||
} catch (error) {
|
||||
core.setFailed(error.message);
|
||||
}
|
||||
}
|
||||
|
||||
run();
|
||||
Reference in New Issue
Block a user