mirror of
https://github.com/fjogeleit/http-request-action.git
synced 2026-02-05 00:55:52 +08:00
Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f91c5933c4 | ||
|
|
3a9531bb34 | ||
|
|
0cc64a5579 | ||
|
|
81e44c2059 | ||
|
|
cc4fc855b5 | ||
|
|
c7cadd574f | ||
|
|
3634ea0063 | ||
|
|
b63e908234 | ||
|
|
60ab747148 | ||
|
|
2ac119cf97 | ||
|
|
ff8539eb83 |
12
.github/workflows/test.yml
vendored
12
.github/workflows/test.yml
vendored
@@ -23,6 +23,18 @@ jobs:
|
||||
method: 'POST'
|
||||
data: '{ "key": "value" }'
|
||||
|
||||
- name: Request Postment Echo POST with Unescaped Newline
|
||||
uses: ./
|
||||
with:
|
||||
url: 'https://postman-echo.com/post'
|
||||
method: 'POST'
|
||||
escapeData: 'true'
|
||||
data: >-
|
||||
{
|
||||
"key":"multi line\ntest
|
||||
text"
|
||||
}
|
||||
|
||||
- name: Request Postment Echo BasicAuth
|
||||
uses: ./
|
||||
with:
|
||||
|
||||
@@ -28,6 +28,8 @@ jobs:
|
||||
|password| Password for Basic Auth ||
|
||||
|bearerToken| Bearer Authentication Token (without Bearer Prefix) ||
|
||||
|customHeaders| Additional header values as JSON string, keys in this object overwrite default headers like Content-Type |'{}'|
|
||||
|preventFailureOnNoResponse| Prevent this Action to fail if the request respond without an response. Use 'true' (string) as value to enable it ||
|
||||
|escapeData| Escape newlines in data string content. Use 'true' (string) as value to enable it ||
|
||||
|
||||
### Output
|
||||
|
||||
|
||||
@@ -29,6 +29,15 @@ inputs:
|
||||
bearerToken:
|
||||
description: 'Bearer Authentication Token'
|
||||
required: false
|
||||
customHeaders:
|
||||
description: 'Custom HTTP Headers'
|
||||
required: false
|
||||
preventFailureOnNoResponse:
|
||||
description: 'Prevent this Action to fail if the request respond without an response'
|
||||
required: false
|
||||
escapeData:
|
||||
description: 'Escape newlines in data string content'
|
||||
required: false
|
||||
outputs:
|
||||
response:
|
||||
description: 'HTTP Response Content'
|
||||
|
||||
1751
dist/index.js
vendored
1751
dist/index.js
vendored
File diff suppressed because it is too large
Load Diff
44
package-lock.json
generated
44
package-lock.json
generated
@@ -1,48 +1,32 @@
|
||||
{
|
||||
"name": "http-request-action",
|
||||
"version": "1.0.0",
|
||||
"version": "1.6.1",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
"@actions/core": {
|
||||
"version": "1.2.2",
|
||||
"resolved": "https://registry.npmjs.org/@actions/core/-/core-1.2.2.tgz",
|
||||
"integrity": "sha512-IbCx7oefq+Gi6FWbSs2Fnw8VkEI6Y4gvjrYprY3RV//ksq/KPMlClOerJ4jRosyal6zkUIc8R9fS/cpRMlGClg==",
|
||||
"version": "1.2.6",
|
||||
"resolved": "https://registry.npmjs.org/@actions/core/-/core-1.2.6.tgz",
|
||||
"integrity": "sha512-ZQYitnqiyBc3D+k7LsgSBmMDVkOVidaagDG7j3fOym77jNunWRuYx7VSHa9GNfFZh+zh61xsCjRj4JxMZlDqTA==",
|
||||
"dev": true
|
||||
},
|
||||
"@zeit/ncc": {
|
||||
"version": "0.21.1",
|
||||
"resolved": "https://registry.npmjs.org/@zeit/ncc/-/ncc-0.21.1.tgz",
|
||||
"integrity": "sha512-M9WzgquSOt2nsjRkYM9LRylBLmmlwNCwYbm3Up3PDEshfvdmIfqpFNSK8EJvR18NwZjGHE5z2avlDtYQx2JQnw=="
|
||||
"version": "0.22.3",
|
||||
"resolved": "https://registry.npmjs.org/@zeit/ncc/-/ncc-0.22.3.tgz",
|
||||
"integrity": "sha512-jnCLpLXWuw/PAiJiVbLjA8WBC0IJQbFeUwF4I9M+23MvIxTxk5pD4Q8byQBSPmHQjz5aBoA7AKAElQxMpjrCLQ=="
|
||||
},
|
||||
"axios": {
|
||||
"version": "0.19.2",
|
||||
"resolved": "https://registry.npmjs.org/axios/-/axios-0.19.2.tgz",
|
||||
"integrity": "sha512-fjgm5MvRHLhx+osE2xoekY70AhARk3a6hkN+3Io1jc00jtquGvxYlKlsFUhmUET0V5te6CcZI7lcv2Ym61mjHA==",
|
||||
"version": "0.21.1",
|
||||
"resolved": "https://registry.npmjs.org/axios/-/axios-0.21.1.tgz",
|
||||
"integrity": "sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA==",
|
||||
"requires": {
|
||||
"follow-redirects": "1.5.10"
|
||||
}
|
||||
},
|
||||
"debug": {
|
||||
"version": "3.1.0",
|
||||
"resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz",
|
||||
"integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==",
|
||||
"requires": {
|
||||
"ms": "2.0.0"
|
||||
"follow-redirects": "^1.10.0"
|
||||
}
|
||||
},
|
||||
"follow-redirects": {
|
||||
"version": "1.5.10",
|
||||
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.5.10.tgz",
|
||||
"integrity": "sha512-0V5l4Cizzvqt5D44aTXbFZz+FtyXV1vrDN6qrelxtfYQKW0KO0W2T/hkE8xvGa/540LkZlkaUjO4ailYTFtHVQ==",
|
||||
"requires": {
|
||||
"debug": "=3.1.0"
|
||||
}
|
||||
},
|
||||
"ms": {
|
||||
"version": "2.0.0",
|
||||
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
|
||||
"integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g="
|
||||
"version": "1.13.1",
|
||||
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.13.1.tgz",
|
||||
"integrity": "sha512-SSG5xmZh1mkPGyKzjZP8zLjltIfpW32Y5QpdNJyjcfGxK3qo3NDDkZOZSFiGn1A6SclQxY9GzEwAHQ3dmYRWpg=="
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "http-request-action",
|
||||
"version": "1.3.2",
|
||||
"version": "1.6.1",
|
||||
"description": "",
|
||||
"main": "src/index.js",
|
||||
"private": false,
|
||||
@@ -19,10 +19,10 @@
|
||||
},
|
||||
"homepage": "https://github.com/fjogeleit/http-request-action#readme",
|
||||
"devDependencies": {
|
||||
"@actions/core": "^1.2.2"
|
||||
"@actions/core": "^1.2.6"
|
||||
},
|
||||
"dependencies": {
|
||||
"@zeit/ncc": "^0.21.1",
|
||||
"axios": "^0.19.2"
|
||||
"@zeit/ncc": "^0.22",
|
||||
"axios": "^0.21.1"
|
||||
}
|
||||
}
|
||||
|
||||
39
src/githubActions.js
Normal file
39
src/githubActions.js
Normal file
@@ -0,0 +1,39 @@
|
||||
const core = require("@actions/core");
|
||||
|
||||
class GithubActions {
|
||||
debug(message) {
|
||||
core.debug(message)
|
||||
}
|
||||
|
||||
warning(message) {
|
||||
core.warning(message)
|
||||
}
|
||||
|
||||
setOutput(name, output) {
|
||||
core.setOutput(name, output)
|
||||
}
|
||||
|
||||
setFailed(message) {
|
||||
core.setFailed(message)
|
||||
}
|
||||
}
|
||||
|
||||
class LogActions {
|
||||
debug(message) {
|
||||
console.info(message)
|
||||
}
|
||||
|
||||
warning(message) {
|
||||
console.warn(message)
|
||||
}
|
||||
|
||||
setOutput(name, output) {
|
||||
console.log(name, output)
|
||||
}
|
||||
|
||||
setFailed(message) {
|
||||
console.error(message)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { GithubActions, LogActions }
|
||||
52
src/httpClient.js
Normal file
52
src/httpClient.js
Normal file
@@ -0,0 +1,52 @@
|
||||
const axios = require("axios");
|
||||
|
||||
const METHOD_GET = 'GET'
|
||||
const METHOD_POST = 'POST'
|
||||
|
||||
const request = async({ method, instanceConfig, data, auth, actions, preventFailureOnNoResponse, escapeData }) => {
|
||||
try {
|
||||
const instance = axios.create(instanceConfig);
|
||||
|
||||
if (escapeData) {
|
||||
data = data.replace(/"[^"]*"/g, (match) => {
|
||||
return match.replace(/[\n\r]\s*/g, "\\n");
|
||||
});
|
||||
}
|
||||
|
||||
if (method === METHOD_GET) {
|
||||
data = undefined;
|
||||
}
|
||||
|
||||
const requestData = {
|
||||
auth,
|
||||
method,
|
||||
data
|
||||
}
|
||||
|
||||
actions.debug('Request Data: ' + JSON.stringify(requestData))
|
||||
|
||||
const response = await instance.request(requestData)
|
||||
|
||||
actions.setOutput('response', JSON.stringify(response.data))
|
||||
} catch (error) {
|
||||
if (error.toJSON) {
|
||||
actions.setOutput(JSON.stringify(error.toJSON()));
|
||||
}
|
||||
|
||||
if (error.response) {
|
||||
actions.setFailed(JSON.stringify({ code: error.response.code, message: error.response.data }))
|
||||
} else if (error.request && !preventFailureOnNoResponse) {
|
||||
actions.setFailed(JSON.stringify({ error: "no response received" }));
|
||||
} else if (error.request && preventFailureOnNoResponse) {
|
||||
actions.warning(JSON.stringify(error));
|
||||
} else {
|
||||
actions.setFailed(JSON.stringify({ message: error.message, data }));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
request,
|
||||
METHOD_POST,
|
||||
METHOD_GET,
|
||||
}
|
||||
42
src/index.js
42
src/index.js
@@ -1,8 +1,6 @@
|
||||
const core = require("@actions/core");
|
||||
const axios = require("axios");
|
||||
|
||||
const METHOD_GET = 'GET'
|
||||
const METHOD_POST = 'POST'
|
||||
const { request, METHOD_POST } = require('./httpClient');
|
||||
const { GithubActions } = require('./githubActions');
|
||||
|
||||
let auth = undefined
|
||||
let customHeaders = {}
|
||||
@@ -38,35 +36,9 @@ const instanceConfig = {
|
||||
|
||||
core.debug('Instance Configuration: ' + JSON.stringify(instanceConfig))
|
||||
|
||||
const instance = axios.create(instanceConfig);
|
||||
const data = core.getInput('data') || '{}';
|
||||
const method = core.getInput('method') || METHOD_POST;
|
||||
const preventFailureOnNoResponse = core.getInput('preventFailureOnNoResponse') === 'true';
|
||||
const escapeData = core.getInput('escapeData') === 'true';
|
||||
|
||||
(async() => {
|
||||
try {
|
||||
const method = core.getInput('method') || METHOD_POST;
|
||||
const data = method === METHOD_GET ? undefined : JSON.parse(core.getInput('data') || '{}')
|
||||
|
||||
const requestData = {
|
||||
auth,
|
||||
method,
|
||||
data
|
||||
}
|
||||
|
||||
core.debug('Request Data: ' + JSON.stringify(requestData))
|
||||
|
||||
const response = await instance.request(requestData)
|
||||
|
||||
core.setOutput('response', JSON.stringify(response.data))
|
||||
} catch (error) {
|
||||
if (error.toJSON) {
|
||||
core.setOutput(JSON.stringify(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);
|
||||
}
|
||||
}
|
||||
})()
|
||||
request({ data, method, instanceConfig, auth, preventFailureOnNoResponse, escapeData, actions: new GithubActions() })
|
||||
|
||||
Reference in New Issue
Block a user