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-12-17 14:41:37 +00:00
|
|
|
|
2023-02-04 12:28:08 +00:00
|
|
|
const { createPersistHandler } = require('./handler/persist');
|
2023-12-17 14:41:37 +00:00
|
|
|
const { createOutputHandler } = require('./handler/output');
|
|
|
|
const { createMaskHandler } = require('./handler/mask');
|
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) {
|
2023-03-29 08:53:51 +00:00
|
|
|
core.debug(`Invalid customHeaders string: ${core.getInput('customHeaders')}`)
|
|
|
|
core.error(`Could not parse customHeaders string value: ${error}`)
|
2020-03-25 10:15:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-08-10 08:55:12 +00:00
|
|
|
if (!!core.getInput('httpsCA') || !!core.getInput('httpsCert')) {
|
|
|
|
instanceConfig.httpsAgent = new https.Agent({
|
|
|
|
ca: core.getInput('httpsCA') || undefined,
|
|
|
|
cert: core.getInput('httpsCert') || undefined,
|
|
|
|
key: core.getInput('httpsKey') || undefined
|
|
|
|
})
|
2022-07-19 08:27:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!!core.getInput('username') || !!core.getInput('password')) {
|
|
|
|
core.debug('Add BasicHTTP Auth config')
|
|
|
|
|
|
|
|
instanceConfig.auth = {
|
|
|
|
username: core.getInput('username'),
|
|
|
|
password: core.getInput('password')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-06 15:07:02 +00:00
|
|
|
let retry = 0
|
|
|
|
if (!!core.getInput('retry')) {
|
|
|
|
retry = parseInt(core.getInput('retry'))
|
|
|
|
}
|
|
|
|
|
|
|
|
let retryWait = 3000
|
|
|
|
if (!!core.getInput('retryWait')) {
|
|
|
|
retry = parseInt(core.getInput('retryWait'))
|
|
|
|
}
|
|
|
|
|
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 actions = new GithubActions();
|
|
|
|
|
2023-12-17 14:41:37 +00:00
|
|
|
const handler = [];
|
|
|
|
|
|
|
|
if (core.getBooleanInput('maskResponse')) {
|
|
|
|
handler.push(createMaskHandler(actions))
|
|
|
|
}
|
|
|
|
|
|
|
|
handler.push(createOutputHandler(actions))
|
|
|
|
|
2023-02-04 12:28:08 +00:00
|
|
|
if (!!responseFile) {
|
|
|
|
handler.push(createPersistHandler(responseFile, actions))
|
|
|
|
}
|
|
|
|
|
2023-03-06 15:07:02 +00:00
|
|
|
const options = {
|
|
|
|
ignoredCodes,
|
|
|
|
preventFailureOnNoResponse,
|
|
|
|
escapeData,
|
|
|
|
retry,
|
|
|
|
retryWait
|
|
|
|
}
|
|
|
|
|
|
|
|
request({ data, method, instanceConfig, files, file, actions, options }).then(response => {
|
2023-12-17 14:41:37 +00:00
|
|
|
if (response && typeof response == 'object') {
|
2023-02-04 12:28:08 +00:00
|
|
|
handler.forEach(h => h(response))
|
|
|
|
}
|
|
|
|
})
|