-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathAppGroup.service.ts
258 lines (231 loc) · 9.82 KB
/
AppGroup.service.ts
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
/*
* 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 {
get,
post,
put,
ResponseType,
trash,
WorkflowNodeType,
PipelineType,
CommonNodeAttr,
WorkflowType,
getUrlWithSearchParams,
CiPipeline,
} from '@devtron-labs/devtron-fe-common-lib'
import { CdPipelineResult, CiPipelineResult, WorkflowResult } from '../app/details/triggerView/types'
import { WebhookListResponse } from '../ciPipeline/Webhook/types'
import { processWorkflow } from '../app/details/triggerView/workflow.service'
import { WorkflowTrigger } from '../app/details/triggerView/config'
import { ModuleNameMap, Routes, URLS } from '../../config'
import {
AppGroupFilterConfig,
AppGroupList,
CIConfigListType,
CheckPermissionResponse,
CheckPermissionType,
ConfigAppListType,
EditDescRequestResponse,
EnvAppType,
EnvDeploymentStatusType,
EnvGroupListResponse,
EnvGroupResponse,
GetEnvAppListParamsType,
WorkflowsResponseType,
} from './AppGroup.types'
import { getModuleConfigured } from '../app/details/appDetails/appDetails.service'
import { getModuleInfo } from '../v2/devtronStackManager/DevtronStackManager.service'
import { ModuleStatus } from '../v2/devtronStackManager/DevtronStackManager.type'
const getFilteredAppQueryString = (appIds: string): string => {
let _appIdsQueryParam = ''
if (appIds) {
_appIdsQueryParam = `?appIds=${appIds}`
}
return _appIdsQueryParam
}
export function getEnvWorkflowList(envId: string, appIds: string) {
return get(`${Routes.ENV_WORKFLOW}/${envId}/${Routes.APP_WF}${getFilteredAppQueryString(appIds)}`)
}
export function getCIConfig(envID: string, appIds: string): Promise<ResponseType> {
return get(`${Routes.ENV_WORKFLOW}/${envID}/${URLS.APP_CI_CONFIG}${getFilteredAppQueryString(appIds)}`)
}
export function getCIConfigMin(envID: string, appIds: string): Promise<ResponseType> {
return get(`${Routes.ENV_WORKFLOW}/${envID}/${Routes.APP_CI_PIPELINE}${getFilteredAppQueryString(appIds)}`)
}
export function getCDConfig(envID: string, appIds: string): Promise<ResponseType> {
return get(`${Routes.ENV_WORKFLOW}/${envID}/${URLS.APP_CD_CONFIG}${getFilteredAppQueryString(appIds)}`)
}
export function getAppsCDConfigMin(envID: string, appIds: string): Promise<ResponseType> {
return get(`${Routes.ENV_WORKFLOW}/${envID}/${URLS.APP_CD_CONFIG}/min${getFilteredAppQueryString(appIds)}`)
}
export function getExternalCIList(envID: string, appIds: string): Promise<WebhookListResponse> {
return get(`${Routes.ENV_WORKFLOW}/${envID}/${URLS.APP_EXTERNAL_CI_CONFIG}${getFilteredAppQueryString(appIds)}`)
}
export const getWorkflowStatus = (envID: string, appIds: string) => {
return get(`${Routes.ENV_WORKFLOW}/${envID}/${Routes.WORKFLOW_STATUS}${getFilteredAppQueryString(appIds)}`)
}
export const getWorkflows = (envID: string, appIds: string): Promise<WorkflowsResponseType> => {
const _workflows: WorkflowType[] = []
const _filteredCIPipelines: Map<string, any> = new Map()
return Promise.all([
getEnvWorkflowList(envID, appIds),
getCIConfig(envID, appIds),
getCDConfig(envID, appIds),
getExternalCIList(envID, appIds),
]).then(([workflow, ciConfig, cdConfig, externalCIConfig]) => {
const _ciConfigMap = new Map<number, CiPipelineResult>()
for (const _ciConfig of ciConfig.result) {
_ciConfigMap.set(_ciConfig.appId, _ciConfig)
}
if (workflow?.result?.workflows) {
for (const workflowResult of workflow.result.workflows) {
const processWorkflowData = processWorkflow(
{
...workflowResult,
workflows: [workflowResult],
} as WorkflowResult,
_ciConfigMap.get(workflowResult.appId),
cdConfig.result as CdPipelineResult,
externalCIConfig.result,
WorkflowTrigger,
WorkflowTrigger.workflow,
filterChildAndSiblingCD(envID),
)
_workflows.push(...processWorkflowData.workflows)
_filteredCIPipelines.set(workflowResult.appId, processWorkflowData.filteredCIPipelines)
}
}
return { workflows: _workflows, filteredCIPipelines: _filteredCIPipelines }
})
}
export const getCIConfigList = (envID: string, appIds: string): Promise<CIConfigListType> => {
const _appCIPipelineMap: Map<string, CiPipeline> = new Map()
return Promise.all([
getCIConfigMin(envID, appIds),
getModuleInfo(ModuleNameMap.SECURITY),
getModuleConfigured(ModuleNameMap.BLOB_STORAGE),
]).then(([ciConfig, securityInfo, moduleConfig]) => {
return {
pipelineList: ciConfig.result,
securityModuleInstalled: securityInfo?.result?.status === ModuleStatus.INSTALLED,
blobStorageConfigured: moduleConfig?.result?.enabled,
}
})
}
const filterChildAndSiblingCD = function (envID: string): (workflows: WorkflowType[]) => WorkflowType[] {
return (workflows: WorkflowType[]): WorkflowType[] => {
workflows.forEach((wf) => {
const nodes = new Map(wf.nodes.map((node) => [`${node.type}-${node.id}`, node] as [string, CommonNodeAttr]))
let node = wf.nodes.find((node) => node.environmentId === +envID)
if (!node) {
wf.nodes = []
return wf
}
node.downstreamNodes = []
node.downstreams = []
if (node.postNode) {
node.downstreams = [`${WorkflowNodeType.POST_CD}-${node.id}`]
node.postNode.downstreams = []
}
const finalNodes = [node]
while (node) {
node = getParentNode(nodes, node)
if (node) {
finalNodes.push(node)
}
}
wf.nodes = finalNodes
return wf
})
return workflows
}
}
function getParentNode(nodes: Map<string, CommonNodeAttr>, node: CommonNodeAttr): CommonNodeAttr | undefined {
let parentType = WorkflowNodeType.CD
if (node.parentPipelineType == PipelineType.CI_PIPELINE) {
parentType = WorkflowNodeType.CI
} else if (node.parentPipelineType == PipelineType.WEBHOOK) {
parentType = WorkflowNodeType.WEBHOOK
}
const parentNode = nodes.get(`${parentType}-${node.parentPipelineId}`)
const type = node.preNode ? WorkflowNodeType.PRE_CD : node.type
if (parentNode) {
if (parentNode.postNode) {
parentNode.postNode.downstreams = [`${type}-${node.id}`]
} else {
parentNode.downstreams = [`${type}-${node.id}`]
}
parentNode.downstreamNodes = [node]
}
return parentNode
}
export const getConfigAppList = (envId: number, appIds: string): Promise<ConfigAppListType> => {
return get(`${Routes.ENVIRONMENT}/${envId}/${Routes.ENV_APPLICATIONS}${getFilteredAppQueryString(appIds)}`)
}
export const getEnvAppList = (
filterConfig?: Partial<AppGroupFilterConfig>,
signal?: AbortSignal,
): Promise<EnvAppType> => {
const options = signal ? { signal } : null
const { searchKey = '', cluster = [], offset, pageSize } = filterConfig ?? {}
const params: GetEnvAppListParamsType = {
envName: searchKey.toLowerCase(),
clusterIds: cluster?.join(),
offset,
size: pageSize,
}
const url = getUrlWithSearchParams(Routes.ENVIRONMENT_APPS, params)
return get(url, options)
}
export const getDeploymentStatus = (envId: number, appIds: string): Promise<EnvDeploymentStatusType> => {
return get(`${Routes.ENVIRONMENT}/${envId}/${Routes.ENV_DEPLOYMENT_STATUS}${getFilteredAppQueryString(appIds)}`)
}
export const getAppGroupList = (envId: number): Promise<AppGroupList> => {
return get(`${Routes.APP_LIST_GROUP}/${envId}`)
}
export const getEnvGroupList = (envId: number, filterParentType?: string): Promise<EnvGroupListResponse> => {
let filterParentTypeQuery = ''
if (filterParentType) {
filterParentTypeQuery = `?groupType=${filterParentType}`
}
return get(`${Routes.ENVIRONMENT}/${envId}/${Routes.GROUPS}${filterParentTypeQuery}`)
}
export const getEnvGroup = (envId: number, groupId: number): Promise<EnvGroupResponse> => {
return get(`${Routes.ENVIRONMENT}/${envId}/${Routes.GROUP}/${groupId}`)
}
export const createEnvGroup = (envId: string, data, isEdit: boolean): Promise<EnvGroupResponse> => {
if (isEdit) {
return put(`${Routes.ENVIRONMENT}/${envId}/${Routes.GROUP}`, data)
}
return post(`${Routes.ENVIRONMENT}/${envId}/${Routes.GROUP}`, data)
}
export const appGroupPermission = (envId: string, data: CheckPermissionType): Promise<CheckPermissionResponse> => {
return post(`${Routes.ENVIRONMENT}/${envId}/${Routes.GROUP}/${Routes.PERMISSION}`, data)
}
export const deleteEnvGroup = (
envId: string,
groupId: string,
filterParentType?: string,
): Promise<EnvGroupResponse> => {
let filterParentTypeQuery = ''
if (filterParentType) {
filterParentTypeQuery = `?groupType=${filterParentType}`
}
return trash(`${Routes.ENVIRONMENT}/${envId}/${Routes.GROUP}/${groupId}${filterParentTypeQuery}`)
}
export const editDescription = (payload): Promise<EditDescRequestResponse> => {
return put(Routes.ENVIRONMENT, payload)
}