diff --git a/src/api/upload.js b/src/api/upload.js index 64783b2c3..0f0af8489 100644 --- a/src/api/upload.js +++ b/src/api/upload.js @@ -1,5 +1,6 @@ /* Copyright (C) 2021 Shruti Agarwal (mail2shruti.ag@gmail.com), Aman Dwivedi (aman.dwivedi5@gmail.com) + Copyright (C) 2022 Samuel Dushimimana (dushsam100@gmail.com) SPDX-License-Identifier: GPL-2.0 @@ -26,17 +27,55 @@ import { getToken } from "shared/authHelper"; import sendRequest from "./sendRequest"; // Create Uploads from File -export const createUploadApi = ( - folderId, - uploadDescription, - accessLevel, - ignoreScm, - fileInput -) => { +export const createUploadApi = (uploadFileData, scanData) => { + const { folderId, uploadDescription, accessLevel, ignoreScm, fileInput } = + uploadFileData; + + 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; + const url = endpoints.upload.uploadCreate(); const formdata = new FormData(); + const 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, + }, + }; + if (fileInput) { formdata.append("fileInput", fileInput, fileInput?.name); + formdata.append("scanOptions", JSON.stringify(scanOptions)); } return sendRequest({ url, @@ -48,15 +87,27 @@ export const createUploadApi = ( uploadDescription, public: accessLevel, ignoreScm, - uploadType: "", + uploadType: "file", }, body: formdata, }); }; // 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", @@ -64,13 +115,54 @@ export const createUploadVcsApi = (header, body) => { ...header, Authorization: getToken(), }, - body, + body: { + data: 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, + }, + }, + }, }); }; // Create Uploads from URL -export const createUploadUrlApi = (header, body) => { +export const createUploadUrlApi = (header, urlData, 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", @@ -78,11 +170,40 @@ export const createUploadUrlApi = (header, body) => { ...header, Authorization: getToken(), }, - body, + body: { + data: urlData, + 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, + }, + }, + }, }); }; -// Getting a Upload by id +// Getting an Upload by id export const getUploadByIdApi = (uploadId, retries) => { const url = endpoints.upload.getId(uploadId); return sendRequest({ @@ -95,7 +216,7 @@ export const getUploadByIdApi = (uploadId, retries) => { }); }; -// Getting a Upload Summary +// Getting an Upload Summary export const getUploadSummaryApi = (uploadId) => { const url = endpoints.upload.getSummary(uploadId); return sendRequest({ @@ -107,7 +228,7 @@ export const getUploadSummaryApi = (uploadId) => { }); }; -// Getting a Upload License +// Getting an Upload License export const getUploadLicenseApi = (uploadId, agent) => { const url = endpoints.upload.getLicense(uploadId); return sendRequest({ diff --git a/src/api/upload.test.js b/src/api/upload.test.js index 7228c04f8..c55083ebe 100644 --- a/src/api/upload.test.js +++ b/src/api/upload.test.js @@ -1,5 +1,7 @@ /* Copyright (C) 2021 Edgar Sherman (edgarshermangh14@gmail.com) + Copyright (C) 2022 Samuel Dushimimana (dushsam100@gmail.com) + SPDX-License-Identifier: GPL-2.0 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License @@ -27,23 +29,48 @@ jest.mock("api/sendRequest"); describe("upload", () => { test("createUploadApi without fileInput", () => { - const folderId = 1; - const uploadDescription = "uploadDescription"; - const accessLevel = 2; - const ignoreScm = true; - const fileInput = null; + const uploadFileData = { + uploadDescription: "test", + folderId: 1, + accessLevel: 2, + ignoreScm: true, + fileInput: null, + }; + const scanOptions = { + 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 { folderId, uploadDescription, accessLevel, ignoreScm } = + uploadFileData; + const url = endpoints.upload.uploadCreate(); sendRequest.mockImplementation(() => true); - expect( - createUploadApi( - folderId, - uploadDescription, - accessLevel, - ignoreScm, - fileInput - ) - ).toBe(sendRequest({})); + expect(createUploadApi(uploadFileData, scanOptions)).toBe(sendRequest({})); expect(sendRequest).toHaveBeenCalledWith( expect.objectContaining({ url, @@ -55,7 +82,7 @@ describe("upload", () => { uploadDescription, public: accessLevel, ignoreScm, - uploadType: "", + uploadType: "file", }, body: new FormData(), }) @@ -63,27 +90,53 @@ describe("upload", () => { }); test("createUploadApi with fileInput", () => { - const folderId = 1; - const uploadDescription = "uploadDescription"; - const accessLevel = 2; - const ignoreScm = true; - const fileInput = new File(["My File"], "file.txt", { - type: "text/plain", - }); + const uploadFileData = { + uploadDescription: "uploadDescription", + folderId: 1, + accessLevel: 2, + ignoreScm: true, + fileInput: new File(["My File"], "file.txt", { + type: "text/plain", + }), + }; + const scanOptions = { + 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 { folderId, uploadDescription, accessLevel, ignoreScm, fileInput } = + uploadFileData; + const expectedBody = new FormData(); expectedBody.append("fileInput", fileInput, fileInput?.name); + expectedBody.append("scanOptions", JSON.stringify(scanOptions)); + const url = endpoints.upload.uploadCreate(); sendRequest.mockImplementation(() => true); - expect( - createUploadApi( - folderId, - uploadDescription, - accessLevel, - ignoreScm, - fileInput - ) - ).toBe(sendRequest({})); + expect(createUploadApi(uploadFileData, scanOptions)).toBe(sendRequest({})); expect(sendRequest).toHaveBeenCalledWith( expect.objectContaining({ url, @@ -95,7 +148,7 @@ describe("upload", () => { uploadDescription, public: accessLevel, ignoreScm, - uploadType: "", + uploadType: "file", }, body: expectedBody, }) @@ -104,11 +157,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, @@ -117,18 +197,74 @@ describe("upload", () => { ...header, Authorization: getToken(), }, - body, + body: { + data: 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, + }, + }, + }, }) ); }); test("createUploadUrlApi", () => { const header = "header"; - const body = "body"; const url = endpoints.upload.uploadCreate(); + 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", + }, + }; + sendRequest.mockImplementation(() => true); - expect(createUploadUrlApi(header, body)).toBe(sendRequest({})); + expect(createUploadUrlApi(header, null, scanData)).toBe(sendRequest({})); expect(sendRequest).toHaveBeenCalledWith( expect.objectContaining({ url, @@ -137,7 +273,36 @@ describe("upload", () => { ...header, Authorization: getToken(), }, - body, + body: { + data: null, + 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, + }, + }, + }, }) ); }); diff --git a/src/pages/Upload/File/index.jsx b/src/pages/Upload/File/index.jsx index 98418a246..5bc07bc79 100644 --- a/src/pages/Upload/File/index.jsx +++ b/src/pages/Upload/File/index.jsx @@ -30,7 +30,6 @@ import CommonFields from "components/Upload/CommonFields"; // Required functions for calling APIs import { createUploadFile } from "services/upload"; -import { scheduleAnalysis } from "services/jobs"; import { getAllFolders } from "services/folders"; // Helper function for error handling @@ -41,11 +40,12 @@ import { initialStateFile, initialScanFileDataFile, initialFolderListFile, + initialScanFileData, + initialFolderList, } from "../../../constants/constants"; const UploadFile = () => { - // Upload Id required for scheduling Analysis - let uploadId; + // Upload I'd required for scheduling Analysis // Data required for creating the upload const [uploadFileData, setUploadFileData] = useState(initialStateFile); @@ -61,36 +61,24 @@ const UploadFile = () => { const [showMessage, setShowMessage] = useState(false); const [message, setMessage] = useState(); + // Function to reset the state variables to initial state + const resetStateVariables = () => { + setScanFileData(initialScanFileData); + setFolderList(initialFolderList); + setUploadFileData(initialStateFile); + }; + const handleSubmit = (e) => { e.preventDefault(); setLoading(true); - createUploadFile(uploadFileData) - .then((res) => { + createUploadFile(uploadFileData, scanFileData) + .then(() => { + resetStateVariables(); window.scrollTo({ top: 0 }); setMessage({ type: "success", - text: messages.uploadSuccess, + text: `${messages.uploadSuccess}`, }); - uploadId = res.message; - }) - .then(() => { - setTimeout( - () => - scheduleAnalysis(uploadFileData.folderId, uploadId, scanFileData) - .then(() => { - window.scrollTo({ top: 0 }); - setMessage({ - type: "success", - text: messages.scheduledAnalysis, - }); - setUploadFileData(initialStateFile); - setScanFileData(initialScanFileDataFile); - }) - .catch((error) => { - handleError(error, setMessage); - }), - 1200 - ); }) .catch((error) => { handleError(error, setMessage); diff --git a/src/pages/Upload/Url/index.jsx b/src/pages/Upload/Url/index.jsx index c54da5c9f..55dedd052 100644 --- a/src/pages/Upload/Url/index.jsx +++ b/src/pages/Upload/Url/index.jsx @@ -30,8 +30,7 @@ import CommonFields from "components/Upload/CommonFields"; // Required functions for calling APIs import { getAllFolders } from "services/folders"; -import { createUploadUrl, getUploadById } from "services/upload"; -import { scheduleAnalysis } from "services/jobs"; +import { createUploadUrl } from "services/upload"; // constants import { @@ -42,9 +41,6 @@ import { } from "constants/constants"; const UploadFromUrl = () => { - // Upload Id required for scheduling Analysis - let uploadId; - // Data required for creating the upload const [uploadUrlData, setUploadUrlData] = useState(initialStateUrl); @@ -62,39 +58,25 @@ const UploadFromUrl = () => { const [showMessage, setShowMessage] = useState(false); const [message, setMessage] = useState(); + // Function to reset the state variables to initial state + const resetStateVariables = () => { + setScanFileData(initialScanFileData); + setFolderList(initialFolderList); + setUploadUrlData(initialStateUrl); + setUrlData(initialUrlData); + }; + const handleSubmit = (e) => { e.preventDefault(); setLoading(true); - createUploadUrl(uploadUrlData, urlData) - .then((res) => { + createUploadUrl(uploadUrlData, urlData, scanFileData) + .then(() => { + resetStateVariables(); + 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(uploadUrlData.folderId, uploadId, scanFileData) - .then(() => { - setMessage({ - type: "success", - text: messages.scheduledAnalysis, - }); - setUploadUrlData(initialStateUrl); - setScanFileData(initialScanFileData); - }) - .catch((error) => { - setMessage({ - type: "danger", - text: error.message, - }); - }), - 150000 - ); }) .catch((error) => { setMessage({ diff --git a/src/pages/Upload/Vcs/index.jsx b/src/pages/Upload/Vcs/index.jsx index 006b373a6..6c6d6f5df 100644 --- a/src/pages/Upload/Vcs/index.jsx +++ b/src/pages/Upload/Vcs/index.jsx @@ -1,5 +1,6 @@ /* Copyright (C) 2021 Shruti Agarwal (mail2shruti.ag@gmail.com), Aman Dwivedi (aman.dwivedi5@gmail.com) + Copyright (C) 2022 Samuel Dushimimana (dushsam100@gmail.com) SPDX-License-Identifier: GPL-2.0 @@ -17,7 +18,6 @@ */ import React, { useState, useEffect } from "react"; -import messages from "constants/messages"; // Title import Title from "components/Title"; @@ -30,8 +30,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 { @@ -44,11 +43,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); @@ -66,39 +63,25 @@ const UploadFromVcs = () => { const [loading, setLoading] = useState(false); const [showMessage, setShowMessage] = useState(false); + // Function to reset the state variables to initial state + const resetStateVariables = () => { + setUploadVcsData(initialStateVcs); + setVcsData(initialVcsData); + setScanFileData(initialScanFileData); + setFolderList(initialFolderList); + }; const handleSubmit = (e) => { e.preventDefault(); setLoading(true); - createUploadVcs(uploadVcsData, vcsData) - .then((res) => { + createUploadVcs(uploadVcsData, vcsData, scanFileData) + .then(() => { + resetStateVariables(); 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); }) diff --git a/src/services/upload.js b/src/services/upload.js index c1b091a21..e99d51b18 100644 --- a/src/services/upload.js +++ b/src/services/upload.js @@ -1,5 +1,7 @@ /* Copyright (C) 2021 Shruti Agarwal (mail2shruti.ag@gmail.com), Aman Dwivedi (aman.dwivedi5@gmail.com) + Copyright (C) 2022 Samuel Dushimimana (dushsam100@gmail.com) + SPDX-License-Identifier: GPL-2.0 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License @@ -23,53 +25,41 @@ import { } from "api/upload"; // Create Uploads from File -export const createUploadFile = ({ - folderId, - uploadDescription, - accessLevel, - ignoreScm, - fileInput, -}) => { - return createUploadApi( - folderId, - uploadDescription, - accessLevel, - ignoreScm, - fileInput - ).then((res) => { +export const createUploadFile = (uploadFileData, scanData) => { + return createUploadApi(uploadFileData, scanData).then((res) => { return res; }); }; // 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; }); }; // Create Uploads from URL -export const createUploadUrl = (header, body) => { - return createUploadUrlApi(header, body).then((res) => { +export const createUploadUrl = (header, urlData, scanData) => { + return createUploadUrlApi(header, urlData, scanData).then((res) => { return res; }); }; -// 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;