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
2 changes: 1 addition & 1 deletion packages/contentstack-audit/src/messages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const auditMsg = {
SCAN_ASSET_SUCCESS_MSG: `Successfully completed the scanning of Asset with UID '{uid}'.`,
SCAN_ASSET_WARN_MSG: `The locale '{locale}' or environment '{environment}' are not present for asset with uid '{uid}'`,
ENTRY_PUBLISH_DETAILS: `Removing the publish detials for entry '{uid}' of ct '{ctuid}' in locale '{locale}' as locale '{publocale}' or environment '{environment}' does not exist`,
CT_REFERENCE_FIELD: `The mentioned Reference Field is not Array field name 'reference_to' having display name 'display_name'`,
CT_REFERENCE_FIELD: `The mentioned Reference field is not Array field reference is '{reference_to}' having display name '{display_name}''`,
ASSET_NOT_EXIST: `The publish_details either does not exist or is not an array for asset uid '{uid}'`,
ENTRY_PUBLISH_DETAILS_NOT_EXIST: `The publish_details either does not exist or is not an array for entry uid '{uid}'`,
};
Expand Down
128 changes: 77 additions & 51 deletions packages/contentstack-audit/src/modules/content-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,20 @@ export default class ContentType {
this.gfSchema = gfSchema;
this.moduleName = this.validateModules(moduleName!, this.config.moduleConfig);
this.fileName = config.moduleConfig[this.moduleName].fileName;
this.folderPath = resolve(sanitizePath(config.basePath), sanitizePath(config.moduleConfig[this.moduleName].dirName));
this.folderPath = resolve(
sanitizePath(config.basePath),
sanitizePath(config.moduleConfig[this.moduleName].dirName),
);
}

validateModules(moduleName: keyof typeof auditConfig.moduleConfig, moduleConfig: Record<string, unknown>): keyof typeof auditConfig.moduleConfig {
validateModules(
moduleName: keyof typeof auditConfig.moduleConfig,
moduleConfig: Record<string, unknown>,
): keyof typeof auditConfig.moduleConfig {
if (Object.keys(moduleConfig).includes(moduleName)) {
return moduleName;
}
return 'content-types'
return 'content-types';
}
/**
* The `run` function checks if a folder path exists, sets the schema based on the module name,
Expand Down Expand Up @@ -121,7 +127,7 @@ export default class ContentType {
if (existsSync(extensionPath)) {
try {
this.extensions = Object.keys(JSON.parse(readFileSync(extensionPath, 'utf8')));
} catch (error) { }
} catch (error) {}
}

if (existsSync(marketplacePath)) {
Expand All @@ -134,7 +140,7 @@ export default class ContentType {
) as string[];
this.extensions.push(...metaData);
}
} catch (error) { }
} catch (error) {}
}
}

Expand Down Expand Up @@ -270,19 +276,19 @@ export default class ContentType {

return missingRefs.length
? [
{
tree,
data_type,
missingRefs,
display_name,
ct_uid: this.currentUid,
name: this.currentTitle,
treeStr: tree
.map(({ name }) => name)
.filter((val) => val)
.join(' ➜ '),
},
]
{
tree,
data_type,
missingRefs,
display_name,
ct_uid: this.currentUid,
name: this.currentTitle,
treeStr: tree
.map(({ name }) => name)
.filter((val) => val)
.join(' ➜ '),
},
]
: [];
}

Expand Down Expand Up @@ -383,37 +389,46 @@ export default class ContentType {
const missingRefs: string[] = [];
let { reference_to, display_name, data_type } = field;

if(!Array.isArray(reference_to)) {
this.log($t(auditMsg.CT_REFERENCE_FIELD, { reference_to, data_type, display_name }), { color: 'green' });
}
for (const reference of reference_to ?? []) {
// NOTE Can skip specific references keys (Ex, system defined keys can be skipped)
if (this.config.skipRefs.includes(reference)) {
continue;
if (!Array.isArray(reference_to)) {
this.log($t(auditMsg.CT_REFERENCE_FIELD, { reference_to, data_type, display_name }), 'error');
this.log($t(auditMsg.CT_REFERENCE_FIELD, { reference_to, display_name }), 'info');
if (!this.config.skipRefs.includes(reference_to)) {
const refExist = find(this.ctSchema, { uid: reference_to });

if (!refExist) {
missingRefs.push(reference_to);
}
}
} else {
for (const reference of reference_to ?? []) {
// NOTE Can skip specific references keys (Ex, system defined keys can be skipped)
if (this.config.skipRefs.includes(reference)) {
continue;
}

const refExist = find(this.ctSchema, { uid: reference });
const refExist = find(this.ctSchema, { uid: reference });

if (!refExist) {
missingRefs.push(reference);
if (!refExist) {
missingRefs.push(reference);
}
}
}

return missingRefs.length
? [
{
tree,
data_type,
missingRefs,
display_name,
ct_uid: this.currentUid,
name: this.currentTitle,
treeStr: tree
.map(({ name }) => name)
.filter((val) => val)
.join(' ➜ '),
},
]
{
tree,
data_type,
missingRefs,
display_name,
ct_uid: this.currentUid,
name: this.currentTitle,
treeStr: tree
.map(({ name }) => name)
.filter((val) => val)
.join(' ➜ '),
},
]
: [];
}

Expand Down Expand Up @@ -638,19 +653,30 @@ export default class ContentType {
let fixStatus;
const missingRefs: string[] = [];
const { reference_to, data_type, display_name } = field;
if(!Array.isArray(reference_to)) {
this.log($t(auditMsg.CT_REFERENCE_FIELD, { reference_to, data_type, display_name }), { color: 'green' });
}
for (const reference of reference_to ?? []) {
// NOTE Can skip specific references keys (Ex, system defined keys can be skipped)
if (this.config.skipRefs.includes(reference)) {
continue;
if (!Array.isArray(reference_to)) {
this.log($t(auditMsg.CT_REFERENCE_FIELD, { reference_to, display_name }), 'error');
this.log($t(auditMsg.CT_REFERENCE_FIELD, { reference_to, display_name }), 'info');
if (!this.config.skipRefs.includes(reference_to)) {
const refExist = find(this.ctSchema, { uid: reference_to });

if (!refExist) {
missingRefs.push(reference_to);
}
}

const refExist = find(this.ctSchema, { uid: reference });
field.reference_to = [reference_to];
} else {
for (const reference of reference_to ?? []) {
// NOTE Can skip specific references keys (Ex, system defined keys can be skipped)
if (this.config.skipRefs.includes(reference)) {
continue;
}

if (!refExist) {
missingRefs.push(reference);
const refExist = find(this.ctSchema, { uid: reference });

if (!refExist) {
missingRefs.push(reference);
}
}
}

Expand Down
79 changes: 57 additions & 22 deletions packages/contentstack-audit/src/modules/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export default class Entries {

const localKey = this.locales.map((locale: any) => locale.code);

if(this.entries[entryUid]?.publish_details && !Array.isArray(this.entries[entryUid].publish_details)) {
if (this.entries[entryUid]?.publish_details && !Array.isArray(this.entries[entryUid].publish_details)) {
this.log($t(auditMsg.ENTRY_PUBLISH_DETAILS_NOT_EXIST, { uid: entryUid }), { color: 'red' });
}

Expand All @@ -194,11 +194,23 @@ export default class Entries {
{ color: 'red' },
);
if (!Object.keys(this.missingEnvLocale).includes(entryUid)) {
this.missingEnvLocale[entryUid] = [{ entry_uid: entryUid, publish_locale: pd.locale, publish_environment: pd.environment, ctUid: ctSchema.uid, ctLocale: code }];
this.missingEnvLocale[entryUid] = [
{
entry_uid: entryUid,
publish_locale: pd.locale,
publish_environment: pd.environment,
ctUid: ctSchema.uid,
ctLocale: code,
},
];
} else {
this.missingEnvLocale[entryUid].push(
{ entry_uid: entryUid, publish_locale: pd.locale, publish_environment: pd.environment, ctUid: ctSchema.uid, ctLocale: code },
);
this.missingEnvLocale[entryUid].push({
entry_uid: entryUid,
publish_locale: pd.locale,
publish_environment: pd.environment,
ctUid: ctSchema.uid,
ctLocale: code,
});
}
return false;
}
Expand Down Expand Up @@ -332,7 +344,6 @@ export default class Entries {
field: ContentTypeStruct | GlobalFieldDataType | ModularBlockType | GroupFieldDataType,
entry: EntryFieldType,
) {

if (this.fix) {
entry = this.runFixOnSchema(tree, field.schema as ContentTypeSchemaType[], entry);
}
Expand Down Expand Up @@ -631,18 +642,29 @@ export default class Entries {
if (this.fix) return [];

const missingRefs: Record<string, any>[] = [];
const { uid: data_type, display_name } = fieldStructure;
const { uid: data_type, display_name, reference_to } = fieldStructure;

for (const index in field ?? []) {
const reference = field[index];
const reference: any = field[index];
const { uid } = reference;
if (!uid && reference.startsWith('blt')) {
const refExist = find(this.entryMetaData, { uid: reference });
if (!refExist) {
if(Array.isArray(reference_to) && reference_to.length===1) {
missingRefs.push({uid:reference, _content_type_uid: reference_to[0]});
} else {
missingRefs.push(reference);
}
}
}
// NOTE Can skip specific references keys (Ex, system defined keys can be skipped)
// if (this.config.skipRefs.includes(reference)) continue;
else {
const refExist = find(this.entryMetaData, { uid });

const refExist = find(this.entryMetaData, { uid });

if (!refExist) {
missingRefs.push(reference);
if (!refExist) {
missingRefs.push(reference);
}
}
}

Expand Down Expand Up @@ -847,7 +869,7 @@ export default class Entries {
* @returns
*/
fixSelectField(tree: Record<string, unknown>[], field: SelectFeildStruct, entry: any) {
if(!this.config.fixSelectField) {
if (!this.config.fixSelectField) {
return entry;
}
const { enum: selectOptions, multiple, min_instance, display_type, display_name, uid } = field;
Expand Down Expand Up @@ -1197,16 +1219,29 @@ export default class Entries {
entry = JSON.parse(stringReference);
}
entry = entry
?.map((reference) => {
?.map((reference: any) => {
const { uid } = reference;
const refExist = find(this.entryMetaData, { uid });

if (!refExist) {
missingRefs.push(reference);
return null;
const { reference_to } = field;
if (!uid && reference.startsWith('blt')) {
const refExist = find(this.entryMetaData, { uid: reference });
if (!refExist) {
if(Array.isArray(reference_to) && reference_to.length===1) {
missingRefs.push({uid:reference, _content_type_uid: reference_to[0]});
} else {
missingRefs.push(reference);
}
} else {
return { uid: reference, _content_type_uid: refExist.ctUid };
}
} else {
const refExist = find(this.entryMetaData, { uid });
if (!refExist) {
missingRefs.push(reference);
return null;
} else {
return reference;
}
}

return reference;
})
.filter((val) => val) as EntryReferenceFieldDataType[];

Expand Down Expand Up @@ -1364,7 +1399,7 @@ export default class Entries {
`error`,
);
}
this.entryMetaData.push({ uid: entryUid, title });
this.entryMetaData.push({ uid: entryUid, title, ctUid:uid });
}
}
}
Expand Down
Loading