Skip to content

Commit

Permalink
fix(MULTIPLE-API-CALLS) merge multiple upload-api calls into one
Browse files Browse the repository at this point in the history
  • Loading branch information
dushimsam committed Aug 12, 2022
1 parent 133c1af commit 3e1f574
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 41 deletions.
45 changes: 43 additions & 2 deletions src/api/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,57 @@ export const createUploadApi = (
};

// Create Uploads from Version Control System
export const createUploadVcsApi = (header, body) => {
export const createUploadVcsApi = (header, vcsData, scanData) => {
const url = endpoints.upload.uploadCreate();
const { bucket, copyrightEmailAuthor, ecc, keyword, mime, monk, nomos, ojo } =
scanData?.analysis;
const { nomosMonk, bulkReused, newScanner, ojoDecider } = scanData?.decider;
const {
reuseUpload,
reuseGroup,
reuseMain,
reuseEnhanced,
reuseReport,
reuseCopyright,
} = scanData?.reuse;

return sendRequest({
url,
method: "POST",
headers: {
...header,
Authorization: getToken(),
},
body,
body: {
vcsData,
scanOptions: {
analysis: {
bucket,
copyright_email_author: copyrightEmailAuthor,
ecc,
keyword,
mime,
monk,
nomos,
ojo,
package: scanData.analysis.package,
},
decider: {
nomos_monk: nomosMonk,
bulk_reused: bulkReused,
new_scanner: newScanner,
ojo_decider: ojoDecider,
},
reuse: {
reuse_upload: reuseUpload,
reuse_group: reuseGroup,
reuse_main: reuseMain,
reuse_enhanced: reuseEnhanced,
reuse_report: reuseReport,
reuse_copyright: reuseCopyright,
},
},
},
});
};

Expand Down
62 changes: 59 additions & 3 deletions src/api/upload.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,38 @@ describe("upload", () => {

test("createUploadVcsApi", () => {
const header = "header";
const body = "body";
const vcsData = "vcsData";
const scanData = {
analysis: {
bucket: "bucket",
copyrightEmailAuthor: "copyrightEmailAuthor",
ecc: "ecc",
keyword: "keyword",
mime: "mime",
monk: "monk",
nomos: "nomos",
ojo: "ojo",
package: "package",
},
decider: {
nomosMonk: "nomosMonk",
bulkReused: "bulkReused",
newScanner: "newScanner",
ojoDecider: "ojoDecider",
},
reuse: {
reuseUpload: "reuseUpload",
reuseGroup: "reuseGroup",
reuseMain: "reuseMain",
reuseEnhanced: "reuseEnhanced",
reuseReport: "reuseReport",
reuseCopyright: "reuseCopyright",
},
};
const url = endpoints.upload.uploadCreate();
sendRequest.mockImplementation(() => true);

expect(createUploadVcsApi(header, body)).toBe(sendRequest({}));
expect(createUploadVcsApi(header, vcsData, scanData)).toBe(sendRequest({}));
expect(sendRequest).toHaveBeenCalledWith(
expect.objectContaining({
url,
Expand All @@ -117,7 +144,36 @@ describe("upload", () => {
...header,
Authorization: getToken(),
},
body,
body: {
vcsData,
scanOptions: {
analysis: {
bucket: scanData.analysis.bucket,
copyright_email_author: scanData.analysis.copyrightEmailAuthor,
ecc: scanData.analysis.ecc,
keyword: scanData.analysis.keyword,
mime: scanData.analysis.mime,
monk: scanData.analysis.monk,
nomos: scanData.analysis.nomos,
ojo: scanData.analysis.ojo,
package: scanData.analysis.package,
},
decider: {
nomos_monk: scanData.decider.nomosMonk,
bulk_reused: scanData.decider.bulkReused,
new_scanner: scanData.decider.newScanner,
ojo_decider: scanData.decider.ojoDecider,
},
reuse: {
reuse_upload: scanData.reuse.reuseUpload,
reuse_group: scanData.reuse.reuseGroup,
reuse_main: scanData.reuse.reuseMain,
reuse_enhanced: scanData.reuse.reuseEnhanced,
reuse_report: scanData.reuse.reuseReport,
reuse_copyright: scanData.reuse.reuseCopyright,
},
},
},
})
);
});
Expand Down
36 changes: 5 additions & 31 deletions src/pages/Upload/Vcs/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*/

import React, { useState, useEffect } from "react";
import messages from "constants/messages";

// Title
import Title from "components/Title";
Expand All @@ -30,8 +29,7 @@ import CommonFields from "components/Upload/CommonFields";

// Required functions for calling APIs
import { getAllFolders } from "services/folders";
import { createUploadVcs, getUploadById } from "services/upload";
import { scheduleAnalysis } from "services/jobs";
import { createUploadVcs } from "services/upload";

// Default Agents list
import {
Expand All @@ -44,11 +42,9 @@ import {

// Helper function for error handling
import { handleError } from "shared/helper";
import messages from "../../../constants/messages";

const UploadFromVcs = () => {
// Upload Id required for scheduling Analysis
let uploadId;

// Data required for creating the upload
const [uploadVcsData, setUploadVcsData] = useState(initialStateVcs);

Expand All @@ -69,36 +65,14 @@ const UploadFromVcs = () => {
const handleSubmit = (e) => {
e.preventDefault();
setLoading(true);
createUploadVcs(uploadVcsData, vcsData)
.then((res) => {
createUploadVcs(uploadVcsData, vcsData, scanFileData)
.then(() => {
window.scrollTo({ top: 0 });
setMessage({
type: "success",
text: `${messages.queuedUpload} #${res.message}`,
text: `${messages.uploadSuccess}`,
});
uploadId = res.message;
})
// Calling the api for maximum 10 times to check whether the upload is unpacked by the agent
.then(() => getUploadById(uploadId, 10))
.then(() =>
setTimeout(
() =>
scheduleAnalysis(uploadVcsData.folderId, uploadId, scanFileData)
.then(() => {
window.scrollTo({ top: 0 });
setMessage({
type: "success",
text: messages.scheduledAnalysis,
});
setUploadVcsData(initialStateVcs);
setScanFileData(initialScanFileData);
})
.catch((error) => {
handleError(error, setMessage);
}),
200000
)
)
.catch((error) => {
handleError(error, setMessage);
})
Expand Down
10 changes: 5 additions & 5 deletions src/services/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ export const createUploadFile = ({
};

// Create Uploads from Version Control System
export const createUploadVcs = (header, body) => {
return createUploadVcsApi(header, body).then((res) => {
export const createUploadVcs = (header, vcsData, scanData) => {
return createUploadVcsApi(header, vcsData, scanData).then((res) => {
return res;
});
};
Expand All @@ -55,21 +55,21 @@ export const createUploadUrl = (header, body) => {
});
};

// Getting a Upload by id
// Getting an Upload by id
export const getUploadById = (uploadId, retries) => {
return getUploadByIdApi(uploadId, retries).then((res) => {
return res;
});
};

// Getting a Upload Summary
// Getting an Upload Summary
export const getUploadSummary = (uploadId) => {
return getUploadSummaryApi(uploadId).then((res) => {
return res;
});
};

// Getting a Upload License
// Getting an Upload License
export const getUploadLicense = (uploadId, agent) => {
return getUploadLicenseApi(uploadId, agent).then((res) => {
return res;
Expand Down

0 comments on commit 3e1f574

Please sign in to comment.