-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkflows.ts
More file actions
305 lines (260 loc) · 13.1 KB
/
workflows.ts
File metadata and controls
305 lines (260 loc) · 13.1 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import { join, resolve } from 'path';
import { existsSync, readFileSync, writeFileSync } from 'fs';
import { cloneDeep } from 'lodash';
import { ContentTypeStruct, CtConstructorParam, ModuleConstructorParam, Workflow } from '../types';
import { cliux, sanitizePath, log } from '@contentstack/cli-utilities';
import auditConfig from '../config';
import { $t, auditMsg, commonMsg } from '../messages';
import { values } from 'lodash';
import BaseClass from './base-class';
export default class Workflows extends BaseClass {
protected fix: boolean;
public fileName: any;
public folderPath: string;
public workflowSchema: Workflow[];
public ctSchema: ContentTypeStruct[];
public moduleName: keyof typeof auditConfig.moduleConfig;
public ctUidSet: Set<string>;
public missingCtInWorkflows: Workflow[];
public missingCts: Set<string>;
public workflowPath: string;
public isBranchFixDone: boolean;
constructor({
fix,
config,
moduleName,
ctSchema,
}: ModuleConstructorParam & Pick<CtConstructorParam, 'ctSchema'>) {
super({ config });
this.fix = fix ?? false;
this.ctSchema = ctSchema;
this.workflowSchema = [];
log.debug(`Initializing Workflows module`, this.config.auditContext);
log.debug(`Fix mode: ${this.fix}`, this.config.auditContext);
log.debug(`Content types count: ${ctSchema.length}`, this.config.auditContext);
log.debug(`Module name: ${moduleName}`, this.config.auditContext);
this.moduleName = this.validateModules(moduleName!, this.config.moduleConfig);
this.fileName = config.moduleConfig[this.moduleName].fileName;
log.debug(`File name: ${this.fileName}`, this.config.auditContext);
this.folderPath = resolve(
sanitizePath(config.basePath),
sanitizePath(config.moduleConfig[this.moduleName].dirName),
);
log.debug(`Folder path: ${this.folderPath}`, this.config.auditContext);
this.ctUidSet = new Set(['$all']);
this.missingCtInWorkflows = [];
this.missingCts = new Set();
this.workflowPath = '';
this.isBranchFixDone = false;
log.debug(`Workflows module initialization completed`, this.config.auditContext);
}
validateModules(
moduleName: keyof typeof auditConfig.moduleConfig,
moduleConfig: Record<string, unknown>,
): keyof typeof auditConfig.moduleConfig {
log.debug(`Validating module: ${moduleName}`, this.config.auditContext);
log.debug(`Available modules: ${Object.keys(moduleConfig).join(', ')}`, this.config.auditContext);
if (Object.keys(moduleConfig).includes(moduleName)) {
log.debug(`Module ${moduleName} is valid`, this.config.auditContext);
return moduleName;
}
log.debug(`Module ${moduleName} not found, defaulting to 'workflows'`, this.config.auditContext);
return 'workflows';
}
/**
* Check whether the given path for the workflow exists or not
* If path exist read
* From the ctSchema add all the content type UID into ctUidSet to check whether the content-type is present or not
* @returns Array of object containing the workflow name, uid and content_types that are missing
*/
async run(totalCount?: number) {
try {
if (!existsSync(this.folderPath)) {
log.debug(`Skipping ${this.moduleName} audit - path does not exist`, this.config.auditContext);
log.warn(`Skipping ${this.moduleName} audit`, this.config.auditContext);
cliux.print($t(auditMsg.NOT_VALID_PATH, { path: this.folderPath }), { color: 'yellow' });
return {};
}
this.workflowPath = join(this.folderPath, this.fileName);
log.debug(`Workflows file path: ${this.workflowPath}`, this.config.auditContext);
// Load workflows schema with loading spinner
await this.withLoadingSpinner('WORKFLOWS: Loading workflows schema...', async () => {
this.workflowSchema = existsSync(this.workflowPath)
? values(JSON.parse(readFileSync(this.workflowPath, 'utf8')) as Workflow[])
: [];
});
log.debug(`Loaded ${this.workflowSchema.length} workflows`, this.config.auditContext);
log.debug(`Building content type UID set from ${this.ctSchema.length} content types`, this.config.auditContext);
this.ctSchema.forEach((ct) => this.ctUidSet.add(ct.uid));
log.debug(`Content type UID set contains: ${Array.from(this.ctUidSet).join(', ')}`, this.config.auditContext);
// Create progress manager if we have a total count
if (totalCount && totalCount > 0) {
const progress = this.createSimpleProgress(this.moduleName, totalCount);
progress.updateStatus('Validating workflows...');
}
log.debug(`Processing ${this.workflowSchema.length} workflows`, this.config.auditContext);
for (const workflow of this.workflowSchema) {
const { name, uid } = workflow;
log.debug(`Processing workflow: ${name} (${uid})`, this.config.auditContext);
log.debug(`Workflow content types: ${workflow.content_types?.join(', ') || 'none'}`, this.config.auditContext);
log.debug(`Workflow branches: ${workflow.branches?.join(', ') || 'none'}`, this.config.auditContext);
const ctNotPresent = workflow.content_types.filter((ct) => !this.ctUidSet.has(ct));
log.debug(`Missing content types in workflow: ${ctNotPresent?.join(', ') || 'none'}`, this.config.auditContext);
log.debug(`Config branch : ${this.config.branch}`, this.config.auditContext);
let branchesToBeRemoved: string[] = [];
if (this.config?.branch) {
branchesToBeRemoved = workflow?.branches?.filter((branch) => branch !== this.config?.branch) || [];
log.debug(`Branches to be removed: ${branchesToBeRemoved?.join(', ') || 'none'}`, this.config.auditContext);
} else {
log.debug(`No branch configuration found`, this.config.auditContext);
}
if (ctNotPresent.length || branchesToBeRemoved?.length) {
log.debug(`Workflow ${name} has issues - missing content types: ${ctNotPresent.length}, branches to remove: ${branchesToBeRemoved.length}`, this.config.auditContext);
const tempwf = cloneDeep(workflow);
tempwf.content_types = ctNotPresent || [];
if (workflow?.branches && this.config?.branch) {
tempwf.branches = branchesToBeRemoved;
}
if (branchesToBeRemoved?.length) {
log.debug(`Branch fix will be needed`, this.config.auditContext);
this.isBranchFixDone = true;
}
ctNotPresent.forEach((ct) => {
log.debug(`Adding missing content type: ${ct} to the Audit report.`, this.config.auditContext);
this.missingCts.add(ct);
});
this.missingCtInWorkflows.push(tempwf);
} else {
log.debug(`Workflow ${name} has no issues`, this.config.auditContext);
}
log.info(
$t(auditMsg.SCAN_WF_SUCCESS_MSG, {
name: workflow.name,
uid: workflow.uid,
}),
this.config.auditContext
);
}
log.debug(`Workflows audit completed. Found ${this.missingCtInWorkflows.length} workflows with issues`, this.config.auditContext);
log.debug(`Total missing content types: ${this.missingCts.size}`, this.config.auditContext);
log.debug(`Branch fix needed: ${this.isBranchFixDone}`, this.config.auditContext);
if (this.fix && (this.missingCtInWorkflows.length || this.isBranchFixDone)) {
log.debug(`Fix mode enabled, fixing ${this.missingCtInWorkflows.length} workflows`, this.config.auditContext);
await this.fixWorkflowSchema();
this.missingCtInWorkflows.forEach((wf) => {
log.debug(`Marking workflow ${wf.name} as fixed`, this.config.auditContext);
wf.fixStatus = 'Fixed';
});
log.debug(`Workflows fix completed`, this.config.auditContext);
this.completeProgress(true);
return this.missingCtInWorkflows;
}
log.debug(`Workflows audit completed without fixes`, this.config.auditContext);
this.completeProgress(true);
return this.missingCtInWorkflows;
} catch (error: any) {
this.completeProgress(false, error?.message || 'Workflows audit failed');
throw error;
}
}
async fixWorkflowSchema() {
log.debug(`Starting workflow schema fix`, this.config.auditContext);
const newWorkflowSchema: Record<string, Workflow> = existsSync(this.workflowPath)
? JSON.parse(readFileSync(this.workflowPath, 'utf8'))
: {};
log.debug(`Loaded ${Object.keys(newWorkflowSchema).length} workflows for fixing`, this.config.auditContext);
const hasWorkflowsToFix = Object.keys(newWorkflowSchema).length !== 0;
let userConfirm: boolean;
if (!hasWorkflowsToFix) {
userConfirm = true;
} else if (
this.config.flags['copy-dir'] ||
this.config.flags['external-config']?.skipConfirm ||
this.config.flags.yes
) {
userConfirm = true;
} else {
this.completeProgress(true);
userConfirm = await cliux.confirm(commonMsg.FIX_CONFIRMATION);
}
if (hasWorkflowsToFix) {
log.debug(`Processing ${this.workflowSchema.length} workflows for fixes`, this.config.auditContext);
for (const workflow of this.workflowSchema) {
const { name, uid } = workflow;
log.debug(`Fixing workflow: ${name} (${uid})`, this.config.auditContext);
const fixedCts = workflow.content_types.filter((ct) => !this.missingCts.has(ct));
log.debug(`Fixed content types: ${fixedCts.join(', ') || 'none'}`, this.config.auditContext);
const fixedBranches: string[] = [];
if (this.config.branch) {
log.debug(`Config branch : ${this.config.branch}`, this.config.auditContext);
log.debug(`Processing branches for workflow ${name}`, this.config.auditContext);
workflow?.branches?.forEach((branch) => {
if (branch !== this.config?.branch) {
log.debug(`Removing branch: ${branch} from workflow ${name}`, this.config.auditContext);
cliux.print($t(commonMsg.WF_BRANCH_REMOVAL, { uid, name, branch }), { color: 'yellow' });
} else {
log.debug(`Keeping branch: ${branch} for workflow ${name}`, this.config.auditContext);
fixedBranches.push(branch);
}
});
if (fixedBranches.length > 0) {
log.debug(`Setting ${fixedBranches.length} fixed branches for workflow ${name}`, this.config.auditContext);
newWorkflowSchema[workflow.uid].branches = fixedBranches;
}
} else {
log.debug(`No branch configuration for workflow ${name}`, this.config.auditContext);
}
if (fixedCts.length) {
log.debug(`Setting ${fixedCts.length} fixed content types for workflow ${name}`, this.config.auditContext);
newWorkflowSchema[workflow.uid].content_types = fixedCts;
} else {
const { name, uid } = workflow;
log.debug(`No valid content types for workflow ${name}, considering deletion`, this.config.auditContext);
const warningMessage = $t(commonMsg.WORKFLOW_FIX_WARN, { name, uid });
cliux.print(warningMessage, { color: 'yellow' });
if (userConfirm) {
log.debug(`Deleting workflow ${name} (${uid})`, this.config.auditContext);
delete newWorkflowSchema[workflow.uid];
} else {
log.debug(`Keeping workflow ${name} (${uid}) despite no valid content types`, this.config.auditContext);
}
}
}
} else {
log.debug(`No workflows found to fix`, this.config.auditContext);
}
log.debug(`Workflow schema fix completed`, this.config.auditContext);
await this.writeFixContent(newWorkflowSchema, userConfirm);
}
async writeFixContent(newWorkflowSchema: Record<string, Workflow>, preConfirmed?: boolean) {
log.debug(`Writing fix content`, this.config.auditContext);
log.debug(`Fix mode: ${this.fix}`, this.config.auditContext);
log.debug(`Copy directory flag: ${this.config.flags['copy-dir']}`, this.config.auditContext);
log.debug(`External config skip confirm: ${this.config.flags['external-config']?.skipConfirm}`, this.config.auditContext);
log.debug(`Yes flag: ${this.config.flags.yes}`, this.config.auditContext);
log.debug(`Workflows to write: ${Object.keys(newWorkflowSchema).length}`, this.config.auditContext);
let shouldWrite: boolean;
if (!this.fix) {
shouldWrite = false;
} else if (preConfirmed !== undefined) {
shouldWrite = preConfirmed;
} else if (
this.config.flags['copy-dir'] ||
this.config.flags['external-config']?.skipConfirm ||
this.config.flags.yes
) {
shouldWrite = true;
} else {
this.completeProgress(true);
shouldWrite = await cliux.confirm(commonMsg.FIX_CONFIRMATION);
}
if (shouldWrite) {
const outputPath = join(this.folderPath, this.config.moduleConfig[this.moduleName].fileName);
log.debug(`Writing fixed workflows to: ${outputPath}`, this.config.auditContext);
writeFileSync(outputPath, JSON.stringify(newWorkflowSchema));
log.debug(`Successfully wrote fixed workflows to file`, this.config.auditContext);
} else {
log.debug(`Skipping file write - fix mode disabled or user declined confirmation`, this.config.auditContext);
}
}
}