-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom-roles.ts
More file actions
329 lines (289 loc) · 15.1 KB
/
custom-roles.ts
File metadata and controls
329 lines (289 loc) · 15.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import isEmpty from 'lodash/isEmpty';
import values from 'lodash/values';
import { join } from 'node:path';
import { forEach, map } from 'lodash';
import { log, handleAndLogError } from '@contentstack/cli-utilities';
import { fsUtil, fileHelper } from '../../utils';
import BaseClass, { ApiOptions } from './base-class';
import { ModuleClassParams, CustomRoleConfig } from '../../types';
export default class ImportCustomRoles extends BaseClass {
private customRolesMapperPath: string;
private customRolesFolderPath: string;
private customRolesUidMapperPath: string;
private envUidMapperFolderPath: string;
private entriesUidMapperFolderPath: string;
private assetsUidMapperFolderPath: string;
private createdCustomRolesPath: string;
private customRolesFailsPath: string;
private customRolesConfig: CustomRoleConfig;
private customRoles: Record<string, unknown>;
private customRolesLocales: Record<string, unknown>;
private customRolesUidMapper: Record<string, unknown>;
private createdCustomRoles: Record<string, unknown>[];
private failedCustomRoles: Record<string, unknown>[];
private environmentsUidMap: Record<string, unknown>;
private entriesUidMap: Record<string, unknown>;
private localesUidMap: Record<string, unknown>;
private assetsUidMap: Record<string, unknown>;
private assetsFolderUidMap: Record<string, unknown>;
public targetLocalesMap: Record<string, unknown>;
public sourceLocalesMap: Record<string, unknown>;
constructor({ importConfig, stackAPIClient }: ModuleClassParams) {
super({ importConfig, stackAPIClient });
this.importConfig.context.module = 'custom-roles';
this.customRolesConfig = importConfig.modules.customRoles;
this.customRolesMapperPath = join(this.importConfig.backupDir, 'mapper', 'custom-roles');
this.customRolesFolderPath = join(this.importConfig.backupDir, this.customRolesConfig.dirName);
this.customRolesUidMapperPath = join(this.customRolesMapperPath, 'uid-mapping.json');
this.envUidMapperFolderPath = join(this.importConfig.backupDir, 'mapper', 'environments');
this.entriesUidMapperFolderPath = join(this.importConfig.backupDir, 'mapper', 'entries');
this.assetsUidMapperFolderPath = join(this.importConfig.backupDir, 'mapper', 'assets');
this.createdCustomRolesPath = join(this.customRolesMapperPath, 'success.json');
this.customRolesFailsPath = join(this.customRolesMapperPath, 'fails.json');
this.customRoles = {};
this.failedCustomRoles = [];
this.createdCustomRoles = [];
this.customRolesUidMapper = {};
this.customRolesLocales = {};
this.environmentsUidMap = {};
this.entriesUidMap = {};
this.localesUidMap = {};
this.assetsUidMap = {};
this.assetsFolderUidMap = {};
}
/**
* @method start
* @returns {Promise<void>} Promise<void>
*/
async start(): Promise<void> {
log.debug('Checking for custom roles folder existence...', this.importConfig.context);
//Step1 check folder exists or not
if (fileHelper.fileExistsSync(this.customRolesFolderPath)) {
log.debug(`Found custom roles folder: ${this.customRolesFolderPath}`, this.importConfig.context);
this.customRoles = fsUtil.readFile(join(this.customRolesFolderPath, this.customRolesConfig.fileName),true) as Record<string, unknown>;
this.customRolesLocales = fsUtil.readFile(join(this.customRolesFolderPath, this.customRolesConfig.customRolesLocalesFileName),true) as Record<string, unknown>;
} else {
log.info(`No custom roles found in: ${this.customRolesFolderPath}`, this.importConfig.context);
return;
}
//create webhooks in mapper directory
log.debug('Creating custom roles mapper directory...', this.importConfig.context);
await fsUtil.makeDirectory(this.customRolesMapperPath);
log.debug('Loading existing custom roles UID data...', this.importConfig.context);
this.customRolesUidMapper = fileHelper.fileExistsSync(this.customRolesUidMapperPath)
? (fsUtil.readFile(join(this.customRolesUidMapperPath), true) as Record<string, unknown>) || {}
: {};
log.debug('Loading environments UID data...', this.importConfig.context);
this.environmentsUidMap = fileHelper.fileExistsSync(this.envUidMapperFolderPath)
? (fsUtil.readFile(join(this.envUidMapperFolderPath, 'uid-mapping.json'), true) as Record<string, unknown>) || {}
: {};
log.debug('Loading entries UID data...', this.importConfig.context);
this.entriesUidMap = fileHelper.fileExistsSync(this.entriesUidMapperFolderPath)
? (fsUtil.readFile(join(this.entriesUidMapperFolderPath, 'uid-mapping.json'), true) as Record<string, unknown>) || {}
: {};
log.debug('Loading assets UID data...', this.importConfig.context);
this.assetsUidMap = fileHelper.fileExistsSync(this.assetsUidMapperFolderPath)
? (fsUtil.readFile(join(this.assetsUidMapperFolderPath, 'uid-mapping.json'), true) as Record<string, unknown>) || {}
: {};
log.debug('Loading asset folders UID data...', this.importConfig.context);
this.assetsFolderUidMap = fileHelper.fileExistsSync(this.assetsUidMapperFolderPath)
? (fsUtil.readFile(join(this.assetsUidMapperFolderPath, 'folder-mapping.json'), true) as Record<string, unknown>) || {}
: {};
if (this.customRolesUidMapper && Object.keys(this.customRolesUidMapper || {}).length > 0) {
const customRolesUidCount = Object.keys(this.customRolesUidMapper || {}).length;
log.debug(`Loaded existing custom roles UID data: ${customRolesUidCount} items`, this.importConfig.context);
} else {
log.debug('No existing custom roles UID data found.', this.importConfig.context);
}
if (this.environmentsUidMap && Object.keys(this.environmentsUidMap || {})?.length > 0) {
const envUidCount = Object.keys(this.environmentsUidMap || {}).length;
log.debug(`Loaded environments UID data: ${envUidCount} items`, this.importConfig.context);
} else {
log.debug('No environments UID data found.', this.importConfig.context);
}
if (this.entriesUidMap && Object.keys(this.entriesUidMap || {}).length > 0) {
const entriesUidCount = Object.keys(this.entriesUidMap || {}).length;
log.debug(`Loaded entries UID data: ${entriesUidCount} items`, this.importConfig.context);
} else {
log.debug('No entries UID data found.', this.importConfig.context);
}
//source and target stack locale map
log.debug('Getting locales UID mapping...', this.importConfig.context);
await this.getLocalesUidMap();
log.debug('Starting custom roles import...', this.importConfig.context);
await this.importCustomRoles();
log.debug('Processing custom roles import results...', this.importConfig.context);
if (this.createdCustomRoles?.length) {
fsUtil.writeFile(this.createdCustomRolesPath, this.createdCustomRoles);
log.debug(`Written ${this.createdCustomRoles.length} successful custom roles to file`, this.importConfig.context);
}
if (this.failedCustomRoles?.length) {
fsUtil.writeFile(this.customRolesFailsPath, this.failedCustomRoles);
log.debug(`Written ${this.failedCustomRoles.length} failed custom roles to file`, this.importConfig.context);
}
log.success('Custom roles have been imported successfully!', this.importConfig.context);
}
async getLocalesUidMap(): Promise<void> {
log.debug('Fetching target stack locales...', this.importConfig.context);
const { items } = await this.stack
.locale()
.query()
.find()
.then((data: any) => {
log.debug(`Found ${data.items?.length || 0} locales in target stack`, this.importConfig.context);
return data;
})
.catch((error) => {
log.debug('Error fetching target stack locales!', this.importConfig.context);
handleAndLogError(error, { ...this.importConfig.context});
});
this.targetLocalesMap = {};
this.sourceLocalesMap = {};
log.debug('Building target locales mapping...', this.importConfig.context);
forEach(items, (locale: any) => {
this.targetLocalesMap[locale.code] = locale.uid;
});
log.debug('Building source locales mapping...', this.importConfig.context);
for (const key in this.customRolesLocales) {
const sourceLocales = this.customRolesLocales[key] as Record<string, any>;
this.sourceLocalesMap[sourceLocales.code] = key;
}
log.debug('Creating locale UID mapping...', this.importConfig.context);
for (const key in this.sourceLocalesMap) {
const sourceLocaleKey = this.sourceLocalesMap[key] as string;
this.localesUidMap[sourceLocaleKey] = this.targetLocalesMap[key];
}
const localesMappingCount = Object.keys(this.localesUidMap || {}).length;
log.debug(`Created ${localesMappingCount} locale UID mappings`, this.importConfig.context);
}
async importCustomRoles() {
log.debug('Starting custom roles import process...', this.importConfig.context);
if (this.customRoles === undefined || isEmpty(this.customRoles)) {
log.info('No custom roles found', this.importConfig.context);
return;
}
const apiContent = values(this.customRoles);
log.debug(`Importing ${apiContent.length} custom roles`, this.importConfig.context);
const onSuccess = ({ response, apiData: { uid, name } = { uid: null, name: '' } }: any) => {
this.createdCustomRoles.push(response);
this.customRolesUidMapper[uid] = response.uid;
log.success(`custom-role '${name}' imported successfully`, this.importConfig.context);
log.debug(`Custom role import completed: ${name} (${uid})`, this.importConfig.context);
fsUtil.writeFile(this.customRolesUidMapperPath, this.customRolesUidMapper);
};
const onReject = ({ error, apiData }: any) => {
const err = error?.message ? JSON.parse(error.message) : error;
const { name } = apiData;
log.debug(`Custom role '${name}' import failed`, this.importConfig.context);
if (err?.errors?.name) {
log.info(`Custom role '${name}' already exists.`, this.importConfig.context);
} else {
this.failedCustomRoles.push(apiData);
handleAndLogError(error, { ...this.importConfig.context, name }, `custom-role '${name}' failed to be import`);
}
};
log.debug(`Using concurrency limit: ${this.importConfig.fetchConcurrency || 1}`, this.importConfig.context);
await this.makeConcurrentCall(
{
apiContent,
processName: 'create custom role',
apiParams: {
serializeData: this.serializeWebhooks.bind(this),
reject: onReject.bind(this),
resolve: onSuccess.bind(this),
entity: 'create-custom-role',
includeParamOnCompletion: true,
},
concurrencyLimit: this.importConfig.fetchConcurrency || 1,
},
undefined,
false,
);
log.debug('Custom roles import process completed.', this.importConfig.context);
}
/**
* @method serializeWebhooks
* @param {ApiOptions} apiOptions ApiOptions
* @returns {ApiOptions} ApiOptions
*/
serializeWebhooks(apiOptions: ApiOptions): ApiOptions {
const { apiData: customRole } = apiOptions;
log.debug(`Serializing custom role: ${customRole.name} (${customRole.uid})`, this.importConfig.context);
if (this.customRolesUidMapper.hasOwnProperty(customRole.uid)) {
log.info(
`custom-role '${customRole.name}' already exists. Skipping it to avoid duplicates!`,
this.importConfig.context,
);
log.debug(`Skipping custom role serialization for: ${customRole.uid}`, this.importConfig.context);
apiOptions.entity = undefined;
} else {
let branchRuleExists: boolean = false;
log.debug(`Processing ${customRole.rules?.length || 0} rules for custom role: ${customRole.name}`, this.importConfig.context);
forEach(customRole.rules, (rule: Record<string, any>) => {
rule = this.getTransformUidsFactory(rule);
// rules.branch is required to create custom roles.
if (rule.module === 'branch') {
branchRuleExists = true;
log.debug(`Found branch rule in custom role: ${customRole.name}`, this.importConfig.context);
}
});
if (!branchRuleExists) {
log.debug(`Adding default branch rule to custom role: ${customRole.name}`, this.importConfig.context);
customRole.rules.push({
module: 'branch',
branches: ['main'],
acl: { read: true },
});
}
log.debug(`Custom role serialization completed: ${customRole.name}`, this.importConfig.context);
apiOptions.apiData = customRole;
}
return apiOptions;
}
getTransformUidsFactory = (rule: Record<string, any>) => {
log.debug(`Transforming UIDs for rule module: ${rule.module}`, this.importConfig.context);
if (rule.module === 'environment') {
if(!isEmpty(this.environmentsUidMap)){
const originalEnvs = rule.environments?.length || 0;
rule.environments = map(rule.environments, (env: any) => this.environmentsUidMap[env]);
log.debug(`Transformed ${originalEnvs} environment UIDs for rule`, this.importConfig.context);
} else {
log.debug('No environment UID mappings available for transformation.', this.importConfig.context);
}
} else if (rule.module === 'locale') {
if(!isEmpty(this.localesUidMap)){
const originalLocales = rule.locales?.length || 0;
rule.locales = map(rule.locales, (locale: any) => this.localesUidMap[locale]);
log.debug(`Transformed ${originalLocales} locale UIDs for rule`, this.importConfig.context);
} else {
log.debug('No locale UID mappings available for transformation.', this.importConfig.context);
}
} else if (rule.module === 'entry') {
if(!isEmpty(this.entriesUidMap)){
const originalEntries = rule.entries?.length || 0;
rule.entries = map(rule.entries, (entry: any) => this.entriesUidMap[entry]);
log.debug(`Transformed ${originalEntries} entry UIDs for rule`, this.importConfig.context);
} else {
log.debug('No entry UID mappings available for transformation.', this.importConfig.context);
}
} else if (rule.module === 'asset') {
if (!isEmpty(this.assetsUidMap)) {
const originalAssets = rule.assets?.length || 0;
rule.assets = map(rule.assets, (uid: string) => (this.assetsUidMap[uid] as string) ?? uid);
log.debug(`Transformed ${originalAssets} asset UIDs for rule`, this.importConfig.context);
} else {
log.debug('No asset UID mappings available for transformation.', this.importConfig.context);
}
} else if (rule.module === 'folder') {
if (!isEmpty(this.assetsFolderUidMap)) {
const originalFolders = rule.folders?.length || 0;
rule.folders = map(rule.folders, (uid: string) => (this.assetsFolderUidMap[uid] as string) ?? uid);
rule.heirarchy = map(rule.heirarchy, (uid: string) => (this.assetsFolderUidMap[uid] as string) ?? uid);
log.debug(`Transformed ${originalFolders} folder UIDs for rule`, this.importConfig.context);
} else {
log.debug('No asset folder UID mappings available for transformation.', this.importConfig.context);
}
}
return rule;
};
}