From dcd5f04d85f53d32473f57690fda0617c7535c63 Mon Sep 17 00:00:00 2001 From: sunil-lakshman <104969541+sunil-lakshman@users.noreply.github.com> Date: Tue, 13 Aug 2024 15:01:31 +0530 Subject: [PATCH] Implemented variants entries bulk publish feature --- .../src/commands/cm/entries/publish.js | 12 +++++- .../src/producer/publish-entries.js | 41 +++++++++++++++---- 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/packages/contentstack-bulk-publish/src/commands/cm/entries/publish.js b/packages/contentstack-bulk-publish/src/commands/cm/entries/publish.js index 8b50c8b1ee..6e41d1da8e 100644 --- a/packages/contentstack-bulk-publish/src/commands/cm/entries/publish.js +++ b/packages/contentstack-bulk-publish/src/commands/cm/entries/publish.js @@ -21,6 +21,7 @@ class PublishEntriesCommand extends Command { entriesFlags.publishAllContentTypes = entriesFlags['publish-all-content-types'] || entriesFlags.publishAllContentTypes || false; entriesFlags.apiVersion = entriesFlags['api-version'] || '3'; + entriesFlags.includeVariants = entriesFlags['include-variants'] || entriesFlags.includeVariants || false; delete entriesFlags['api-version']; delete entriesFlags['retry-failed']; delete entriesFlags['content-types']; @@ -57,6 +58,7 @@ class PublishEntriesCommand extends Command { } else { this.error('Please use `--alias` or `--stack-api-key` to proceed.', { exit: 2 }); } + updatedFlags.includeVariantsFlag = entriesFlags.includeVariants; updatedFlags.bulkPublish = updatedFlags.bulkPublish !== 'false'; stack = await getStack(config); } @@ -244,6 +246,11 @@ PublishEntriesCommand.flags = { }), 'delivery-token': flags.string({ description: 'Delivery token for source environment' }), 'source-env': flags.string({ description: 'Source environment' }), + 'include-variants': flags.boolean({ + default: false, // set the default value to false + allowNo: true, // this allows the flag to be negated with `--no-include-variants` + description: 'Include Variants flag will publish all associated variant entries.' + }), }; PublishEntriesCommand.examples = [ @@ -267,11 +274,14 @@ PublishEntriesCommand.examples = [ '', 'Using --stack-api-key', 'csdx cm:entries:publish -e [ENVIRONMENT 1] [ENVIRONMENT 2] --locales [LOCALE 1] [LOCALE 2] --stack-api-key [STACK API KEY] --source-env [SOURCE ENVIRONMENT] --delivery-token [DELIVERY TOKEN]', + '', + 'Using --include-variants', + 'csdx cm:entries:publish -e [ENVIRONMENT 1] [ENVIRONMENT 2] --locales [LOCALE 1] [LOCALE 2] --stack-api-key [STACK API KEY] --source-env [SOURCE ENVIRONMENT] --delivery-token [DELIVERY TOKEN] [--include-variants]', ]; PublishEntriesCommand.aliases = ['cm:bulk-publish:entries']; PublishEntriesCommand.usage = - 'cm:entries:publish [-a ] [--retry-failed ] [--bulk-publish ] [--publish-all-content-types] [--content-types ] [--locales ] [-e ] [-c ] [-y] [--branch ] [--delivery-token ] [--source-env ]'; + 'cm:entries:publish [-a ] [--retry-failed ] [--bulk-publish ] [--publish-all-content-types] [--content-types ] [--locales ] [-e ] [-c ] [-y] [--branch ] [--delivery-token ] [--source-env ] [--include-variants]'; module.exports = PublishEntriesCommand; diff --git a/packages/contentstack-bulk-publish/src/producer/publish-entries.js b/packages/contentstack-bulk-publish/src/producer/publish-entries.js index 126260ce73..a1f75688c4 100644 --- a/packages/contentstack-bulk-publish/src/producer/publish-entries.js +++ b/packages/contentstack-bulk-publish/src/producer/publish-entries.js @@ -8,6 +8,7 @@ const { performBulkPublish, publishEntry, initializeLogger } = require('../consu const retryFailedLogs = require('../util/retryfailed'); const { validateFile } = require('../util/fs'); const { isEmpty } = require('../util'); +const VARIANTS_PUBLISH_API_VERSION = '3.2'; const queue = getQueue(); @@ -18,7 +19,7 @@ let allContentTypes = []; let bulkPublishSet = []; let filePath; -async function getEntries(stack, contentType, locale, bulkPublish, environments, apiVersion, skip = 0) { +async function getEntries(stack, contentType, locale, bulkPublish, environments, apiVersion, variantsFlag = false, skip = 0) { return new Promise((resolve, reject) => { skipCount = skip; @@ -29,6 +30,10 @@ async function getEntries(stack, contentType, locale, bulkPublish, environments, include_publish_details: true, }; + if (variantsFlag) { + queryParams.apiVersion = VARIANTS_PUBLISH_API_VERSION; + } + stack .contentType(contentType) .entry() @@ -37,7 +42,23 @@ async function getEntries(stack, contentType, locale, bulkPublish, environments, .then(async (entriesResponse) => { skipCount += entriesResponse.items.length; let entries = entriesResponse.items; - for (let index = 0; index < entriesResponse.items.length; index++) { + + for (let index = 0; index < entries.length; index++) { + let variants = []; + + if (variantsFlag) { + const variantsEntriesResponse = await stack + .contentType(contentType) + .entry(entries[index].uid) + .variants() + .query(queryParams) + .find(); + + variants = variantsEntriesResponse.items.map(entry => ({ + uid: entry.variants_uid, + })); + } + if (bulkPublish) { if (bulkPublishSet.length < 10) { bulkPublishSet.push({ @@ -45,6 +66,7 @@ async function getEntries(stack, contentType, locale, bulkPublish, environments, content_type: contentType, locale, publish_details: entries[index].publish_details || [], + variants: variants }); } @@ -61,7 +83,7 @@ async function getEntries(stack, contentType, locale, bulkPublish, environments, } if ( - index === entriesResponse.items.length - 1 && + index === entries.length - 1 && bulkPublishSet.length <= 10 && bulkPublishSet.length > 0 ) { @@ -74,7 +96,7 @@ async function getEntries(stack, contentType, locale, bulkPublish, environments, apiVersion }); bulkPublishSet = []; - } // bulkPublish + } } else { await queue.Enqueue({ content_type: contentType, @@ -92,7 +114,7 @@ async function getEntries(stack, contentType, locale, bulkPublish, environments, bulkPublishSet = []; return resolve(); } - await getEntries(stack, contentType, locale, bulkPublish, environments, apiVersion, skipCount); + await getEntries(stack, contentType, locale, bulkPublish, environments, apiVersion, variantsFlag, skipCount); return resolve(); }) .catch((error) => reject(error)); @@ -135,7 +157,7 @@ function setConfig(conf, bp) { } async function start( - { retryFailed, bulkPublish, publishAllContentTypes, contentTypes, locales, environments, apiVersion }, + { retryFailed, bulkPublish, publishAllContentTypes, contentTypes, locales, environments, apiVersion, includeVariantsFlag }, stack, config, ) { @@ -149,6 +171,11 @@ async function start( } process.exit(0); }); + + if (includeVariantsFlag) { + apiVersion = VARIANTS_PUBLISH_API_VERSION; + } + if (retryFailed) { if (typeof retryFailed === 'string') { if (!validateFile(retryFailed, ['publish-entries', 'bulk-publish-entries'])) { @@ -173,7 +200,7 @@ async function start( for (let loc = 0; loc < locales.length; loc += 1) { for (let i = 0; i < allContentTypes.length; i += 1) { /* eslint-disable no-await-in-loop */ - await getEntries(stack, allContentTypes[i].uid || allContentTypes[i], locales[loc], bulkPublish, environments, apiVersion); + await getEntries(stack, allContentTypes[i].uid || allContentTypes[i], locales[loc], bulkPublish, environments, apiVersion, includeVariantsFlag); /* eslint-enable no-await-in-loop */ } }