-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom-roles.ts
More file actions
268 lines (232 loc) · 12.5 KB
/
custom-roles.ts
File metadata and controls
268 lines (232 loc) · 12.5 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
import { join, resolve } from 'path';
import { existsSync, readFileSync, writeFileSync } from 'fs';
import { cloneDeep } from 'lodash';
import { CtConstructorParam, ModuleConstructorParam, CustomRole, Rule } 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 CustomRoles extends BaseClass {
protected fix: boolean;
public fileName: any;
public folderPath: string;
public customRoleSchema: CustomRole[];
public moduleName: keyof typeof auditConfig.moduleConfig;
public missingFieldsInCustomRoles: CustomRole[];
public customRolePath: string;
public isBranchFixDone: boolean;
constructor({ fix, config, moduleName }: ModuleConstructorParam & Pick<CtConstructorParam, 'ctSchema'>) {
super({ config });
log.debug(`Initializing Custom Roles module`, this.config.auditContext);
this.fix = fix ?? false;
this.customRoleSchema = [];
this.moduleName = this.validateModules(moduleName!, this.config.moduleConfig);
this.fileName = config.moduleConfig[this.moduleName].fileName;
this.folderPath = resolve(
sanitizePath(config.basePath),
sanitizePath(config.moduleConfig[this.moduleName].dirName),
);
this.missingFieldsInCustomRoles = [];
this.customRolePath = '';
this.isBranchFixDone = false;
log.debug(`Starting ${this.moduleName} audit process`, this.config.auditContext);
log.debug(`Data directory: ${this.folderPath}`, this.config.auditContext);
log.debug(`Fix mode: ${this.fix}`, this.config.auditContext);
log.debug(`Branch filter: ${this.config?.branch || 'none'}`, 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 in config: ${Object.keys(moduleConfig).join(', ')}`, this.config.auditContext);
if (Object.keys(moduleConfig).includes(moduleName)) {
log.debug(`Module ${moduleName} found in config, returning: ${moduleName}`, this.config.auditContext);
return moduleName;
}
log.debug(`Module ${moduleName} not found in config, defaulting to: custom-roles`, this.config.auditContext);
return 'custom-roles';
}
/**
* Check whether the given path for the custom role 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 custom role 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.customRolePath = join(this.folderPath, this.fileName);
log.debug(`Custom roles file path: ${this.customRolePath}`, this.config.auditContext);
// Load custom roles schema with loading spinner
await this.withLoadingSpinner('CUSTOM-ROLES: Loading custom roles schema...', async () => {
this.customRoleSchema = existsSync(this.customRolePath)
? values(JSON.parse(readFileSync(this.customRolePath, 'utf8')) as CustomRole[])
: [];
});
log.debug(`Found ${this.customRoleSchema.length} custom roles to audit`, 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 custom roles...');
}
for (let index = 0; index < this.customRoleSchema?.length; index++) {
const customRole = this.customRoleSchema[index];
log.debug(`Processing custom role: ${customRole.name} (${customRole.uid})`, this.config.auditContext);
let branchesToBeRemoved: string[] = [];
if (this.config?.branch) {
log.debug(`Config branch : ${this.config.branch}`, this.config.auditContext);
log.debug(`Checking branch rules for custom role: ${customRole.name}`, this.config.auditContext);
customRole?.rules?.filter((rule) => {
if (rule.module === 'branch') {
log.debug(`Found branch rule with branches: ${rule?.branches?.join(', ') || 'none'}`, this.config.auditContext);
branchesToBeRemoved = rule?.branches?.filter((branch) => branch !== this.config?.branch) || [];
log.debug(`Branches to be removed: ${branchesToBeRemoved.join(', ') || 'none'}`, this.config.auditContext);
}
});
} else {
log.debug(`No branch filter configured, skipping branch validation`, this.config.auditContext);
}
if (branchesToBeRemoved?.length) {
log.debug(`Custom role ${customRole.name} has branches to be removed: ${branchesToBeRemoved.join(', ')}`, this.config.auditContext);
this.isBranchFixDone = true;
const tempCR = cloneDeep(customRole);
if (customRole?.rules && this.config?.branch) {
log.debug(`Applying branch fix to custom role: ${customRole.name}`, this.config.auditContext);
tempCR.rules.forEach((rule: Rule) => {
if (rule.module === 'branch') {
log.debug(`Updating branch rule branches from ${rule.branches?.join(', ')} to ${branchesToBeRemoved.join(', ')}`, this.config.auditContext);
rule.branches = branchesToBeRemoved;
}
});
}
this.missingFieldsInCustomRoles.push(tempCR);
log.debug(`Added custom role ${customRole.name} to missing fields list`, this.config.auditContext);
} else {
log.debug(`Custom role ${customRole.name} has no branch issues`, this.config.auditContext);
}
log.info(
$t(auditMsg.SCAN_CR_SUCCESS_MSG, {
name: customRole.name,
uid: customRole.uid,
}),
this.config.auditContext
);
// Track progress for each custom role processed
if (this.progressManager) {
this.progressManager.tick(true, `custom-role: ${customRole.name}`, null);
}
}
log.debug(`Found ${this.missingFieldsInCustomRoles.length} custom roles with issues`, this.config.auditContext);
log.debug(`Branch fix done: ${this.isBranchFixDone}`, this.config.auditContext);
if (this.fix && (this.missingFieldsInCustomRoles.length || this.isBranchFixDone)) {
log.debug('Fix mode enabled and issues found, applying fixes', this.config.auditContext);
await this.fixCustomRoleSchema();
this.missingFieldsInCustomRoles.forEach((cr) => (cr.fixStatus = 'Fixed'));
log.debug(`Applied fixes to ${this.missingFieldsInCustomRoles.length} custom roles`, this.config.auditContext);
} else {
log.debug('No fixes needed or fix mode disabled', this.config.auditContext);
}
log.debug(`${this.moduleName} audit completed. Found ${this.missingFieldsInCustomRoles.length} custom roles with issues`, this.config.auditContext);
this.completeProgress(true);
return this.missingFieldsInCustomRoles;
} catch (error: any) {
this.completeProgress(false, error?.message || 'Custom roles audit failed');
throw error;
}
}
async fixCustomRoleSchema() {
log.debug('Starting custom role schema fix process', this.config.auditContext);
const newCustomRoleSchema: Record<string, CustomRole> = existsSync(this.customRolePath)
? JSON.parse(readFileSync(this.customRolePath, 'utf8'))
: {};
log.debug(`Loaded ${Object.keys(newCustomRoleSchema).length} custom roles from file`, this.config.auditContext);
if (Object.keys(newCustomRoleSchema).length === 0 || !this.customRoleSchema?.length) {
log.debug('No custom roles to fix or empty schema, skipping fix process', this.config.auditContext);
return;
}
log.debug(`Processing ${this.customRoleSchema.length} custom roles for branch fixes`, this.config.auditContext);
this.customRoleSchema.forEach((customRole) => {
log.debug(`Fixing custom role: ${customRole.name} (${customRole.uid})`, this.config.auditContext);
if (!this.config.branch) {
log.debug(`No branch configured, skipping fix for ${customRole.name}`, this.config.auditContext);
return;
}
log.debug(`Looking for branch rules in custom role: ${customRole.name}`, this.config.auditContext);
const fixedBranches = customRole.rules
?.filter((rule) => rule.module === 'branch' && rule.branches?.length)
?.reduce((acc: string[], rule) => {
log.debug(`Processing branch rule with branches: ${rule.branches?.join(', ')}`, this.config.auditContext);
const relevantBranches =
rule.branches?.filter((branch) => {
if (branch !== this.config.branch) {
log.debug(`Removing branch ${branch} from custom role ${customRole.name}`, this.config.auditContext);
log.debug(
$t(commonMsg.CR_BRANCH_REMOVAL, {
uid: customRole.uid,
name: customRole.name,
branch,
}),
this.config.auditContext
);
return false;
} else {
log.debug(`Keeping branch ${branch} for custom role ${customRole.name}`, this.config.auditContext);
}
return true;
}) || [];
log.debug(`Relevant branches after filtering: ${relevantBranches.join(', ')}`, this.config.auditContext);
return [...acc, ...relevantBranches];
}, []);
log.debug(`Fixed branches for ${customRole.name}: ${fixedBranches?.join(', ') || 'none'}`, this.config.auditContext);
if (fixedBranches?.length) {
log.debug(`Applying branch fix to custom role ${customRole.name}`, this.config.auditContext);
newCustomRoleSchema[customRole.uid].rules
?.filter((rule: Rule) => rule.module === 'branch')
?.forEach((rule) => {
log.debug(`Updating branch rule from ${rule.branches?.join(', ')} to ${fixedBranches.join(', ')}`, this.config.auditContext);
rule.branches = fixedBranches;
});
} else {
log.debug(`No branch fixes needed for custom role ${customRole.name}`, this.config.auditContext);
}
});
log.debug('Writing fixed custom role schema to file', this.config.auditContext);
await this.writeFixContent(newCustomRoleSchema);
log.debug('Custom role schema fix process completed', this.config.auditContext);
}
async writeFixContent(newCustomRoleSchema: Record<string, CustomRole>) {
log.debug('Starting writeFixContent process for custom roles', this.config.auditContext);
const filePath = join(this.folderPath, this.config.moduleConfig[this.moduleName].fileName);
log.debug(`Target file path: ${filePath}`, this.config.auditContext);
log.debug(`Custom roles to write: ${Object.keys(newCustomRoleSchema).length}`, this.config.auditContext);
if (this.fix) {
log.debug('Fix mode enabled, checking write permissions', this.config.auditContext);
const skipConfirm = this.config.flags['copy-dir'] ||
this.config.flags['external-config']?.skipConfirm ||
this.config.flags.yes;
if (skipConfirm) {
log.debug('Skipping confirmation due to copy-dir, external-config, or yes flags', this.config.auditContext);
} else {
log.debug('Asking user for confirmation to write fix content', this.config.auditContext);
this.completeProgress(true);
}
const canWrite = skipConfirm || (await cliux.confirm(commonMsg.FIX_CONFIRMATION));
if (canWrite) {
log.debug(`Writing fixed custom roles to: ${filePath}`, this.config.auditContext);
writeFileSync(filePath, JSON.stringify(newCustomRoleSchema));
log.debug(`Successfully wrote ${Object.keys(newCustomRoleSchema).length} custom roles to file`, this.config.auditContext);
} else {
log.debug('User declined to write fix content', this.config.auditContext);
}
} else {
log.debug('Skipping writeFixContent - not in fix mode', this.config.auditContext);
}
}
}