Skip to content
This repository was archived by the owner on Oct 14, 2020. It is now read-only.

Commit 29c3312

Browse files
committed
Refactore and add update methods in the hook sdk
1 parent 231f4f2 commit 29c3312

File tree

1 file changed

+78
-12
lines changed

1 file changed

+78
-12
lines changed

hook-sdk/nodejs/hook-wrapper.js

+78-12
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,81 @@ const axios = require("axios");
22
const { persist } = require("./persistence/persist");
33
const k8s = require("@kubernetes/client-node");
44

5-
async function main() {
5+
function downloadFile(url) {
6+
return axios.get(url);
7+
}
8+
9+
function getRawResults() {
610
const rawResultUrl = process.argv[2];
7-
const getRawResult = ((url) => () =>
8-
axios.get(url).then(({ data: findings }) => {
9-
console.log(`Fetched ${findings.length} findings from the file storage`);
10-
return findings;
11-
}))(rawResultUrl);
11+
return downloadFile(rawResultUrl).then(({ data }) => {
12+
console.log(`Fetched raw result file contents from the file storage`);
13+
return data;
14+
});
15+
}
1216

17+
function getFindings() {
1318
const findingsUrl = process.argv[3];
14-
const getFindings = ((url) => () =>
15-
axios.get(url).then((res) => {
16-
console.log(`Fetched raw result file contents from the file storage`);
17-
return res.data;
18-
}))(findingsUrl);
19+
return downloadFile(findingsUrl).then(({ data: findings }) => {
20+
console.log(`Fetched ${findings.length} findings from the file storage`);
21+
return findings;
22+
});
23+
}
24+
25+
function uploadFile(url) {
26+
return axios
27+
.put(url, findingsWithIds, {
28+
headers: { "content-type": "" },
29+
})
30+
.catch(function(error) {
31+
if (error.response) {
32+
// The request was made and the server responded with a status code
33+
// that falls out of the range of 2xx
34+
console.error(
35+
`File Upload Failed with Response Code: ${error.response.status}`
36+
);
37+
console.error(`Error Response Body: ${error.response.data}`);
38+
} else if (error.request) {
39+
console.error(
40+
"No response received from FileStorage when uploading finding"
41+
);
42+
console.error(error);
43+
} else {
44+
// Something happened in setting up the request that triggered an Error
45+
console.log("Error", error.message);
46+
}
47+
process.exit(1);
48+
});
49+
}
50+
51+
function uploadRawResults() {
52+
const rawResultUploadUrl = process.argv[4];
53+
if (rawResultUploadUrl === undefined) {
54+
console.error(
55+
"Tried to upload RawResults but didn't find a valid URL to upload the findings to."
56+
);
57+
console.error("This probably means that this hook is a ReadOnly hook.");
58+
console.error(
59+
"If you want to change RawResults you'll need to use a ReadAndWrite Hook."
60+
);
61+
}
62+
return uploadFile(rawResultUploadUrl);
63+
}
1964

65+
function uploadFindings() {
66+
const findingsUploadUrl = process.argv[5];
67+
if (findingsUploadUrl === undefined) {
68+
console.error(
69+
"Tried to upload Findings but didn't find a valid URL to upload the findings to."
70+
);
71+
console.error("This probably means that this hook is a ReadOnly hook.");
72+
console.error(
73+
"If you want to change Findings you'll need to use a ReadAndWrite Hook."
74+
);
75+
}
76+
return uploadFile(findingsUploadUrl);
77+
}
78+
79+
async function main() {
2080
const scanName = process.env["SCAN_NAME"];
2181
const namespace = process.env["NAMESPACE"];
2282
console.log(`Starting PersistenceProvider for Scan "${scanName}"`);
@@ -43,7 +103,13 @@ async function main() {
43103
}
44104

45105
try {
46-
await persist({ getRawResult, getFindings, scan });
106+
await persist({
107+
getRawResult,
108+
getFindings,
109+
uploadRawResults,
110+
uploadFindings,
111+
scan,
112+
});
47113
} catch (error) {
48114
console.error(
49115
"Error was thrown while running PersistenceProviders persist function"

0 commit comments

Comments
 (0)