Skip to content

Commit 98dce75

Browse files
Implemented cross publish with variant entries (#1529)
* Implemented cross publish variant entries * Fixed PR comments
1 parent 248ad7f commit 98dce75

File tree

4 files changed

+147
-19
lines changed

4 files changed

+147
-19
lines changed

packages/contentstack-bulk-publish/README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ USAGE
3636
* [`csdx cm:assets:publish [-a <value>] [--retry-failed <value>] [-e <value>] [--folder-uid <value>] [--bulk-publish <value>] [-c <value>] [-y] [--locales <value>] [--branch <value>] [--delivery-token <value>] [--source-env <value>]`](#csdx-cmassetspublish--a-value---retry-failed-value--e-value---folder-uid-value---bulk-publish-value--c-value--y---locales-value---branch-value---delivery-token-value---source-env-value-1)
3737
* [`csdx cm:bulk-publish:clear`](#csdx-cmbulk-publishclear)
3838
* [`csdx cm:bulk-publish:configure`](#csdx-cmbulk-publishconfigure)
39-
* [`csdx cm:bulk-publish:cross-publish [-a <value>] [--retry-failed <value>] [--bulk-publish <value>] [--content-type <value>] [--locales <value>] [--source-env <value>] [--environments <value>] [--delivery-token <value>] [-c <value>] [-y] [--branch <value>] [--onlyAssets] [--onlyEntries]`](#csdx-cmbulk-publishcross-publish--a-value---retry-failed-value---bulk-publish-value---content-type-value---locales-value---source-env-value---environments-value---delivery-token-value--c-value--y---branch-value---onlyassets---onlyentries)
39+
* [`csdx cm:bulk-publish:cross-publish [-a <value>] [--retry-failed <value>] [--bulk-publish <value>] [--content-type <value>] [--locales <value>] [--source-env <value>] [--environments <value>] [--delivery-token <value>] [-c <value>] [-y] [--branch <value>] [--onlyAssets] [--onlyEntries] [--include-variants]`](#csdx-cmbulk-publishcross-publish--a-value---retry-failed-value---bulk-publish-value---content-type-value---locales-value---source-env-value---environments-value---delivery-token-value--c-value--y---branch-value---onlyassets---onlyentries---include-variants)
4040
* [`csdx cm:entries:publish [-a <value>] [--retry-failed <value>] [--bulk-publish <value>] [--publish-all-content-types] [--content-types <value>] [--locales <value>] [-e <value>] [-c <value>] [-y] [--branch <value>] [--delivery-token <value>] [--source-env <value>] [--entry-uid <value>] [--include-variants]`](#csdx-cmentriespublish--a-value---retry-failed-value---bulk-publish-value---publish-all-content-types---content-types-value---locales-value--e-value--c-value--y---branch-value---delivery-token-value---source-env-value---entry-uid-value---include-variants)
4141
* [`csdx cm:entries:publish-modified [-a <value>] [--retry-failed <value>] [--bulk-publish <value>] [--source-env <value>] [--content-types <value>] [--locales <value>] [-e <value>] [-c <value>] [-y] [--branch <value>]`](#csdx-cmentriespublish-modified--a-value---retry-failed-value---bulk-publish-value---source-env-value---content-types-value---locales-value--e-value--c-value--y---branch-value)
4242
* [`csdx cm:entries:publish-non-localized-fields [-a <value>] [--retry-failed <value>] [--bulk-publish <value>] [--source-env <value>] [--content-types <value>] [-e <value>] [-c <value>] [-y] [--branch <value>]`](#csdx-cmentriespublish-non-localized-fields--a-value---retry-failed-value---bulk-publish-value---source-env-value---content-types-value--e-value--c-value--y---branch-value)
@@ -422,15 +422,15 @@ EXAMPLES
422422
$ csdx cm:stacks:publish-configure --stack-api-key <stack_api_key>
423423
```
424424

425-
## `csdx cm:bulk-publish:cross-publish [-a <value>] [--retry-failed <value>] [--bulk-publish <value>] [--content-type <value>] [--locales <value>] [--source-env <value>] [--environments <value>] [--delivery-token <value>] [-c <value>] [-y] [--branch <value>] [--onlyAssets] [--onlyEntries]`
425+
## `csdx cm:bulk-publish:cross-publish [-a <value>] [--retry-failed <value>] [--bulk-publish <value>] [--content-type <value>] [--locales <value>] [--source-env <value>] [--environments <value>] [--delivery-token <value>] [-c <value>] [-y] [--branch <value>] [--onlyAssets] [--onlyEntries] [--include-variants]`
426426

427427
Publish entries and assets from one environment to other environments
428428

429429
```
430430
USAGE
431431
$ csdx cm:bulk-publish:cross-publish [-a <value>] [--retry-failed <value>] [--bulk-publish <value>] [--content-type
432432
<value>] [--locales <value>] [--source-env <value>] [--environments <value>] [--delivery-token <value>] [-c <value>]
433-
[-y] [--branch <value>] [--onlyAssets] [--onlyEntries]
433+
[-y] [--branch <value>] [--onlyAssets] [--onlyEntries] [--include-variants]
434434
435435
FLAGS
436436
-B, --branch=<value> [default: main] Specify the branch to fetch the content (by default the main branch is
@@ -445,6 +445,7 @@ FLAGS
445445
--content-type=<value>... The Contenttypes from which entries will be published
446446
--delivery-token=<value> Delivery token for source environment
447447
--environments=<value>... Destination Environments
448+
--include-variants Include Variants flag will publish all associated variant entries.
448449
--locales=<value> Source locale
449450
--onlyAssets Unpublish only assets
450451
--onlyEntries Unpublish only entries
@@ -495,6 +496,12 @@ EXAMPLES
495496
Using --stack-api-key flag
496497
497498
$ csdx cm:bulk-publish:cross-publish --content-type [CONTENT TYPE] --source-env [SOURCE ENV] --environments [DESTINATION ENVIRONMENT] --locales [LOCALE] --stack-api-key [STACK API KEY] --delivery-token [DELIVERY TOKEN]
499+
500+
501+
502+
Using --include-variants flag
503+
504+
$ csdx cm:bulk-publish:cross-publish --content-type [CONTENT TYPE] --source-env [SOURCE ENV] --environments [DESTINATION ENVIRONMENT] --locales [LOCALE] --stack-api-key [STACK API KEY] --delivery-token [DELIVERY TOKEN] [--include-variants]
498505
```
499506

500507
_See code: [src/commands/cm/bulk-publish/cross-publish.js](https://github.com/contentstack/cli/blob/main/packages/contentstack-bulk-publish/src/commands/cm/bulk-publish/cross-publish.js)_

packages/contentstack-bulk-publish/src/commands/cm/bulk-publish/cross-publish.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class CrossPublishCommand extends Command {
4646
updatedFlags.deliveryToken = await cliux.prompt('Enter delivery token of your source environment');
4747
}
4848
updatedFlags.bulkPublish = updatedFlags.bulkPublish === 'false' ? false : true;
49-
49+
updatedFlags.includeVariants = updatedFlags.includeVariants === false ? false : true;
5050
stack = await getStack(config);
5151
}
5252

@@ -155,6 +155,10 @@ class CrossPublishCommand extends Command {
155155
_flags.deliveryToken = _flags['delivery-token'];
156156
delete _flags['delivery-token'];
157157
}
158+
if ('include-variants' in _flags) {
159+
_flags.includeVariants = _flags['include-variants'];
160+
delete _flags['include-variants'];
161+
}
158162
return _flags;
159163
}
160164
}
@@ -257,6 +261,10 @@ CrossPublishCommand.flags = {
257261
}),
258262
onlyAssets: flags.boolean({ description: 'Unpublish only assets', default: false }),
259263
onlyEntries: flags.boolean({ description: 'Unpublish only entries', default: false }),
264+
'include-variants': flags.boolean({
265+
description: 'Include Variants flag will publish all associated variant entries.',
266+
default: false,
267+
}),
260268
};
261269

262270
CrossPublishCommand.examples = [
@@ -278,8 +286,11 @@ CrossPublishCommand.examples = [
278286
'Using --stack-api-key flag',
279287
'csdx cm:bulk-publish:cross-publish --content-type [CONTENT TYPE] --source-env [SOURCE ENV] --environments [DESTINATION ENVIRONMENT] --locales [LOCALE] --stack-api-key [STACK API KEY] --delivery-token [DELIVERY TOKEN]',
280288
'',
289+
'Using --include-variants flag',
290+
'csdx cm:bulk-publish:cross-publish --content-type [CONTENT TYPE] --source-env [SOURCE ENV] --environments [DESTINATION ENVIRONMENT] --locales [LOCALE] --stack-api-key [STACK API KEY] --delivery-token [DELIVERY TOKEN] [--include-variants]',
291+
'',
281292
];
282293

283-
CrossPublishCommand.usage = `cm:bulk-publish:cross-publish [-a <value>] [--retry-failed <value>] [--bulk-publish <value>] [--content-type <value>] [--locales <value>] [--source-env <value>] [--environments <value>] [--delivery-token <value>] [-c <value>] [-y] [--branch <value>] [--onlyAssets] [--onlyEntries]`;
294+
CrossPublishCommand.usage = `cm:bulk-publish:cross-publish [-a <value>] [--retry-failed <value>] [--bulk-publish <value>] [--content-type <value>] [--locales <value>] [--source-env <value>] [--environments <value>] [--delivery-token <value>] [-c <value>] [-y] [--branch <value>] [--onlyAssets] [--onlyEntries] [--include-variants]`;
284295

285296
module.exports = CrossPublishCommand;

packages/contentstack-bulk-publish/src/producer/cross-publish.js

Lines changed: 80 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const assetQueue = getQueue();
1515
const { Command } = require('@contentstack/cli-command');
1616
const command = new Command();
1717
const { isEmpty } = require('../util');
18+
const VARIANTS_PUBLISH_API_VERSION = '3.2';
1819

1920
let bulkPublishSet = [];
2021
let bulkPublishAssetSet = [];
@@ -33,20 +34,27 @@ function getQueryParams(filter) {
3334
return queryString;
3435
}
3536

36-
async function bulkAction(stack, items, bulkPublish, filter, destEnv, apiVersion) {
37+
async function bulkAction(stack, items, bulkPublish, filter, destEnv, apiVersion, variantsFlag = false) {
3738
return new Promise(async (resolve) => {
3839
for (let index = 0; index < items.length; index++) {
3940
changedFlag = true;
4041

4142
if (bulkPublish) {
4243
if (bulkPublishSet.length < 10 && items[index].type === 'entry_published') {
43-
bulkPublishSet.push({
44+
const entry = {
4445
uid: items[index].data.uid,
4546
content_type: items[index].content_type_uid,
4647
locale: items[index].data.locale || 'en-us',
4748
version: items[index].data._version,
4849
publish_details: [items[index].data.publish_details] || [],
49-
});
50+
};
51+
52+
if (variantsFlag) {
53+
entry.variants = items[index].data.variants || [];
54+
entry.publish_with_base_entry = true;
55+
}
56+
57+
bulkPublishSet.push(JSON.parse(JSON.stringify(entry)));
5058
}
5159

5260
if (bulkPublishAssetSet.length < 10 && items[index].type === 'asset_published') {
@@ -143,6 +151,7 @@ async function getSyncEntries(
143151
deliveryToken,
144152
destEnv,
145153
apiVersion,
154+
variantsFlag = false,
146155
paginationToken = null,
147156
) {
148157
return new Promise(async (resolve, reject) => {
@@ -198,8 +207,18 @@ async function getSyncEntries(
198207
);
199208
}
200209

210+
if (variantsFlag) {
211+
for (let index = 0; index < entriesResponse?.items?.length; index++) {
212+
let variants = [];
213+
variants = await getVariantEntries(stack, entries[index].content_type_uid, entries, index, queryParamsObj);
214+
if (variants.length > 0) {
215+
entriesResponse.items[index].data.variants = variants;
216+
}
217+
}
218+
}
219+
201220
if (entriesResponse.items.length > 0) {
202-
await bulkAction(stack, entriesResponse.items, bulkPublish, filter, destEnv, apiVersion);
221+
await bulkAction(stack, entriesResponse.items, bulkPublish, filter, destEnv, apiVersion, variantsFlag);
203222
}
204223
if (!entriesResponse.pagination_token) {
205224
if (!changedFlag) console.log('No Entries/Assets Found published on specified environment');
@@ -241,6 +260,48 @@ function setConfig(conf, bp) {
241260
filePath = initializeLogger(logFileName);
242261
}
243262

263+
async function getVariantEntries(stack, contentType, entries, index, queryParams, skip = 0) {
264+
try {
265+
let variantQueryParams = {
266+
locale: queryParams.locale || 'en-us',
267+
include_count: true,
268+
skip: skip, // Adding skip parameter for pagination
269+
limit: 100, // Set a limit to fetch up to 100 entries per request
270+
};
271+
272+
const variantsEntriesResponse = await stack
273+
.contentType(contentType)
274+
.entry(entries[index].data.uid)
275+
.variants()
276+
.query(variantQueryParams)
277+
.find();
278+
279+
const variants = variantsEntriesResponse.items.map((entry) => ({
280+
uid: entry.variants_uid,
281+
}));
282+
283+
if (variantsEntriesResponse.items.length === variantQueryParams.limit) {
284+
const nextVariants = await getVariantEntries(
285+
stack,
286+
contentType,
287+
entries,
288+
index,
289+
queryParams,
290+
skip + variantQueryParams.limit,
291+
);
292+
return Array.isArray(nextVariants) ? variants.concat(nextVariants) : variants;
293+
}
294+
return variants;
295+
} catch (error) {
296+
const errorMessage =
297+
error?.errorMessage ||
298+
error?.message ||
299+
error?.errors ||
300+
'Falied to fetch the variant entries, Please contact the admin for support.';
301+
throw new Error(`Error fetching variants: ${errorMessage}`);
302+
}
303+
}
304+
244305
async function start(
245306
{
246307
retryFailed,
@@ -255,6 +316,7 @@ async function start(
255316
destEnv,
256317
f_types,
257318
apiVersion,
319+
includeVariants,
258320
},
259321
stack,
260322
config,
@@ -306,7 +368,20 @@ async function start(
306368
setConfig(config, bulkPublish);
307369
// filter.type = (f_types) ? f_types : types // types mentioned in the config file (f_types) are given preference
308370
const queryParams = getQueryParams(filter);
309-
await getSyncEntries(stack, config, queryParams, bulkPublish, filter, deliveryToken, destEnv, apiVersion);
371+
if (includeVariants) {
372+
apiVersion = VARIANTS_PUBLISH_API_VERSION;
373+
}
374+
await getSyncEntries(
375+
stack,
376+
config,
377+
queryParams,
378+
bulkPublish,
379+
filter,
380+
deliveryToken,
381+
destEnv,
382+
apiVersion,
383+
includeVariants,
384+
);
310385
}
311386
}
312387

0 commit comments

Comments
 (0)