Skip to content

Commit

Permalink
Merge branch 'development' into fix/CS-43247
Browse files Browse the repository at this point in the history
  • Loading branch information
aman19K authored Jan 9, 2024
2 parents 8c5a08f + c227748 commit 6a5c3d8
Show file tree
Hide file tree
Showing 21 changed files with 151 additions and 50 deletions.
17 changes: 9 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion packages/contentstack-audit/src/audit-base-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export abstract class AuditBaseCommand extends BaseCommand<typeof AuditBaseComma
* @param {string} command - The `command` parameter is a string that represents the current command
* being executed.
*/
async start(command: CommandNames): Promise<void> {
async start(command: CommandNames): Promise<boolean> {
this.currentCommand = command;
await this.promptQueue();
await this.createBackUp();
Expand Down Expand Up @@ -69,6 +69,8 @@ export abstract class AuditBaseCommand extends BaseCommand<typeof AuditBaseComma
rmSync(this.sharedConfig.basePath, { recursive: true });
}
}

return !isEmpty(missingCtRefs) || !isEmpty(missingGfRefs) || !isEmpty(missingEntryRefs);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion packages/contentstack-audit/src/base-command.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import merge from 'lodash/merge';
import isEmpty from 'lodash/isEmpty';
import { existsSync, readFileSync } from 'fs';
import { Command } from '@contentstack/cli-command';
import { Flags, FlagInput, Interfaces, cliux, ux, PrintOptions } from '@contentstack/cli-utilities';
Expand Down Expand Up @@ -57,7 +58,7 @@ export abstract class BaseCommand<T extends typeof Command> extends Command {

this.sharedConfig = Object.assign(this.sharedConfig, { flags: this.flags });

if (this.flags['external-config']?.config) {
if (!isEmpty(this.flags['external-config']?.config)) {
this.sharedConfig = Object.assign(this.sharedConfig, this.flags['external-config']?.config);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ export default class AuditFix extends AuditBaseCommand {
* The `run` function is an asynchronous function that performs an audit on different modules
* (content-types, global-fields, entries) and generates a report.
*/
async run(): Promise<void | ConfigType> {
async run(): Promise<void | { config: ConfigType; hasFix: boolean }> {
try {
await this.start('cm:stacks:audit:fix');
const hasFix = await this.start('cm:stacks:audit:fix');

if (this.flags['external-config']?.returnConfig) {
return this.sharedConfig;
if (this.flags['external-config']?.returnResponse) {
return { config: this.sharedConfig, hasFix };
}
} catch (error) {
this.log(error instanceof Error ? error.message : error, 'error');
Expand Down
5 changes: 4 additions & 1 deletion packages/contentstack-audit/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export default {};
import Audit from "./commands/cm/stacks/audit";
import AuditFix from "./commands/cm/stacks/audit/fix";

export { Audit, AuditFix };
2 changes: 1 addition & 1 deletion packages/contentstack-audit/src/modules/content-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export default class ContentType {
let canWrite = true;

if (!this.inMemoryFix && this.fix) {
if (!this.config.flags['copy-dir']) {
if (!this.config.flags['copy-dir'] && !this.config.flags['external-config']?.skipConfirm) {
canWrite = this.config.flags.yes ?? (await ux.confirm(commonMsg.FIX_CONFIRMATION));
}

Expand Down
2 changes: 1 addition & 1 deletion packages/contentstack-audit/src/modules/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export default class Entries {
let canWrite = true;

if (this.fix) {
if (!this.config.flags['copy-dir']) {
if (!this.config.flags['copy-dir'] && !this.config.flags['external-config']?.skipConfirm) {
canWrite = this.config.flags.yes || (await ux.confirm(commonMsg.FIX_CONFIRMATION));
}

Expand Down
4 changes: 2 additions & 2 deletions packages/contentstack-clone/package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "@contentstack/cli-cm-clone",
"description": "Contentstack stack clone plugin",
"version": "1.8.0",
"version": "1.9.0",
"author": "Contentstack",
"bugs": "https://github.com/rohitmishra209/cli-cm-clone/issues",
"dependencies": {
"@contentstack/cli-cm-export": "~1.10.2",
"@contentstack/cli-cm-import": "~1.12.2",
"@contentstack/cli-cm-import": "~1.13.0",
"@contentstack/cli-command": "~1.2.16",
"@contentstack/cli-utilities": "~1.5.10",
"@colors/colors": "^1.5.0",
Expand Down
4 changes: 4 additions & 0 deletions packages/contentstack-clone/src/commands/cm/stacks/clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class StackCloneCommand extends Command {
const listOfTokens = configHandler.get('tokens');

config.forceStopMarketplaceAppsPrompt = yes;
config.skipAudit = cloneCommandFlags['skip-audit'];

if (cloneType) {
config.cloneType = cloneType;
Expand Down Expand Up @@ -245,6 +246,9 @@ b) Structure with content (all modules including entries & assets)
required: false,
description: '[Optional] Override marketplace prompts',
}),
'skip-audit': flags.boolean({
description: 'Skips the audit fix.',
}),
};

StackCloneCommand.usage =
Expand Down
2 changes: 2 additions & 0 deletions packages/contentstack-clone/src/lib/util/clone-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,8 @@ class CloneHandler {
cmd.push('--import-webhook-status', config.importWebhookStatus);
}

if (config.skipAudit) cmd.push('--skip-audit');

if (config.forceStopMarketplaceAppsPrompt) cmd.push('-y');

fs.writeFileSync(path.join(__dirname, 'dummyConfig.json'), JSON.stringify(config));
Expand Down
8 changes: 5 additions & 3 deletions packages/contentstack-import/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ $ npm install -g @contentstack/cli-cm-import
$ csdx COMMAND
running command...
$ csdx (--version)
@contentstack/cli-cm-import/1.12.2 darwin-arm64 node-v20.8.0
@contentstack/cli-cm-import/1.12.2 darwin-arm64 node-v20.10.0
$ csdx --help [COMMAND]
USAGE
$ csdx COMMAND
Expand Down Expand Up @@ -83,6 +83,7 @@ FLAGS
<options: disable|current>
--replace-existing Replaces the existing module in the target stack.
--skip-app-recreation [optional] Skip private apps recreation if already exist
--skip-audit Skips the audit fix.
--skip-existing Skips the module exists warning messages.
DESCRIPTION
Expand All @@ -106,7 +107,7 @@ EXAMPLES
$ csdx cm:stacks:import --alias <management_token_alias> --config <path/of/config/file>
$ csdx cm:stacks:import --branch <branch name> --yes
$ csdx cm:stacks:import --branch <branch name> --yes --skip-audit
```

## `csdx cm:stacks:import [-c <value>] [-k <value>] [-d <value>] [-a <value>] [--module <value>] [--backup-dir <value>] [--branch <value>] [--import-webhook-status disable|current]`
Expand All @@ -131,6 +132,7 @@ FLAGS
<options: disable|current>
--replace-existing Replaces the existing module in the target stack.
--skip-app-recreation [optional] Skip private apps recreation if already exist
--skip-audit Skips the audit fix.
--skip-existing Skips the module exists warning messages.
DESCRIPTION
Expand All @@ -154,7 +156,7 @@ EXAMPLES
$ csdx cm:stacks:import --alias <management_token_alias> --config <path/of/config/file>
$ csdx cm:stacks:import --branch <branch name> --yes
$ csdx cm:stacks:import --branch <branch name> --yes --skip-audit
```

_See code: [src/commands/cm/stacks/import.ts](https://github.com/contentstack/cli/blob/main/packages/contentstack-import/src/commands/cm/stacks/import.ts)_
Expand Down
3 changes: 2 additions & 1 deletion packages/contentstack-import/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"name": "@contentstack/cli-cm-import",
"description": "Contentstack CLI plugin to import content into stack",
"version": "1.12.2",
"version": "1.13.0",
"author": "Contentstack",
"bugs": "https://github.com/contentstack/cli/issues",
"dependencies": {
"@contentstack/cli-audit": "^1.3.2",
"@contentstack/cli-command": "~1.2.16",
"@contentstack/cli-utilities": "~1.5.10",
"@contentstack/management": "~1.13.0",
Expand Down
14 changes: 10 additions & 4 deletions packages/contentstack-import/src/commands/cm/stacks/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
ContentstackClient,
} from '@contentstack/cli-utilities';

import { trace } from '../../../utils/log';
import { ImportConfig } from '../../../types';
import { ModuleImporter } from '../../../import';
import { setupImportConfig, formatError, log } from '../../../utils';
Expand All @@ -25,7 +24,7 @@ export default class ImportCommand extends Command {
`csdx cm:stacks:import --alias <management_token_alias>`,
`csdx cm:stacks:import --alias <management_token_alias> --data-dir <path/of/export/destination/dir>`,
`csdx cm:stacks:import --alias <management_token_alias> --config <path/of/config/file>`,
`csdx cm:stacks:import --branch <branch name> --yes`,
`csdx cm:stacks:import --branch <branch name> --yes --skip-audit`,
];

static flags: FlagInput = {
Expand Down Expand Up @@ -106,6 +105,9 @@ export default class ImportCommand extends Command {
default: false,
description: 'Skips the module exists warning messages.',
}),
'skip-audit': flags.boolean({
description: 'Skips the audit fix.',
}),
};

static aliases: string[] = ['cm:import'];
Expand All @@ -127,8 +129,12 @@ export default class ImportCommand extends Command {

const managementAPIClient: ContentstackClient = await managementSDKClient(importConfig);
const moduleImporter = new ModuleImporter(managementAPIClient, importConfig);
await moduleImporter.start();
log(importConfig, `The content has been imported to the stack ${importConfig.apiKey} successfully!`, 'success');
const result = await moduleImporter.start();

if (!result?.noSuccessMsg) {
log(importConfig, `The content has been imported to the stack ${importConfig.apiKey} successfully!`, 'success');
}

log(
importConfig,
`The log has been stored at '${path.join(importConfig.backupDir, 'logs', 'import')}'`,
Expand Down
95 changes: 84 additions & 11 deletions packages/contentstack-import/src/import/module-importer.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { addLocale, ContentstackClient } from '@contentstack/cli-utilities';
import { resolve } from 'path';
import { AuditFix } from '@contentstack/cli-audit';
import messages, { $t } from '@contentstack/cli-audit/lib/messages';
import { addLocale, cliux, ContentstackClient, Logger } from '@contentstack/cli-utilities';

import startModuleImport from './modules';
import startJSModuleImport from './modules-js';
import { ImportConfig, Modules } from '../types';
import { backupHandler, log, validateBranch, masterLocalDetails, sanitizeStack, initLogger } from '../utils';
import { backupHandler, log, validateBranch, masterLocalDetails, sanitizeStack, initLogger, trace } from '../utils';

class ModuleImporter {
private managementAPIClient: ContentstackClient;
Expand All @@ -24,18 +27,10 @@ class ModuleImporter {
await validateBranch(this.stackAPIClient, this.importConfig, this.importConfig.branchName);
}

// Temporarily adding this api call to verify management token has read and write permissions
// TODO: CS-40354 - CLI | import rewrite | Migrate HTTP call to SDK call once fix is ready from SDK side

if (this.importConfig.management_token) {
await addLocale(this.importConfig.apiKey, this.importConfig.management_token, this.importConfig.host);
}

if (!this.importConfig.master_locale) {
let masterLocalResponse = await masterLocalDetails(this.stackAPIClient);
this.importConfig['master_locale'] = { code: masterLocalResponse.code };
this.importConfig.masterLocale = { code: masterLocalResponse.code };
}
const backupDir = await backupHandler(this.importConfig);
if (backupDir) {
this.importConfig.backupDir = backupDir;
Expand All @@ -44,7 +39,24 @@ class ModuleImporter {
}

// NOTE init log
initLogger(this.importConfig);
const logger = initLogger(this.importConfig);

// NOTE audit and fix the import content.
if (
!this.importConfig.skipAudit &&
(!this.importConfig.moduleName ||
['content-types', 'global-fields', 'entries'].includes(this.importConfig.moduleName))
) {
if (!(await this.auditImportData(logger))) {
return { noSuccessMsg: true };
}
}

if (!this.importConfig.master_locale) {
let masterLocalResponse = await masterLocalDetails(this.stackAPIClient);
this.importConfig['master_locale'] = { code: masterLocalResponse.code };
this.importConfig.masterLocale = { code: masterLocalResponse.code };
}

await sanitizeStack(this.stackAPIClient);

Expand Down Expand Up @@ -90,6 +102,67 @@ class ModuleImporter {
await this.importByModuleByName(moduleName);
}
}

/**
* The `auditImportData` function performs an audit process on imported data, using a specified
* configuration, and returns a boolean indicating whether a fix is needed.
* @returns The function `auditImportData()` returns a boolean value. It returns `true` if there is a
* fix available and the user confirms to proceed with the fix, otherwise it returns `false`.
*/
async auditImportData(logger: Logger) {
const basePath = resolve(this.importConfig.backupDir, 'logs', 'audit');
const auditConfig = {
noLog: false, // Skip logs printing on terminal
skipConfirm: true, // Skip confirmation if any
returnResponse: true, // On process completion should return config used in the command
noTerminalOutput: false, // Skip final audit table output on terminal
config: { basePath }, // To overwrite any build-in config. This config is equal to --config flag.
};
try {
const args = [
'--data-dir',
this.importConfig.backupDir,
'--external-config',
JSON.stringify(auditConfig),
'--report-path',
basePath,
];

if (this.importConfig.moduleName) {
args.push('--modules', this.importConfig.moduleName);
}

log(this.importConfig, 'Starting audit process', 'info');
const result = await AuditFix.run(args);
log(this.importConfig, 'Audit process completed', 'info');

if (result) {
const { hasFix, config } = result;

if (hasFix) {
logger.log($t(messages.FINAL_REPORT_PATH, { path: config.reportPath }), 'warn');

if (
this.importConfig.forceStopMarketplaceAppsPrompt ||
(await cliux.inquire({
type: 'confirm',
name: 'confirmation',
message: 'Can you check the fix on the given path and confirm if you would like to proceed with the fix?',
}))
) {
return true;
}

return false;
}
}

return true;
} catch (error) {
trace(error);
log(this.importConfig, `Audit failed with following error. ${error}`, 'error');
}
}
}

export default ModuleImporter;
Loading

0 comments on commit 6a5c3d8

Please sign in to comment.