-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge-handler.ts
More file actions
415 lines (377 loc) · 15.7 KB
/
merge-handler.ts
File metadata and controls
415 lines (377 loc) · 15.7 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
import os from 'os';
import path from 'path';
import forEach from 'lodash/forEach';
import { cliux } from '@contentstack/cli-utilities';
import { getChalk } from '@contentstack/cli-utilities';
import { MergeInputOptions, MergeSummary } from '../interfaces';
import {
selectMergeStrategy,
selectMergeStrategySubOptions,
selectMergeExecution,
prepareMergeRequestPayload,
displayMergeSummary,
askExportMergeSummaryPath,
askMergeComment,
writeFile,
executeMerge,
generateMergeScripts,
selectCustomPreferences,
selectContentMergePreference,
selectContentMergeCustomPreferences,
} from '../utils';
export default class MergeHandler {
private strategy: string;
private strategySubOption?: string;
private branchCompareData: any;
private mergeSettings: any;
private executeOption?: string;
private displayFormat: string;
private exportSummaryPath: string;
private mergeSummary: MergeSummary;
private stackAPIKey: string;
private userInputs: MergeInputOptions;
private host: string;
private enableEntryExp: boolean;
constructor(options: MergeInputOptions) {
this.stackAPIKey = options.stackAPIKey;
this.strategy = options.strategy;
this.strategySubOption = options.strategySubOption;
this.executeOption = options.executeOption;
this.branchCompareData = options.branchCompareData;
this.displayFormat = options.format;
this.exportSummaryPath = options.exportSummaryPath || path.resolve(process.cwd());
this.mergeSummary = options.mergeSummary;
this.userInputs = options;
this.mergeSettings = {
baseBranch: options.baseBranch, // UID of the base branch, where the changes will be merged into
compareBranch: options.compareBranch, // UID of the branch to merge
mergeComment: options.mergeComment,
mergeContent: {},
noRevert: options.noRevert,
};
this.host = options.host;
this.enableEntryExp = options.enableEntryExp;
}
async start() {
if (this.mergeSummary) {
this.loadMergeSettings();
await this.displayMergeSummary();
return await this.executeMerge(this.mergeSummary.requestPayload);
}
await this.collectMergeSettings();
const mergePayload = prepareMergeRequestPayload(this.mergeSettings);
if (this.executeOption === 'execute') {
await this.exportSummary(mergePayload);
await this.executeMerge(mergePayload);
} else if (this.executeOption === 'export') {
await this.exportSummary(mergePayload);
} else if (this.executeOption === 'merge_n_scripts') {
this.enableEntryExp = true;
await this.executeMerge(mergePayload);
} else if (this.executeOption === 'summary_n_scripts') {
this.enableEntryExp = true;
await this.exportSummary(mergePayload);
} else {
await this.exportSummary(mergePayload);
await this.executeMerge(mergePayload);
}
}
async collectMergeSettings() {
if (!this.strategy) {
this.strategy = await selectMergeStrategy();
}
if (
!this.strategySubOption &&
this.strategy !== 'custom_preferences' &&
this.strategy !== 'overwrite_with_compare'
) {
const strategyResponse = await selectMergeStrategySubOptions();
if (strategyResponse === 'previous') {
this.strategy = null;
return await this.collectMergeSettings();
} else if (strategyResponse === 'restart') {
return await this.restartMergeProcess();
} else {
this.strategySubOption = strategyResponse;
}
}
if (this.strategy === 'custom_preferences') {
this.mergeSettings.itemMergeStrategies = [];
for (let module in this.branchCompareData) {
this.mergeSettings.mergeContent[module] = {
added: [],
modified: [],
deleted: [],
};
const selectedItems = await selectCustomPreferences(module, this.branchCompareData[module]);
if (selectedItems?.length) {
forEach(selectedItems, (item) => {
this.mergeSettings.mergeContent[module][item.status].push(item.value);
this.mergeSettings.itemMergeStrategies.push(item.value);
});
this.mergeSettings.strategy = 'ignore';
}
}
} else if (this.strategy === 'merge_prefer_base') {
if (this.strategySubOption === 'new') {
this.mergeSettings.strategy = 'merge_new_only';
} else if (this.strategySubOption === 'modified') {
this.mergeSettings.strategy = 'merge_modified_only_prefer_base';
} else if (this.strategySubOption === 'both') {
this.mergeSettings.strategy = 'merge_prefer_base';
}
} else if (this.strategy === 'merge_prefer_compare') {
if (this.strategySubOption === 'new') {
this.mergeSettings.strategy = 'merge_new_only';
} else if (this.strategySubOption === 'modified') {
this.mergeSettings.strategy = 'merge_modified_only_prefer_compare';
} else if (this.strategySubOption === 'both') {
this.mergeSettings.strategy = 'merge_prefer_compare';
}
} else if (this.strategy === 'overwrite_with_compare') {
this.mergeSettings.strategy = 'overwrite_with_compare';
}
const { allEmpty, moduleStatus } = this.checkEmptySelection();
const strategyName = this.mergeSettings.strategy;
if (allEmpty) {
cliux.print(getChalk().red(`No items selected according to the '${strategyName}' strategy.`));
process.exit(1);
}
for (const [type, { exists, empty }] of Object.entries(moduleStatus)) {
if (exists && empty) {
const readable = type === 'contentType' ? 'Content Types' : 'Global fields';
cliux.print('\n')
cliux.print(getChalk().yellow(`Note: No ${readable} selected according to the '${strategyName}' strategy.`));
}
}
this.displayMergeSummary();
if (!this.executeOption) {
const executionResponse = await selectMergeExecution();
if (executionResponse === 'previous') {
if (this.strategy !== 'custom_preferences' && this.strategy !== 'overwrite_with_compare') {
this.strategySubOption = null;
return await this.collectMergeSettings();
} else {
return await this.restartMergeProcess();
}
} else if (executionResponse === 'restart') {
return await this.restartMergeProcess();
} else {
this.executeOption = executionResponse;
}
}
}
/**
* Checks whether the selection of modules in the compare branch data is empty.
*
* This method evaluates the branch compare data and determines if there are any changes
* (added, modified, or deleted) in the modules based on the merge strategy defined in the
* merge settings. It categorizes the status of each module as either existing and empty or
* not empty.
*
* @returns An object containing:
* - `allEmpty`: A boolean indicating whether all modules are either non-existent or empty.
* - `moduleStatus`: A record mapping module types (`contentType` and `globalField`) to their
* respective statuses, which include:
* - `exists`: A boolean indicating whether the module exists in the branch comparison data.
* - `empty`: A boolean indicating whether the module has no changes (added, modified, or deleted).
*/
checkEmptySelection(): {
allEmpty: boolean;
moduleStatus: Record<string, { exists: boolean; empty: boolean }>;
} {
const strategy = this.mergeSettings.strategy;
const useMergeContent = new Set(['custom_preferences', 'ignore']);
const modifiedOnlyStrategies = new Set(['merge_modified_only_prefer_base', 'merge_modified_only_prefer_compare']);
const addedOnlyStrategies = new Set(['merge_new_only']);
const moduleStatus: Record<string, { exists: boolean; empty: boolean }> = {
contentType: { exists: false, empty: true },
globalField: { exists: false, empty: true },
};
for (const module in this.branchCompareData) {
const content = useMergeContent.has(strategy)
? this.mergeSettings.mergeContent[module]
: this.branchCompareData[module];
if (!content) continue;
const isGlobalField = module === 'global_fields';
const type = isGlobalField ? 'globalField' : 'contentType';
moduleStatus[type].exists = true;
let hasChanges = false;
if (modifiedOnlyStrategies.has(strategy)) {
hasChanges = Array.isArray(content.modified) && content.modified.length > 0;
} else if (addedOnlyStrategies.has(strategy)) {
hasChanges = Array.isArray(content.added) && content.added.length > 0;
} else {
hasChanges =
(Array.isArray(content.modified) && content.modified.length > 0) ||
(Array.isArray(content.added) && content.added.length > 0) ||
(Array.isArray(content.deleted) && content.deleted.length > 0);
}
if (hasChanges) {
moduleStatus[type].empty = false;
}
}
const allEmpty = Object.values(moduleStatus).every(
(status) => !status.exists || status.empty
);
return { allEmpty, moduleStatus };
}
displayMergeSummary() {
if (this.mergeSettings.strategy !== 'ignore') {
for (let module in this.branchCompareData) {
this.mergeSettings.mergeContent[module] = {};
this.filterBranchCompareData(module, this.branchCompareData[module]);
}
}
displayMergeSummary({
format: this.displayFormat,
compareData: this.mergeSettings.mergeContent,
});
}
filterBranchCompareData(module, moduleBranchCompareData) {
const { strategy, mergeContent } = this.mergeSettings;
switch (strategy) {
case 'merge_prefer_base':
mergeContent[module].added = moduleBranchCompareData.added;
mergeContent[module].modified = moduleBranchCompareData.modified;
mergeContent[module].deleted = moduleBranchCompareData.deleted;
break;
case 'merge_prefer_compare':
mergeContent[module].added = moduleBranchCompareData.added;
mergeContent[module].modified = moduleBranchCompareData.modified;
mergeContent[module].deleted = moduleBranchCompareData.deleted;
break;
case 'merge_new_only':
mergeContent[module].added = moduleBranchCompareData.added;
break;
case 'merge_modified_only_prefer_base':
mergeContent[module].modified = moduleBranchCompareData.modified;
break;
case 'merge_modified_only_prefer_compare':
mergeContent[module].modified = moduleBranchCompareData.modified;
break;
case 'merge_modified_only_prefer_compare':
mergeContent[module].modified = moduleBranchCompareData.modified;
break;
case 'overwrite_with_compare':
mergeContent[module].added = moduleBranchCompareData.added;
mergeContent[module].modified = moduleBranchCompareData.modified;
mergeContent[module].deleted = moduleBranchCompareData.deleted;
break;
default:
cliux.error(`Error: Invalid strategy '${strategy}'`);
process.exit(1);
}
}
async exportSummary(mergePayload) {
if (!this.exportSummaryPath) {
this.exportSummaryPath = await askExportMergeSummaryPath();
}
const summary: MergeSummary = {
requestPayload: mergePayload,
};
await writeFile(path.join(this.exportSummaryPath, 'merge-summary.json'), summary);
cliux.success('Exported the summary successfully');
if (this.enableEntryExp) {
this.executeEntryExpFlow(this.stackAPIKey, mergePayload);
}
}
async executeMerge(mergePayload) {
let spinner;
try {
if (!this.mergeSettings.mergeComment) {
this.mergeSettings.mergeComment = await askMergeComment();
mergePayload.merge_comment = this.mergeSettings.mergeComment;
}
spinner = cliux.loaderV2('Merging the changes...');
const mergeResponse = await executeMerge(this.stackAPIKey, mergePayload, this.host);
cliux.loaderV2('', spinner);
cliux.success(`Merged the changes successfully. Merge UID: ${mergeResponse.uid}`);
if (this.enableEntryExp) {
this.executeEntryExpFlow(mergeResponse.uid, mergePayload);
}
} catch (error) {
cliux.loaderV2('', spinner);
cliux.error('Failed to merge the changes', error.message || error);
}
}
async executeEntryExpFlow(mergeJobUID: string, mergePayload) {
const { mergeContent } = this.mergeSettings;
let mergePreference = await selectContentMergePreference();
const updateEntryMergeStrategy = (items, mergeStrategy) => {
items &&
items.forEach((item) => {
item.entry_merge_strategy = mergeStrategy;
});
};
const mergePreferencesMap = {
existing_new: 'merge_existing_new',
new: 'merge_new',
existing: 'merge_existing',
ask_preference: 'custom',
};
const selectedMergePreference = mergePreferencesMap[mergePreference];
if (selectedMergePreference) {
if (selectedMergePreference === 'custom') {
const selectedMergeItems = await selectContentMergeCustomPreferences(mergeContent.content_types);
mergeContent.content_types = {
added: [],
modified: [],
deleted: [],
};
selectedMergeItems?.forEach((item) => {
mergeContent.content_types[item.status].push(item.value);
});
} else {
updateEntryMergeStrategy(mergeContent.content_types.added, selectedMergePreference);
updateEntryMergeStrategy(mergeContent.content_types.modified, selectedMergePreference);
}
} else {
cliux.error(`error: Invalid preference ${mergePreference}`);
process.exit(1);
}
let scriptFolderPath = generateMergeScripts(mergeContent.content_types, mergeJobUID);
if (scriptFolderPath !== undefined) {
cliux.success(`\nSuccess! We have generated entry migration files in the folder ${scriptFolderPath}`);
cliux.print(
'\nWARNING!!! Migration is not intended to be run more than once. Migrated(entries/assets) will be duplicated if run more than once',
{ color: 'yellow' },
);
let migrationCommand: string;
if (os.platform() === 'win32') {
migrationCommand = `csdx cm:stacks:migration --multiple --file-path ./${scriptFolderPath} --config compare-branch:${mergePayload.compare_branch} file-path:./${scriptFolderPath} --branch ${mergePayload.base_branch} --stack-api-key ${this.stackAPIKey}`;
} else {
migrationCommand = `csdx cm:stacks:migration --multiple --file-path ./${scriptFolderPath} --config {compare-branch:${mergePayload.compare_branch},file-path:./${scriptFolderPath}} --branch ${mergePayload.base_branch} --stack-api-key ${this.stackAPIKey}`;
}
cliux.print(
`\nKindly follow the steps in the guide "https://www.contentstack.com/docs/developers/cli/entry-migration" to update the migration scripts, and then run the command:\n\n${migrationCommand}`,
{ color: 'blue' },
);
}
}
async restartMergeProcess() {
if (!this.userInputs.strategy) {
this.strategy = null;
}
if (!this.userInputs.strategySubOption) {
this.strategySubOption = null;
}
if (!this.userInputs.executeOption) {
this.executeOption = null;
}
if (!this.userInputs.executeOption) {
this.executeOption = null;
}
this.mergeSettings.strategy = null;
this.mergeSettings.itemMergeStrategies = [];
await this.start();
}
loadMergeSettings() {
this.mergeSettings.baseBranch = this.mergeSummary.requestPayload.base_branch;
this.mergeSettings.compareBranch = this.mergeSummary.requestPayload.compare_branch;
this.mergeSettings.strategy = this.mergeSummary.requestPayload.default_merge_strategy;
this.mergeSettings.itemMergeStrategies = this.mergeSummary.requestPayload.item_merge_strategies;
this.mergeSettings.noRevert = this.mergeSummary.requestPayload.no_revert;
this.mergeSettings.mergeComment = this.mergeSummary.requestPayload.merge_comment;
}
}