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

fix: Handled pagination during the script creation #1422

Merged
merged 3 commits into from
Jun 4, 2024
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
1,006 changes: 557 additions & 449 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions packages/contentstack-branches/package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "@contentstack/cli-cm-branches",
"description": "Contentstack CLI plugin to do branches operations",
"version": "1.0.26",
"version": "1.1.0",
"author": "Contentstack",
"bugs": "https://github.com/contentstack/cli/issues",
"dependencies": {
"@contentstack/cli-command": "~1.2.18",
"@contentstack/cli-utilities": "~1.6.2",
"@oclif/core": "^2.9.3",
"@oclif/core": "^3.26.5",
"async": "^3.2.4",
"big-json": "^3.2.0",
"bluebird": "^3.7.2",
Expand Down Expand Up @@ -95,4 +95,4 @@
}
},
"repository": "https://github.com/contentstack/cli"
}
}
41 changes: 31 additions & 10 deletions packages/contentstack-branches/src/utils/entry-create-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,21 +462,42 @@ export function entryCreateScript(contentType) {
}
}

const getEntries = async (branchName, contentType, skip = 0, limit = 100, entries = []) => {
let requestObject = {
skip,
limit,
include_count: true,
};

const entriesSearchResponse = await managementAPIClient
.stack({ api_key: stackSDKInstance.api_key, branch_uid: branchName })
.contentType('${contentType}')
.entry()
.query(requestObject)
.find();

if (entriesSearchResponse?.items?.length > 0) {
skip += limit || 100;
entries = [...entries, ...entriesSearchResponse.items];
if (skip >= entriesSearchResponse.count) {
return entries;
}
return await getEntries(branchName, skip, limit, entries);
}
return entries;
};


const createEntryTask = () => {
return {
title: 'Create Entries',
successTitle: 'Entries Created Successfully',
failedTitle: 'Failed to create entries',
task: async () => {

const compareBranchEntries = await managementAPIClient
.stack({ api_key: stackSDKInstance.api_key, branch_uid: compareBranch })
.contentType('${contentType}')
.entry()
.query()
.find();
const compareBranchEntries = await getEntries(compareBranch, '${contentType}')

const compareFilteredProperties = compareBranchEntries.items.map((entry) => {
const compareFilteredProperties = compareBranchEntries.map((entry) => {
keysToRemove.map((key) => delete entry[key]);
return entry;
});
Expand All @@ -486,9 +507,9 @@ export function entryCreateScript(contentType) {
.contentType('${contentType}')
.fetch();

for (let i = 0; i < compareBranchEntries?.items?.length; i++) {
assetRefPath[compareBranchEntries.items[i].uid] = []
findAssets(contentType.schema, compareBranchEntries.items[i], assetRefPath[compareBranchEntries.items[i].uid]);
for (let i = 0; i < compareBranchEntries?.length; i++) {
assetRefPath[compareBranchEntries[i].uid] = []
findAssets(contentType.schema, compareBranchEntries[i], assetRefPath[compareBranchEntries[i].uid]);
cAssetDetails = [...new Map(cAssetDetails.map((item) => [item['uid'], item])).values()];
}
if (cAssetDetails && cAssetDetails.length) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ export function entryCreateUpdateScript(contentType) {

function converter(data) {
let arr = [];
for (const elm of data.entries()) {
for (const elm of data) {
// @ts-ignore
arr.push([elm[1].title, elm[1]]);
arr.push([elm.title, elm]);
}
return arr;
}
Expand Down Expand Up @@ -476,35 +476,54 @@ export function entryCreateUpdateScript(contentType) {
}
};

const getEntries = async (branchName, contentType, skip = 0, limit = 100, entries = []) => {
let requestObject = {
skip,
limit,
include_count: true,
};

const entriesSearchResponse = await managementAPIClient
.stack({ api_key: stackSDKInstance.api_key, branch_uid: branchName })
.contentType('${contentType}')
.entry()
.query(requestObject)
.find();

if (entriesSearchResponse?.items?.length > 0) {
skip += limit || 100;
entries = [...entries, ...entriesSearchResponse.items];
if (skip >= entriesSearchResponse.count) {
return entries;
}
return await getEntries(branchName, skip, limit, entries);
}
return entries;
};

const updateEntryTask = () => {
return {
title: 'Update Entries',
successMessage: 'Entries Updated Successfully',
failedMessage: 'Failed to update entries',
task: async () => {
let compareBranchEntries = await managementAPIClient
.stack({ api_key: stackSDKInstance.api_key, branch_uid: compareBranch })
.contentType('${contentType}')
.entry()
.query()
.find();
let compareBranchEntries = await getEntries(compareBranch, '${contentType}')

let baseBranchEntries = await stackSDKInstance.contentType('${contentType}').entry().query().find();
let baseBranchEntries = await getEntries(branch, '${contentType}');

let contentType = await managementAPIClient
.stack({ api_key: stackSDKInstance.api_key, branch_uid: compareBranch })
.contentType('${contentType}')
.fetch();

for (let i = 0; i < compareBranchEntries?.items?.length; i++) {
assetRefPath[compareBranchEntries.items[i].uid] = []
findAssets(contentType.schema, compareBranchEntries.items[i], assetRefPath[compareBranchEntries.items[i].uid]);
for (let i = 0; i < compareBranchEntries?.length; i++) {
assetRefPath[compareBranchEntries[i].uid] = []
findAssets(contentType.schema, compareBranchEntries[i], assetRefPath[compareBranchEntries[i].uid]);
}

for (let i = 0; i < baseBranchEntries?.items?.length; i++) {
assetRefPath[baseBranchEntries.items[i].uid] = []
findAssets(contentType.schema, baseBranchEntries.items[i], assetRefPath[baseBranchEntries.items[i].uid]);
assetRefPath[baseBranchEntries[i].uid] = []
findAssets(contentType.schema, baseBranchEntries[i], assetRefPath[baseBranchEntries[i].uid]);
}
assetDetails = [...new Map(assetDetails.map((item) => [item['uid'], item])).values()];
newAssetDetails = assetDetails;
Expand Down Expand Up @@ -568,12 +587,12 @@ export function entryCreateUpdateScript(contentType) {
if(entryDetails !== undefined){
entryDetails = updateAssetDetailsInEntries(entryDetails);

if (baseBranchEntries && baseBranchEntries.items.length) {
let baseEntryUid = baseBranchEntries.items[0].uid;
if (baseBranchEntries && baseBranchEntries.length) {
let baseEntryUid = baseBranchEntries[0].uid;
let entry = await stackSDKInstance.contentType('${contentType}').entry(baseEntryUid);

if (flag.references) {
await updateReferences(entryDetails, baseBranchEntries.items[0], references);
await updateReferences(entryDetails, baseBranchEntries[0], references);
}

await updateEntry(entry, entryDetails);
Expand All @@ -589,19 +608,20 @@ export function entryCreateUpdateScript(contentType) {
}
});
} else {
let compareMap = new Map(converter(compareBranchEntries.items));
let baseMap = new Map(converter(baseBranchEntries.items));
let compareMap = new Map(converter(compareBranchEntries));
let baseMap = new Map(converter(baseBranchEntries));

//NOTE: Filter distinct entries from the base and compare branches according to their titles.
//TODO: Need to discuss this approach and replace it with uid condition
let arr = uniquelyConcatenateArrays(Array.from(compareMap.keys()), Array.from(baseMap.keys()));

arr.map(async (el) => {
let entryDetails = deleteUnwantedKeysFromObject(compareMap.get(el), keysToRemove);
// Change
for(let i = 0; i < arr.length ; i++){
let entryDetails = deleteUnwantedKeysFromObject(compareMap.get(arr[i]), keysToRemove);
//NOTE: In the compare branch, entry must exist. Condition of deleted entry not handled
if(entryDetails !== undefined){
entryDetails = updateAssetDetailsInEntries(entryDetails);
if (compareMap.get(el) && !baseMap.get(el)) {
if (compareMap.get(arr[i]) && !baseMap.get(arr[i])) {
let createdEntry = await stackSDKInstance
.contentType('${contentType}')
.entry()
Expand All @@ -613,8 +633,8 @@ export function entryCreateUpdateScript(contentType) {
}
await updateEntry(createdEntry, entryDetails);
}
} else if (compareMap.get(el) && baseMap.get(el)) {
let baseEntry = baseMap.get(el);
} else if (compareMap.get(arr[i]) && baseMap.get(arr[i])) {
let baseEntry = baseMap.get(arr[i]);
let entry = await stackSDKInstance.contentType('${contentType}').entry(baseEntry.uid);

if (flag.references) {
Expand All @@ -624,7 +644,7 @@ export function entryCreateUpdateScript(contentType) {
await updateEntry(entry, entryDetails);
}
}
});
}
}
} catch (error) {
throw error;
Expand Down
62 changes: 41 additions & 21 deletions packages/contentstack-branches/src/utils/entry-update-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ export function entryUpdateScript(contentType) {

function converter(data) {
let arr = [];
for (const elm of data.entries()) {
for (const elm of data) {
// @ts-ignore
arr.push([elm[1].title, elm[1]]);
arr.push([elm.title, elm]);
}
return arr;
}
Expand Down Expand Up @@ -476,35 +476,55 @@ export function entryUpdateScript(contentType) {
}
};

const getEntries = async (branchName, contentType, skip = 0, limit = 100, entries = []) => {
let requestObject = {
skip,
limit,
include_count: true,
};

const entriesSearchResponse = await managementAPIClient
.stack({ api_key: stackSDKInstance.api_key, branch_uid: branchName })
.contentType('${contentType}')
.entry()
.query(requestObject)
.find();

if (entriesSearchResponse?.items?.length > 0) {
skip += limit || 100;
entries = [...entries, ...entriesSearchResponse.items];
if (skip >= entriesSearchResponse.count) {
return entries;
}
return await getEntries(branchName, contentType, skip, limit, entries);
}
return entries;
};

const updateEntryTask = () => {
return {
title: 'Update Entries',
successMessage: 'Entries Updated Successfully',
failedMessage: 'Failed to update entries',
task: async () => {

let compareBranchEntries = await managementAPIClient
.stack({ api_key: stackSDKInstance.api_key, branch_uid: compareBranch })
.contentType('${contentType}')
.entry()
.query()
.find();
let compareBranchEntries = await getEntries(compareBranch, '${contentType}');

let baseBranchEntries = await stackSDKInstance.contentType('${contentType}').entry().query().find();
let baseBranchEntries = await getEntries(branch, '${contentType}');

let contentType = await managementAPIClient
.stack({ api_key: stackSDKInstance.api_key, branch_uid: compareBranch })
.contentType('${contentType}')
.fetch();

for (let i = 0; i < compareBranchEntries?.items?.length; i++) {
assetRefPath[compareBranchEntries.items[i].uid] = []
findAssets(contentType.schema, compareBranchEntries.items[i], assetRefPath[compareBranchEntries.items[i].uid]);
for (let i = 0; i < compareBranchEntries?.length; i++) {
assetRefPath[compareBranchEntries[i].uid] = []
findAssets(contentType.schema, compareBranchEntries[i], assetRefPath[compareBranchEntries[i].uid]);
}

for (let i = 0; i < baseBranchEntries?.items?.length; i++) {
assetRefPath[baseBranchEntries.items[i].uid] = []
findAssets(contentType.schema, baseBranchEntries.items[i], assetRefPath[baseBranchEntries.items[i].uid]);
for (let i = 0; i < baseBranchEntries?.length; i++) {
assetRefPath[baseBranchEntries[i].uid] = []
findAssets(contentType.schema, baseBranchEntries[i], assetRefPath[baseBranchEntries[i].uid]);
}
assetDetails = [...new Map(assetDetails.map((item) => [item['uid'], item])).values()];
newAssetDetails = assetDetails;
Expand Down Expand Up @@ -561,16 +581,16 @@ export function entryUpdateScript(contentType) {

try {
if (contentType?.options?.singleton) {
compareBranchEntries?.items?.map(async (el) => {
compareBranchEntries?.map(async (el) => {
let entryDetails = deleteUnwantedKeysFromObject(el, keysToRemove);
if(entryDetails !== undefined){
entryDetails = updateAssetDetailsInEntries(entryDetails);
if (baseBranchEntries && baseBranchEntries.items.length) {
let baseEntryUid = baseBranchEntries.items[0].uid;
if (baseBranchEntries && baseBranchEntries.length) {
let baseEntryUid = baseBranchEntries[0].uid;
let entry = await stackSDKInstance.contentType('${contentType}').entry(baseEntryUid);

if (flag.references) {
await updateReferences(entryDetails, baseBranchEntries.items[0], references);
await updateReferences(entryDetails, baseBranchEntries[0], references);
}

await updateEntry(entry, entryDetails);
Expand All @@ -586,8 +606,8 @@ export function entryUpdateScript(contentType) {
}
});
} else {
let compareMap = new Map(converter(compareBranchEntries.items));
let baseMap = new Map(converter(baseBranchEntries.items));
let compareMap = new Map(converter(compareBranchEntries));
let baseMap = new Map(converter(baseBranchEntries));

//NOTE: Filter distinct entries from the base and compare branches according to their titles.
//TODO: Need to discuss this approach and replace it with uid approach
Expand Down
6 changes: 3 additions & 3 deletions packages/contentstack/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@contentstack/cli",
"description": "Command-line tool (CLI) to interact with Contentstack",
"version": "1.18.2",
"version": "1.19.0",
"author": "Contentstack",
"bin": {
"csdx": "./bin/run.js"
Expand All @@ -25,7 +25,7 @@
"@contentstack/cli-audit": "~1.6.2",
"@contentstack/cli-auth": "~1.3.18",
"@contentstack/cli-cm-bootstrap": "~1.9.3",
"@contentstack/cli-cm-branches": "~1.0.26",
"@contentstack/cli-cm-branches": "~1.1.0",
"@contentstack/cli-cm-bulk-publish": "~1.4.7",
"@contentstack/cli-cm-export": "~1.11.4",
"@contentstack/cli-cm-clone": "~1.10.5",
Expand Down Expand Up @@ -160,4 +160,4 @@
}
},
"repository": "https://github.com/contentstack/cli"
}
}
Loading
Loading