-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathmerge-handler.ts
More file actions
347 lines (322 loc) · 12.9 KB
/
merge-handler.ts
File metadata and controls
347 lines (322 loc) · 12.9 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
import os from 'os';
import path from 'path';
import forEach from 'lodash/forEach';
import { cliux } from '@contentstack/cli-utilities';
import chalk from 'chalk';
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) {
cliux.print(chalk.red('No items were selected'));
process.exit(1);
}
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';
}
if (this.checkEmptySelection()) {
cliux.print(chalk.red('No items selected'));
} else {
await 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;
}
}
}
checkEmptySelection() {
for (let module in this.branchCompareData) {
if (this.mergeSettings.mergeContent[module]?.modified?.length
|| this.mergeSettings.mergeContent[module]?.added?.length
|| this.mergeSettings.mergeContent[module]?.deleted?.length) {
return false;
}
}
return true;
}
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;
}
}