Skip to content

refactor: CLI error handling and updates how authentication metadata is captured in import/export commands #2003

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 18, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/contentstack-auth/src/base-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ export abstract class BaseCommand<T extends typeof Command> extends Command {
return {
command: this.context.info.command,
module: '',
userId: configHandler.get('userId'),
userId: configHandler.get('userUid'),
email: configHandler.get('email'),
sessionId: this.context.sessionId,
apiKey: apiKey || '',
orgId: configHandler.get('organization_uid') || '',
orgId: configHandler.get('oauthOrgUid') || '',
};
}
}
10 changes: 5 additions & 5 deletions packages/contentstack-export/src/commands/cm/stacks/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export default class ExportCommand extends Command {
const { flags } = await this.parse(ExportCommand);
const exportConfig = await setupExportConfig(flags);
// Prepare the context object
const context = this.createExportContext(exportConfig.apiKey);
const context = this.createExportContext(exportConfig.apiKey, exportConfig.authMethod);
exportConfig.context = { ...context };
log.info(`Using Cli Version: ${this.context?.plugin?.version}`, exportConfig.context);

Expand All @@ -139,16 +139,16 @@ export default class ExportCommand extends Command {
}

// Create export context object
private createExportContext(apiKey: string): Context {
private createExportContext(apiKey: string, authMethod: string): Context {
return {
command: this.context.info.command,
module: '',
userId: configHandler.get('userId'),
userId: configHandler.get('userUid'),
email: configHandler.get('email'),
sessionId: this.context.sessionId,
apiKey: apiKey || '',
orgId: configHandler.get('organization_uid') || '',
authMethod: this.context.authMethod || 'Basic Auth',
orgId: configHandler.get('oauthOrgUid') || '',
authMethod: authMethod || 'Basic Auth',
};
}

Expand Down
1 change: 1 addition & 0 deletions packages/contentstack-export/src/types/export-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export default interface ExportConfig extends DefaultConfig {
source_stack?: string;
sourceStackName?: string;
region: Region;
authMethod?: string;
}

type branch = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const setupConfig = async (exportCmdFlags: any): Promise<ExportConfig> => {
const { token, apiKey } = configHandler.get(`tokens.${managementTokenAlias}`) || {};
config.management_token = token;
config.apiKey = apiKey;
authMethod = 'management_token';
authMethod = 'Management Token';
if (!config.management_token) {
log.debug('Management token not found for alias', { alias: managementTokenAlias });
throw new Error(`No management token found on given alias ${managementTokenAlias}`);
Expand All @@ -72,7 +72,7 @@ const setupConfig = async (exportCmdFlags: any): Promise<ExportConfig> => {
const isOAuthUser = configHandler.get('authorisationType') === 'OAUTH' || false;

if (isOAuthUser) {
authMethod = 'Oauth';
authMethod = 'OAuth';
log.debug('User authenticated via OAuth');
} else {
authMethod = 'Basic Auth';
Expand Down
10 changes: 5 additions & 5 deletions packages/contentstack-import/src/commands/cm/stacks/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export default class ImportCommand extends Command {
const { flags } = await this.parse(ImportCommand);
let importConfig: ImportConfig = await setupImportConfig(flags);
// Prepare the context object
const context = this.createImportContext(importConfig.apiKey);
const context = this.createImportContext(importConfig.apiKey, importConfig.authMethod);
importConfig.context = {...context};
log.info(`Using Cli Version: ${this.context?.plugin?.version}`, importConfig.context);

Expand Down Expand Up @@ -194,16 +194,16 @@ export default class ImportCommand extends Command {
}

// Create export context object
private createImportContext(apiKey: string): Context {
private createImportContext(apiKey: string, authMethod: string): Context {
return {
command: this.context.info.command,
module: '',
userId: configHandler.get('userId'),
userId: configHandler.get('userUid'),
email: configHandler.get('email'),
sessionId: this.context.sessionId,
apiKey: apiKey || '',
orgId: configHandler.get('organization_uid') || '',
authMethod: this.context.authMethod || 'Basic Auth',
orgId: configHandler.get('oauthOrgUid') || '',
authMethod: authMethod || 'Basic Auth',
};
}
}
2 changes: 1 addition & 1 deletion packages/contentstack-import/src/types/import-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface ExternalConfig {
}

export default interface ImportConfig extends DefaultConfig, ExternalConfig {
authMethod: string;
authMethod?: string;
skipAssetsPublish?: boolean;
skipEntriesPublish?: boolean;
cliLogsPath: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const setupConfig = async (importCmdFlags: any): Promise<ImportConfig> => {
const { token, apiKey } = configHandler.get(`tokens.${managementTokenAlias}`) ?? {};
config.management_token = token;
config.apiKey = apiKey;
authMethod = 'management_token';
authMethod = 'Management Token';
if (!config.management_token) {
throw new Error(`No management token found on given alias ${managementTokenAlias}`);
}
Expand All @@ -62,7 +62,7 @@ const setupConfig = async (importCmdFlags: any): Promise<ImportConfig> => {
if (config.email && config.password) {
log.debug('Using basic authentication with username/password');
await login(config);
authMethod = 'basic_auth';
authMethod = 'Basic Auth';
log.debug('Basic authentication successful');
} else {
log.debug('No authentication method available');
Expand All @@ -73,7 +73,7 @@ const setupConfig = async (importCmdFlags: any): Promise<ImportConfig> => {
const isOAuthUser = configHandler.get('authorisationType') === 'OAUTH' || false;

if (isOAuthUser) {
authMethod = 'Oauth';
authMethod = 'OAuth';
log.debug('User authenticated via OAuth');
} else {
authMethod = 'Basic Auth';
Expand Down
Loading
Loading