From aa797307f385910a15495e8d6070741bdb15653c Mon Sep 17 00:00:00 2001 From: Aman Kumar Date: Fri, 8 Dec 2023 15:46:52 +0530 Subject: [PATCH] fix: handle entry deleted from compare branch condition --- package-lock.json | 6 +- packages/contentstack-branches/README.md | 2 +- packages/contentstack-branches/package.json | 2 +- .../src/utils/entry-create-script.ts | 18 ++-- .../src/utils/entry-create-update-script.ts | 88 +++++++++++-------- .../src/utils/entry-update-script.ts | 86 ++++++++++-------- packages/contentstack/package.json | 2 +- pnpm-lock.yaml | 2 +- 8 files changed, 118 insertions(+), 88 deletions(-) diff --git a/package-lock.json b/package-lock.json index 9db7a05ea8..c47c19a051 100644 --- a/package-lock.json +++ b/package-lock.json @@ -22770,7 +22770,7 @@ "@contentstack/cli-audit": "~1.3.0", "@contentstack/cli-auth": "~1.3.17", "@contentstack/cli-cm-bootstrap": "~1.7.0", - "@contentstack/cli-cm-branches": "~1.0.18", + "@contentstack/cli-cm-branches": "~1.0.19", "@contentstack/cli-cm-bulk-publish": "~1.3.15", "@contentstack/cli-cm-clone": "~1.8.0", "@contentstack/cli-cm-export": "~1.10.2", @@ -23652,7 +23652,7 @@ }, "packages/contentstack-branches": { "name": "@contentstack/cli-cm-branches", - "version": "1.0.18", + "version": "1.0.19", "license": "MIT", "dependencies": { "@contentstack/cli-command": "~1.2.16", @@ -27200,7 +27200,7 @@ "@contentstack/cli-audit": "~1.3.0", "@contentstack/cli-auth": "~1.3.17", "@contentstack/cli-cm-bootstrap": "~1.7.0", - "@contentstack/cli-cm-branches": "~1.0.18", + "@contentstack/cli-cm-branches": "~1.0.19", "@contentstack/cli-cm-bulk-publish": "~1.3.15", "@contentstack/cli-cm-clone": "~1.8.0", "@contentstack/cli-cm-export": "~1.10.2", diff --git a/packages/contentstack-branches/README.md b/packages/contentstack-branches/README.md index 0a929cc3a2..2cdc9f6ddf 100755 --- a/packages/contentstack-branches/README.md +++ b/packages/contentstack-branches/README.md @@ -37,7 +37,7 @@ $ npm install -g @contentstack/cli-cm-branches $ csdx COMMAND running command... $ csdx (--version) -@contentstack/cli-cm-branches/1.0.18 darwin-arm64 node-v20.8.0 +@contentstack/cli-cm-branches/1.0.19 darwin-arm64 node-v20.8.0 $ csdx --help [COMMAND] USAGE $ csdx COMMAND diff --git a/packages/contentstack-branches/package.json b/packages/contentstack-branches/package.json index 63351ba072..f01124f31d 100644 --- a/packages/contentstack-branches/package.json +++ b/packages/contentstack-branches/package.json @@ -1,7 +1,7 @@ { "name": "@contentstack/cli-cm-branches", "description": "Contentstack CLI plugin to do branches operations", - "version": "1.0.18", + "version": "1.0.19", "author": "Contentstack", "bugs": "https://github.com/contentstack/cli/issues", "dependencies": { diff --git a/packages/contentstack-branches/src/utils/entry-create-script.ts b/packages/contentstack-branches/src/utils/entry-create-script.ts index 435bb4a372..e40f18cb16 100644 --- a/packages/contentstack-branches/src/utils/entry-create-script.ts +++ b/packages/contentstack-branches/src/utils/entry-create-script.ts @@ -475,15 +475,17 @@ export function entryCreateScript(contentType) { try { compareFilteredProperties.length !== 0 && compareFilteredProperties.forEach(async (entryDetails) => { - entryDetails = updateAssetDetailsInEntries(entryDetails); - let createdEntry = await stackSDKInstance.contentType('${contentType}').entry().create({ entry: entryDetails }).catch(error => { - throw error; - }); - if(createdEntry){ - if (flag.references) { - await updateReferences(entryDetails, createdEntry, references); + if(entryDetails !== undefined){ + entryDetails = updateAssetDetailsInEntries(entryDetails); + let createdEntry = await stackSDKInstance.contentType('${contentType}').entry().create({ entry: entryDetails }).catch(error => { + throw error; + }); + if(createdEntry){ + if (flag.references) { + await updateReferences(entryDetails, createdEntry, references); + } + await updateEntry(createdEntry, entryDetails); } - await updateEntry(createdEntry, entryDetails); } }); } catch (error) { diff --git a/packages/contentstack-branches/src/utils/entry-create-update-script.ts b/packages/contentstack-branches/src/utils/entry-create-update-script.ts index c3661024e1..194a218aad 100644 --- a/packages/contentstack-branches/src/utils/entry-create-update-script.ts +++ b/packages/contentstack-branches/src/utils/entry-create-update-script.ts @@ -494,56 +494,70 @@ export function entryCreateUpdateScript(contentType) { } try { - if (contentType.options.singleton) { - compareBranchEntries.items.map(async (el) => { + if (contentType?.options?.singleton) { + compareBranchEntries?.items?.map(async (el) => { let entryDetails = deleteUnwantedKeysFromObject(el, keysToRemove); - entryDetails = updateAssetDetailsInEntries(entryDetails); - - if (baseBranchEntries && baseBranchEntries.items.length) { - let baseEntryUid = baseBranchEntries.items[0].uid; - let entry = await stackSDKInstance.contentType('${contentType}').entry(baseEntryUid); + if(entryDetails !== undefined){ + entryDetails = updateAssetDetailsInEntries(entryDetails); + + if (baseBranchEntries && baseBranchEntries.items.length) { + let baseEntryUid = baseBranchEntries.items[0].uid; + let entry = await stackSDKInstance.contentType('${contentType}').entry(baseEntryUid); + + if (flag.references) { + await updateReferences(entryDetails, baseBranchEntries.items[0], references); + } + + await updateEntry(entry, entryDetails); + } else { + let createdEntry = await stackSDKInstance.contentType('${contentType}').entry().create({ entry: entryDetails }); - if (flag.references) { - await updateReferences(entryDetails, baseBranchEntries.items[0], references); - } - - await updateEntry(entry, entryDetails); - } else { - let createdEntry = await stackSDKInstance.contentType('${contentType}').entry().create({ entry: entryDetails }); - - if (flag.references) { - await updateReferences(entryDetails, createdEntry, references); + if (flag.references) { + await updateReferences(entryDetails, createdEntry, references); + } + + await updateEntry(createdEntry, entryDetails); } - - await updateEntry(createdEntry, entryDetails); } }); } else { let compareMap = new Map(converter(compareBranchEntries.items)); let baseMap = new Map(converter(baseBranchEntries.items)); - + + //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); - entryDetails = updateAssetDetailsInEntries(entryDetails); - if (compareMap.get(el) && !baseMap.get(el)) { - let createdEntry = await stackSDKInstance.contentType('${contentType}').entry().create({ entry: entryDetails }); - - if (flag.references) { - await updateReferences(entryDetails, createdEntry, references); - } + //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)) { + let createdEntry = await stackSDKInstance + .contentType('${contentType}') + .entry() + .create({ entry: entryDetails }) + .catch(err => { + (err?.errorMessage || err?.message) ? err?.errorMessage || err?.message : 'Something went wrong!' + }) + + if(createdEntry){ + if (flag.references) { + await updateReferences(entryDetails, createdEntry, references); + } + await updateEntry(createdEntry, entryDetails); + } + } else if (compareMap.get(el) && baseMap.get(el)) { + let baseEntry = baseMap.get(el); + let entry = await stackSDKInstance.contentType('${contentType}').entry(baseEntry.uid); + + if (flag.references) { + await updateReferences(entryDetails, baseEntry, references); + } - await updateEntry(createdEntry, entryDetails); - } else if (compareMap.get(el) && baseMap.get(el)) { - let baseEntry = baseMap.get(el); - let entry = await stackSDKInstance.contentType('${contentType}').entry(baseEntry.uid); - - if (flag.references) { - await updateReferences(entryDetails, baseEntry, references); + await updateEntry(entry, entryDetails); } - - await updateEntry(entry, entryDetails); } }); } diff --git a/packages/contentstack-branches/src/utils/entry-update-script.ts b/packages/contentstack-branches/src/utils/entry-update-script.ts index d6021f5fe4..19b6549bda 100644 --- a/packages/contentstack-branches/src/utils/entry-update-script.ts +++ b/packages/contentstack-branches/src/utils/entry-update-script.ts @@ -491,56 +491,70 @@ export function entryUpdateScript(contentType) { } try { - if (contentType.options.singleton) { - compareBranchEntries.items.map(async (el) => { + if (contentType?.options?.singleton) { + compareBranchEntries?.items?.map(async (el) => { let entryDetails = deleteUnwantedKeysFromObject(el, keysToRemove); - entryDetails = updateAssetDetailsInEntries(entryDetails); - - if (baseBranchEntries && baseBranchEntries.items.length) { - let baseEntryUid = baseBranchEntries.items[0].uid; - let entry = await stackSDKInstance.contentType('${contentType}').entry(baseEntryUid); + if(entryDetails !== undefined){ + entryDetails = updateAssetDetailsInEntries(entryDetails); + if (baseBranchEntries && baseBranchEntries.items.length) { + let baseEntryUid = baseBranchEntries.items[0].uid; + let entry = await stackSDKInstance.contentType('${contentType}').entry(baseEntryUid); + + if (flag.references) { + await updateReferences(entryDetails, baseBranchEntries.items[0], references); + } + + await updateEntry(entry, entryDetails); + } else { + let createdEntry = await stackSDKInstance.contentType('${contentType}').entry().create({ entry: entryDetails }); - if (flag.references) { - await updateReferences(entryDetails, baseBranchEntries.items[0], references); + if (flag.references) { + await updateReferences(entryDetails, createdEntry, references); + } + + await updateEntry(createdEntry, entryDetails); } - - await updateEntry(entry, entryDetails); - } else { - let createdEntry = await stackSDKInstance.contentType('${contentType}').entry().create({ entry: entryDetails }); - - if (flag.references) { - await updateReferences(entryDetails, createdEntry, references); - } - - await updateEntry(createdEntry, entryDetails); } }); } else { let compareMap = new Map(converter(compareBranchEntries.items)); let baseMap = new Map(converter(baseBranchEntries.items)); - + + //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 let arr = uniquelyConcatenateArrays(Array.from(compareMap.keys()), Array.from(baseMap.keys())); arr.map(async (el) => { let entryDetails = deleteUnwantedKeysFromObject(compareMap.get(el), keysToRemove); - entryDetails = updateAssetDetailsInEntries(entryDetails); - if (compareMap.get(el) && !baseMap.get(el)) { - let createdEntry = await stackSDKInstance.contentType('${contentType}').entry().create({ entry: entryDetails }); - - if (flag.references) { - await updateReferences(entryDetails, createdEntry, references); - } - - await updateEntry(createdEntry, entryDetails); - } else if (compareMap.get(el) && baseMap.get(el)) { - let baseEntry = baseMap.get(el); - let entry = await stackSDKInstance.contentType('${contentType}').entry(baseEntry.uid); + //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)) { + let createdEntry = await stackSDKInstance + .contentType('${contentType}') + .entry() + .create({ entry: entryDetails }) + .catch(err => { + (err?.errorMessage || err?.message) ? err?.errorMessage || err?.message : 'Something went wrong!' + }) - if (flag.references) { - await updateReferences(entryDetails, baseEntry, references); + if(createdEntry){ + if (flag.references) { + await updateReferences(entryDetails, createdEntry, references); + } + + await updateEntry(createdEntry, entryDetails); + } + } else if (compareMap.get(el) && baseMap.get(el)) { + let baseEntry = baseMap.get(el); + let entry = await stackSDKInstance.contentType('${contentType}').entry(baseEntry.uid); + + if (flag.references) { + await updateReferences(entryDetails, baseEntry, references); + } + + await updateEntry(entry, entryDetails); } - - await updateEntry(entry, entryDetails); } }); } diff --git a/packages/contentstack/package.json b/packages/contentstack/package.json index 848805a9e9..ab644a3170 100755 --- a/packages/contentstack/package.json +++ b/packages/contentstack/package.json @@ -25,7 +25,7 @@ "@contentstack/cli-audit": "~1.3.0", "@contentstack/cli-auth": "~1.3.17", "@contentstack/cli-cm-bootstrap": "~1.7.0", - "@contentstack/cli-cm-branches": "~1.0.18", + "@contentstack/cli-cm-branches": "~1.0.19", "@contentstack/cli-cm-bulk-publish": "~1.3.15", "@contentstack/cli-cm-clone": "~1.8.0", "@contentstack/cli-cm-export": "~1.10.2", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index c0d5503304..101888e0ea 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -13,7 +13,7 @@ importers: '@contentstack/cli-audit': ~1.3.0 '@contentstack/cli-auth': ~1.3.17 '@contentstack/cli-cm-bootstrap': ~1.7.0 - '@contentstack/cli-cm-branches': ~1.0.18 + '@contentstack/cli-cm-branches': ~1.0.19 '@contentstack/cli-cm-bulk-publish': ~1.3.15 '@contentstack/cli-cm-clone': ~1.8.0 '@contentstack/cli-cm-export': ~1.10.2