Skip to content
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
6 changes: 5 additions & 1 deletion .talismanrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
fileignoreconfig:
- filename: package-lock.json
checksum: 87b205a27082d15199ba291550e9ca3e8c784fe5bade7fcb89bf2acb1e850b23
checksum: 16a20411514058d2030b6240573078fad7640b745545a0463e9b70985f92425c
- filename: pnpm-lock.yaml
checksum: d28afee9b463d66004354b9476b1e39344f903e2db3a15c5f83bd84e688c01d6
- filename: packages/contentstack-import-setup/test/unit/backup-handler.test.ts
Expand Down Expand Up @@ -57,4 +57,8 @@ fileignoreconfig:
checksum: f93aa9b0c964608b60c88d4c72ff33840b58ec900297c4bae1f4ea365aa51048
- filename: packages/contentstack-auth/test/utils/mfa-handler.test.ts
checksum: b067f93cf0185d794e8419cc41e8fac96ed790dea8fc48dc083ee242ccacbd4d
- filename: packages/contentstack-utilities/src/logger/logger.ts
checksum: 76429bc87e279624b386f00e7eb3f4ec25621ace7056289f812b9a076d6e184e
- filename: packages/contentstack-import/src/import/modules/entries.ts
checksum: 2fd4e8ecf75e077632a6408d09997f0921d2a3508f9f2cb8f47fe79a28592300
version: "1.0"
827 changes: 388 additions & 439 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/contentstack-import/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ const config: DefaultConfig = {
getEncryptionKeyMaxRetry: 3,
// useBackedupDir: '',
// backupConcurrency: 10,
onlyTSModules: ['taxonomies', 'personalize', 'variant-entries'],
onlyTSModules: ['taxonomies', 'personalize', 'variant-entries', 'stack'],
auditConfig: {
noLog: false, // Skip logs printing on terminal
skipConfirm: true, // Skip confirmation if any
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export default class ContentTypesImport extends BaseClass {
log.debug('Created content types mapper directory', this.importConfig.context);

this.installedExtensions = (
((await fsUtil.readFile(this.marketplaceAppMapperPath)) as any) || { extension_uid: {} }
(fsUtil.readFile(this.marketplaceAppMapperPath) as any) || { extension_uid: {} }
).extension_uid;
log.debug(
`Loaded ${Object.keys(this.installedExtensions)?.length} installed extensions`,
Expand Down
300 changes: 212 additions & 88 deletions packages/contentstack-import/src/import/modules/entries.ts

Large diffs are not rendered by default.

22 changes: 12 additions & 10 deletions packages/contentstack-import/src/utils/asset-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const uploadAssetHelper = function (config: ImportConfig, req: any, fsPat
return new Bluebird(function (resolve, reject) {
try {
log.debug(`Starting asset upload helper for path: ${fsPath}`);

managementSDKClient(config)
.then((APIClient: ContentstackClient) => {
validate(req);
Expand All @@ -34,13 +34,13 @@ export const uploadAssetHelper = function (config: ImportConfig, req: any, fsPat
}

log.debug(`Uploading asset (attempt ${RETRY}/${MAX_RETRY_LIMIT}): ${fsPath}`);

req.upload = fsPath;
const stackAPIClient = APIClient.stack({
api_key: config.target_stack,
management_token: config.management_token,
});

stackAPIClient
.asset()
.create(req)
Expand Down Expand Up @@ -81,9 +81,9 @@ export const lookupAssets = function (
) {
throw new Error('Invalid inputs for lookupAssets!');
}

log.debug(`Starting asset lookup for entry: ${data.entry?.uid}, content type: ${data.content_type?.uid}`);

let parent: string[] = [];
let assetUids: string[] = [];
let assetUrls: string[] = [];
Expand Down Expand Up @@ -138,7 +138,9 @@ export const lookupAssets = function (
findAssetIdsFromJsonCustomFields(data.entry, data.content_type.schema);
} else if (schema[i].data_type === 'json' && schema[i].field_metadata.extension) {
if (installedExtensions && installedExtensions[schema[i].extension_uid]) {
log.debug(`Mapping extension UID: ${schema[i].extension_uid} to ${installedExtensions[schema[i].extension_uid]}`);
log.debug(
`Mapping extension UID: ${schema[i].extension_uid} to ${installedExtensions[schema[i].extension_uid]}`,
);
schema[i].extension_uid = installedExtensions[schema[i].extension_uid];
}
}
Expand Down Expand Up @@ -267,9 +269,9 @@ export const lookupAssets = function (
updateFileFields(data.entry, data, null, mappedAssetUids, matchedUids, unmatchedUids, mappedAssetUrls);
assetUids = _.uniq(assetUids);
assetUrls = _.uniq(assetUrls);

log.debug(`Found ${assetUids.length} unique asset UIDs and ${assetUrls.length} unique asset URLs`);

let entry = JSON.stringify(data.entry);

assetUrls.forEach(function (assetUrl: any) {
Expand Down Expand Up @@ -310,7 +312,7 @@ export const lookupAssets = function (
}

if (unmatchedUids.length) {
log.warn(`Found ${unmatchedUids.length} unmatched asset UIDs`);
log.debug(`Found ${unmatchedUids.length} unmatched asset UIDs`);
let unmatchedAssetUids = helper.readFileSync(path.join(assetUidMapperPath, 'unmatched-asset-uids.json'));
unmatchedAssetUids = unmatchedAssetUids || {};
if (unmatchedAssetUids.hasOwnProperty(data.content_type.uid)) {
Expand All @@ -324,7 +326,7 @@ export const lookupAssets = function (
}

if (unmatchedUrls.length) {
log.warn(`Found ${unmatchedUrls.length} unmatched asset URLs`);
log.debug(`Found ${unmatchedUrls.length} unmatched asset URLs`);
let unmatchedAssetUrls = helper.readFileSync(path.join(assetUidMapperPath, 'unmatched-asset-urls.json'));
unmatchedAssetUrls = unmatchedAssetUrls || {};
if (unmatchedAssetUrls.hasOwnProperty(data.content_type.uid)) {
Expand Down
33 changes: 16 additions & 17 deletions packages/contentstack-import/src/utils/content-type-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const schemaTemplate = {

export const suppressSchemaReference = function (schema: any, flag: any) {
log.debug('Starting schema reference suppression process');

for (var i in schema) {
if (schema[i].data_type === 'group' || schema[i].data_type === 'global_field') {
log.debug(`Processing ${schema[i].data_type} field: ${schema[i].uid}`);
Expand Down Expand Up @@ -95,7 +95,7 @@ export const suppressSchemaReference = function (schema: any, flag: any) {
}
}
}

log.debug('Schema reference suppression completed');
};

Expand All @@ -105,7 +105,7 @@ export const removeReferenceFields = async function (
stackAPIClient: any,
): Promise<boolean | void> {
log.debug('Starting reference field removal process');

if (schema?.length) {
for (let i = 0; i < schema.length; i++) {
if (schema[i].data_type === 'group') {
Expand All @@ -119,11 +119,11 @@ export const removeReferenceFields = async function (
} else if (schema[i].data_type === 'reference') {
log.debug(`Processing reference field: ${schema[i].uid}`);
flag.supressed = true;

// Check if content-type exists
// If exists, then no change should be required.
let isContentTypeError = false;

for (let j = 0; j < schema[i].reference_to.length; j++) {
try {
log.debug(`Checking if content type exists: ${schema[i].reference_to[j]}`);
Expand All @@ -132,16 +132,15 @@ export const removeReferenceFields = async function (
} catch (error) {
// Else warn and modify the schema object.
isContentTypeError = true;
log.warn(`Content type does not exist: ${schema[i].reference_to[j]}`);
console.warn(`Content-type ${schema[i].reference_to[j]} does not exist. Removing the field from schema`);
log.warn(`Content-type ${schema[i].reference_to[j]} does not exist. Removing the field from schema`);
}
}

if (isContentTypeError) {
log.debug(`Removing reference field due to missing content types: ${schema[i].uid}`);
schema.splice(i, 1);
--i;

if (schema.length < 1) {
log.debug('Adding dummy field to prevent empty schema');
schema.push({
Expand Down Expand Up @@ -197,45 +196,45 @@ export const removeReferenceFields = async function (
}
}
}

log.debug('Reference field removal process completed');
};

export const updateFieldRules = function (contentType: any) {
log.debug(`Starting field rules update for content type: ${contentType.uid}`);

const fieldDataTypeMap: { [key: string]: string } = {};
for (let i = 0; i < contentType.schema.length; i++) {
const field = contentType.schema[i];
fieldDataTypeMap[field.uid] = field.data_type;
}

log.debug(`Created field data type mapping for ${Object.keys(fieldDataTypeMap).length} fields`);

const fieldRules = [...contentType.field_rules];
let len = fieldRules.length;
let removedRules = 0;

// Looping backwards as we need to delete elements as we move.
for (let i = len - 1; i >= 0; i--) {
const conditions = fieldRules[i].conditions;
let isReference = false;

for (let j = 0; j < conditions.length; j++) {
const field = conditions[j].operand_field;
if (fieldDataTypeMap[field] === 'reference') {
log.debug(`Found reference field in rule condition: ${field}`);
isReference = true;
}
}

if (isReference) {
log.debug(`Removing field rule with reference condition`);
fieldRules.splice(i, 1);
removedRules++;
}
}

log.debug(`Field rules update completed. Removed ${removedRules} rules with reference conditions`);
return fieldRules;
};
20 changes: 10 additions & 10 deletions packages/contentstack-import/src/utils/entries-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const lookupEntries = function (
uidMapperPath: string,
) {
log.debug(`Starting entry lookup for entry: ${data.entry?.uid}, content type: ${data.content_type?.uid}`);

let parent: string[] = [];
let uids: string[] = [];
let unmapped: string[] = [];
Expand Down Expand Up @@ -162,7 +162,7 @@ export const lookupEntries = function (

function findEntryIdsFromJsonRte(entry: any, ctSchema: any = []) {
log.debug('Processing JSON RTE fields for entry references');

for (const element of ctSchema) {
switch (element.data_type) {
case 'blocks': {
Expand Down Expand Up @@ -211,9 +211,9 @@ export const lookupEntries = function (
log.debug('Processing new reference field format');
findUidsInNewRefFields(data.entry, uids);
}

uids = _.flattenDeep(uids);

// if no references are found, return
if (uids.length === 0) {
log.debug('No entry references found');
Expand All @@ -222,9 +222,9 @@ export const lookupEntries = function (

uids = _.uniq(uids);
log.debug(`Found ${uids.length} unique entry references`);

let entry = JSON.stringify(data.entry);

uids?.forEach(function (uid: any) {
if (mappedUids.hasOwnProperty(uid)) {
const sanitizedUid = escapeRegExp(uid);
Expand All @@ -243,7 +243,7 @@ export const lookupEntries = function (
});

if (unmapped.length > 0) {
log.warn(`Found ${unmapped.length} unmapped entry references`);
log.debug(`Found ${unmapped.length} unmapped entry references`);
let unmappedUids = fileHelper.readFileSync(path.join(uidMapperPath, 'unmapped-uids.json'));
unmappedUids = unmappedUids || {};
if (unmappedUids.hasOwnProperty(data.content_type.uid)) {
Expand Down Expand Up @@ -299,7 +299,7 @@ export const removeUidsFromJsonRteFields = (
ctSchema: Record<string, any>[] = [],
): Record<string, any> => {
log.debug('Removing UIDs from JSON RTE fields');

for (const element of ctSchema) {
switch (element.data_type) {
case 'blocks': {
Expand Down Expand Up @@ -332,7 +332,7 @@ export const removeUidsFromJsonRteFields = (
case 'json': {
if (entry[element.uid] && element?.field_metadata?.rich_text_type) {
log.debug(`Processing JSON RTE field for UID removal: ${element.uid}`);

if (element.multiple) {
entry[element.uid] = entry[element.uid].map((jsonRteData: any) => {
delete jsonRteData.uid; // remove uid
Expand Down Expand Up @@ -363,7 +363,7 @@ export const removeUidsFromJsonRteFields = (
}
}
}

log.debug('Completed removing UIDs from JSON RTE fields');
return entry;
};
Expand Down
6 changes: 3 additions & 3 deletions packages/contentstack-import/src/utils/extension-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export const lookupExtension = function (
log.debug(`Mapping global field reference: ${global_fields_key_value} -> ${mappedValue}`);
schema[i].reference_to = global_fields_data[global_fields_key_value];
} else {
log.warn(`No mapping found for global field: ${global_fields_key_value}`);
log.debug(`No mapping found for global field: ${global_fields_key_value}`);
}
} else if (schema[i].hasOwnProperty('extension_uid')) {
log.debug(`Processing field with extension UID: ${schema[i].uid}`);
Expand All @@ -93,7 +93,7 @@ export const lookupExtension = function (
log.debug(`Using installed extension mapping: ${schema[i].extension_uid}`);
schema[i].extension_uid = installedExtensions[schema[i].extension_uid];
} else {
log.warn(`No mapping found for extension: ${extension_key_value}`);
log.debug(`No mapping found for extension: ${extension_key_value}`);
}
}
} else if (schema[i].data_type === 'json' && schema[i].hasOwnProperty('plugins') && schema[i].plugins.length > 0) {
Expand All @@ -116,7 +116,7 @@ export const lookupExtension = function (
log.debug(`Mapping marketplace app extension: ${extension_key_value} -> ${mappedMarketplaceExt}`);
newPluginUidsArray.push(marketPlaceAppsData.extension_uid[extension_key_value]);
} else {
log.warn(`No mapping found for plugin extension: ${extension_key_value}`);
log.debug(`No mapping found for plugin extension: ${extension_key_value}`);
}
});

Expand Down
16 changes: 10 additions & 6 deletions packages/contentstack-utilities/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export const formatError = function (error: any) {
}

// Append detailed error information if available
if (parsedError.errors && Object.keys(parsedError.errors).length > 0) {
if (parsedError.errors && typeof parsedError.errors === 'object' && Object.keys(parsedError.errors).length > 0) {
const entityNames: { [key: string]: string } = {
authorization: 'Authentication',
api_key: 'Stack API key',
Expand All @@ -184,11 +184,15 @@ export const formatError = function (error: any) {
access_token: 'Delivery Token',
};

message +=
' ' +
Object.entries(parsedError.errors)
.map(([key, value]) => `${entityNames[key] || key} ${value}`)
.join(' ');
const errorList = Object.entries(parsedError.errors)
.map(([field, errors]) => {
const errorArray = Array.isArray(errors) ? errors : [errors];
const fieldName = entityNames[field] || field;
return ` • ${fieldName}: ${errorArray.join(', ')}`;
})
.join('\n');

message += `\n\nAPI Errors:\n${errorList}`;
}

return message;
Expand Down
Loading
Loading