Skip to content

Commit c4008dc

Browse files
authored
Merge pull request #2085 from contentstack/feat/DX-3421
DX - 3421 - Added Branch Alias in Import Plugin.
2 parents bac8bfc + 4994004 commit c4008dc

File tree

15 files changed

+714
-683
lines changed

15 files changed

+714
-683
lines changed

.talismanrc

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
fileignoreconfig:
22
- filename: package-lock.json
3-
checksum: 60cf06e944b51a620589f6785874c0c771e6e12a01cb561ec60fc92bfddf70ee
3+
checksum: 353b5b39c9f8242902e6198b3f6c5981c813af16ce16ebbe845ebf369c1c1ff1
44
- filename: pnpm-lock.yaml
5-
checksum: bee7058cd14780ac1a012ca95dccb89e62918f0e1bc9ea0d13d4c58d284e21e0
5+
checksum: 3e8de376815b1514490bcd0348e91178fa467467a3a33460300abd50edc633f9
66
- filename: packages/contentstack-import-setup/test/unit/backup-handler.test.ts
77
checksum: 0582d62b88834554cf12951c8690a73ef3ddbb78b82d2804d994cf4148e1ef93
88
- filename: packages/contentstack-import-setup/test/config.json
@@ -57,8 +57,10 @@ fileignoreconfig:
5757
checksum: f93aa9b0c964608b60c88d4c72ff33840b58ec900297c4bae1f4ea365aa51048
5858
- filename: packages/contentstack-auth/test/utils/mfa-handler.test.ts
5959
checksum: b067f93cf0185d794e8419cc41e8fac96ed790dea8fc48dc083ee242ccacbd4d
60-
- filename: packages/contentstack-utilities/src/logger/logger.ts
61-
checksum: 76429bc87e279624b386f00e7eb3f4ec25621ace7056289f812b9a076d6e184e
62-
- filename: packages/contentstack-import/src/import/modules/entries.ts
63-
checksum: 2fd4e8ecf75e077632a6408d09997f0921d2a3508f9f2cb8f47fe79a28592300
64-
version: "1.0"
60+
- filename: packages/contentstack-import/src/import/module-importer.ts
61+
checksum: 93fac2407e20070aa393f783e5a21093e99424e5fd2873aabc2099ac3ea02b27
62+
- filename: packages/contentstack-import/src/utils/import-config-handler.ts
63+
checksum: bb8093633dc7de888541990623c3e02a482b7e6f5db0ba396bedc20c4c74b782
64+
- filename: packages/contentstack-import/src/utils/setup-branch.ts
65+
checksum: a4a968a20d5ab7cbc08c266819907541bbf793cc098521a5e810ada3cbacbee6
66+
version: "1.0"

package-lock.json

Lines changed: 202 additions & 202 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/contentstack-clone/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"dependencies": {
88
"@colors/colors": "^1.6.0",
99
"@contentstack/cli-cm-export": "~1.20.0",
10-
"@contentstack/cli-cm-import": "~1.26.3",
10+
"@contentstack/cli-cm-import": "~1.27.0",
1111
"@contentstack/cli-command": "~1.6.1",
1212
"@contentstack/cli-utilities": "~1.14.0",
1313
"@oclif/core": "^4.3.0",

packages/contentstack-import/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@contentstack/cli-cm-import",
33
"description": "Contentstack CLI plugin to import content into stack",
4-
"version": "1.26.3",
4+
"version": "1.27.0",
55
"author": "Contentstack",
66
"bugs": "https://github.com/contentstack/cli/issues",
77
"dependencies": {

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

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ export default class ImportCommand extends Command {
8787
description:
8888
"The name of the branch where you want to import your content. If you don't mention the branch name, then by default the content will be imported to the main branch.",
8989
parse: printFlagDeprecation(['-B'], ['--branch']),
90+
exclusive: ['branch-alias'],
91+
}),
92+
'branch-alias': flags.string({
93+
description:
94+
"The alias of the branch where you want to import your content. If you don't mention the branch alias, then by default the content will be imported to the main branch.",
95+
exclusive: ['branch'],
9096
}),
9197
'import-webhook-status': flags.string({
9298
description:
@@ -161,26 +167,6 @@ export default class ImportCommand extends Command {
161167

162168
const managementAPIClient: ContentstackClient = await managementSDKClient(importConfig);
163169

164-
if (!flags.branch) {
165-
try {
166-
// Use stack configuration to check for branch availability
167-
// false positive - no hardcoded secret here
168-
// @ts-ignore-next-line secret-detection
169-
const keyProp = 'api_key';
170-
const branches = await managementAPIClient
171-
.stack({ [keyProp]: importConfig.apiKey })
172-
.branch()
173-
.query()
174-
.find()
175-
.then(({ items }: any) => items);
176-
if (branches.length) {
177-
flags.branch = 'main';
178-
}
179-
} catch (error) {
180-
// Branch not enabled, just the let flow continue
181-
}
182-
}
183-
184170
const moduleImporter = new ModuleImporter(managementAPIClient, importConfig);
185171
const result = await moduleImporter.start();
186172
backupDir = importConfig.backupDir;

packages/contentstack-import/src/import/module-importer.ts

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@ import { addLocale, cliux, ContentstackClient, Logger } from '@contentstack/cli-
66
import startModuleImport from './modules';
77
import startJSModuleImport from './modules-js';
88
import { ImportConfig, Modules } from '../types';
9-
import { backupHandler, log, validateBranch, masterLocalDetails, sanitizeStack, initLogger, trace } from '../utils';
9+
import {
10+
backupHandler,
11+
log,
12+
masterLocalDetails,
13+
sanitizeStack,
14+
initLogger,
15+
setupBranchConfig,
16+
} from '../utils';
1017

1118
class ModuleImporter {
1219
private managementAPIClient: ContentstackClient;
@@ -28,8 +35,14 @@ class ModuleImporter {
2835
this.importConfig.stackName = stackDetails.name as string;
2936
this.importConfig.org_uid = stackDetails.org_uid as string;
3037
}
31-
if (this.importConfig.branchName) {
32-
await validateBranch(this.stackAPIClient, this.importConfig, this.importConfig.branchName);
38+
39+
await setupBranchConfig(this.importConfig, this.stackAPIClient);
40+
if (this.importConfig.branchAlias && this.importConfig.branchName) {
41+
this.stackAPIClient = this.managementAPIClient.stack({
42+
api_key: this.importConfig.apiKey,
43+
management_token: this.importConfig.management_token,
44+
branch_uid: this.importConfig.branchName,
45+
});
3346
}
3447

3548
if (this.importConfig.management_token) {
@@ -50,15 +63,9 @@ class ModuleImporter {
5063
if (
5164
!this.importConfig.skipAudit &&
5265
(!this.importConfig.moduleName ||
53-
[
54-
'content-types',
55-
'global-fields',
56-
'entries',
57-
'extensions',
58-
'workflows',
59-
'custom-roles',
60-
'assets'
61-
].includes(this.importConfig.moduleName))
66+
['content-types', 'global-fields', 'entries', 'extensions', 'workflows', 'custom-roles', 'assets'].includes(
67+
this.importConfig.moduleName,
68+
))
6269
) {
6370
if (!(await this.auditImportData(logger))) {
6471
return { noSuccessMsg: true };
@@ -150,15 +157,9 @@ class ModuleImporter {
150157
} else if (this.importConfig.modules.types.length) {
151158
this.importConfig.modules.types
152159
.filter((val) =>
153-
[
154-
'content-types',
155-
'global-fields',
156-
'entries',
157-
'extensions',
158-
'workflows',
159-
'custom-roles',
160-
'assets'
161-
].includes(val),
160+
['content-types', 'global-fields', 'entries', 'extensions', 'workflows', 'custom-roles', 'assets'].includes(
161+
val,
162+
),
162163
)
163164
.forEach((val) => {
164165
args.push('--modules', val);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export default interface ImportConfig extends DefaultConfig, ExternalConfig {
2929
branches?: branch[];
3030
branchEnabled?: boolean;
3131
branchDir?: string;
32+
branchAlias?: string;
3233
moduleName?: Modules;
3334
master_locale: masterLocale;
3435
headers?: {

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import merge from 'merge';
22
import * as path from 'path';
33
import { omit, filter, includes, isArray } from 'lodash';
4-
import { configHandler, isAuthenticated, cliux, sanitizePath, log } from '@contentstack/cli-utilities';
4+
import {
5+
configHandler,
6+
isAuthenticated,
7+
cliux,
8+
sanitizePath,
9+
log,
10+
} from '@contentstack/cli-utilities';
511
import defaultConfig from '../config';
612
import { readFile, fileExistsSync } from './file-helper';
713
import { askContentDir, askAPIKey } from './interactive';
@@ -98,6 +104,10 @@ const setupConfig = async (importCmdFlags: any): Promise<ImportConfig> => {
98104
config.importWebhookStatus = importCmdFlags['import-webhook-status'];
99105
config.skipPrivateAppRecreationIfExist = !importCmdFlags['skip-app-recreation'];
100106

107+
if (importCmdFlags['branch-alias']) {
108+
config.branchAlias = importCmdFlags['branch-alias'];
109+
}
110+
101111
if (importCmdFlags['branch']) {
102112
config.branchName = importCmdFlags['branch'];
103113
config.branchDir = config.contentDir;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export { setupBranchConfig } from './setup-branch';
12
export * as interactive from './interactive';
23
export { default as setupImportConfig } from './import-config-handler';
34
export * as fileHelper from './file-helper';
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { ContentstackClient, getBranchFromAlias, log } from '@contentstack/cli-utilities';
2+
import { ImportConfig } from 'src/types';
3+
import { validateBranch } from './common-helper';
4+
5+
export const setupBranchConfig = async (
6+
config: ImportConfig,
7+
stackAPIClient: ReturnType<ContentstackClient['stack']>,
8+
): Promise<void> => {
9+
if (config.branchName) {
10+
await validateBranch(stackAPIClient, config, config.branchName);
11+
return;
12+
}
13+
14+
if (config.branchAlias) {
15+
config.branchName = await getBranchFromAlias(stackAPIClient, config.branchAlias);
16+
}
17+
try {
18+
const branches = await stackAPIClient
19+
.branch()
20+
.query()
21+
.find()
22+
.then(({ items }) => items);
23+
if (branches.length) {
24+
log.info(`Stack is branch Enabled and Branch is not passed by default import will be done in main branch`);
25+
}
26+
} catch (error) {
27+
// Here the stack is not branch enabled or any network issue
28+
log.debug('Failed to fetch branches', { error });
29+
}
30+
};

0 commit comments

Comments
 (0)