Skip to content
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

Implemented cross publish with variant entries #1529

Merged
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 10 additions & 3 deletions packages/contentstack-bulk-publish/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ USAGE
* [`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)
* [`csdx cm:bulk-publish:clear`](#csdx-cmbulk-publishclear)
* [`csdx cm:bulk-publish:configure`](#csdx-cmbulk-publishconfigure)
* [`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)
* [`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)
* [`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)
* [`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)
* [`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)
Expand Down Expand Up @@ -422,15 +422,15 @@ EXAMPLES
$ csdx cm:stacks:publish-configure --stack-api-key <stack_api_key>
```

## `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 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]`

Publish entries and assets from one environment to other environments

```
USAGE
$ 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]
[-y] [--branch <value>] [--onlyAssets] [--onlyEntries] [--include-variants]

FLAGS
-B, --branch=<value> [default: main] Specify the branch to fetch the content (by default the main branch is
Expand All @@ -445,6 +445,7 @@ FLAGS
--content-type=<value>... The Contenttypes from which entries will be published
--delivery-token=<value> Delivery token for source environment
--environments=<value>... Destination Environments
--include-variants Include Variants flag will publish all associated variant entries.
--locales=<value> Source locale
--onlyAssets Unpublish only assets
--onlyEntries Unpublish only entries
Expand Down Expand Up @@ -495,6 +496,12 @@ EXAMPLES
Using --stack-api-key flag

$ 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]



Using --include-variants flag

$ 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]
```

_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)_
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class CrossPublishCommand extends Command {
updatedFlags.deliveryToken = await cliux.prompt('Enter delivery token of your source environment');
}
updatedFlags.bulkPublish = updatedFlags.bulkPublish === 'false' ? false : true;

updatedFlags.includeVariants = updatedFlags.includeVariants === false ? false : true;
stack = await getStack(config);
}

Expand Down Expand Up @@ -155,6 +155,10 @@ class CrossPublishCommand extends Command {
_flags.deliveryToken = _flags['delivery-token'];
delete _flags['delivery-token'];
}
if ('include-variants' in _flags) {
_flags.includeVariants = _flags['include-variants'];
delete _flags['include-variants'];
}
return _flags;
}
}
Expand Down Expand Up @@ -257,6 +261,10 @@ CrossPublishCommand.flags = {
}),
onlyAssets: flags.boolean({ description: 'Unpublish only assets', default: false }),
onlyEntries: flags.boolean({ description: 'Unpublish only entries', default: false }),
'include-variants': flags.boolean({
description: 'Include Variants flag will publish all associated variant entries.',
default: false,
}),
};

CrossPublishCommand.examples = [
Expand All @@ -278,8 +286,11 @@ CrossPublishCommand.examples = [
'Using --stack-api-key flag',
'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]',
'',
'Using --include-variants flag',
'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]',
'',
];

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]`;
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]`;

module.exports = CrossPublishCommand;
85 changes: 80 additions & 5 deletions packages/contentstack-bulk-publish/src/producer/cross-publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const assetQueue = getQueue();
const { Command } = require('@contentstack/cli-command');
const command = new Command();
const { isEmpty } = require('../util');
const VARIANTS_PUBLISH_API_VERSION = '3.2';

let bulkPublishSet = [];
let bulkPublishAssetSet = [];
Expand All @@ -33,20 +34,27 @@ function getQueryParams(filter) {
return queryString;
}

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

if (bulkPublish) {
if (bulkPublishSet.length < 10 && items[index].type === 'entry_published') {
bulkPublishSet.push({
const entry = {
uid: items[index].data.uid,
content_type: items[index].content_type_uid,
locale: items[index].data.locale || 'en-us',
version: items[index].data._version,
publish_details: [items[index].data.publish_details] || [],
});
};

if (variantsFlag) {
entry.variants = items[index].data.variants || [];
entry.publish_with_base_entry = true;
}

bulkPublishSet.push(JSON.parse(JSON.stringify(entry)));
}

if (bulkPublishAssetSet.length < 10 && items[index].type === 'asset_published') {
Expand Down Expand Up @@ -143,6 +151,7 @@ async function getSyncEntries(
deliveryToken,
destEnv,
apiVersion,
variantsFlag = false,
paginationToken = null,
) {
return new Promise(async (resolve, reject) => {
Expand Down Expand Up @@ -198,8 +207,18 @@ async function getSyncEntries(
);
}

if (variantsFlag) {
for (let index = 0; index < entriesResponse?.items?.length; index++) {
let variants = [];
variants = await getVariantEntries(stack, entries[index].content_type_uid, entries, index, queryParamsObj);
if (variants.length > 0) {
entriesResponse.items[index].data.variants = variants;
}
}
}

if (entriesResponse.items.length > 0) {
await bulkAction(stack, entriesResponse.items, bulkPublish, filter, destEnv, apiVersion);
await bulkAction(stack, entriesResponse.items, bulkPublish, filter, destEnv, apiVersion, variantsFlag);
}
if (!entriesResponse.pagination_token) {
if (!changedFlag) console.log('No Entries/Assets Found published on specified environment');
Expand Down Expand Up @@ -241,6 +260,48 @@ function setConfig(conf, bp) {
filePath = initializeLogger(logFileName);
}

async function getVariantEntries(stack, contentType, entries, index, queryParams, skip = 0) {
try {
let variantQueryParams = {
locale: queryParams.locale || 'en-us',
include_count: true,
skip: skip, // Adding skip parameter for pagination
limit: 100, // Set a limit to fetch up to 100 entries per request
};

const variantsEntriesResponse = await stack
.contentType(contentType)
.entry(entries[index].data.uid)
.variants()
.query(variantQueryParams)
.find();

const variants = variantsEntriesResponse.items.map((entry) => ({
uid: entry.variants_uid,
}));

if (variantsEntriesResponse.items.length === variantQueryParams.limit) {
const nextVariants = await getVariantEntries(
stack,
contentType,
entries,
index,
queryParams,
skip + variantQueryParams.limit,
);
return Array.isArray(nextVariants) ? variants.concat(nextVariants) : variants;
}
return variants;
} catch (error) {
const errorMessage =
error?.errorMessage ||
error?.message ||
error?.errors ||
'Falied to fetch the variant entries, Please contact the admin for support.';
throw new Error(`Error fetching variants: ${errorMessage}`);
}
}

async function start(
{
retryFailed,
Expand All @@ -255,6 +316,7 @@ async function start(
destEnv,
f_types,
apiVersion,
includeVariants,
},
stack,
config,
Expand Down Expand Up @@ -306,7 +368,20 @@ async function start(
setConfig(config, bulkPublish);
// filter.type = (f_types) ? f_types : types // types mentioned in the config file (f_types) are given preference
const queryParams = getQueryParams(filter);
await getSyncEntries(stack, config, queryParams, bulkPublish, filter, deliveryToken, destEnv, apiVersion);
if (includeVariants) {
apiVersion = VARIANTS_PUBLISH_API_VERSION;
}
await getSyncEntries(
stack,
config,
queryParams,
bulkPublish,
filter,
deliveryToken,
destEnv,
apiVersion,
includeVariants,
);
}
}

Expand Down
Loading
Loading