-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathutils.ts
More file actions
285 lines (258 loc) · 9.56 KB
/
utils.ts
File metadata and controls
285 lines (258 loc) · 9.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
* Copyright (c) 2024. Devtron Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {
DeploymentAppTypes,
MaterialType,
ReleaseMode,
SourceTypeMap,
TriggerType,
} from '@devtron-labs/devtron-fe-common-lib'
import { GeneratedHelmPush } from '@Components/cdPipeline/cdPipeline.types'
import { ValidationRules } from '@Components/ciPipeline/validationRules'
import { createClusterEnvGroup, getDeploymentAppType } from '@Components/common'
import { ENV_ALREADY_EXIST_ERROR } from '@Config/constants'
import { CreateCICDPipelineData, CreateCICDPipelineFormError } from './types'
export const getCiCdPipelineDefaultState = (): CreateCICDPipelineData => ({
ci: {
name: '',
materials: [],
gitHost: null,
webhookEvents: [],
ciPipelineSourceTypeOptions: [],
webhookConditionList: [],
triggerType: '',
scanEnabled: false,
workflowCacheConfig: null,
isBlobStorageConfigured: false,
isSecurityModuleInstalled: false,
ciPipelineEditable: true,
},
cd: {
name: '',
deploymentAppType: window._env_.HIDE_GITOPS_OR_HELM_OPTION ? '' : DeploymentAppTypes.HELM,
releaseMode: ReleaseMode.NEW_DEPLOYMENT,
triggerType: TriggerType.Auto,
strategies: [],
savedStrategies: [],
preStageConfigMapSecretNames: { configMaps: [], secrets: [] },
postStageConfigMapSecretNames: { configMaps: [], secrets: [] },
preBuildStage: {
id: 0,
triggerType: TriggerType.Auto,
steps: [],
},
postBuildStage: {
id: 0,
triggerType: TriggerType.Auto,
steps: [],
},
isClusterCdActive: false,
deploymentAppCreated: false,
clusterName: '',
clusterId: null,
runPreStageInEnv: false,
runPostStageInEnv: false,
containerRegistryName: '',
repoName: '',
selectedRegistry: null,
generatedHelmPushAction: GeneratedHelmPush.DO_NOT_PUSH,
isDigestEnforcedForPipeline: false,
isDigestEnforcedForEnv: false,
selectedEnvironment: null,
environments: [],
},
})
export const getCiCdPipelineFormErrorDefaultState = (): CreateCICDPipelineFormError => ({ ci: {}, cd: {} })
export const getSelectedMaterial = ({
type,
selectedWebhookEvent,
ciPipelineSourceTypeOptions,
isBranchRegex,
}: Required<
Pick<MaterialType, 'type'> &
Pick<CreateCICDPipelineData['ci'], 'ciPipelineSourceTypeOptions'> & {
selectedWebhookEvent: CreateCICDPipelineData['ci']['webhookEvents'][number]
isBranchRegex: boolean
}
>) => {
if (type === SourceTypeMap.WEBHOOK && !selectedWebhookEvent) {
return null
}
if (ciPipelineSourceTypeOptions.length === 1) {
return ciPipelineSourceTypeOptions[0]
}
return (
ciPipelineSourceTypeOptions.find((i) => {
if (i.value === SourceTypeMap.WEBHOOK) {
return i.isSelected
}
return isBranchRegex ? i.value === SourceTypeMap.BranchRegex : i.value === type
}) || ciPipelineSourceTypeOptions[0]
)
}
export const getBranchValue = ({
selectedMaterial,
isBranchRegex,
regex,
value,
}: Required<
Pick<MaterialType, 'regex' | 'value'> & {
selectedMaterial: CreateCICDPipelineData['ci']['ciPipelineSourceTypeOptions'][number]
isBranchRegex: boolean
}
>) => {
if (selectedMaterial) {
return isBranchRegex ? regex : value
}
return ''
}
export const getEnvironmentOptions = (environments: CreateCICDPipelineData['cd']['environments']) =>
createClusterEnvGroup(environments, 'clusterName').map(({ label, options }) => ({
label: `Cluster: ${label}`,
options: options.map((option) => ({
...option,
label: option.name,
value: option.id,
})),
}))
export const validateCreateCICDPipelineData = (
ciCdPipeline: CreateCICDPipelineData,
envIds: number[],
): { isValid: boolean; ciCdPipelineFormError: CreateCICDPipelineFormError } => {
const copyPipelineData = structuredClone(ciCdPipeline)
const validationRules = new ValidationRules()
let isValid = true
const ciPipelineFormError = copyPipelineData.ci.materials.reduce<CreateCICDPipelineFormError['ci']>(
(acc, { gitMaterialId, type, regex, value }) => {
if (type === SourceTypeMap.BranchFixed || type === SourceTypeMap.BranchRegex) {
const isBranchRegex = type === SourceTypeMap.BranchRegex
const inputValue = isBranchRegex ? regex : value
const errorObj = validationRules.sourceValue(inputValue, isBranchRegex)
acc[gitMaterialId] = {
branch: errorObj.message,
}
if (isValid) {
isValid = errorObj.isValid
}
}
return acc
},
{},
)
const selectedEnvId = ciCdPipeline.cd.selectedEnvironment?.id
const cdPipelineFormError: CreateCICDPipelineFormError['cd'] = {
environment: envIds.includes(selectedEnvId)
? ENV_ALREADY_EXIST_ERROR
: validationRules.environment(selectedEnvId).message,
}
isValid = isValid && !Object.values(cdPipelineFormError).some(Boolean)
return { isValid, ciCdPipelineFormError: { ci: ciPipelineFormError, cd: cdPipelineFormError } }
}
/**
* Generates the payload for saving CI pipeline materials.
*
* ## Behavior:
* - In **multi-git pipeline scenarios** (i.e., more than one material),
* if the user selects a **Webhook-based trigger** like "Pull Request" or "Tag Creation",
* we **only send the webhook material** and ignore the others.
* - If there's only one material, return it as-is.
*
* ## Why:
* Webhook triggers like PR or Tag creation are specific to a single git repository.
* Sending multiple repositories in this context would be invalid.
*
* @param materials - Array of source materials for the CI pipeline configuration.
* @returns A filtered array containing only the webhook material in multi-git + webhook scenarios,
* or the original array if it's single-material or not a webhook-based trigger.
*/
export const getSaveCIPipelineMaterialsPayload = (materials: MaterialType[]): MaterialType[] => {
if (materials.length > 1) {
const webhookMaterial = materials.find((m) => m.type === SourceTypeMap.WEBHOOK)
return webhookMaterial ? [webhookMaterial] : []
}
return materials
}
const getPrePostStageInEnv = (isVirtualEnvironment: boolean, isRunPrePostStageInEnv: boolean): boolean => {
if (isVirtualEnvironment) {
return true
}
return isRunPrePostStageInEnv ?? false
}
export const getSaveCDPipelinesPayload = ({
cd,
appWorkflowId,
ciPipelineId,
}: { ciPipelineId: number; appWorkflowId: number } & Pick<CreateCICDPipelineData, 'cd'>) => {
const {
name,
selectedEnvironment,
savedStrategies,
triggerType,
deploymentAppType,
deploymentAppCreated,
releaseMode,
preStageConfigMapSecretNames,
postStageConfigMapSecretNames,
generatedHelmPushAction,
containerRegistryName,
repoName,
isClusterCdActive,
runPreStageInEnv,
runPostStageInEnv,
isDigestEnforcedForPipeline,
} = cd
const pipeline = {
id: 0,
name,
appWorkflowId,
ciPipelineId,
environmentId: selectedEnvironment.id,
namespace: selectedEnvironment.namespace,
strategies: savedStrategies,
parentPipelineType: 'CI_PIPELINE',
parentPipelineId: ciPipelineId,
isClusterCdActive: selectedEnvironment.isClusterCdActive,
deploymentAppType: getDeploymentAppType(
selectedEnvironment.allowedDeploymentTypes,
deploymentAppType,
selectedEnvironment.isVirtualEnvironment,
),
deploymentAppName: '',
releaseMode,
deploymentAppCreated,
triggerType: selectedEnvironment.isVirtualEnvironment ? TriggerType.Manual : triggerType,
environmentName: selectedEnvironment.name,
preStageConfigMapSecretNames,
postStageConfigMapSecretNames,
containerRegistryName: generatedHelmPushAction === GeneratedHelmPush.PUSH ? containerRegistryName : '',
repoName: generatedHelmPushAction === GeneratedHelmPush.PUSH ? repoName : '',
manifestStorageType: generatedHelmPushAction === GeneratedHelmPush.PUSH ? 'helm_repo' : '',
runPreStageInEnv: getPrePostStageInEnv(
selectedEnvironment.isVirtualEnvironment,
isClusterCdActive && runPreStageInEnv,
),
runPostStageInEnv: getPrePostStageInEnv(
selectedEnvironment.isVirtualEnvironment,
isClusterCdActive && runPostStageInEnv,
),
preDeployStage: {},
postDeployStage: {},
addType: 'PARALLEL',
isDigestEnforcedForPipeline,
isDigestEnforcedForEnv: selectedEnvironment.isDigestEnforcedForEnv,
}
return [pipeline]
}