-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.js
55 lines (48 loc) · 1.4 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const core = require('@actions/core');
const webrequest = require('./webrequest');
async function main() {
try {
// inputs from action
const url = core.getInput('url');
const methodInput = core.getInput('method');
const method = methodInput.toLowerCase();
const payloadInput = core.getInput('payload');
const payload = payloadInput ? JSON.parse(payloadInput) : null;
const headersInput = core.getInput('headers');
const headers = headersInput ? JSON.parse(headersInput) : null;
const username = core.getInput('username');
const password = core.getInput('password');
// current time
const time = new Date().toTimeString();
// http request to external API
const response = await webrequest(
url,
method,
payload,
headers,
username,
password
);
const statusCode = response.status;
const data = response.data;
const outputObject = {
url,
method,
payload,
time,
statusCode,
data
};
const consoleOutputJSON = JSON.stringify(outputObject, undefined, 2);
console.log(consoleOutputJSON);
if (statusCode >= 400) {
core.setFailed(`HTTP request failed with status code: ${statusCode}`);
} else {
const outputJSON = JSON.stringify(outputObject);
core.setOutput('output', outputJSON);
}
} catch (error) {
core.setFailed(error.message);
}
}
main();