Skip to content

Commit 37e1564

Browse files
Merge pull request #2713 from devtron-labs/fix/cd-pipeline-strategy
chore: cd pipeline strategy auto select
2 parents 4819cfb + 179240e commit 37e1564

File tree

1 file changed

+74
-61
lines changed

1 file changed

+74
-61
lines changed

src/components/cdPipeline/CDPipeline.tsx

Lines changed: 74 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -334,78 +334,91 @@ export default function CDPipeline({
334334
handlePluginDataStoreUpdate(getUpdatedPluginStore(pluginDataStore, parentPluginStore, pluginVersionStore))
335335
}
336336

337-
const getEnvCDPipelineName = (form) => {
338-
Promise.all([getCDPipelineNameSuggestion(appId, isTemplateView), getEnvironmentListMinPublic(true)])
339-
.then(([cpPipelineName, envList]) => {
340-
form.name = cpPipelineName.result
341-
let list = envList.result || []
342-
list = list.map((env) => {
343-
return {
344-
id: env.id,
345-
clusterId: env.cluster_id,
346-
clusterName: env.cluster_name,
347-
name: env.environment_name,
348-
namespace: env.namespace || '',
349-
active: false,
350-
isClusterCdActive: env.isClusterCdActive,
351-
description: env.description,
352-
isVirtualEnvironment: env.isVirtualEnvironment,
353-
allowedDeploymentTypes: env.allowedDeploymentTypes || [],
354-
isDigestEnforcedForEnv: env.isDigestEnforcedForEnv,
355-
}
356-
})
357-
sortObjectArrayAlphabetically(list, 'name')
358-
form.environments = list
359-
setFormData((prevState) => {
360-
return {
361-
...form,
362-
// Can retain the release mode since this method is called only when creating a new pipeline
363-
releaseMode: prevState.releaseMode,
364-
}
365-
})
366-
setPageState(ViewType.FORM)
367-
setIsAdvanced(false)
368-
})
369-
.catch((error) => {
370-
showError(error)
371-
})
337+
const getNewPipelineNameAndEnvironments = async (): Promise<Pick<typeof formData, 'name' | 'environments'>> => {
338+
try {
339+
const [cpPipelineName, envList] = await Promise.all([
340+
getCDPipelineNameSuggestion(appId, isTemplateView),
341+
getEnvironmentListMinPublic(true),
342+
])
343+
const list = (envList.result || []).map((env) => ({
344+
id: env.id,
345+
clusterId: env.cluster_id,
346+
clusterName: env.cluster_name,
347+
name: env.environment_name,
348+
namespace: env.namespace || '',
349+
active: false,
350+
isClusterCdActive: env.isClusterCdActive,
351+
description: env.description,
352+
isVirtualEnvironment: env.isVirtualEnvironment,
353+
allowedDeploymentTypes: env.allowedDeploymentTypes || [],
354+
isDigestEnforcedForEnv: env.isDigestEnforcedForEnv,
355+
}))
356+
357+
return {
358+
name: cpPipelineName.result,
359+
environments: sortObjectArrayAlphabetically(list, 'name'),
360+
}
361+
} catch (error) {
362+
showError(error)
363+
}
364+
365+
return {
366+
name: '',
367+
environments: [],
368+
}
372369
}
373370

374371
const getInit = () => {
375372
Promise.all([
376373
getDeploymentStrategyList(appId, isTemplateView),
377374
getGlobalVariables({ appId: Number(appId), isCD: true }),
378375
getDockerRegistryMinAuth(appId, true),
376+
!cdPipelineId ? getNewPipelineNameAndEnvironments() : { name: '', environments: [] },
379377
])
380-
.then(([pipelineStrategyResponse, globalVariablesOptions, dockerResponse]) => {
381-
const strategies = pipelineStrategyResponse.result.pipelineStrategy || []
382-
const dockerRegistries = dockerResponse.result || []
383-
const _allStrategies = {}
384-
385-
strategies.forEach((strategy) => {
386-
if (!_allStrategies[strategy.deploymentTemplate]) {
387-
_allStrategies[strategy.deploymentTemplate] = {}
388-
}
389-
_allStrategies[strategy.deploymentTemplate] = strategy.config
390-
})
391-
allStrategies.current = _allStrategies
392-
noStrategyAvailable.current = strategies.length === 0
378+
.then(
379+
([
380+
pipelineStrategyResponse,
381+
globalVariablesOptions,
382+
dockerResponse,
383+
newPipelineNameAndEnvironments,
384+
]) => {
385+
const strategies = pipelineStrategyResponse.result.pipelineStrategy || []
386+
const dockerRegistries = dockerResponse.result || []
387+
const _allStrategies = {}
388+
389+
strategies.forEach((strategy) => {
390+
if (!_allStrategies[strategy.deploymentTemplate]) {
391+
_allStrategies[strategy.deploymentTemplate] = {}
392+
}
393+
_allStrategies[strategy.deploymentTemplate] = strategy.config
394+
})
395+
allStrategies.current = _allStrategies
396+
noStrategyAvailable.current = strategies.length === 0
393397

394-
const _form = { ...formData }
395-
_form.strategies = strategies
396-
if (cdPipelineId) {
397-
getCDPipeline(_form, dockerRegistries)
398-
} else {
399-
getEnvCDPipelineName(_form)
400-
if (strategies.length > 0) {
401-
const defaultStrategy = strategies.find((strategy) => strategy.default)
402-
handleStrategy(defaultStrategy.deploymentTemplate)
398+
if (cdPipelineId) {
399+
const _form = { ...formData }
400+
_form.strategies = strategies
401+
getCDPipeline(_form, dockerRegistries)
402+
} else {
403+
if (strategies.length > 0) {
404+
const defaultStrategy = strategies.find((strategy) => strategy.default)
405+
handleStrategy(defaultStrategy.deploymentTemplate)
406+
}
407+
408+
setFormData((prevState) => ({
409+
...prevState,
410+
...newPipelineNameAndEnvironments,
411+
strategies,
412+
}))
413+
414+
setPageState(ViewType.FORM)
415+
setIsAdvanced(false)
403416
}
404-
}
405417

406-
setGlobalVariables(globalVariablesOptions)
407-
setDockerRegistries(dockerRegistries)
408-
})
418+
setGlobalVariables(globalVariablesOptions)
419+
setDockerRegistries(dockerRegistries)
420+
},
421+
)
409422
.catch((error: ServerErrors) => {
410423
showError(error)
411424
setErrorCode(error.code)

0 commit comments

Comments
 (0)