From 605613e220a14bfdb3e16162db9f4e3b32b48a5d Mon Sep 17 00:00:00 2001 From: Pablo Lara Date: Tue, 11 Mar 2025 17:47:47 +0100 Subject: [PATCH] feat(scans): allow running a scan once (#7188) --- .../workflow/forms/test-connection-form.tsx | 40 ++++++++++++++----- .../launch-scan-workflow-form.tsx | 1 - ui/types/formSchemas.ts | 1 + 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/ui/components/providers/workflow/forms/test-connection-form.tsx b/ui/components/providers/workflow/forms/test-connection-form.tsx index 726ab3be30..58ead9a5c6 100644 --- a/ui/components/providers/workflow/forms/test-connection-form.tsx +++ b/ui/components/providers/workflow/forms/test-connection-form.tsx @@ -2,6 +2,7 @@ import { zodResolver } from "@hookform/resolvers/zod"; import { Icon } from "@iconify/react"; +import { Checkbox } from "@nextui-org/react"; import Link from "next/link"; import { useRouter } from "next/navigation"; import { useState } from "react"; @@ -12,7 +13,7 @@ import { checkConnectionProvider, deleteCredentials, } from "@/actions/providers"; -import { scheduleDaily } from "@/actions/scans"; +import { scanOnDemand, scheduleDaily } from "@/actions/scans"; import { getTask } from "@/actions/task/tasks"; import { CheckIcon, RocketIcon } from "@/components/icons"; import { useToast } from "@/components/ui"; @@ -57,9 +58,10 @@ export const TestConnectionForm = ({ }) => { const { toast } = useToast(); const router = useRouter(); + const providerType = searchParams.type; const providerId = searchParams.id; - const formSchema = testConnectionFormSchema; + const [apiErrorMessage, setApiErrorMessage] = useState(null); const [connectionStatus, setConnectionStatus] = useState<{ connected: boolean; @@ -68,10 +70,13 @@ export const TestConnectionForm = ({ const [isResettingCredentials, setIsResettingCredentials] = useState(false); const [isRedirecting, setIsRedirecting] = useState(false); + const formSchema = testConnectionFormSchema; + const form = useForm({ resolver: zodResolver(formSchema), defaultValues: { providerId, + runOnce: false, }, }); @@ -120,8 +125,16 @@ export const TestConnectionForm = ({ if (connected && !isUpdated) { try { - // Schedule daily scan by default - const data = await scheduleDaily(formData); + // Check if the runOnce checkbox is checked + const runOnce = form.watch("runOnce"); + + let data; + + if (runOnce) { + data = await scanOnDemand(formData); + } else { + data = await scheduleDaily(formData); + } if (data.error) { setApiErrorMessage(data.error); @@ -218,7 +231,7 @@ export const TestConnectionForm = ({

{!isUpdated - ? "A successful connection will launch a daily scheduled scan." + ? "A successful connection will start a scheduled scan every 24 hours. To run it just once, mark the checkbox below." : "A successful connection will redirect you to the providers page."}

@@ -258,11 +271,18 @@ export const TestConnectionForm = ({ providerUID={providerData.data.attributes.uid} /> - {/* {!isResettingCredentials && !connectionStatus?.error && ( -

- Test connection and launch scan -

- )} */} + {!isUpdated && !connectionStatus?.error && ( + + Run a one-time scan (no daily schedule). + + )} {isUpdated && !connectionStatus?.error && (

diff --git a/ui/components/scans/launch-workflow/launch-scan-workflow-form.tsx b/ui/components/scans/launch-workflow/launch-scan-workflow-form.tsx index 044ff55e1d..9937b5cc9d 100644 --- a/ui/components/scans/launch-workflow/launch-scan-workflow-form.tsx +++ b/ui/components/scans/launch-workflow/launch-scan-workflow-form.tsx @@ -26,7 +26,6 @@ export const LaunchScanWorkflow = ({ }: { providers: ProviderInfo[]; }) => { - console.log("providers from launch scan workflow", providers); const formSchema = onDemandScanFormSchema(); const form = useForm>({ resolver: zodResolver(formSchema), diff --git a/ui/types/formSchemas.ts b/ui/types/formSchemas.ts index 68319f4a73..e23d47eb55 100644 --- a/ui/types/formSchemas.ts +++ b/ui/types/formSchemas.ts @@ -162,6 +162,7 @@ export const addCredentialsRoleFormSchema = (providerType: string) => export const testConnectionFormSchema = z.object({ providerId: z.string(), + runOnce: z.boolean().default(false), }); export const launchScanFormSchema = () =>