Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Add taskgroup when uploading binary #1280

Merged
merged 2 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import {
Application,
IReadFile,
TaskData,
Taskgroup,
TaskgroupTask,
Expand All @@ -36,9 +35,8 @@
} from "./schema";
import { useAsyncYupValidation } from "@app/hooks/useAsyncYupValidation";
import { CustomRules } from "./custom-rules";
import { useSetting } from "@app/queries/settings";
import defaultSources from "./sources";
import { useFetchIdentities } from "@app/queries/identities";
import { TaskGroupProvider, useTaskGroup } from "./components/TaskGroupContext";

interface IAnalysisWizard {
applications: Application[];
Expand Down Expand Up @@ -67,7 +65,7 @@
},
};

const defaultTaskgroup: Taskgroup = {
export const defaultTaskgroup: Taskgroup = {
name: `taskgroup.analyzer`,
addon: "analyzer",
data: {
Expand Down Expand Up @@ -96,18 +94,16 @@

const { pushNotification } = React.useContext(NotificationsContext);

const [currentTaskgroup, setCurrentTaskgroup] =
React.useState<Taskgroup | null>();
const { taskGroup, updateTaskGroup } = useTaskGroup();

const [stepIdReached, setStepIdReached] = React.useState(1);
const isMutating = useIsMutating();

const onCreateTaskgroupSuccess = (data: Taskgroup) => {
setCurrentTaskgroup(data);
updateTaskGroup(data);

Check warning on line 103 in client/src/app/pages/applications/analysis-wizard/analysis-wizard.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/pages/applications/analysis-wizard/analysis-wizard.tsx#L103

Added line #L103 was not covered by tests
};

const onCreateTaskgroupError = (error: Error | unknown) => {
console.log("Taskgroup creation failed: ", error);
pushNotification({
title: "Taskgroup creation failed",
variant: "danger",
Expand Down Expand Up @@ -139,11 +135,10 @@
);

const onDeleteTaskgroupSuccess = () => {
setCurrentTaskgroup(null);
updateTaskGroup(null);

Check warning on line 138 in client/src/app/pages/applications/analysis-wizard/analysis-wizard.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/pages/applications/analysis-wizard/analysis-wizard.tsx#L138

Added line #L138 was not covered by tests
};

const onDeleteTaskgroupError = (error: Error | unknown) => {
console.log("Taskgroup: delete failed: ", error);
pushNotification({
title: "Taskgroup: delete failed",
variant: "danger",
Expand Down Expand Up @@ -282,20 +277,20 @@
const isModeValid = applications.every((app) => isModeSupported(app, mode));

const handleCancel = () => {
if (currentTaskgroup && currentTaskgroup.id) {
deleteTaskgroup(currentTaskgroup.id);
if (taskGroup && taskGroup.id) {
deleteTaskgroup(taskGroup.id);

Check warning on line 281 in client/src/app/pages/applications/analysis-wizard/analysis-wizard.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/pages/applications/analysis-wizard/analysis-wizard.tsx#L281

Added line #L281 was not covered by tests
}
setCurrentTaskgroup(null);
updateTaskGroup(null);

Check warning on line 283 in client/src/app/pages/applications/analysis-wizard/analysis-wizard.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/pages/applications/analysis-wizard/analysis-wizard.tsx#L283

Added line #L283 was not covered by tests
reset();
onClose();
};

const onSubmit = (fieldValues: AnalysisWizardFormValues) => {
if (currentTaskgroup) {
const taskgroup = setupTaskgroup(currentTaskgroup, fieldValues);
if (taskGroup) {
const taskgroup = setupTaskgroup(taskGroup, fieldValues);

Check warning on line 290 in client/src/app/pages/applications/analysis-wizard/analysis-wizard.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/pages/applications/analysis-wizard/analysis-wizard.tsx#L290

Added line #L290 was not covered by tests
submitTaskgroup(taskgroup);
}
setCurrentTaskgroup(null);
updateTaskGroup(null);

Check warning on line 293 in client/src/app/pages/applications/analysis-wizard/analysis-wizard.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/pages/applications/analysis-wizard/analysis-wizard.tsx#L293

Added line #L293 was not covered by tests
reset();
onClose();
};
Expand All @@ -306,7 +301,7 @@
) => {
if (id && stepIdReached < (id as number)) setStepIdReached(id as number);
if (id === StepId.SetTargets) {
if (!currentTaskgroup) {
if (!taskGroup) {
createTaskgroup(defaultTaskgroup);
}
}
Expand Down Expand Up @@ -335,11 +330,6 @@
component: (
<SetMode
isSingleApp={applications.length === 1 ? true : false}
taskgroupID={
currentTaskgroup && currentTaskgroup?.id
? currentTaskgroup.id
: null
}
isModeValid={isModeValid}
/>
),
Expand All @@ -365,15 +355,7 @@
{
id: StepId.CustomRules,
name: t("wizard.terms.customRules"),
component: (
<CustomRules
taskgroupID={
currentTaskgroup && currentTaskgroup?.id
? currentTaskgroup.id
: null
}
/>
),
component: <CustomRules />,
...getStepNavProps(StepId.CustomRules),
},
{
Expand All @@ -392,7 +374,6 @@
...getStepNavProps(StepId.Review),
},
];

return (
<>
{isOpen && (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Taskgroup } from "@app/api/models";
import React, { createContext, useContext, useState } from "react";

interface TaskGroupContext {
updateTaskGroup: (taskGroup: Taskgroup | null) => void;
taskGroup: Taskgroup | null;
}

const TaskGroupContext = createContext<TaskGroupContext>({
updateTaskGroup: () => {},

Check warning on line 10 in client/src/app/pages/applications/analysis-wizard/components/TaskGroupContext.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/pages/applications/analysis-wizard/components/TaskGroupContext.tsx#L10

Added line #L10 was not covered by tests
taskGroup: null,
});

export const useTaskGroup = () => useContext(TaskGroupContext);

interface TaskGroupProvider {
children: React.ReactNode;
}

export const TaskGroupProvider: React.FunctionComponent<TaskGroupProvider> = ({
children,
}) => {
const [taskGroup, setTaskGroup] = useState<Taskgroup | null>(null);

Check warning on line 23 in client/src/app/pages/applications/analysis-wizard/components/TaskGroupContext.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/pages/applications/analysis-wizard/components/TaskGroupContext.tsx#L22-L23

Added lines #L22 - L23 were not covered by tests

const updateTaskGroup = (newTaskGroup: Taskgroup | null) => {
setTaskGroup(newTaskGroup);

Check warning on line 26 in client/src/app/pages/applications/analysis-wizard/components/TaskGroupContext.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/pages/applications/analysis-wizard/components/TaskGroupContext.tsx#L25-L26

Added lines #L25 - L26 were not covered by tests
};

return (

Check warning on line 29 in client/src/app/pages/applications/analysis-wizard/components/TaskGroupContext.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/pages/applications/analysis-wizard/components/TaskGroupContext.tsx#L29

Added line #L29 was not covered by tests
<TaskGroupContext.Provider value={{ taskGroup, updateTaskGroup }}>
{children}
</TaskGroupContext.Provider>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import { useFormContext } from "react-hook-form";

import {
useCreateTaskgroupMutation,
useRemoveUploadedFileMutation,
useUploadFileTaskgroupMutation,
} from "@app/queries/taskgroups";
Expand All @@ -19,12 +20,12 @@
import { uploadLimit } from "@app/Constants";
import { NotificationsContext } from "@app/components/NotificationsContext";
import { AnalysisWizardFormValues } from "../schema";
import { useTaskGroup } from "./TaskGroupContext";
import { Taskgroup } from "@app/api/models";
import { defaultTaskgroup } from "../analysis-wizard";

interface IUploadBinary {
taskgroupID: number;
}

export const UploadBinary: React.FC<IUploadBinary> = ({ taskgroupID }) => {
export const UploadBinary: React.FC = () => {
const { taskGroup, updateTaskGroup } = useTaskGroup();

Check warning on line 28 in client/src/app/pages/applications/analysis-wizard/components/upload-binary.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/pages/applications/analysis-wizard/components/upload-binary.tsx#L28

Added line #L28 was not covered by tests
const { setValue, watch } = useFormContext<AnalysisWizardFormValues>();
const artifact = watch("artifact");

Expand Down Expand Up @@ -90,6 +91,22 @@
completedRemove,
failedRemove
);
const onCreateTaskgroupSuccess = (data: Taskgroup) => {
updateTaskGroup(data);

Check warning on line 95 in client/src/app/pages/applications/analysis-wizard/components/upload-binary.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/pages/applications/analysis-wizard/components/upload-binary.tsx#L94-L95

Added lines #L94 - L95 were not covered by tests
};

const onCreateTaskgroupError = (error: Error | unknown) => {
console.log("Taskgroup creation failed: ", error);
pushNotification({

Check warning on line 100 in client/src/app/pages/applications/analysis-wizard/components/upload-binary.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/pages/applications/analysis-wizard/components/upload-binary.tsx#L98-L100

Added lines #L98 - L100 were not covered by tests
title: "Taskgroup creation failed",
variant: "danger",
});
};

const { mutateAsync: createTaskgroup } = useCreateTaskgroupMutation(

Check warning on line 106 in client/src/app/pages/applications/analysis-wizard/components/upload-binary.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/pages/applications/analysis-wizard/components/upload-binary.tsx#L106

Added line #L106 was not covered by tests
onCreateTaskgroupSuccess,
onCreateTaskgroupError
);

const handleFileDrop = (_: DropEvent, droppedFiles: File[]) => {
if (droppedFiles[0]) {
Expand All @@ -98,12 +115,26 @@
setFileUploadStatus(undefined);
const form = new FormData();
form.append("file", droppedFiles[0]);
uploadFile({
id: taskgroupID,
path: `binary/${droppedFiles[0].name}`,
formData: form,
file: droppedFiles[0],
});
if (!taskGroup) {
createTaskgroup(defaultTaskgroup).then((data) => {
updateTaskGroup(data);

Check warning on line 120 in client/src/app/pages/applications/analysis-wizard/components/upload-binary.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/pages/applications/analysis-wizard/components/upload-binary.tsx#L119-L120

Added lines #L119 - L120 were not covered by tests
data.id &&
uploadFile({

Check warning on line 122 in client/src/app/pages/applications/analysis-wizard/components/upload-binary.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/pages/applications/analysis-wizard/components/upload-binary.tsx#L122

Added line #L122 was not covered by tests
id: data?.id,
path: `binary/${droppedFiles[0].name}`,
formData: form,
file: droppedFiles[0],
});
});
} else {

Check warning on line 129 in client/src/app/pages/applications/analysis-wizard/components/upload-binary.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/pages/applications/analysis-wizard/components/upload-binary.tsx#L129

Added line #L129 was not covered by tests
taskGroup.id &&
uploadFile({

Check warning on line 131 in client/src/app/pages/applications/analysis-wizard/components/upload-binary.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/pages/applications/analysis-wizard/components/upload-binary.tsx#L131

Added line #L131 was not covered by tests
id: taskGroup?.id,
path: `binary/${droppedFiles[0].name}`,
formData: form,
file: droppedFiles[0],
});
}
setValue("artifact", droppedFiles[0]);
}
};
Expand Down Expand Up @@ -175,10 +206,11 @@
key={artifact.name}
customFileHandler={handleFile}
onClearClick={() => {
removeFile({
id: taskgroupID,
path: `binary/${artifact}`,
});
taskGroup?.id &&
removeFile({

Check warning on line 210 in client/src/app/pages/applications/analysis-wizard/components/upload-binary.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/pages/applications/analysis-wizard/components/upload-binary.tsx#L210

Added line #L210 was not covered by tests
id: taskGroup?.id,
path: `binary/${artifact}`,
});
setValue("artifact", null);
}}
progressAriaLabel={"text"}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@
import { toOptionLike } from "@app/utils/model-utils";
import { useFetchIdentities } from "@app/queries/identities";
import useRuleFiles from "@app/hooks/useRuleFiles";
interface CustomRulesProps {
taskgroupID: number | null;
}
export const CustomRules: React.FC<CustomRulesProps> = (props) => {
import { useTaskGroup } from "./components/TaskGroupContext";

export const CustomRules: React.FC = () => {
const { t } = useTranslation();
const { taskGroup, updateTaskGroup } = useTaskGroup();

Check warning on line 61 in client/src/app/pages/applications/analysis-wizard/custom-rules.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/pages/applications/analysis-wizard/custom-rules.tsx#L61

Added line #L61 was not covered by tests

const { watch, setValue, control, getValues } =
useFormContext<AnalysisWizardFormValues>();
Expand Down Expand Up @@ -92,7 +92,7 @@
successfullyReadFileCount,
handleFile,
removeFiles,
} = useRuleFiles(props?.taskgroupID, values.customRulesFiles);
} = useRuleFiles(taskGroup?.id, values.customRulesFiles);

Check warning on line 95 in client/src/app/pages/applications/analysis-wizard/custom-rules.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/pages/applications/analysis-wizard/custom-rules.tsx#L95

Added line #L95 was not covered by tests
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only bit that looks suspicious to me under brain compile.

What happens if the taskGroup changes? Maybe something like from undefined to { id: 5 }?

I'll run through some smoke tests in a bit. Maybe that will clear up my confusion.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 Fair concern. The taskgroup should always exist by the time the user arrives at the custom-rules step as it is created either 1) when onMove to step 2 of the wizard or 2) user uploads a binary on step 1 of the wizard.

Copy link
Member

@sjd78 sjd78 Aug 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


const repositoryTypeOptions: OptionWithValue<string>[] = [
{
Expand Down
11 changes: 2 additions & 9 deletions client/src/app/pages/applications/analysis-wizard/set-mode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,10 @@ import { HookFormPFGroupController } from "@app/components/HookFormPFFields";

interface ISetMode {
isSingleApp: boolean;
taskgroupID: number | null;
isModeValid: boolean;
}

export const SetMode: React.FC<ISetMode> = ({
isSingleApp,
taskgroupID,
isModeValid,
}) => {
export const SetMode: React.FC<ISetMode> = ({ isSingleApp, isModeValid }) => {
const { t } = useTranslation();

const { watch, control, setValue } =
Expand Down Expand Up @@ -89,9 +84,7 @@ export const SetMode: React.FC<ISetMode> = ({
<p>{t("wizard.label.notAllAnalyzableDetails")}</p>
</Alert>
)}
{mode === "binary-upload" && taskgroupID && (
<UploadBinary taskgroupID={taskgroupID} />
)}
{mode === "binary-upload" && <UploadBinary />}
</Form>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ import { AppTableWithControls } from "@app/components/AppTableWithControls";
import { ToolbarBulkSelector } from "@app/components/ToolbarBulkSelector";
import { KebabDropdown } from "@app/components/KebabDropdown";
import { NoDataEmptyState } from "@app/components/NoDataEmptyState";
import { TaskGroupProvider } from "../analysis-wizard/components/TaskGroupContext";

const ENTITY_FIELD = "entity";

Expand Down Expand Up @@ -611,13 +612,15 @@ export const ApplicationsTableAnalyze: React.FC = () => {
onClose={() => setSaveApplicationsModalState(null)}
/>
</Modal>{" "}
<AnalysisWizard
applications={selectedRows}
isOpen={isAnalyzeModalOpen}
onClose={() => {
setAnalyzeModalOpen(false);
}}
/>
<TaskGroupProvider>
<AnalysisWizard
applications={selectedRows}
isOpen={isAnalyzeModalOpen}
onClose={() => {
setAnalyzeModalOpen(false);
}}
/>
</TaskGroupProvider>
<Modal
isOpen={isApplicationImportModalOpen}
variant="medium"
Expand Down
Loading