Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

24.0 KB
Raw Blame
import { Controller, useFormContext, useWatch } from "react-hook-form";
import { Info } from "lucide-react";

import { SecretSyncConnectionField } from "@app/components/secret-syncs/forms/SecretSyncConnectionField";
import {
  Combobox,
  Field,
  FieldContent,
  FieldError,
  FieldGroup,
  FieldLabel,
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue,
  Tabs,
  TabsContent,
  TabsList,
  TabsTrigger,
  Tooltip,
  TooltipContent,
  TooltipTrigger
} from "@app/components/v3";
import {
  useOctopusDeployConnectionGetScopeValues,
  useOctopusDeployConnectionListProjects,
  useOctopusDeployConnectionListSpaces
} from "@app/hooks/api/appConnections/octopus-deploy/queries";
import { SecretSync } from "@app/hooks/api/secretSyncs";
import { OctopusDeploySyncScope } from "@app/hooks/api/secretSyncs/types/octopus-deploy-sync";

import { TSecretSyncForm } from "../schemas";

const EMPTY_SCOPE_VALUES = {
  environments: [],
  roles: [],
  processes: [],
  actions: [],
  machines: [],
  channels: []
};

export const OctopusDeploySyncFields = () => {
  const { control, setValue } = useFormContext<
    TSecretSyncForm & { destination: SecretSync.OctopusDeploy }
  >();

  const connectionId = useWatch({ name: "connection.id", control });
  const spaceId = useWatch({ name: "destinationConfig.spaceId", control });
  const scope = useWatch({ name: "destinationConfig.scope", control });
  const projectId = useWatch({ name: "destinationConfig.projectId", control });

  const { data: spaces = [], isLoading: isSpacesLoading } = useOctopusDeployConnectionListSpaces(
    connectionId,
    {
      enabled: Boolean(connectionId)
    }
  );

  const { data: projects = [], isLoading: isProjectsLoading } =
    useOctopusDeployConnectionListProjects(connectionId, spaceId, {
      enabled: Boolean(connectionId && spaceId && scope)
    });

  const { data: scopeValuesData, isLoading: isScopeValuesLoading } =
    useOctopusDeployConnectionGetScopeValues(connectionId, spaceId, projectId, {
      enabled: Boolean(connectionId && spaceId && projectId && scope)
    });

  return (
    <FieldGroup>
      <SecretSyncConnectionField
        onChange={() => {
          setValue("destinationConfig.spaceId", "");
          setValue("destinationConfig.spaceName", "");
          setValue("destinationConfig.projectId", "");
          setValue("destinationConfig.projectName", "");
          setValue("destinationConfig.scopeValues", EMPTY_SCOPE_VALUES);
        }}
      />

      <Tabs defaultValue="general">
        <TabsList className="mb-2">
          <TabsTrigger value="general">General</TabsTrigger>
          <TabsTrigger value="advanced">Advanced</TabsTrigger>
        </TabsList>
        <TabsContent value="general">
          <FieldGroup>
            <Controller
              name="destinationConfig.spaceId"
              control={control}
              render={({ field: { value, onChange }, fieldState: { error } }) => (
                <Field>
                  <FieldLabel
                    id="secret-sync-octopus-deploy-space-id-label"
                    htmlFor="secret-sync-octopus-deploy-space-id"
                  >
                    Space
                    <Tooltip>
                      <TooltipTrigger asChild>
                        <Info />
                      </TooltipTrigger>
                      <TooltipContent className="max-w-md">
                        Select the Octopus Deploy space where your project is located.
                      </TooltipContent>
                    </Tooltip>
                  </FieldLabel>
                  <FieldContent>
                    <Combobox
                      aria-labelledby="secret-sync-octopus-deploy-space-id-label"
                      aria-describedby={
                        error ? "secret-sync-octopus-deploy-space-id-error" : undefined
                      }
                      id="secret-sync-octopus-deploy-space-id"
                      isError={Boolean(error)}
                      isLoading={isSpacesLoading && Boolean(connectionId)}
                      isDisabled={!connectionId}
                      value={spaces?.find((space) => space.id === value) ?? null}
                      onValueChange={(option) => {
                        const selectedSpace = option;
                        onChange(selectedSpace?.id ?? null);
                        setValue("destinationConfig.spaceName", selectedSpace?.name ?? "");
                        setValue("destinationConfig.projectId", "");
                        setValue("destinationConfig.projectName", "");
                        setValue("destinationConfig.scopeValues", EMPTY_SCOPE_VALUES);
                      }}
                      options={spaces}
                      placeholder={spaces?.length ? "Select a space..." : "No spaces found..."}
                      getOptionLabel={(option) => option.name}
                      getOptionValue={(option) => option.id}
                      getOptionKeywords={(option) => [option.id, option.slug]}
                      modal
                    />
                    <FieldError id="secret-sync-octopus-deploy-space-id-error" errors={[error]} />
                  </FieldContent>
                </Field>
              )}
            />

            <Controller
              name="destinationConfig.scope"
              control={control}
              defaultValue={OctopusDeploySyncScope.Project}
              render={({ field: { value, onChange }, fieldState: { error } }) => (
                <Field>
                  <FieldLabel>Scope</FieldLabel>
                  <FieldContent>
                    <Select
                      value={value || OctopusDeploySyncScope.Project}
                      onValueChange={(val) => {
                        onChange(val);
                        setValue("destinationConfig.projectId", "");
                        setValue("destinationConfig.projectName", "");
                        setValue("destinationConfig.scopeValues", EMPTY_SCOPE_VALUES);
                      }}
                    >
                      <SelectTrigger className="w-full capitalize" isError={Boolean(error)}>
                        <SelectValue placeholder="Select a scope..." />
                      </SelectTrigger>
                      <SelectContent position="popper">
                        {Object.values(OctopusDeploySyncScope).map((scopeValue) => (
                          <SelectItem className="capitalize" value={scopeValue} key={scopeValue}>
                            {scopeValue}
                          </SelectItem>
                        ))}
                      </SelectContent>
                    </Select>
                    <FieldError errors={[error]} />
                  </FieldContent>
                </Field>
              )}
            />

            {scope === OctopusDeploySyncScope.Project && (
              <Controller
                name="destinationConfig.projectId"
                control={control}
                render={({ field: { value, onChange }, fieldState: { error } }) => (
                  <Field>
                    <FieldLabel
                      id="secret-sync-octopus-deploy-project-id-label"
                      htmlFor="secret-sync-octopus-deploy-project-id"
                    >
                      Project
                      <Tooltip>
                        <TooltipTrigger asChild>
                          <Info />
                        </TooltipTrigger>
                        <TooltipContent className="max-w-md">
                          Ensure the project exists in the selected space.
                        </TooltipContent>
                      </Tooltip>
                    </FieldLabel>
                    <FieldContent>
                      <Combobox
                        aria-labelledby="secret-sync-octopus-deploy-project-id-label"
                        aria-describedby={
                          error ? "secret-sync-octopus-deploy-project-id-error" : undefined
                        }
                        id="secret-sync-octopus-deploy-project-id"
                        isError={Boolean(error)}
                        isLoading={isProjectsLoading && Boolean(connectionId && spaceId)}
                        isDisabled={Boolean(!connectionId || !spaceId)}
                        value={projects?.find((project) => project.id === value) ?? null}
                        onValueChange={(option) => {
                          const selectedProject = option;
                          onChange(selectedProject?.id ?? null);
                          setValue("destinationConfig.projectName", selectedProject?.name ?? "");
                          setValue("destinationConfig.scopeValues", EMPTY_SCOPE_VALUES);
                        }}
                        options={projects}
                        placeholder={
                          spaceId && projects?.length
                            ? "Select a project..."
                            : "No projects found..."
                        }
                        getOptionLabel={(option) => option.name}
                        getOptionValue={(option) => option.id}
                        getOptionKeywords={(option) => [option.id, option.slug]}
                        modal
                      />
                      <FieldError
                        id="secret-sync-octopus-deploy-project-id-error"
                        errors={[error]}
                      />
                    </FieldContent>
                  </Field>
                )}
              />
            )}
          </FieldGroup>
        </TabsContent>

        <TabsContent value="advanced" className="grow">
          {scope === OctopusDeploySyncScope.Project && projectId ? (
            <FieldGroup>
              {/* Environments */}
              <Controller
                name="destinationConfig.scopeValues.environments"
                control={control}
                render={({ field: { value, onChange }, fieldState: { error } }) => (
                  <Field>
                    <FieldLabel
                      id="secret-sync-octopus-deploy-scope-values-environments-label"
                      htmlFor="secret-sync-octopus-deploy-scope-values-environments"
                    >
                      Environments
                    </FieldLabel>
                    <FieldContent>
                      <Combobox
                        aria-labelledby="secret-sync-octopus-deploy-scope-values-environments-label"
                        aria-describedby={
                          error
                            ? "secret-sync-octopus-deploy-scope-values-environments-error"
                            : undefined
                        }
                        id="secret-sync-octopus-deploy-scope-values-environments"
                        isError={Boolean(error)}
                        multiple
                        isLoading={isScopeValuesLoading}
                        value={
                          scopeValuesData?.environments?.filter((opt) =>
                            (value || []).includes(opt.id)
                          ) || []
                        }
                        onValueChange={(options) => {
                          const selectedIds = options.map((opt) => opt.id);
                          onChange(selectedIds);
                        }}
                        options={scopeValuesData?.environments || []}
                        placeholder={
                          scopeValuesData?.environments?.length
                            ? "Select environments..."
                            : "No environments found..."
                        }
                        getOptionLabel={(option) => option.name}
                        getOptionValue={(option) => option.id}
                        getOptionKeywords={(option) => [option.id]}
                        modal
                      />
                      <FieldError
                        id="secret-sync-octopus-deploy-scope-values-environments-error"
                        errors={[error]}
                      />
                    </FieldContent>
                  </Field>
                )}
              />

              {/* Target Tags */}
              <Controller
                name="destinationConfig.scopeValues.roles"
                control={control}
                render={({ field: { value, onChange }, fieldState: { error } }) => (
                  <Field>
                    <FieldLabel
                      id="secret-sync-octopus-deploy-scope-values-roles-label"
                      htmlFor="secret-sync-octopus-deploy-scope-values-roles"
                    >
                      Target Tags
                    </FieldLabel>
                    <FieldContent>
                      <Combobox
                        aria-labelledby="secret-sync-octopus-deploy-scope-values-roles-label"
                        aria-describedby={
                          error ? "secret-sync-octopus-deploy-scope-values-roles-error" : undefined
                        }
                        id="secret-sync-octopus-deploy-scope-values-roles"
                        isError={Boolean(error)}
                        multiple
                        isLoading={isScopeValuesLoading}
                        value={
                          scopeValuesData?.roles?.filter((opt) => (value || []).includes(opt.id)) ||
                          []
                        }
                        onValueChange={(options) => {
                          const selectedIds = options.map((opt) => opt.id);
                          onChange(selectedIds);
                        }}
                        options={scopeValuesData?.roles || []}
                        placeholder={
                          scopeValuesData?.roles?.length
                            ? "Select target tags..."
                            : "No target tags found..."
                        }
                        getOptionLabel={(option) => option.name}
                        getOptionValue={(option) => option.id}
                        getOptionKeywords={(option) => [option.id]}
                        modal
                      />
                      <FieldError
                        id="secret-sync-octopus-deploy-scope-values-roles-error"
                        errors={[error]}
                      />
                    </FieldContent>
                  </Field>
                )}
              />

              {/* Targets */}
              <Controller
                name="destinationConfig.scopeValues.machines"
                control={control}
                render={({ field: { value, onChange }, fieldState: { error } }) => (
                  <Field>
                    <FieldLabel
                      id="secret-sync-octopus-deploy-scope-values-machines-label"
                      htmlFor="secret-sync-octopus-deploy-scope-values-machines"
                    >
                      Targets
                    </FieldLabel>
                    <FieldContent>
                      <Combobox
                        aria-labelledby="secret-sync-octopus-deploy-scope-values-machines-label"
                        aria-describedby={
                          error
                            ? "secret-sync-octopus-deploy-scope-values-machines-error"
                            : undefined
                        }
                        id="secret-sync-octopus-deploy-scope-values-machines"
                        isError={Boolean(error)}
                        multiple
                        isLoading={isScopeValuesLoading}
                        value={
                          scopeValuesData?.machines?.filter((opt) =>
                            (value || []).includes(opt.id)
                          ) || []
                        }
                        onValueChange={(options) => {
                          const selectedIds = options.map((opt) => opt.id);
                          onChange(selectedIds);
                        }}
                        options={scopeValuesData?.machines || []}
                        placeholder={
                          scopeValuesData?.machines?.length
                            ? "Select targets..."
                            : "No targets found..."
                        }
                        getOptionLabel={(option) => option.name}
                        getOptionValue={(option) => option.id}
                        getOptionKeywords={(option) => [option.id]}
                        modal
                      />
                      <FieldError
                        id="secret-sync-octopus-deploy-scope-values-machines-error"
                        errors={[error]}
                      />
                    </FieldContent>
                  </Field>
                )}
              />

              {/* Processes */}
              <Controller
                name="destinationConfig.scopeValues.processes"
                control={control}
                render={({ field: { value, onChange }, fieldState: { error } }) => (
                  <Field>
                    <FieldLabel
                      id="secret-sync-octopus-deploy-scope-values-processes-label"
                      htmlFor="secret-sync-octopus-deploy-scope-values-processes"
                    >
                      Processes
                    </FieldLabel>
                    <FieldContent>
                      <Combobox
                        aria-labelledby="secret-sync-octopus-deploy-scope-values-processes-label"
                        aria-describedby={
                          error
                            ? "secret-sync-octopus-deploy-scope-values-processes-error"
                            : undefined
                        }
                        id="secret-sync-octopus-deploy-scope-values-processes"
                        isError={Boolean(error)}
                        multiple
                        isLoading={isScopeValuesLoading}
                        value={
                          scopeValuesData?.processes?.filter((opt) =>
                            (value || []).includes(opt.id)
                          ) || []
                        }
                        onValueChange={(options) => {
                          const selectedIds = options.map((opt) => opt.id);
                          onChange(selectedIds);
                        }}
                        options={scopeValuesData?.processes || []}
                        placeholder={
                          scopeValuesData?.processes?.length
                            ? "Select processes..."
                            : "No processes found..."
                        }
                        getOptionLabel={(option) => option.name}
                        getOptionValue={(option) => option.id}
                        getOptionKeywords={(option) => [option.id]}
                        modal
                      />
                      <FieldError
                        id="secret-sync-octopus-deploy-scope-values-processes-error"
                        errors={[error]}
                      />
                    </FieldContent>
                  </Field>
                )}
              />

              {/* Deployment Steps */}
              <Controller
                name="destinationConfig.scopeValues.actions"
                control={control}
                render={({ field: { value, onChange }, fieldState: { error } }) => (
                  <Field>
                    <FieldLabel
                      id="secret-sync-octopus-deploy-scope-values-actions-label"
                      htmlFor="secret-sync-octopus-deploy-scope-values-actions"
                    >
                      Deployment Steps
                    </FieldLabel>
                    <FieldContent>
                      <Combobox
                        aria-labelledby="secret-sync-octopus-deploy-scope-values-actions-label"
                        aria-describedby={
                          error
                            ? "secret-sync-octopus-deploy-scope-values-actions-error"
                            : undefined
                        }
                        id="secret-sync-octopus-deploy-scope-values-actions"
                        isError={Boolean(error)}
                        multiple
                        isLoading={isScopeValuesLoading}
                        value={
                          scopeValuesData?.actions?.filter((opt) =>
                            (value || []).includes(opt.id)
                          ) || []
                        }
                        onValueChange={(options) => {
                          const selectedIds = options.map((opt) => opt.id);
                          onChange(selectedIds);
                        }}
                        options={scopeValuesData?.actions || []}
                        placeholder={
                          scopeValuesData?.actions?.length
                            ? "Select deployment steps..."
                            : "No deployment steps found..."
                        }
                        getOptionLabel={(option) => option.name}
                        getOptionValue={(option) => option.id}
                        getOptionKeywords={(option) => [option.id]}
                        modal
                      />
                      <FieldError
                        id="secret-sync-octopus-deploy-scope-values-actions-error"
                        errors={[error]}
                      />
                    </FieldContent>
                  </Field>
                )}
              />

              {/* Channels */}
              <Controller
                name="destinationConfig.scopeValues.channels"
                control={control}
                render={({ field: { value, onChange }, fieldState: { error } }) => (
                  <Field>
                    <FieldLabel
                      id="secret-sync-octopus-deploy-scope-values-channels-label"
                      htmlFor="secret-sync-octopus-deploy-scope-values-channels"
                    >
                      Channels
                    </FieldLabel>
                    <FieldContent>
                      <Combobox
                        aria-labelledby="secret-sync-octopus-deploy-scope-values-channels-label"
                        aria-describedby={
                          error
                            ? "secret-sync-octopus-deploy-scope-values-channels-error"
                            : undefined
                        }
                        id="secret-sync-octopus-deploy-scope-values-channels"
                        isError={Boolean(error)}
                        multiple
                        isLoading={isScopeValuesLoading}
                        value={
                          scopeValuesData?.channels?.filter((opt) =>
                            (value || []).includes(opt.id)
                          ) || []
                        }
                        onValueChange={(options) => {
                          const selectedIds = options.map((opt) => opt.id);
                          onChange(selectedIds);
                        }}
                        options={scopeValuesData?.channels || []}
                        placeholder={
                          scopeValuesData?.channels?.length
                            ? "Select channels..."
                            : "No channels found..."
                        }
                        getOptionLabel={(option) => option.name}
                        getOptionValue={(option) => option.id}
                        getOptionKeywords={(option) => [option.id]}
                        modal
                      />
                      <FieldError
                        id="secret-sync-octopus-deploy-scope-values-channels-error"
                        errors={[error]}
                      />
                    </FieldContent>
                  </Field>
                )}
              />
            </FieldGroup>
          ) : (
            <div className="py-8 text-center text-muted">
              Please select a project in the Config tab to configure scope values.
            </div>
          )}
        </TabsContent>
      </Tabs>
    </FieldGroup>
  );
};