Skip to content

Commit e25aecb

Browse files
author
Alexis Pierru
committed
Implement support for a sourcemap file in the integration, perform some refactoring
1 parent e26a4f4 commit e25aecb

1 file changed

Lines changed: 142 additions & 134 deletions

File tree

main.ts

Lines changed: 142 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -8,153 +8,161 @@ import fs = require("fs");
88
const maxUploadFiles: number = 3; // no more than 3 files can be uploaded at a time
99
const maxRetries: number = 3; // max number of uploads to retry
1010

11+
async function upload_step_init(dt_upload_api_key: string): Promise<Response> {
12+
return await fetch(
13+
"https://api.securetheorem.com/uploadapi/v1/upload_init",
14+
{
15+
"headers": {
16+
"Accept": "application/json",
17+
"Authorization": "APIKey " + dt_upload_api_key,
18+
"Content-Type": "application/json",
19+
},
20+
"method": "POST",
21+
},
22+
);
23+
}
24+
1125
async function run() {
12-
try {
13-
// Get inputs
14-
// Mandatory
15-
const dt_upload_api_key: string = core.getInput("DT_UPLOAD_API_KEY");
16-
const input_binary_path: string = core.getInput("UPLOAD_BINARY_PATH");
17-
// Optional
18-
const username: string = core.getInput("USERNAME");
19-
const password: string = core.getInput("PASSWORD");
20-
const comments: string = core.getInput("COMMENTS");
21-
const release_id: string = core.getInput("RELEASE_ID");
22-
const platform_variant: string = core.getInput("PLATFORM_VARIANT");
23-
const external_id: string = core.getInput("EXTERNAL_ID");
24-
25-
// Mask the sensitive fields
26-
core.setSecret(dt_upload_api_key);
27-
core.setSecret(password);
28-
29-
// Check that the inputs are set
30-
if (!dt_upload_api_key){
31-
throw new Error(
32-
"DT_UPLOAD_API_KEY must be set!"
33-
);
26+
// Get inputs
27+
// Mandatory
28+
const dt_upload_api_key = core.getInput("DT_UPLOAD_API_KEY");
29+
const input_binary_path = core.getInput("UPLOAD_BINARY_PATH");
30+
const sourcemap_file_path = core.getInput("SOURCEMAP_FILE_PATH");
31+
32+
// Optional
33+
const username = core.getInput("USERNAME");
34+
const password = core.getInput("PASSWORD");
35+
const comments = core.getInput("COMMENTS");
36+
const release_id = core.getInput("RELEASE_ID");
37+
const platform_variant = core.getInput("PLATFORM_VARIANT");
38+
const external_id = core.getInput("EXTERNAL_ID");
39+
40+
// Mask the sensitive fields
41+
core.setSecret(dt_upload_api_key);
42+
core.setSecret(password);
43+
44+
// Check that the inputs are set
45+
if (!dt_upload_api_key){
46+
throw new Error("DT_UPLOAD_API_KEY must be set!");
47+
}
48+
if (!input_binary_path){
49+
throw new Error("UPLOAD_BINARY_PATH must be set!");
50+
}
51+
52+
const files = glob.sync(input_binary_path);
53+
if (!files.length) {
54+
throw new Error("Did not find any files that match path:" + input_binary_path);
55+
}
56+
if (files.length > maxUploadFiles) {
57+
throw new Error(
58+
"Too many files (" + files.length + ") match the provided glob pattern; please write a more restrictive pattern to match no more than " + maxUploadFiles + " files."
59+
);
60+
}
61+
62+
console.log("Found " + files.length + " files to upload.");
63+
64+
// Upload all the files that matched the file path
65+
let file_idx: number = 1;
66+
let output: Array<any> = []
67+
for (const file_path of files) {
68+
if (!fs.existsSync(file_path)) {
69+
throw new Error("Could not access file:" + file_path);
3470
}
35-
if (!input_binary_path){
36-
throw new Error(
37-
"UPLOAD_BINARY_PATH must be set!"
38-
);
71+
72+
console.log("Processing file " + file_path + " (" + file_idx + " of " + files.length + ").");
73+
74+
const form = new FormData();
75+
form.append("file", fs.createReadStream(file_path));
76+
77+
if (sourcemap_file_path) {
78+
form.append("sourcemap", fs.createReadStream(sourcemap_file_path));
3979
}
4080

41-
const files = glob.sync(input_binary_path);
42-
if (!files.length) {
43-
throw new Error(
44-
"Did not find any files that match path:" + input_binary_path
45-
);
81+
// only append optional fields if explicitly set
82+
if (username) {
83+
form.append("username", username);
84+
console.log("DAST username set to: " + username);
4685
}
47-
if (files.length > maxUploadFiles) {
48-
throw new Error(
49-
"Too many files (" + files.length + ") match the provided glob pattern; please write a more restrictive pattern to match no more than " + maxUploadFiles + " files."
50-
);
86+
if (password) {
87+
form.append("password", password);
88+
console.log("DAST password is set to: (hidden)");
89+
}
90+
if (comments) {
91+
form.append("comments", comments);
92+
console.log("Comments are set to: " + comments);
93+
}
94+
if (release_id) {
95+
form.append("release_Id", release_id);
96+
console.log("Release ID is set to: " + release_id);
97+
}
98+
if (platform_variant) {
99+
form.append("platform_variant", platform_variant);
100+
console.log("Platform variant is set to: " + platform_variant);
101+
}
102+
if (external_id) {
103+
form.append("external_id", external_id);
104+
console.log("External ID is set to: " + external_id);
51105
}
52-
53-
console.log("Found " + files.length + " files to upload.");
54-
55-
// Upload all the files that matched the file path
56-
let file_idx: number = 1;
57-
let output: Array<any> = []
58-
for (const file_path of files) {
59-
if (!fs.existsSync(file_path)) {
60-
throw new Error("Could not access file:" + file_path);
61-
}
62106

63-
console.log("Processing file " + file_path + " (" + file_idx + " of " + files.length + ").");
107+
// retry upload maxRetries times
108+
for (let loop_idx = 0; loop_idx < maxRetries; loop_idx++) {
64109

65-
const form = new FormData();
66-
form.append("file", fs.createReadStream(file_path));
67-
// only append optional fields if explicitly set
68-
if(username) {
69-
form.append("username", username);
70-
console.log("DAST username set to: " + username);
71-
}
72-
if(password) {
73-
form.append("password", password);
74-
console.log("DAST password is set to: (hidden)");
75-
}
76-
if(comments) {
77-
form.append("comments", comments);
78-
console.log("Comments are set to: " + comments);
79-
}
80-
if(release_id) {
81-
form.append("release_Id", release_id);
82-
console.log("Release ID is set to: " + release_id);
83-
}
84-
if(platform_variant) {
85-
form.append("platform_variant", platform_variant);
86-
console.log("Platform variant is set to: " + platform_variant);
110+
// Send the auth request to get the upload URL
111+
const auth_response = await upload_step_init(dt_upload_api_key);
112+
113+
let auth_json;
114+
try {
115+
auth_json = await auth_response.json();
116+
} catch (err) {
117+
core.setFailed(err);
87118
}
88-
if(external_id) {
89-
form.append("external_id", external_id);
90-
console.log("External ID is set to: " + external_id);
119+
120+
if (auth_response.status !== 200) {
121+
// handles auth failure
122+
core.setFailed(auth_json);
123+
break;
91124
}
92125

93-
// retry upload maxRetries times
94-
for (let loop_idx = 0; loop_idx < maxRetries; loop_idx++) {
95-
96-
// Send the auth request to get the upload URL
97-
const auth_response = await fetch(
98-
"https://api.securetheorem.com/uploadapi/v1/upload_init",
99-
{
100-
method: "POST",
101-
headers: {
102-
Authorization: "APIKey " + dt_upload_api_key,
103-
Accept: "application/json",
104-
"Content-Type": "application/json",
105-
},
106-
}
107-
);
108-
109-
let auth_json;
110-
try {
111-
auth_json = await auth_response.json();
112-
} catch (err) {
113-
core.setFailed(err);
114-
}
115-
116-
if (auth_response.status !== 200) {
117-
// handles auth failure
118-
core.setFailed(auth_json);
119-
break;
120-
}
121-
122-
// Send the scan request with file
123-
console.log("Starting upload...");
124-
const response = await fetch(auth_json.upload_url, {
125-
method: "POST",
126-
body: form,
127-
});
128-
console.log("Finished upload.");
129-
130-
let jsonformat;
131-
try {
132-
jsonformat = await response.json();
133-
} catch (err) {
134-
core.setFailed(err);
135-
}
136-
output.push(jsonformat)
137-
console.log("Response: HHTP/" + response.status);
138-
console.log(jsonformat);
139-
140-
// Check the response
141-
// If we receive 409 (ownership conflict) or if this is the last try, bail out
142-
if (response.status === 200) {
143-
break;
144-
} else if (response.status === 409) {
145-
core.setFailed(jsonformat);
146-
break;
147-
} else if (loop_idx == (maxRetries - 1)) {
148-
core.setFailed(jsonformat);
149-
}
126+
// Send the scan request with file
127+
console.log("Starting upload...");
128+
const response = await fetch(
129+
auth_json.upload_url,
130+
{
131+
"method": "POST",
132+
"body": form,
133+
},
134+
);
135+
console.log("Finished upload.");
136+
137+
let jsonformat;
138+
try {
139+
jsonformat = await response.json();
140+
} catch (err) {
141+
core.setFailed(err);
142+
}
143+
output.push(jsonformat)
144+
console.log("Response: HTTP/" + response.status);
145+
console.log(jsonformat);
146+
147+
// Check the response
148+
// If we receive 409 (ownership conflict) or if this is the last try, bail out
149+
if (response.status === 200) {
150+
break;
151+
} else if (response.status === 409) {
152+
core.setFailed(jsonformat);
153+
break;
154+
} else if (loop_idx == (maxRetries - 1)) {
155+
core.setFailed(jsonformat);
150156
}
151157
}
152-
153-
core.setOutput("responses", output);
154-
core.setOutput("response", output[0]); // keep the `response` output as the response of the first file upload to maintain compatibility
155-
} catch (err) {
156-
core.setFailed(err.message);
157158
}
159+
160+
core.setOutput("responses", output);
161+
core.setOutput("response", output[0]); // keep the `response` output as the response of the first file upload to maintain compatibility
158162
}
159163

160-
run();
164+
try {
165+
run();
166+
} catch (err) {
167+
core.setFailed(err.message);
168+
}

0 commit comments

Comments
 (0)