Compare commits

..

1 Commits

Author SHA1 Message Date
Frank Jogeleit
6f415ec7c5 Support File Arrays
Signed-off-by: Frank Jogeleit <frank.jogeleit@lovoo.com>
2023-11-30 14:07:10 +01:00
12 changed files with 3169 additions and 1235 deletions

View File

@@ -123,21 +123,31 @@ jobs:
- name: Create Test File
run: |
echo "test" > testfile.txt
echo "test" > testfile1.txt
echo "test" > testfile2.txt
- name: Request Postman Echo POST Multipart
uses: ./
with:
url: 'https://postman-echo.com/post'
method: 'POST'
data: '{ "key": "value" }'
files: '{ "file": "${{ github.workspace }}/testfile.txt" }'
files: '{ "file": "${{ github.workspace }}/testfile1.txt" }'
- name: Request Postman Echo POST Multipart File Array
uses: ./
with:
url: 'https://postman-echo.com/post'
method: 'POST'
data: '{ "key": "value" }'
files: '{ "file": ["${{ github.workspace }}/testfile1.txt", "${{ github.workspace }}/testfile2.txt"] }'
- name: Request Postman Echo POST and persist response
uses: ./
with:
url: 'https://postman-echo.com/post'
method: 'POST'
file: "${{ github.workspace }}/testfile.txt"
file: "${{ github.workspace }}/testfile1.txt"
responseFile: "${{ github.workspace }}/response.json"
- name: Output responseFile
run: |
@@ -148,14 +158,14 @@ jobs:
with:
url: 'https://postman-echo.com/post'
method: 'POST'
files: '{ "file": "${{ github.workspace }}/testfile.txt" }'
files: '{ "file": "${{ github.workspace }}/testfile1.txt" }'
- name: Request Postman Echo POST single file
uses: ./
with:
url: 'https://postman-echo.com/post'
method: 'POST'
file: "${{ github.workspace }}/testfile.txt"
file: "${{ github.workspace }}/testfile1.txt"
- name: Request Postman Echo POST URLEncoded string data
uses: ./

View File

@@ -45,7 +45,6 @@ jobs:
|httpsCert| Client Certificate as string ||
|httpsKey| Client Certificate Key as string ||
|responseFile| Persist the response data to the specified file path ||
|maskResponse| If set to true, the response will be masked in the logs of the action |'false'|
|retry| optional amount of retries if the request is failing, does not retry if the status code is ignored ||
|retryWait| time between each retry in millseconds | 3000 |
@@ -100,4 +99,4 @@ Optionen:
--bearerToken bearer token without Bearer prefix, added as
Authorization header [string]
--timeout request timeout [number] [default: 5000]
```
```

View File

@@ -29,7 +29,7 @@ inputs:
description: 'Auth Password'
required: false
timeout:
description: 'Request Timeout in milliseconds'
description: 'Request Timeout in Sec'
required: false
default: '5000'
bearerToken:
@@ -59,10 +59,6 @@ inputs:
responseFile:
description: 'Persist the response data to the specified file path'
required: false
maskResponse:
description: 'Allows to mark your response as secret and hide the output in the action logs'
required: false
default: 'false'
retry:
description: 'optional amount of retries if the request fails'
required: false
@@ -75,5 +71,5 @@ outputs:
headers:
description: 'HTTP Response Headers'
runs:
using: 'node20'
using: 'node16'
main: 'dist/index.js'

1524
dist/index.js vendored

File diff suppressed because it is too large Load Diff

2777
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -25,7 +25,9 @@
"yargs": "^17.7.2"
},
"dependencies": {
"@actions/core": "^1.10.1"
"@actions/core": "^1.10.1",
"install": "^0.13.0",
"npm": "^10.2.4"
},
"engines": {
"node": ">=16.0.0"

View File

@@ -19,10 +19,6 @@ class GithubActions {
core.setOutput(name, output)
}
setSecret(value) {
core.setSecret(value)
}
setFailed(message) {
core.setFailed(message)
}

View File

@@ -1,15 +0,0 @@
'use strict'
const axios = require('axios');
const { GithubActions } = require('../githubActions');
/**
* @param {GithubActions} actions
*
* @returns {(response: axios.AxiosResponse) => void}
*/
const createMaskHandler = (actions) => (response) => {
actions.setSecret(JSON.stringify(response.data))
}
module.exports = { createMaskHandler }

View File

@@ -1,16 +0,0 @@
'use strict'
const axios = require('axios');
const { GithubActions } = require('../githubActions');
/**
* @param {GithubActions} actions
*
* @returns {(response: axios.AxiosResponse) => void}
*/
const createOutputHandler = (actions) => (response) => {
actions.setOutput('response', JSON.stringify(response.data))
actions.setOutput('headers', response.headers)
}
module.exports = { createOutputHandler }

View File

@@ -24,7 +24,7 @@ const convertToJSON = (value) => {
*
* @returns {FormData}
*/
const convertToFormData = (data, files, convertPaths) => {
const convertToFormData = async (data, files, convertPaths) => {
const formData = new FormData();
for (const [key, value] of Object.entries(data)) {
@@ -32,7 +32,11 @@ const convertToFormData = (data, files, convertPaths) => {
}
for (const [key, value] of Object.entries(files)) {
formData.append(key, fs.createReadStream(value));
if (Array.isArray(value)) {
value.forEach(v => formData.append(key, fs.createReadStream(v)))
} else {
formData.append(key, fs.createReadStream(value));
}
}
return formData;
@@ -55,7 +59,7 @@ const retry = async (callback, options) => {
lastErr = err;
}
if (i < options.retry) {
if (i < options.retries) {
options.actions.warning(`#${i + 1} request failed: ${err}`);
await sleep(options.sleep);
}

View File

@@ -94,10 +94,10 @@ const request = async({ method, instanceConfig, data, files, file, actions, opti
} catch(error) {
if (error.response && options.ignoredCodes.includes(error.response.status)) {
actions.warning(`ignored status code: ${JSON.stringify({ code: error.response.status, message: error.response.data })}`)
return error.response
return null
}
if (!error.response && error.request && options.preventFailureOnNoResponse) {
actions.warning(`no response received: ${JSON.stringify(error)}`);
@@ -119,6 +119,9 @@ const request = async({ method, instanceConfig, data, files, file, actions, opti
return null
}
actions.setOutput('response', JSON.stringify(response.data))
actions.setOutput('headers', response.headers)
return response
} catch (error) {
if ((typeof error === 'object') && (error.isAxiosError === true)) {

View File

@@ -5,10 +5,7 @@ const axios = require('axios');
const https = require('https');
const { request, METHOD_POST } = require('./httpClient');
const { GithubActions } = require('./githubActions');
const { createPersistHandler } = require('./handler/persist');
const { createOutputHandler } = require('./handler/output');
const { createMaskHandler } = require('./handler/mask');
let customHeaders = {}
@@ -76,15 +73,8 @@ if (typeof ignoreStatusCodes === 'string' && ignoreStatusCodes.length > 0) {
ignoredCodes = ignoreStatusCodes.split(',').map(statusCode => parseInt(statusCode.trim()))
}
const actions = new GithubActions();
const handler = [];
if (core.getBooleanInput('maskResponse')) {
handler.push(createMaskHandler(actions))
}
handler.push(createOutputHandler(actions))
const actions = new GithubActions();
if (!!responseFile) {
handler.push(createPersistHandler(responseFile, actions))
@@ -99,7 +89,7 @@ const options = {
}
request({ data, method, instanceConfig, files, file, actions, options }).then(response => {
if (response && typeof response == 'object') {
if (typeof response == 'object') {
handler.forEach(h => h(response))
}
})