Skip to content

Commit

Permalink
Implemented variants entries bulk publish feature
Browse files Browse the repository at this point in the history
  • Loading branch information
sunil-lakshman committed Aug 13, 2024
1 parent 7989b77 commit dcd5f04
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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 = [
Expand All @@ -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 <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>]';
'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>] [--include-variants]';

module.exports = PublishEntriesCommand;
41 changes: 34 additions & 7 deletions packages/contentstack-bulk-publish/src/producer/publish-entries.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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;

Expand All @@ -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()
Expand All @@ -37,14 +42,31 @@ 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({
uid: entries[index].uid,
content_type: contentType,
locale,
publish_details: entries[index].publish_details || [],
variants: variants
});
}

Expand All @@ -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
) {
Expand All @@ -74,7 +96,7 @@ async function getEntries(stack, contentType, locale, bulkPublish, environments,
apiVersion
});
bulkPublishSet = [];
} // bulkPublish
}
} else {
await queue.Enqueue({
content_type: contentType,
Expand All @@ -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));
Expand Down Expand Up @@ -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,
) {
Expand All @@ -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'])) {
Expand All @@ -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 */
}
}
Expand Down

0 comments on commit dcd5f04

Please sign in to comment.