Skip to content

Commit 4683665

Browse files
authored
Merge pull request #2003 from contentstack/feat/DX-3151
refactor: CLI error handling and updates how authentication metadata is captured in import/export commands
2 parents a2eb4ad + 81416dd commit 4683665

File tree

11 files changed

+142
-127
lines changed

11 files changed

+142
-127
lines changed

.talismanrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,8 @@ fileignoreconfig:
158158
checksum: 9ee0a01dbdffa71a972cc8e64c24a219731e27d3c98b4607e2114337a3ebbf94
159159
- filename: packages/contentstack-import/src/utils/extension-helper.ts
160160
checksum: 147ffe7069333c30a20e63f3dcfde3c4f359748f82a8b9a071adc0361a968814
161+
- filename: packages/contentstack-import/src/commands/cm/stacks/import.ts
162+
checksum: 4544b53e063a64b2c49fbee2dd34a62e1653f03b9d6d55f724ef442f236d0741
163+
- filename: packages/contentstack-export/src/commands/cm/stacks/export.ts
164+
checksum: d2222fd14a30cc4c9680e65433d603caf389646a1b4714e0d363bfedff11f423
161165
version: "1.0"

packages/contentstack-auth/src/base-command.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ export abstract class BaseCommand<T extends typeof Command> extends Command {
5353
return {
5454
command: this.context.info.command,
5555
module: '',
56-
userId: configHandler.get('userId'),
56+
userId: configHandler.get('userUid'),
5757
email: configHandler.get('email'),
5858
sessionId: this.context.sessionId,
5959
apiKey: apiKey || '',
60-
orgId: configHandler.get('organization_uid') || '',
60+
orgId: configHandler.get('oauthOrgUid') || '',
6161
};
6262
}
6363
}

packages/contentstack-export/src/commands/cm/stacks/export.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ export default class ExportCommand extends Command {
112112
const { flags } = await this.parse(ExportCommand);
113113
const exportConfig = await setupExportConfig(flags);
114114
// Prepare the context object
115-
const context = this.createExportContext(exportConfig.apiKey);
115+
const context = this.createExportContext(exportConfig.apiKey, exportConfig.authenticationMethod);
116116
exportConfig.context = { ...context };
117117
log.info(`Using Cli Version: ${this.context?.plugin?.version}`, exportConfig.context);
118118

@@ -139,16 +139,16 @@ export default class ExportCommand extends Command {
139139
}
140140

141141
// Create export context object
142-
private createExportContext(apiKey: string): Context {
142+
private createExportContext(apiKey: string, authenticationMethod?: string): Context {
143143
return {
144144
command: this.context.info.command,
145145
module: '',
146-
userId: configHandler.get('userId'),
146+
userId: configHandler.get('userUid'),
147147
email: configHandler.get('email'),
148148
sessionId: this.context.sessionId,
149149
apiKey: apiKey || '',
150-
orgId: configHandler.get('organization_uid') || '',
151-
authMethod: this.context.authMethod || 'Basic Auth',
150+
orgId: configHandler.get('oauthOrgUid') || '',
151+
authenticationMethod: authenticationMethod || 'Basic Auth',
152152
};
153153
}
154154

packages/contentstack-export/src/types/export-config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export default interface ExportConfig extends DefaultConfig {
3131
source_stack?: string;
3232
sourceStackName?: string;
3333
region: Region;
34+
authenticationMethod?: string;
3435
}
3536

3637
type branch = {

packages/contentstack-export/src/types/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ export interface Context {
138138
clientId?: string | undefined;
139139
apiKey: string;
140140
orgId: string;
141-
authMethod?: string;
141+
authenticationMethod?: string;
142142
}
143143

144144
export { default as DefaultConfig } from './default-config';

packages/contentstack-export/src/utils/export-config-handler.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const setupConfig = async (exportCmdFlags: any): Promise<ExportConfig> => {
1212
let config = merge({}, defaultConfig);
1313

1414
// Track authentication method
15-
let authMethod = 'unknown';
15+
let authenticationMethod = 'unknown';
1616

1717
log.debug('Setting up export configuration');
1818

@@ -46,7 +46,7 @@ const setupConfig = async (exportCmdFlags: any): Promise<ExportConfig> => {
4646
const { token, apiKey } = configHandler.get(`tokens.${managementTokenAlias}`) || {};
4747
config.management_token = token;
4848
config.apiKey = apiKey;
49-
authMethod = 'management_token';
49+
authenticationMethod = 'Management Token';
5050
if (!config.management_token) {
5151
log.debug('Management token not found for alias', { alias: managementTokenAlias });
5252
throw new Error(`No management token found on given alias ${managementTokenAlias}`);
@@ -61,7 +61,7 @@ const setupConfig = async (exportCmdFlags: any): Promise<ExportConfig> => {
6161
if (config.username && config.password) {
6262
log.debug('Using basic authentication with username/password');
6363
await login(config);
64-
authMethod = 'Basic Auth';
64+
authenticationMethod = 'Basic Auth';
6565
log.debug('Basic authentication successful');
6666
} else {
6767
log.debug('No authentication method available');
@@ -72,10 +72,10 @@ const setupConfig = async (exportCmdFlags: any): Promise<ExportConfig> => {
7272
const isOAuthUser = configHandler.get('authorisationType') === 'OAUTH' || false;
7373

7474
if (isOAuthUser) {
75-
authMethod = 'Oauth';
75+
authenticationMethod = 'OAuth';
7676
log.debug('User authenticated via OAuth');
7777
} else {
78-
authMethod = 'Basic Auth';
78+
authenticationMethod = 'Basic Auth';
7979
log.debug('User authenticated via auth token');
8080
}
8181

@@ -114,7 +114,7 @@ const setupConfig = async (exportCmdFlags: any): Promise<ExportConfig> => {
114114
}
115115

116116
// Add authentication details to config for context tracking
117-
config.authMethod = authMethod;
117+
config.authenticationMethod = authenticationMethod;
118118
log.debug('Export configuration setup completed', { ...config });
119119

120120
return config;

packages/contentstack-import/src/commands/cm/stacks/import.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ export default class ImportCommand extends Command {
148148
const { flags } = await this.parse(ImportCommand);
149149
let importConfig: ImportConfig = await setupImportConfig(flags);
150150
// Prepare the context object
151-
const context = this.createImportContext(importConfig.apiKey);
151+
const context = this.createImportContext(importConfig.apiKey, importConfig.authenticationMethod);
152152
importConfig.context = {...context};
153153
log.info(`Using Cli Version: ${this.context?.plugin?.version}`, importConfig.context);
154154

@@ -194,16 +194,16 @@ export default class ImportCommand extends Command {
194194
}
195195

196196
// Create export context object
197-
private createImportContext(apiKey: string): Context {
197+
private createImportContext(apiKey: string, authenticationMethod?: string): Context {
198198
return {
199199
command: this.context.info.command,
200200
module: '',
201-
userId: configHandler.get('userId'),
201+
userId: configHandler.get('userUid'),
202202
email: configHandler.get('email'),
203203
sessionId: this.context.sessionId,
204204
apiKey: apiKey || '',
205-
orgId: configHandler.get('organization_uid') || '',
206-
authMethod: this.context.authMethod || 'Basic Auth',
205+
orgId: configHandler.get('oauthOrgUid') || '',
206+
authenticationMethod: authenticationMethod || 'Basic Auth',
207207
};
208208
}
209209
}

packages/contentstack-import/src/types/import-config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export interface ExternalConfig {
1111
}
1212

1313
export default interface ImportConfig extends DefaultConfig, ExternalConfig {
14-
authMethod: string;
14+
authenticationMethod?: string;
1515
skipAssetsPublish?: boolean;
1616
skipEntriesPublish?: boolean;
1717
cliLogsPath: string;

packages/contentstack-import/src/types/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ export interface Context {
114114
clientId?: string | undefined;
115115
apiKey: string;
116116
orgId: string;
117-
authMethod?: string;
117+
authenticationMethod?: string;
118118
}
119119

120120
export { default as DefaultConfig } from './default-config';
@@ -138,5 +138,5 @@ export interface Context {
138138
clientId?: string | undefined;
139139
apiKey: string;
140140
orgId: string;
141-
authMethod?: string;
141+
authenticationMethod?: string;
142142
}

packages/contentstack-import/src/utils/import-config-handler.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { ImportConfig } from '../types';
1111
const setupConfig = async (importCmdFlags: any): Promise<ImportConfig> => {
1212
let config: ImportConfig = merge({}, defaultConfig);
1313
// Track authentication method
14-
let authMethod = 'unknown';
14+
let authenticationMethod = 'unknown';
1515

1616
// setup the config
1717
if (importCmdFlags['config']) {
@@ -50,7 +50,7 @@ const setupConfig = async (importCmdFlags: any): Promise<ImportConfig> => {
5050
const { token, apiKey } = configHandler.get(`tokens.${managementTokenAlias}`) ?? {};
5151
config.management_token = token;
5252
config.apiKey = apiKey;
53-
authMethod = 'management_token';
53+
authenticationMethod = 'Management Token';
5454
if (!config.management_token) {
5555
throw new Error(`No management token found on given alias ${managementTokenAlias}`);
5656
}
@@ -62,7 +62,7 @@ const setupConfig = async (importCmdFlags: any): Promise<ImportConfig> => {
6262
if (config.email && config.password) {
6363
log.debug('Using basic authentication with username/password');
6464
await login(config);
65-
authMethod = 'basic_auth';
65+
authenticationMethod = 'Basic Auth';
6666
log.debug('Basic authentication successful');
6767
} else {
6868
log.debug('No authentication method available');
@@ -73,10 +73,10 @@ const setupConfig = async (importCmdFlags: any): Promise<ImportConfig> => {
7373
const isOAuthUser = configHandler.get('authorisationType') === 'OAUTH' || false;
7474

7575
if (isOAuthUser) {
76-
authMethod = 'Oauth';
76+
authenticationMethod = 'OAuth';
7777
log.debug('User authenticated via OAuth');
7878
} else {
79-
authMethod = 'Basic Auth';
79+
authenticationMethod = 'Basic Auth';
8080
log.debug('User authenticated via auth token');
8181
}
8282
config.apiKey =
@@ -132,7 +132,7 @@ const setupConfig = async (importCmdFlags: any): Promise<ImportConfig> => {
132132
}
133133

134134
// Add authentication details to config for context tracking
135-
config.authMethod = authMethod;
135+
config.authenticationMethod = authenticationMethod;
136136
log.debug('Import configuration setup completed', { ...config });
137137

138138
return config;

0 commit comments

Comments
 (0)