2020-02-24 09:23:15 +00:00
|
|
|
const core = require("@actions/core");
|
|
|
|
const axios = require("axios");
|
|
|
|
|
2020-04-21 11:55:30 +00:00
|
|
|
const METHOD_GET = 'GET'
|
|
|
|
const METHOD_POST = 'POST'
|
|
|
|
|
2020-04-21 10:41:30 +00:00
|
|
|
let auth = undefined
|
2020-03-25 10:15:19 +00:00
|
|
|
let customHeaders = {}
|
|
|
|
|
|
|
|
if (!!core.getInput('customHeaders')) {
|
|
|
|
try {
|
|
|
|
customHeaders = JSON.parse(core.getInput('customHeaders'));
|
|
|
|
} catch(error) {
|
|
|
|
core.error('Could not parse customHeaders string value')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-24 09:23:15 +00:00
|
|
|
const headers = { 'Content-Type': core.getInput('contentType') || 'application/json' }
|
|
|
|
|
2020-04-21 10:41:30 +00:00
|
|
|
if (!!core.getInput('username') || !!core.getInput('password')) {
|
2020-04-21 11:24:39 +00:00
|
|
|
core.debug('Add BasicHTTP Auth config')
|
|
|
|
|
2020-04-21 10:41:30 +00:00
|
|
|
auth = {
|
|
|
|
username: core.getInput('username'),
|
|
|
|
password: core.getInput('password')
|
|
|
|
}
|
2020-02-24 09:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!!core.getInput('bearerToken')) {
|
2020-05-06 18:43:17 +00:00
|
|
|
headers['Authorization'] = `Bearer ${core.getInput('bearerToken')}`;
|
2020-02-24 09:23:15 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 11:24:39 +00:00
|
|
|
const instanceConfig = {
|
2020-02-24 09:23:15 +00:00
|
|
|
baseURL: core.getInput('url', { required: true }),
|
|
|
|
timeout: parseInt(core.getInput('timeout') || 5000, 10),
|
2020-03-25 10:15:19 +00:00
|
|
|
headers: { ...headers, ...customHeaders }
|
2020-04-21 11:24:39 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 12:16:49 +00:00
|
|
|
core.debug('Instance Configuration: ' + JSON.stringify(instanceConfig))
|
2020-04-21 11:24:39 +00:00
|
|
|
|
|
|
|
const instance = axios.create(instanceConfig);
|
2020-02-24 09:23:15 +00:00
|
|
|
|
|
|
|
(async() => {
|
|
|
|
try {
|
2020-04-21 11:55:30 +00:00
|
|
|
const method = core.getInput('method') || METHOD_POST;
|
|
|
|
const data = method === METHOD_GET ? undefined : JSON.parse(core.getInput('data') || '{}')
|
|
|
|
|
2020-04-21 11:24:39 +00:00
|
|
|
const requestData = {
|
2020-02-24 09:23:15 +00:00
|
|
|
auth,
|
2020-04-21 11:55:30 +00:00
|
|
|
method,
|
|
|
|
data
|
2020-04-21 11:24:39 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 12:16:49 +00:00
|
|
|
core.debug('Request Data: ' + JSON.stringify(requestData))
|
2020-04-21 11:24:39 +00:00
|
|
|
|
|
|
|
const response = await instance.request(requestData)
|
2020-02-24 09:23:15 +00:00
|
|
|
|
|
|
|
core.setOutput('response', JSON.stringify(response.data))
|
|
|
|
} catch (error) {
|
2020-06-08 17:59:37 +00:00
|
|
|
if (error.toJSON) {
|
|
|
|
core.setOutput(error.toJSON());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error.response) {
|
|
|
|
core.setFailed(JSON.stringify({ code: error.response.code, message: error.response.data }))
|
|
|
|
} else if (error.request) {
|
|
|
|
core.setFailed(JSON.stringify({ error: "no response received" }));
|
|
|
|
} else {
|
|
|
|
core.setFailed(error.message);
|
|
|
|
}
|
2020-02-24 09:23:15 +00:00
|
|
|
}
|
|
|
|
})()
|