1
0
Fork 0
mirror of https://github.com/fjogeleit/http-request-action.git synced 2024-12-23 12:46:14 +00:00
http-request-action/src/index.js

51 lines
1.6 KiB
JavaScript
Raw Normal View History

2020-02-24 09:23:15 +00:00
const core = require("@actions/core");
const { request, METHOD_POST } = require('./httpClient');
const { GithubActions } = require('./githubActions');
2020-04-21 11:55: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' }
if (!!core.getInput('username') || !!core.getInput('password')) {
2020-04-21 11:24:39 +00:00
core.debug('Add BasicHTTP Auth config')
auth = {
username: core.getInput('username'),
password: core.getInput('password')
}
2020-02-24 09:23:15 +00:00
}
if (!!core.getInput('bearerToken')) {
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
}
const data = core.getInput('data') || '{}';
const files = core.getInput('files') || '{}';
const method = core.getInput('method') || METHOD_POST;
const preventFailureOnNoResponse = core.getInput('preventFailureOnNoResponse') === 'true';
const escapeData = core.getInput('escapeData') === 'true';
2020-04-21 11:24:39 +00:00
const ignoreStatusCodes = core.getInput('ignoreStatusCodes')
let ignoredCodes = []
if (typeof ignoreStatusCodes === 'string' && ignoreStatusCodes.length > 0) {
ignoredCodes = ignoreStatusCodes.split(',').map(statusCode => parseInt(statusCode.trim()))
}
request({ data, method, instanceConfig, auth, preventFailureOnNoResponse, escapeData, files, ignoredCodes, actions: new GithubActions() })