|
| 1 | +import { ChangeEvent, useState } from 'react' |
| 2 | +import { useHistory } from 'react-router-dom' |
| 3 | + |
| 4 | +import { |
| 5 | + API_STATUS_CODES, |
| 6 | + ButtonVariantType, |
| 7 | + ComponentSizeType, |
| 8 | + DeploymentAppTypes, |
| 9 | + Icon, |
| 10 | + InfoBlock, |
| 11 | + noop, |
| 12 | + showError, |
| 13 | +} from '@devtron-labs/devtron-fe-common-lib' |
| 14 | + |
| 15 | +import { GeneratedHelmPush } from '@Components/cdPipeline/cdPipeline.types' |
| 16 | +import { ValidationRules } from '@Components/ciPipeline/validationRules' |
| 17 | +import { EnvironmentWithSelectPickerType } from '@Components/CIPipelineN/types' |
| 18 | +import { GITOPS_REPO_REQUIRED } from '@Components/v2/values/chartValuesDiff/constant' |
| 19 | +import { ENV_ALREADY_EXIST_ERROR } from '@Config/constants' |
| 20 | +import { URLS } from '@Config/routes' |
| 21 | +import { getGitOpsRepoConfig } from '@Services/service' |
| 22 | + |
| 23 | +import { CDPipelineDeploymentAppType } from '../CDPipelineDeploymentAppType' |
| 24 | +import { SourceMaterialsSelector } from '../SourceMaterialsSelector' |
| 25 | +import { CDStepperContentProps } from './types' |
| 26 | +import { getEnvironmentOptions } from './utils' |
| 27 | + |
| 28 | +const validationRules = new ValidationRules() |
| 29 | + |
| 30 | +export const CDStepperContent = ({ |
| 31 | + appId, |
| 32 | + isCreatingWorkflow, |
| 33 | + cdNodeCreateError, |
| 34 | + onRetry, |
| 35 | + ciCdPipeline, |
| 36 | + ciCdPipelineFormError, |
| 37 | + noGitOpsModuleInstalledAndConfigured, |
| 38 | + isGitOpsInstalledButNotConfigured, |
| 39 | + isGitOpsRepoNotConfigured, |
| 40 | + envIds, |
| 41 | + setCiCdPipeline, |
| 42 | + setCiCdPipelineFormError, |
| 43 | + setReloadNoGitOpsRepoConfiguredModal, |
| 44 | +}: CDStepperContentProps) => { |
| 45 | + // STATES |
| 46 | + const [gitopsConflictLoading, setGitopsConflictLoading] = useState(false) |
| 47 | + |
| 48 | + // HOOKS |
| 49 | + const { push } = useHistory() |
| 50 | + |
| 51 | + // CONSTANTS |
| 52 | + const { environments, selectedEnvironment, deploymentAppType } = ciCdPipeline.cd |
| 53 | + const isFormDisabled = isCreatingWorkflow |
| 54 | + const isHelmEnforced = |
| 55 | + selectedEnvironment && |
| 56 | + selectedEnvironment.allowedDeploymentTypes.length === 1 && |
| 57 | + selectedEnvironment.allowedDeploymentTypes[0] === DeploymentAppTypes.HELM |
| 58 | + const gitOpsRepoNotConfiguredAndOptionsHidden = |
| 59 | + window._env_.HIDE_GITOPS_OR_HELM_OPTION && |
| 60 | + !noGitOpsModuleInstalledAndConfigured && |
| 61 | + !isHelmEnforced && |
| 62 | + isGitOpsRepoNotConfigured |
| 63 | + |
| 64 | + // HANDLERS |
| 65 | + const handleEnvironmentChange = (selection: EnvironmentWithSelectPickerType) => { |
| 66 | + const { ci, cd } = structuredClone(ciCdPipeline) |
| 67 | + cd.selectedEnvironment = selection |
| 68 | + cd.generatedHelmPushAction = selection.isVirtualEnvironment |
| 69 | + ? GeneratedHelmPush.DO_NOT_PUSH |
| 70 | + : GeneratedHelmPush.PUSH |
| 71 | + |
| 72 | + setCiCdPipeline({ ci, cd }) |
| 73 | + |
| 74 | + const updatedCiCdPipelineFormError = structuredClone(ciCdPipelineFormError) |
| 75 | + updatedCiCdPipelineFormError.cd.environment = envIds.includes(selection.id) |
| 76 | + ? ENV_ALREADY_EXIST_ERROR |
| 77 | + : validationRules.environment(selection.id).message |
| 78 | + |
| 79 | + setCiCdPipelineFormError(updatedCiCdPipelineFormError) |
| 80 | + } |
| 81 | + |
| 82 | + const handleDeploymentAppTypeChange = (event: ChangeEvent<HTMLInputElement>) => { |
| 83 | + const { ci, cd } = structuredClone(ciCdPipeline) |
| 84 | + cd.deploymentAppType = event.target.value |
| 85 | + setCiCdPipeline({ ci, cd }) |
| 86 | + } |
| 87 | + |
| 88 | + const checkGitOpsRepoConflict = async () => { |
| 89 | + setGitopsConflictLoading(true) |
| 90 | + |
| 91 | + try { |
| 92 | + await getGitOpsRepoConfig(+appId) |
| 93 | + |
| 94 | + setGitopsConflictLoading(false) |
| 95 | + push(`/app/${appId}/edit/${URLS.APP_GITOPS_CONFIG}`) |
| 96 | + } catch (err) { |
| 97 | + setGitopsConflictLoading(false) |
| 98 | + if (err.code === API_STATUS_CODES.CONFLICT) { |
| 99 | + setReloadNoGitOpsRepoConfiguredModal(true) |
| 100 | + } else { |
| 101 | + showError(err) |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + // RENDERERS |
| 107 | + const gitOpsRepoConfigInfoBar = (content: string) => ( |
| 108 | + <InfoBlock |
| 109 | + description={content} |
| 110 | + variant="warning" |
| 111 | + buttonProps={{ |
| 112 | + dataTestId: 'configure-gitops-repo-button', |
| 113 | + variant: ButtonVariantType.text, |
| 114 | + text: 'Configure', |
| 115 | + endIcon: <Icon name="ic-arrow-right" color={null} />, |
| 116 | + onClick: checkGitOpsRepoConflict, |
| 117 | + isLoading: gitopsConflictLoading, |
| 118 | + }} |
| 119 | + /> |
| 120 | + ) |
| 121 | + |
| 122 | + return ( |
| 123 | + <div className="flexbox-col dc__gap-20"> |
| 124 | + {!!cdNodeCreateError && ( |
| 125 | + <InfoBlock |
| 126 | + variant="error" |
| 127 | + heading="Failed to create deployment pipeline" |
| 128 | + description={cdNodeCreateError.errors?.[0]?.userMessage ?? 'failed'} |
| 129 | + size={ComponentSizeType.medium} |
| 130 | + buttonProps={{ |
| 131 | + dataTestId: 'retry-cd-node-creation', |
| 132 | + startIcon: <Icon name="ic-arrow-clockwise" color={null} />, |
| 133 | + variant: ButtonVariantType.text, |
| 134 | + text: 'Retry', |
| 135 | + onClick: onRetry, |
| 136 | + }} |
| 137 | + /> |
| 138 | + )} |
| 139 | + <div> |
| 140 | + <SourceMaterialsSelector |
| 141 | + branchInputProps={{ |
| 142 | + name: 'cd-pipeline-namespace', |
| 143 | + onChange: noop, |
| 144 | + placeholder: |
| 145 | + selectedEnvironment?.isVirtualEnvironment && !selectedEnvironment?.namespace |
| 146 | + ? 'Not available' |
| 147 | + : 'Will be auto-populated based on environment', |
| 148 | + label: 'Namespace', |
| 149 | + value: selectedEnvironment?.namespace, |
| 150 | + disabled: true, |
| 151 | + }} |
| 152 | + sourceTypePickerProps={{ |
| 153 | + inputId: 'cd-pipeline-environment', |
| 154 | + classNamePrefix: 'cd-pipeline-environment', |
| 155 | + label: 'Environment', |
| 156 | + placeholder: 'Select environment', |
| 157 | + isDisabled: isFormDisabled, |
| 158 | + menuPosition: 'fixed', |
| 159 | + options: getEnvironmentOptions(environments), |
| 160 | + value: selectedEnvironment, |
| 161 | + getOptionValue: (option) => option.value as string, |
| 162 | + helperText: selectedEnvironment?.isVirtualEnvironment ? 'Isolated environment' : null, |
| 163 | + error: ciCdPipelineFormError.cd.environment ?? null, |
| 164 | + onChange: handleEnvironmentChange, |
| 165 | + }} |
| 166 | + /> |
| 167 | + <div className="mt-16"> |
| 168 | + {gitOpsRepoNotConfiguredAndOptionsHidden && gitOpsRepoConfigInfoBar(GITOPS_REPO_REQUIRED)} |
| 169 | + </div> |
| 170 | + <CDPipelineDeploymentAppType |
| 171 | + isVirtualEnvironment={selectedEnvironment?.isVirtualEnvironment} |
| 172 | + isGitOpsInstalledButNotConfigured={isGitOpsInstalledButNotConfigured} |
| 173 | + noGitOpsModuleInstalledAndConfigured={noGitOpsModuleInstalledAndConfigured} |
| 174 | + deploymentAppType={deploymentAppType ?? DeploymentAppTypes.HELM} |
| 175 | + handleChange={handleDeploymentAppTypeChange} |
| 176 | + allowedDeploymentTypes={selectedEnvironment?.allowedDeploymentTypes} |
| 177 | + rootClassName="chartrepo-type__radio-group" |
| 178 | + isDisabled={isFormDisabled} |
| 179 | + isGitOpsRepoNotConfigured={isGitOpsRepoNotConfigured} |
| 180 | + gitOpsRepoConfigInfoBar={gitOpsRepoConfigInfoBar} |
| 181 | + areGitopsCredentialsConfigured={!isGitOpsInstalledButNotConfigured} |
| 182 | + /> |
| 183 | + </div> |
| 184 | + </div> |
| 185 | + ) |
| 186 | +} |
0 commit comments