Skip to content

Commit

Permalink
Implemented variants entries bulk unpublish (#1521)
Browse files Browse the repository at this point in the history
* Implemented variants entries bulk unpublish

* Upgraded axios package

* Removed secrets-scan.yml workflow file

* Fixed PR comments

* Fixed PR comments
  • Loading branch information
sunil-lakshman authored Aug 22, 2024
1 parent 0d9db19 commit 248ad7f
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 24 deletions.
9 changes: 8 additions & 1 deletion packages/contentstack-bulk-publish/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1277,7 +1277,7 @@ Unpublish entries from the given environment
USAGE
$ csdx cm:entries:unpublish [-a <value>] [-k <value>] [-e <value>] [-c <value>] [-y] [--locale <value>] [--branch
<value>] [--retry-failed <value>] [--bulk-unpublish <value>] [--api-version <value>] [--content-type <value>]
[--delivery-token <value>]
[--delivery-token <value>] [--include-variants]
FLAGS
-a, --alias=<value> Alias(name) for the management token
Expand All @@ -1292,6 +1292,7 @@ FLAGS
bulkpublish API will be used to unpublish the entries
--content-type=<value> Content type filter
--delivery-token=<value> Delivery token for source environment
--include-variants Include Variants flag will unpublish all associated variant entries.
--locale=<value> Locale filter
--retry-failed=<value> Retry publishing failed entries from the logfile
Expand Down Expand Up @@ -1334,6 +1335,12 @@ EXAMPLES
Using --stack-api-key flag
$ csdx cm:stacks:unpublish --bulk-unpublish --content-type [CONTENT TYPE] --environment [SOURCE ENV] --locale [LOCALE] --stack-api-key [STACK API KEY] --delivery-token [DELIVERY TOKEN]
Using --include-variants flag
$ csdx cm:stacks:unpublish --bulk-unpublish --content-type [CONTENT TYPE] --environment [SOURCE ENV] --locale [LOCALE] --stack-api-key [STACK API KEY] --delivery-token [DELIVERY TOKEN] --include-variants
```

_See code: [src/commands/cm/entries/unpublish.js](https://github.com/contentstack/cli/blob/main/packages/contentstack-bulk-publish/src/commands/cm/entries/unpublish.js)_
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class UnpublishCommand extends Command {
unpublishFlags.onlyAssets = false;
unpublishFlags.onlyEntries = true;
unpublishFlags.apiVersion = unpublishFlags['api-version'] || '3';
unpublishFlags.includeVariants = unpublishFlags['include-variants'];
delete unpublishFlags['api-version'];
delete unpublishFlags['retry-failed'];
delete unpublishFlags['bulk-unpublish'];
Expand Down Expand Up @@ -59,7 +60,7 @@ class UnpublishCommand extends Command {
updatedFlags.deliveryToken = await cliux.prompt('Enter delivery token of your source environment');
}
updatedFlags.bulkUnpublish = updatedFlags.bulkUnpublish === 'false' ? false : true;

updatedFlags.includeVariantsFlag = updatedFlags.includeVariants;
stack = await getStack(config);
}
if (!updatedFlags.deliveryToken && updatedFlags.deliveryToken.length === 0) {
Expand Down Expand Up @@ -177,6 +178,10 @@ UnpublishCommand.flags = {
'delivery-token': flags.string({
description: 'Delivery token for source environment',
}),
'include-variants': flags.boolean({
default: false, // set the default value to false
description: 'Include Variants flag will unpublish all associated variant entries.'
}),
};

UnpublishCommand.examples = [
Expand All @@ -196,6 +201,9 @@ UnpublishCommand.examples = [
'',
'Using --stack-api-key flag',
'csdx cm:stacks:unpublish --bulk-unpublish --content-type [CONTENT TYPE] --environment [SOURCE ENV] --locale [LOCALE] --stack-api-key [STACK API KEY] --delivery-token [DELIVERY TOKEN]',
'',
'Using --include-variants flag',
'csdx cm:stacks:unpublish --bulk-unpublish --content-type [CONTENT TYPE] --environment [SOURCE ENV] --locale [LOCALE] --stack-api-key [STACK API KEY] --delivery-token [DELIVERY TOKEN] --include-variants',
];

module.exports = UnpublishCommand;
80 changes: 71 additions & 9 deletions packages/contentstack-bulk-publish/src/producer/unpublish.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_UNPUBLISH_API_VERSION = '3.2';

let bulkUnPublishSet = [];
let bulkUnPulishAssetSet = [];
Expand Down Expand Up @@ -50,19 +51,25 @@ function getQueryParams(filter) {
return queryString;
}

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

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

if (variantsFlag) {
entryData.publish_with_base_entry = true;
entryData.variants = items[index].data.variants;
}
bulkUnPublishSet.push(entryData);
}

if (bulkUnPulishAssetSet.length < 10 && items[index].type === 'asset_published') {
Expand Down Expand Up @@ -155,6 +162,7 @@ async function getSyncEntries(
environment,
deliveryToken,
apiVersion,
variantsFlag,
paginationToken = null,
) {
return new Promise(async (resolve, reject) => {
Expand Down Expand Up @@ -202,11 +210,15 @@ async function getSyncEntries(
syncData['type'] = queryParamsObj.type;
}

const entriesResponse = await Stack.sync(syncData);

let entriesResponse = await Stack.sync(syncData);
if (entriesResponse.items.length > 0) {
await bulkAction(stack, entriesResponse.items, bulkUnpublish, environment, locale, apiVersion);
if (variantsFlag) {
queryParamsObj.apiVersion = VARIANTS_UNPUBLISH_API_VERSION;
entriesResponse.items = await attachVariantsToItems(stack, entriesResponse.items, queryParamsObj); // with variants data
}
await bulkAction(stack, entriesResponse.items, bulkUnpublish, environment, locale, apiVersion, variantsFlag);
}

if (entriesResponse.items.length === 0) {
if (!changedFlag) console.log('No Entries/Assets Found published on specified environment');
return resolve();
Expand All @@ -230,6 +242,53 @@ async function getSyncEntries(
});
}

async function attachVariantsToItems(stack, items, queryParams) {
for (const item of items) {
const { content_type_uid, data } = item;
const variantEntries = await getVariantEntries(stack, content_type_uid, item, queryParams); // Fetch the variants using fetchVariants method
item.data.variants = variantEntries; // Attach the fetched variants to the data object in the item
}
return items;
}

async function getVariantEntries(stack, contentType, entries, 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.data.uid)
.variants()
.query(variantQueryParams)
.find();

// Map the response items to extract variant UIDs
const variants = variantsEntriesResponse.items.map(entry => ({
uid: entry.variants_uid,
}));

// Check if there are more entries to fetch
if (variantsEntriesResponse.items.length === variantQueryParams.limit) {
// Recursively fetch the next set of variants with updated skip value
const nextVariants = await getVariantEntries(stack, contentType, entries, queryParams, skip + variantQueryParams.limit);

// Ensure nextVariants is an array before concatenation
return Array.isArray(nextVariants) ? variants.concat(nextVariants) : variants;
}

return variants;
} catch (error) {
// Handle error message retrieval from different properties
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 @@ -242,6 +301,7 @@ async function start(
onlyEntries,
f_types,
apiVersion,
includeVariantsFlag,
},
stack,
config,
Expand All @@ -256,7 +316,9 @@ async function start(
}
process.exit(0);
});

if (includeVariantsFlag) {
apiVersion = VARIANTS_UNPUBLISH_API_VERSION;
}
if (retryFailed) {
if (typeof retryFailed === 'string' && retryFailed.length > 0) {
if (!validateFile(retryFailed, ['unpublish', 'bulk-unpublish'])) {
Expand Down Expand Up @@ -294,7 +356,7 @@ async function start(
}
setConfig(config, bulkUnpublish);
const queryParams = getQueryParams(filter);
await getSyncEntries(stack, config, locale, queryParams, bulkUnpublish, environment, deliveryToken, apiVersion);
await getSyncEntries(stack, config, locale, queryParams, bulkUnpublish, environment, deliveryToken, apiVersion, includeVariantsFlag);
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/contentstack-utilities/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@contentstack/cli-utilities",
"version": "1.7.1",
"version": "1.7.2",
"description": "Utilities for contentstack projects",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down Expand Up @@ -35,7 +35,7 @@
"@contentstack/management": "~1.17.0",
"@contentstack/marketplace-sdk": "^1.2.1",
"@oclif/core": "^3.26.5",
"axios": "^1.6.4",
"axios": "^1.7.4",
"chalk": "^4.0.0",
"cli-cursor": "^3.1.0",
"cli-table": "^0.3.11",
Expand Down
9 changes: 8 additions & 1 deletion packages/contentstack/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1851,7 +1851,7 @@ Unpublish entries from the given environment
USAGE
$ csdx cm:entries:unpublish [-a <value>] [-k <value>] [-e <value>] [-c <value>] [-y] [--locale <value>] [--branch
<value>] [--retry-failed <value>] [--bulk-unpublish <value>] [--api-version <value>] [--content-type <value>]
[--delivery-token <value>]
[--delivery-token <value>] [--include-variants]
FLAGS
-a, --alias=<value> Alias(name) for the management token
Expand All @@ -1866,6 +1866,7 @@ FLAGS
bulkpublish API will be used to unpublish the entries
--content-type=<value> Content type filter
--delivery-token=<value> Delivery token for source environment
--include-variants Include Variants flag will unpublish all associated variant entries.
--locale=<value> Locale filter
--retry-failed=<value> Retry publishing failed entries from the logfile
Expand Down Expand Up @@ -1908,6 +1909,12 @@ EXAMPLES
Using --stack-api-key flag
$ csdx cm:stacks:unpublish --bulk-unpublish --content-type [CONTENT TYPE] --environment [SOURCE ENV] --locale [LOCALE] --stack-api-key [STACK API KEY] --delivery-token [DELIVERY TOKEN]
Using --include-variants flag
$ csdx cm:stacks:unpublish --bulk-unpublish --content-type [CONTENT TYPE] --environment [SOURCE ENV] --locale [LOCALE] --stack-api-key [STACK API KEY] --delivery-token [DELIVERY TOKEN] --include-variants
```

_See code: [@contentstack/cli-cm-bulk-publish](https://github.com/contentstack/cli/blob/main/packages/contentstack-bulk-publish/src/commands/cm/entries/unpublish.js)_
Expand Down
2 changes: 1 addition & 1 deletion packages/contentstack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"@contentstack/cli-config": "~1.6.5",
"@contentstack/cli-launch": "~1.1.0",
"@contentstack/cli-migration": "~1.6.1",
"@contentstack/cli-utilities": "~1.7.1",
"@contentstack/cli-utilities": "~1.7.2",
"@contentstack/management": "~1.17.0",
"@oclif/core": "^3.26.5",
"@oclif/plugin-help": "^5",
Expand Down
28 changes: 19 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 248ad7f

Please sign in to comment.