@squiz/dxp-cli-next 5.26.0-develop.2 → 5.26.0-develop.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -36,6 +36,7 @@ const getMigrationCommand = () => {
36
36
  spinner.succeed(`Migration details: ${JSON.stringify(response, null, 2)}`);
37
37
  }
38
38
  catch (error) {
39
+ spinner.fail();
39
40
  (0, utils_1.handleCommandError)(getCommand, error);
40
41
  }
41
42
  }));
@@ -6,9 +6,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const commander_1 = require("commander");
7
7
  const createMigration_1 = __importDefault(require("./create/createMigration"));
8
8
  const getMigration_1 = __importDefault(require("./get/getMigration"));
9
+ const revert_1 = __importDefault(require("./revert/revert"));
10
+ const setMigrationSettings_1 = __importDefault(require("./settings/setMigrationSettings"));
9
11
  const migrationCommand = new commander_1.Command('migration');
10
12
  migrationCommand
11
13
  .description('AI Page Migration Service Commands')
12
14
  .addCommand((0, createMigration_1.default)())
13
- .addCommand((0, getMigration_1.default)());
15
+ .addCommand((0, getMigration_1.default)())
16
+ .addCommand((0, revert_1.default)())
17
+ .addCommand((0, setMigrationSettings_1.default)());
14
18
  exports.default = migrationCommand;
@@ -0,0 +1,3 @@
1
+ import { Command } from 'commander';
2
+ declare const revertMigrationCommand: () => Command;
3
+ export default revertMigrationCommand;
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const chalk_1 = __importDefault(require("chalk"));
16
+ const commander_1 = require("commander");
17
+ const utils_1 = require("../utils");
18
+ const ora_1 = __importDefault(require("ora"));
19
+ const revertMigrationCommand = () => {
20
+ const revertCommand = new commander_1.Command('revert')
21
+ .name('revert')
22
+ .description('Revert the current stage of a migration using the AI Page migration service')
23
+ .addOption(new commander_1.Option('--migration-id <string>', 'The ID of the migration to revert').makeOptionMandatory())
24
+ .addOption(new commander_1.Option('--asset-id <string>', 'The ID of the asset to revert the migration for'))
25
+ .addOption(new commander_1.Option('-t, --tenant <string>', 'Tenant ID to run against. If not provided will use configured tenant from login'))
26
+ .configureOutput({
27
+ outputError(str, write) {
28
+ write(chalk_1.default.red(str));
29
+ },
30
+ })
31
+ .action((options) => __awaiter(void 0, void 0, void 0, function* () {
32
+ const spinner = (0, ora_1.default)('Reverting...').start();
33
+ yield (0, utils_1.throwErrorIfNotLoggedIn)(revertCommand);
34
+ try {
35
+ const response = yield (0, utils_1.revertMigration)(options);
36
+ spinner.succeed(response.message);
37
+ }
38
+ catch (error) {
39
+ spinner.fail();
40
+ (0, utils_1.handleCommandError)(revertCommand, error);
41
+ }
42
+ }));
43
+ if (process.env.ENABLE_OVERRIDE_MIGRATION_URL === 'true') {
44
+ revertCommand.addOption(new commander_1.Option('-ou, --overrideUrl <string>', 'Developer option to override the entire migration url with a custom value'));
45
+ }
46
+ return revertCommand;
47
+ };
48
+ exports.default = revertMigrationCommand;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,150 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ var __importDefault = (this && this.__importDefault) || function (mod) {
35
+ return (mod && mod.__esModule) ? mod : { "default": mod };
36
+ };
37
+ Object.defineProperty(exports, "__esModule", { value: true });
38
+ const revert_1 = __importDefault(require("./revert"));
39
+ const utils = __importStar(require("../utils"));
40
+ jest.mock('../utils');
41
+ jest.mock('../../ApplicationStore');
42
+ const mockUtils = utils;
43
+ describe('revertMigrationCommand', () => {
44
+ beforeEach(() => {
45
+ jest.clearAllMocks();
46
+ jest.resetAllMocks();
47
+ });
48
+ describe('command configuration', () => {
49
+ it('should have correct command name and description', () => {
50
+ const program = (0, revert_1.default)();
51
+ expect(program.name()).toBe('revert');
52
+ expect(program.description()).toBe('Revert the current stage of a migration using the AI Page migration service');
53
+ });
54
+ it('should have required migration-id option', () => {
55
+ const program = (0, revert_1.default)();
56
+ const options = program.options;
57
+ const migrationIdOption = options.find((opt) => opt.long === '--migration-id');
58
+ expect(migrationIdOption).toBeDefined();
59
+ expect(migrationIdOption === null || migrationIdOption === void 0 ? void 0 : migrationIdOption.mandatory).toBe(true);
60
+ });
61
+ it('should have optional asset-id option', () => {
62
+ const program = (0, revert_1.default)();
63
+ const options = program.options;
64
+ const assetIdOption = options.find((opt) => opt.long === '--asset-id');
65
+ expect(assetIdOption).toBeDefined();
66
+ expect(assetIdOption === null || assetIdOption === void 0 ? void 0 : assetIdOption.mandatory).toBe(false);
67
+ });
68
+ it('should have optional tenant option', () => {
69
+ const program = (0, revert_1.default)();
70
+ const options = program.options;
71
+ const tenantOption = options.find((opt) => opt.long === '--tenant');
72
+ expect(tenantOption).toBeDefined();
73
+ expect(tenantOption === null || tenantOption === void 0 ? void 0 : tenantOption.mandatory).toBe(false);
74
+ });
75
+ });
76
+ describe('command execution', () => {
77
+ it('should revert migration successfully with required options', () => __awaiter(void 0, void 0, void 0, function* () {
78
+ const mockResponse = { message: 'Migration reverted successfully' };
79
+ mockUtils.revertMigration.mockResolvedValue(mockResponse);
80
+ mockUtils.throwErrorIfNotLoggedIn.mockResolvedValue();
81
+ const program = (0, revert_1.default)();
82
+ yield program.parseAsync([
83
+ 'node',
84
+ 'dxp-cli',
85
+ 'migration',
86
+ 'revert',
87
+ '--migration-id',
88
+ 'migration-123',
89
+ '--asset-id',
90
+ 'asset-456',
91
+ ]);
92
+ expect(mockUtils.throwErrorIfNotLoggedIn).toHaveBeenCalledWith(program);
93
+ expect(mockUtils.revertMigration).toHaveBeenCalledWith({
94
+ migrationId: 'migration-123',
95
+ assetId: 'asset-456',
96
+ });
97
+ expect(mockUtils.handleCommandError).not.toHaveBeenCalled();
98
+ }));
99
+ it('should revert migration with tenant option', () => __awaiter(void 0, void 0, void 0, function* () {
100
+ const mockResponse = { message: 'Migration reverted successfully' };
101
+ mockUtils.revertMigration.mockResolvedValue(mockResponse);
102
+ mockUtils.throwErrorIfNotLoggedIn.mockResolvedValue();
103
+ const program = (0, revert_1.default)();
104
+ yield program.parseAsync([
105
+ 'node',
106
+ 'dxp-cli',
107
+ 'migration',
108
+ 'revert',
109
+ '--migration-id',
110
+ 'migration-123',
111
+ '--asset-id',
112
+ 'asset-456',
113
+ '--tenant',
114
+ 'test-tenant',
115
+ ]);
116
+ expect(mockUtils.revertMigration).toHaveBeenCalledWith({
117
+ migrationId: 'migration-123',
118
+ assetId: 'asset-456',
119
+ tenant: 'test-tenant',
120
+ });
121
+ }));
122
+ it('should handle revert migration error', () => __awaiter(void 0, void 0, void 0, function* () {
123
+ const error = new Error('Revert failed');
124
+ mockUtils.revertMigration.mockRejectedValue(error);
125
+ mockUtils.throwErrorIfNotLoggedIn.mockResolvedValue();
126
+ const program = (0, revert_1.default)();
127
+ yield program.parseAsync([
128
+ 'node',
129
+ 'dxp-cli',
130
+ 'migration',
131
+ 'revert',
132
+ '--migration-id',
133
+ 'migration-123',
134
+ '--asset-id',
135
+ 'asset-456',
136
+ ]);
137
+ expect(mockUtils.handleCommandError).toHaveBeenCalledWith(program, error);
138
+ }));
139
+ it('should include override URL option when environment variable is set', () => __awaiter(void 0, void 0, void 0, function* () {
140
+ const originalEnv = process.env.ENABLE_OVERRIDE_MIGRATION_URL;
141
+ process.env.ENABLE_OVERRIDE_MIGRATION_URL = 'true';
142
+ const program = (0, revert_1.default)();
143
+ const options = program.options;
144
+ const overrideUrlOption = options.find((opt) => opt.long === '--overrideUrl');
145
+ expect(overrideUrlOption).toBeDefined();
146
+ expect(overrideUrlOption === null || overrideUrlOption === void 0 ? void 0 : overrideUrlOption.mandatory).toBe(false);
147
+ process.env.ENABLE_OVERRIDE_MIGRATION_URL = originalEnv;
148
+ }));
149
+ });
150
+ });
@@ -0,0 +1,3 @@
1
+ import { Command } from 'commander';
2
+ declare const setMigrationSettingsCommand: () => Command;
3
+ export default setMigrationSettingsCommand;
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const chalk_1 = __importDefault(require("chalk"));
16
+ const commander_1 = require("commander");
17
+ const utils_1 = require("../utils");
18
+ const ora_1 = __importDefault(require("ora"));
19
+ const setMigrationSettingsCommand = () => {
20
+ const settingsCommand = new commander_1.Command('settings')
21
+ .name('settings')
22
+ .description('Set settings for the migration service')
23
+ .addOption(new commander_1.Option('--matrix-url <string>', 'The URL of the Matrix instance').makeOptionMandatory())
24
+ .addOption(new commander_1.Option('--matrix-identifier <string>', 'The component service identifier used by the asset and components being migrated').makeOptionMandatory())
25
+ .addOption(new commander_1.Option('--matrix-key <string>', 'The Asset Management API key for the Migrator API to interact with Matrix API').makeOptionMandatory())
26
+ .addOption(new commander_1.Option('-t, --tenant <string>', 'Tenant ID to run against. If not provided will use configured tenant from login'))
27
+ .configureOutput({
28
+ outputError(str, write) {
29
+ write(chalk_1.default.red(str));
30
+ },
31
+ })
32
+ .action((options) => __awaiter(void 0, void 0, void 0, function* () {
33
+ const spinner = (0, ora_1.default)('Setting migration configuration...').start();
34
+ yield (0, utils_1.throwErrorIfNotLoggedIn)(settingsCommand);
35
+ try {
36
+ const response = yield (0, utils_1.setMigrationSetting)(options);
37
+ spinner.succeed(`Migration settings updated: ${JSON.stringify(response, null, 2)}`);
38
+ }
39
+ catch (error) {
40
+ spinner.fail();
41
+ (0, utils_1.handleCommandError)(settingsCommand, error);
42
+ }
43
+ }));
44
+ if (process.env.ENABLE_OVERRIDE_MIGRATION_URL === 'true') {
45
+ settingsCommand.addOption(new commander_1.Option('-ou, --overrideUrl <string>', 'Developer option to override the entire migration url with a custom value'));
46
+ }
47
+ return settingsCommand;
48
+ };
49
+ exports.default = setMigrationSettingsCommand;
@@ -0,0 +1,275 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ var __importDefault = (this && this.__importDefault) || function (mod) {
35
+ return (mod && mod.__esModule) ? mod : { "default": mod };
36
+ };
37
+ Object.defineProperty(exports, "__esModule", { value: true });
38
+ const nock_1 = __importDefault(require("nock"));
39
+ const setMigrationSettings_1 = __importDefault(require("./setMigrationSettings"));
40
+ const utils = __importStar(require("../utils"));
41
+ const ApplicationStore = __importStar(require("../../ApplicationStore"));
42
+ jest.mock('../utils');
43
+ jest.mock('../../ApplicationStore');
44
+ const mockUtils = utils;
45
+ const mockApplicationStore = ApplicationStore;
46
+ describe('setMigrationSettingsCommand', () => {
47
+ let logSpy;
48
+ let mockSetMigrationSettingResponse;
49
+ beforeEach(() => {
50
+ nock_1.default.cleanAll();
51
+ jest.clearAllMocks();
52
+ jest.resetAllMocks();
53
+ logSpy = jest.spyOn(console, 'log').mockImplementation(() => { });
54
+ mockApplicationStore.getApplicationFile.mockResolvedValue('session-cookie');
55
+ mockUtils.throwErrorIfNotLoggedIn.mockResolvedValue(undefined);
56
+ mockSetMigrationSettingResponse = {
57
+ matrixUrl: 'https://matrix.example.com',
58
+ matrixIdentifier: '@user:matrix.example.com',
59
+ matrixKey: '***********123',
60
+ };
61
+ mockUtils.setMigrationSetting.mockResolvedValue(mockSetMigrationSettingResponse);
62
+ });
63
+ afterEach(() => {
64
+ logSpy.mockRestore();
65
+ });
66
+ describe('successful migration setting update', () => {
67
+ it('should set migration settings successfully with required options', () => __awaiter(void 0, void 0, void 0, function* () {
68
+ const program = (0, setMigrationSettings_1.default)();
69
+ yield program.parseAsync([
70
+ 'node',
71
+ 'dxp-cli',
72
+ 'migration',
73
+ 'settings',
74
+ '--matrix-url',
75
+ 'https://matrix.example.com',
76
+ '--matrix-identifier',
77
+ '@user:matrix.example.com',
78
+ '--matrix-key',
79
+ 'secret-key-123',
80
+ ]);
81
+ expect(mockUtils.throwErrorIfNotLoggedIn).toHaveBeenCalledWith(program);
82
+ expect(mockUtils.setMigrationSetting).toHaveBeenCalledWith({
83
+ matrixUrl: 'https://matrix.example.com',
84
+ matrixIdentifier: '@user:matrix.example.com',
85
+ matrixKey: 'secret-key-123',
86
+ });
87
+ expect(mockUtils.setMigrationSetting).toHaveBeenCalledTimes(1);
88
+ expect(mockUtils.handleCommandError).not.toHaveBeenCalled();
89
+ }));
90
+ it('should set migration settings with tenant option', () => __awaiter(void 0, void 0, void 0, function* () {
91
+ const program = (0, setMigrationSettings_1.default)();
92
+ yield program.parseAsync([
93
+ 'node',
94
+ 'dxp-cli',
95
+ 'migration',
96
+ 'settings',
97
+ '--matrix-url',
98
+ 'https://matrix.example.com',
99
+ '--matrix-identifier',
100
+ '@user:matrix.example.com',
101
+ '--matrix-key',
102
+ 'secret-key-123',
103
+ '--tenant',
104
+ 'test-tenant',
105
+ ]);
106
+ expect(mockUtils.setMigrationSetting).toHaveBeenCalledWith({
107
+ matrixUrl: 'https://matrix.example.com',
108
+ matrixIdentifier: '@user:matrix.example.com',
109
+ matrixKey: 'secret-key-123',
110
+ tenant: 'test-tenant',
111
+ });
112
+ }));
113
+ it('should set migration settings with override URL when environment variable is set', () => __awaiter(void 0, void 0, void 0, function* () {
114
+ const originalEnv = process.env.ENABLE_OVERRIDE_MIGRATION_URL;
115
+ process.env.ENABLE_OVERRIDE_MIGRATION_URL = 'true';
116
+ const program = (0, setMigrationSettings_1.default)();
117
+ yield program.parseAsync([
118
+ 'node',
119
+ 'dxp-cli',
120
+ 'migration',
121
+ 'settings',
122
+ '--matrix-url',
123
+ 'https://matrix.example.com',
124
+ '--matrix-identifier',
125
+ '@user:matrix.example.com',
126
+ '--matrix-key',
127
+ 'secret-key-123',
128
+ '--overrideUrl',
129
+ 'https://custom.migration.url',
130
+ ]);
131
+ expect(mockUtils.setMigrationSetting).toHaveBeenCalledWith({
132
+ matrixUrl: 'https://matrix.example.com',
133
+ matrixIdentifier: '@user:matrix.example.com',
134
+ matrixKey: 'secret-key-123',
135
+ overrideUrl: 'https://custom.migration.url',
136
+ });
137
+ process.env.ENABLE_OVERRIDE_MIGRATION_URL = originalEnv;
138
+ }));
139
+ });
140
+ describe('error scenarios', () => {
141
+ it('should handle setMigrationSetting API error', () => __awaiter(void 0, void 0, void 0, function* () {
142
+ const apiError = new Error('Settings update failed');
143
+ mockUtils.setMigrationSetting.mockRejectedValue(apiError);
144
+ mockUtils.handleCommandError.mockImplementation(() => { });
145
+ const program = (0, setMigrationSettings_1.default)();
146
+ yield program.parseAsync([
147
+ 'node',
148
+ 'dxp-cli',
149
+ 'migration',
150
+ 'settings',
151
+ '--matrix-url',
152
+ 'https://matrix.example.com',
153
+ '--matrix-identifier',
154
+ '@user:matrix.example.com',
155
+ '--matrix-key',
156
+ 'secret-key-123',
157
+ ]);
158
+ expect(mockUtils.handleCommandError).toHaveBeenCalledWith(program, apiError);
159
+ }));
160
+ it('should handle network error', () => __awaiter(void 0, void 0, void 0, function* () {
161
+ const networkError = new Error('Network connection failed');
162
+ mockUtils.setMigrationSetting.mockRejectedValue(networkError);
163
+ mockUtils.handleCommandError.mockImplementation(() => { });
164
+ const program = (0, setMigrationSettings_1.default)();
165
+ yield program.parseAsync([
166
+ 'node',
167
+ 'dxp-cli',
168
+ 'migration',
169
+ 'settings',
170
+ '--matrix-url',
171
+ 'https://matrix.example.com',
172
+ '--matrix-identifier',
173
+ '@user:matrix.example.com',
174
+ '--matrix-key',
175
+ 'secret-key-123',
176
+ ]);
177
+ expect(mockUtils.handleCommandError).toHaveBeenCalledWith(program, networkError);
178
+ }));
179
+ });
180
+ describe('required options validation', () => {
181
+ it('should require matrixUrl option', () => {
182
+ const program = (0, setMigrationSettings_1.default)().exitOverride();
183
+ expect(() => {
184
+ program.parse([
185
+ 'node',
186
+ 'dxp-cli',
187
+ 'migration',
188
+ 'settings',
189
+ '--matrix-identifier',
190
+ '@user:matrix.example.com',
191
+ '--matrix-key',
192
+ 'secret-key-123',
193
+ ]);
194
+ }).toThrow();
195
+ });
196
+ it('should require matrixIdentifier option', () => {
197
+ const program = (0, setMigrationSettings_1.default)().exitOverride();
198
+ expect(() => {
199
+ program.parse([
200
+ 'node',
201
+ 'dxp-cli',
202
+ 'migration',
203
+ 'settings',
204
+ '--matrix-url',
205
+ 'https://matrix.example.com',
206
+ '--matrix-key',
207
+ 'secret-key-123',
208
+ ]);
209
+ }).toThrow();
210
+ });
211
+ it('should require matrixKey option', () => {
212
+ const program = (0, setMigrationSettings_1.default)().exitOverride();
213
+ expect(() => {
214
+ program.parse([
215
+ 'node',
216
+ 'dxp-cli',
217
+ 'migration',
218
+ 'settings',
219
+ '--matrix-url',
220
+ 'https://matrix.example.com',
221
+ '--matrix-identifier',
222
+ '@user:matrix.example.com',
223
+ ]);
224
+ }).toThrow();
225
+ });
226
+ });
227
+ describe('command configuration', () => {
228
+ it('should have correct command name and description', () => {
229
+ const program = (0, setMigrationSettings_1.default)();
230
+ expect(program.name()).toBe('settings');
231
+ expect(program.description()).toBe('Set settings for the migration service');
232
+ });
233
+ it('should parse options correctly', () => {
234
+ const program = (0, setMigrationSettings_1.default)();
235
+ program.parse([
236
+ 'node',
237
+ 'dxp-cli',
238
+ 'migration',
239
+ 'settings',
240
+ '--matrix-url',
241
+ 'https://matrix.example.com',
242
+ '--matrix-identifier',
243
+ '@user:matrix.example.com',
244
+ '--matrix-key',
245
+ 'secret-key-123',
246
+ '--tenant',
247
+ 'test-tenant',
248
+ ]);
249
+ const opts = program.opts();
250
+ expect(opts.matrixUrl).toBe('https://matrix.example.com');
251
+ expect(opts.matrixIdentifier).toBe('@user:matrix.example.com');
252
+ expect(opts.matrixKey).toBe('secret-key-123');
253
+ expect(opts.tenant).toBe('test-tenant');
254
+ });
255
+ it('should parse options with kebab-case to camelCase conversion', () => {
256
+ const program = (0, setMigrationSettings_1.default)();
257
+ program.parse([
258
+ 'node',
259
+ 'dxp-cli',
260
+ 'migration',
261
+ 'settings',
262
+ '--matrix-url',
263
+ 'https://matrix.example.com',
264
+ '--matrix-identifier',
265
+ '@user:matrix.example.com',
266
+ '--matrix-key',
267
+ 'secret-key-123',
268
+ ]);
269
+ const opts = program.opts();
270
+ expect(opts.matrixUrl).toBe('https://matrix.example.com');
271
+ expect(opts.matrixIdentifier).toBe('@user:matrix.example.com');
272
+ expect(opts.matrixKey).toBe('secret-key-123');
273
+ });
274
+ });
275
+ });
@@ -35,3 +35,20 @@ export interface GetMigrationAPIResponse {
35
35
  previewPageAssetId?: string;
36
36
  componentsTarDownloadUrl?: string;
37
37
  }
38
+ export interface RevertMigrationOptions extends GetMigrationOptions {
39
+ }
40
+ export interface RevertMigrationAPIResponse {
41
+ message: string;
42
+ }
43
+ export interface SetMigrationSettingOptions {
44
+ matrixUrl: string;
45
+ matrixIdentifier: string;
46
+ matrixKey: string;
47
+ tenant?: string;
48
+ overrideUrl?: string;
49
+ }
50
+ export interface SetMigrationSettingAPIResponse {
51
+ matrixUrl: string;
52
+ matrixIdentifier: string;
53
+ matrixKey: string;
54
+ }
@@ -1,12 +1,15 @@
1
1
  import { Command } from 'commander';
2
- import { CreateMigrationOptions, CreateMigrationAPIResponse, GetMigrationOptions, GetMigrationAPIResponse } from './types';
2
+ import { CreateMigrationOptions, CreateMigrationAPIResponse, GetMigrationOptions, GetMigrationAPIResponse, RevertMigrationOptions, RevertMigrationAPIResponse, SetMigrationSettingOptions, SetMigrationSettingAPIResponse } from './types';
3
3
  export declare function handleCommandError(command: Command, error: Error): void;
4
4
  export declare function throwErrorIfNotLoggedIn(command: Command): Promise<void>;
5
5
  export declare function buildMigrationUrl(tenantID?: string, overrideUrl?: string): Promise<string>;
6
6
  export declare function validateAxiosStatus(status: number): boolean;
7
+ export declare function redactKey(key: string, visibleChars?: number): string;
7
8
  export declare function validateExportFolder(exportPath: string): void;
8
9
  export declare function createTarFile(exportPath: string): Promise<string>;
9
10
  export declare function getMigrationHeaders(tenantID?: string): Promise<Record<string, string>>;
10
11
  export declare function uploadFileToS3(uploadUrl: string, filePath: string, tenantID?: string): Promise<string>;
11
12
  export declare function createMigration(options: CreateMigrationOptions): Promise<CreateMigrationAPIResponse>;
12
13
  export declare function getMigration(options: GetMigrationOptions): Promise<GetMigrationAPIResponse>;
14
+ export declare function revertMigration(options: RevertMigrationOptions): Promise<RevertMigrationAPIResponse>;
15
+ export declare function setMigrationSetting(options: SetMigrationSettingOptions): Promise<SetMigrationSettingAPIResponse>;
@@ -12,7 +12,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
12
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.getMigration = exports.createMigration = exports.uploadFileToS3 = exports.getMigrationHeaders = exports.createTarFile = exports.validateExportFolder = exports.validateAxiosStatus = exports.buildMigrationUrl = exports.throwErrorIfNotLoggedIn = exports.handleCommandError = void 0;
15
+ exports.setMigrationSetting = exports.revertMigration = exports.getMigration = exports.createMigration = exports.uploadFileToS3 = exports.getMigrationHeaders = exports.createTarFile = exports.validateExportFolder = exports.redactKey = exports.validateAxiosStatus = exports.buildMigrationUrl = exports.throwErrorIfNotLoggedIn = exports.handleCommandError = void 0;
16
16
  const fs_1 = __importDefault(require("fs"));
17
17
  const path_1 = __importDefault(require("path"));
18
18
  const chalk_1 = __importDefault(require("chalk"));
@@ -70,6 +70,16 @@ function validateAxiosStatus(status) {
70
70
  return status < 400;
71
71
  }
72
72
  exports.validateAxiosStatus = validateAxiosStatus;
73
+ function redactKey(key, visibleChars = 3) {
74
+ if (!key)
75
+ return '';
76
+ const len = key.length;
77
+ if (len <= visibleChars)
78
+ return '*'.repeat(len);
79
+ const masked = '*'.repeat(len - visibleChars);
80
+ return masked + key.slice(-visibleChars);
81
+ }
82
+ exports.redactKey = redactKey;
73
83
  function validateExportFolder(exportPath) {
74
84
  // Check if the export folder exists
75
85
  if (!fs_1.default.existsSync(exportPath)) {
@@ -223,3 +233,66 @@ function getMigration(options) {
223
233
  });
224
234
  }
225
235
  exports.getMigration = getMigration;
236
+ function revertMigration(options) {
237
+ return __awaiter(this, void 0, void 0, function* () {
238
+ const apiService = new ApiService_1.ApiService({
239
+ validateStatus: validateAxiosStatus,
240
+ });
241
+ const migrationUrl = yield buildMigrationUrl(options.tenant, options.overrideUrl);
242
+ try {
243
+ const response = yield apiService.client.post(`${migrationUrl}/migrations/${options.migrationId}/assets/${options.assetId}/rollback`, {}, {
244
+ headers: Object.assign({ 'Content-Type': 'application/json' }, (yield getMigrationHeaders(options.tenant))),
245
+ });
246
+ if (response.status !== 200) {
247
+ throw new Error(`Failed to revert migration: ${response.status}`);
248
+ }
249
+ return response.data;
250
+ }
251
+ catch (error) {
252
+ if (error instanceof Error) {
253
+ throw error;
254
+ }
255
+ throw new Error(`Failed to revert migration: ${error}`);
256
+ }
257
+ });
258
+ }
259
+ exports.revertMigration = revertMigration;
260
+ function setMigrationSetting(options) {
261
+ return __awaiter(this, void 0, void 0, function* () {
262
+ const apiService = new ApiService_1.ApiService({
263
+ validateStatus: validateAxiosStatus,
264
+ });
265
+ const migrationUrl = yield buildMigrationUrl(options.tenant, options.overrideUrl);
266
+ try {
267
+ const payload = {
268
+ matrixUrl: options.matrixUrl,
269
+ matrixIdentifier: options.matrixIdentifier,
270
+ matrixKey: options.matrixKey,
271
+ };
272
+ const response = yield apiService.client.post(`${migrationUrl}/settings`, payload, {
273
+ headers: Object.assign({ 'Content-Type': 'application/json' }, (yield getMigrationHeaders(options.tenant))),
274
+ });
275
+ if (response.status !== 200 && response.status !== 201) {
276
+ throw new Error(`Migration settings update failed with status: ${response.status}`);
277
+ }
278
+ if (!(response === null || response === void 0 ? void 0 : response.data)) {
279
+ throw new Error('No data returned from migration service');
280
+ }
281
+ const responseData = {
282
+ matrixUrl: response.data.matrixUrl,
283
+ matrixIdentifier: response.data.matrixIdentifier,
284
+ matrixKey: response.data.matrixKey
285
+ ? redactKey(response.data.matrixKey, 3)
286
+ : '',
287
+ };
288
+ return responseData;
289
+ }
290
+ catch (error) {
291
+ if (error instanceof Error) {
292
+ throw error;
293
+ }
294
+ throw new Error(`Failed to set migration settings: ${error}`);
295
+ }
296
+ });
297
+ }
298
+ exports.setMigrationSetting = setMigrationSetting;
@@ -145,6 +145,13 @@ describe('Migration Utils', () => {
145
145
  expect((0, utils_1.validateAxiosStatus)(500)).toBe(false);
146
146
  });
147
147
  });
148
+ describe('redactKey', () => {
149
+ it('redacts key correctly', () => {
150
+ expect((0, utils_1.redactKey)('1234567890', 3)).toBe('*******890');
151
+ expect((0, utils_1.redactKey)('1234567890', 1)).toBe('*********0');
152
+ expect((0, utils_1.redactKey)('1234567890', 10)).toBe('**********');
153
+ });
154
+ });
148
155
  describe('validateExportFolder', () => {
149
156
  beforeEach(() => {
150
157
  mockPath.join.mockImplementation((...paths) => paths.join('/'));
@@ -540,4 +547,71 @@ describe('Migration Utils', () => {
540
547
  expect(mockApiServiceInstance.client.get).toHaveBeenCalledWith('https://example.com/__dxp/service/aiapps/migration/migrations/migration-123/assets/asset-456', expect.any(Object));
541
548
  }));
542
549
  });
550
+ describe('revertMigration', () => {
551
+ const mockOptions = {
552
+ migrationId: 'migration-123',
553
+ assetId: 'asset-456',
554
+ tenant: 'test-tenant',
555
+ };
556
+ const mockApiServiceInstance = {
557
+ client: {
558
+ post: jest.fn(),
559
+ },
560
+ };
561
+ beforeEach(() => {
562
+ mockApiService.mockImplementation(() => mockApiServiceInstance);
563
+ mockFetchApplicationConfig.mockResolvedValue({
564
+ baseUrl: 'https://test.example.com',
565
+ tenant: 'test-tenant',
566
+ region: 'us-east-1',
567
+ });
568
+ });
569
+ it('should revert migration successfully', () => __awaiter(void 0, void 0, void 0, function* () {
570
+ const mockResponse = {
571
+ status: 200,
572
+ data: { message: 'Migration reverted successfully' },
573
+ };
574
+ mockApiServiceInstance.client.post.mockResolvedValue(mockResponse);
575
+ const result = yield (0, utils_1.revertMigration)(mockOptions);
576
+ expect(result).toEqual({ message: 'Migration reverted successfully' });
577
+ expect(mockApiServiceInstance.client.post).toHaveBeenCalledWith('https://test.example.com/__dxp/service/aiapps/migration/migrations/migration-123/assets/asset-456/rollback', {}, {
578
+ headers: {
579
+ 'Content-Type': 'application/json',
580
+ 'x-dxp-tenant': 'test-tenant',
581
+ },
582
+ });
583
+ }));
584
+ it('should handle non-success status codes', () => __awaiter(void 0, void 0, void 0, function* () {
585
+ const mockResponse = {
586
+ status: 400,
587
+ data: {},
588
+ };
589
+ mockApiServiceInstance.client.post.mockResolvedValue(mockResponse);
590
+ yield expect((0, utils_1.revertMigration)(mockOptions)).rejects.toThrow('Failed to revert migration: 400');
591
+ }));
592
+ it('should handle API service errors', () => __awaiter(void 0, void 0, void 0, function* () {
593
+ const error = new Error('Network error');
594
+ mockApiServiceInstance.client.post.mockRejectedValue(error);
595
+ yield expect((0, utils_1.revertMigration)(mockOptions)).rejects.toThrow('Network error');
596
+ }));
597
+ it('should handle unknown errors', () => __awaiter(void 0, void 0, void 0, function* () {
598
+ mockApiServiceInstance.client.post.mockRejectedValue('Unknown error');
599
+ yield expect((0, utils_1.revertMigration)(mockOptions)).rejects.toThrow('Failed to revert migration: Unknown error');
600
+ }));
601
+ it('should use override URL when provided', () => __awaiter(void 0, void 0, void 0, function* () {
602
+ const mockResponse = {
603
+ status: 200,
604
+ data: { message: 'Migration reverted successfully' },
605
+ };
606
+ mockApiServiceInstance.client.post.mockResolvedValue(mockResponse);
607
+ const optionsWithOverride = Object.assign(Object.assign({}, mockOptions), { overrideUrl: 'https://custom.migration.url' });
608
+ yield (0, utils_1.revertMigration)(optionsWithOverride);
609
+ expect(mockApiServiceInstance.client.post).toHaveBeenCalledWith('https://custom.migration.url/migrations/migration-123/assets/asset-456/rollback', {}, {
610
+ headers: {
611
+ 'Content-Type': 'application/json',
612
+ 'x-dxp-tenant': 'test-tenant',
613
+ },
614
+ });
615
+ }));
616
+ });
543
617
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@squiz/dxp-cli-next",
3
- "version": "5.26.0-develop.2",
3
+ "version": "5.26.0-develop.4",
4
4
  "repository": {
5
5
  "url": "https://gitlab.squiz.net/dxp/dxp-cli-next"
6
6
  },