-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkflows.ts
More file actions
176 lines (149 loc) · 7.11 KB
/
workflows.ts
File metadata and controls
176 lines (149 loc) · 7.11 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
import omit from 'lodash/omit';
import isEmpty from 'lodash/isEmpty';
import { resolve as pResolve } from 'node:path';
import { handleAndLogError, messageHandler, log } from '@contentstack/cli-utilities';
import BaseClass from './base-class';
import { WorkflowConfig, ModuleClassParams } from '../../types';
import { fsUtil, getExportBasePath, MODULE_CONTEXTS, MODULE_NAMES } from '../../utils';
export default class ExportWorkFlows extends BaseClass {
private workflows: Record<string, Record<string, string>>;
private workflowConfig: WorkflowConfig;
public webhooksFolderPath: string;
private qs: {
include_count: boolean;
skip?: number;
};
constructor({ exportConfig, stackAPIClient }: ModuleClassParams) {
super({ exportConfig, stackAPIClient });
this.workflows = {};
this.workflowConfig = exportConfig.modules.workflows;
this.qs = { include_count: true };
this.exportConfig.context.module = MODULE_CONTEXTS.WORKFLOWS;
this.currentModuleName = MODULE_NAMES[MODULE_CONTEXTS.WORKFLOWS];
}
async start(): Promise<void> {
try {
log.debug('Starting workflows export process...', this.exportConfig.context);
// Setup with loading spinner
const [totalCount] = await this.withLoadingSpinner('WORKFLOWS: Analyzing workflows...', async () => {
this.webhooksFolderPath = pResolve(
getExportBasePath(this.exportConfig),
this.workflowConfig.dirName,
);
await fsUtil.makeDirectory(this.webhooksFolderPath);
// Get count for progress tracking
const countResponse = await this.stack.workflow().fetchAll({ ...this.qs, limit: 1 });
const workflowCount =
countResponse.count !== undefined ? countResponse.count : countResponse.items?.length || 0;
return [workflowCount];
});
if (totalCount === 0) {
log.info(messageHandler.parse('WORKFLOW_NOT_FOUND'), this.exportConfig.context);
return;
}
// Create nested progress manager for complex workflow processing
const progress = this.createSimpleProgress(this.currentModuleName, totalCount);
// Fetch workflows
progress.updateStatus('Fetching workflow definitions...');
await this.getWorkflows();
log.debug(`Retrieved ${Object.keys(this.workflows || {}).length} workflows`, this.exportConfig.context);
if (this.workflows === undefined || isEmpty(this.workflows)) {
log.info(messageHandler.parse('WORKFLOW_NOT_FOUND'), this.exportConfig.context);
} else {
const workflowsFilePath = pResolve(this.webhooksFolderPath, this.workflowConfig.fileName);
log.debug(`Writing workflows to: ${workflowsFilePath}`, this.exportConfig.context);
fsUtil.writeFile(workflowsFilePath, this.workflows);
}
this.completeProgressWithMessage();
} catch (error) {
handleAndLogError(error, { ...this.exportConfig.context });
this.completeProgress(false, error?.message || 'Workflows export failed');
}
}
async getWorkflows(skip = 0): Promise<void> {
if (skip) {
this.qs.skip = skip;
log.debug(`Fetching workflows with skip: ${skip}`, this.exportConfig.context);
}
log.debug(`Query parameters: ${JSON.stringify(this.qs)}`, this.exportConfig.context);
await this.stack
.workflow()
.fetchAll(this.qs)
.then(async (data: any) => {
const { items, count } = data;
//NOTE - Handle the case where old workflow api is enabled in that case getting responses as objects.
const workflowCount = count !== undefined ? count : items.length;
log.debug(`Fetched ${items?.length || 0} workflows out of total ${workflowCount}`, this.exportConfig.context);
if (items?.length) {
log.debug(`Processing ${items.length} workflows`, this.exportConfig.context);
await this.sanitizeAttribs(items);
skip += this.workflowConfig.limit || 100;
if (skip >= workflowCount) {
log.debug('Completed fetching all workflows', this.exportConfig.context);
return;
}
log.debug(`Continuing to fetch workflows with skip: ${skip}`, this.exportConfig.context);
return await this.getWorkflows(skip);
} else {
log.debug('No workflows found to process', this.exportConfig.context);
}
})
.catch((error: any) => {
log.debug('Error occurred while fetching workflows', this.exportConfig.context);
handleAndLogError(error, { ...this.exportConfig.context });
});
}
async sanitizeAttribs(workflows: Record<string, string>[]) {
log.debug(`Sanitizing ${workflows.length} workflows`, this.exportConfig.context);
for (let index = 0; index < workflows?.length; index++) {
const workflowUid = workflows[index].uid;
const workflowName = workflows[index]?.name || '';
log.debug(`Processing workflow: ${workflowName} (${workflowUid})`, this.exportConfig.context);
try {
await this.getWorkflowRoles(workflows[index]);
this.workflows[workflowUid] = omit(workflows[index], this.workflowConfig.invalidKeys);
log.success(messageHandler.parse('WORKFLOW_EXPORT_SUCCESS', workflowName), this.exportConfig.context);
// Track progress for each workflow
this.progressManager?.tick(true, `workflow: ${workflowName}`);
} catch (error) {
log.error(`Failed to process workflow: ${workflowName}`, this.exportConfig.context);
this.progressManager?.tick(false, `workflow: ${workflowName}`, error?.message || 'Processing failed', 'Fetch');
}
}
log.debug(
`Sanitization complete. Total workflows processed: ${Object.keys(this.workflows).length}`,
this.exportConfig.context,
);
}
async getWorkflowRoles(workflow: Record<string, any>) {
log.debug(`Processing workflow roles for workflow: ${workflow.uid}`, this.exportConfig.context);
for (const stage of workflow?.workflow_stages) {
log.debug(`Processing workflow stage: ${stage.name}`, this.exportConfig.context);
for (let i = 0; i < stage?.SYS_ACL?.roles?.uids?.length; i++) {
const roleUid = stage.SYS_ACL.roles.uids[i];
log.debug(`Fetching role data for role UID: ${roleUid}`, this.exportConfig.context);
try {
const roleData = await this.getRoles(roleUid);
stage.SYS_ACL.roles.uids[i] = roleData;
} catch (error) {
log.error(`Failed to fetch role ${roleUid}`, this.exportConfig.context);
}
}
}
}
async getRoles(roleUid: number): Promise<any> {
log.debug(`Fetching role with UID: ${roleUid}`, this.exportConfig.context);
return await this.stack
.role(roleUid)
.fetch({ include_rules: true, include_permissions: true })
.then((data: any) => {
log.debug(`Successfully fetched role data for UID: ${roleUid}`, this.exportConfig.context);
return data;
})
.catch((err: any): any => {
log.debug(`Failed to fetch role data for UID: ${roleUid}`, this.exportConfig.context);
handleAndLogError(err, { ...this.exportConfig.context });
return undefined; // Return undefined instead of throwing to handle gracefully
});
}
}