2022-11-04 09:28:14 +00:00
|
|
|
'use strict'
|
|
|
|
|
2022-07-19 08:27:31 +00:00
|
|
|
const core = require('@actions/core');
|
|
|
|
const axios = require('axios');
|
|
|
|
const https = require('https');
|
2020-07-30 16:27:27 +00:00
|
|
|
const { request, METHOD_POST } = require('./httpClient');
|
|
|
|
const { GithubActions } = require('./githubActions');
|
2023-02-04 12:28:08 +00:00
|
|
|
const { createPersistHandler } = require('./handler/persist');
|
2020-04-21 11:55:30 +00:00
|
|
|
|
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('bearerToken')) {
|
2020-05-06 18:43:17 +00:00
|
|
|
headers['Authorization'] = `Bearer ${core.getInput('bearerToken')}`;
|
2020-02-24 09:23:15 +00:00
|
|
|
}
|
|
|
|
|
2022-07-19 08:27:31 +00:00
|
|
|
/** @type {axios.AxiosRequestConfig} */
|
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
|
|
|
}
|
|
|
|
|
2022-07-19 08:27:31 +00:00
|
|
|
if (!!core.getInput('httpsCA')) {
|
|
|
|
instanceConfig.httpsAgent = new https.Agent({ ca: core.getInput('httpsCA') })
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!!core.getInput('username') || !!core.getInput('password')) {
|
|
|
|
core.debug('Add BasicHTTP Auth config')
|
|
|
|
|
|
|
|
instanceConfig.auth = {
|
|
|
|
username: core.getInput('username'),
|
|
|
|
password: core.getInput('password')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-30 16:27:27 +00:00
|
|
|
const data = core.getInput('data') || '{}';
|
2021-01-24 12:14:13 +00:00
|
|
|
const files = core.getInput('files') || '{}';
|
2022-02-10 03:14:07 +00:00
|
|
|
const file = core.getInput('file')
|
2023-02-04 12:28:08 +00:00
|
|
|
const responseFile = core.getInput('responseFile')
|
2020-07-30 16:27:27 +00:00
|
|
|
const method = core.getInput('method') || METHOD_POST;
|
|
|
|
const preventFailureOnNoResponse = core.getInput('preventFailureOnNoResponse') === 'true';
|
2020-10-07 16:29:38 +00:00
|
|
|
const escapeData = core.getInput('escapeData') === 'true';
|
2020-04-21 11:24:39 +00:00
|
|
|
|
2023-02-04 12:28:08 +00:00
|
|
|
const ignoreStatusCodes = core.getInput('ignoreStatusCodes');
|
|
|
|
let ignoredCodes = [];
|
2021-03-19 16:28:53 +00:00
|
|
|
|
|
|
|
if (typeof ignoreStatusCodes === 'string' && ignoreStatusCodes.length > 0) {
|
|
|
|
ignoredCodes = ignoreStatusCodes.split(',').map(statusCode => parseInt(statusCode.trim()))
|
|
|
|
}
|
|
|
|
|
2023-02-04 12:28:08 +00:00
|
|
|
const handler = [];
|
|
|
|
const actions = new GithubActions();
|
|
|
|
|
|
|
|
if (!!responseFile) {
|
|
|
|
handler.push(createPersistHandler(responseFile, actions))
|
|
|
|
}
|
|
|
|
|
|
|
|
request({ data, method, instanceConfig, preventFailureOnNoResponse, escapeData, files, file, ignoredCodes, actions }).then(response => {
|
|
|
|
if (typeof response == 'object') {
|
|
|
|
handler.forEach(h => h(response))
|
|
|
|
}
|
|
|
|
})
|