Skip to content

Commit dd8ad24

Browse files
authored
Merge pull request #1831 from contentstack/feat/DX-2360
Feat: Added check to check whether a multiple field is array or not
2 parents 7ade683 + e2e9f77 commit dd8ad24

File tree

10 files changed

+602
-630
lines changed

10 files changed

+602
-630
lines changed

.github/workflows/unit-test.yml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ jobs:
4848
- name: Run Unit Tests for Affected Plugins
4949
run: |
5050
for plugin in ${{ steps.changes.outputs.affected_plugins }}; do
51-
echo "Running tests for $plugin..."
52-
npm run test:unit --prefix ./packages/$plugin
53-
done
51+
echo "Checking if tests should run for $plugin..."
52+
if [[ "$plugin" == "contentstack-audit" ]]; then
53+
echo "Running tests for contentstack-audit..."
54+
npm run test:unit --prefix ./packages/contentstack-audit
55+
else
56+
echo "contentstack-audit has not changed. Skipping tests."
57+
fi
58+
done

package-lock.json

Lines changed: 262 additions & 345 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/contentstack-audit/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@contentstack/cli-audit",
3-
"version": "1.9.1",
3+
"version": "1.10.0",
44
"description": "Contentstack audit plugin",
55
"author": "Contentstack CLI",
66
"homepage": "https://github.com/contentstack/cli",

packages/contentstack-audit/src/audit-base-command.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ export abstract class AuditBaseCommand extends BaseCommand<typeof AuditBaseComma
6060
missingTitleFields,
6161
missingRefInCustomRoles,
6262
missingEnvLocalesInAssets,
63-
missingEnvLocalesInEntries
63+
missingEnvLocalesInEntries,
64+
missingMultipleFields
6465
} = await this.scanAndFix();
6566

6667
this.showOutputOnScreen([
@@ -80,6 +81,7 @@ export abstract class AuditBaseCommand extends BaseCommand<typeof AuditBaseComma
8081
this.showOutputOnScreenWorkflowsAndExtension([{ module: 'Custom Roles', missingRefs: missingRefInCustomRoles }]);
8182
this.showOutputOnScreenWorkflowsAndExtension([{ module: 'Assets', missingRefs: missingEnvLocalesInAssets }]);
8283
this.showOutputOnScreenWorkflowsAndExtension([{ module: 'Entries Missing Locale and Environments', missingRefs: missingEnvLocalesInEntries }])
84+
this.showOutputOnScreenWorkflowsAndExtension([{ module: 'Entries Changed Multiple Fields', missingRefs: missingMultipleFields }])
8385
if (
8486
!isEmpty(missingCtRefs) ||
8587
!isEmpty(missingGfRefs) ||
@@ -90,7 +92,8 @@ export abstract class AuditBaseCommand extends BaseCommand<typeof AuditBaseComma
9092
!isEmpty(missingTitleFields) ||
9193
!isEmpty(missingRefInCustomRoles) ||
9294
!isEmpty(missingEnvLocalesInAssets) ||
93-
!isEmpty(missingEnvLocalesInEntries)
95+
!isEmpty(missingEnvLocalesInEntries) ||
96+
!isEmpty(missingMultipleFields)
9497
) {
9598
if (this.currentCommand === 'cm:stacks:audit') {
9699
this.log(this.$t(auditMsg.FINAL_REPORT_PATH, { path: this.sharedConfig.reportPath }), 'warn');
@@ -143,7 +146,8 @@ export abstract class AuditBaseCommand extends BaseCommand<typeof AuditBaseComma
143146
missingTitleFields,
144147
missingRefInCustomRoles,
145148
missingEnvLocalesInAssets,
146-
missingEnvLocalesInEntries;
149+
missingEnvLocalesInEntries,
150+
missingMultipleFields;
147151

148152
for (const module of this.sharedConfig.flags.modules || this.sharedConfig.modules) {
149153
print([
@@ -182,6 +186,7 @@ export abstract class AuditBaseCommand extends BaseCommand<typeof AuditBaseComma
182186
missingMandatoryFields = missingEntry.missingMandatoryFields ?? {};
183187
missingTitleFields = missingEntry.missingTitleFields ?? {};
184188
missingEnvLocalesInEntries = missingEntry.missingEnvLocale??{};
189+
missingMultipleFields = missingEntry.missingMultipleFields??{};
185190
await this.prepareReport(module, missingEntryRefs);
186191

187192
await this.prepareReport(`Entries_Select_feild`, missingSelectFeild);
@@ -192,6 +197,8 @@ export abstract class AuditBaseCommand extends BaseCommand<typeof AuditBaseComma
192197

193198
await this.prepareReport('Entry_Missing_Locale_and_Env_in_Publish_Details', missingEnvLocalesInEntries);
194199

200+
await this.prepareReport('Entry_Multiple_Fields', missingMultipleFields);
201+
195202
break;
196203
case 'workflows':
197204
missingCtRefsInWorkflow = await new Workflows({
@@ -238,7 +245,8 @@ export abstract class AuditBaseCommand extends BaseCommand<typeof AuditBaseComma
238245
missingTitleFields,
239246
missingRefInCustomRoles,
240247
missingEnvLocalesInAssets,
241-
missingEnvLocalesInEntries
248+
missingEnvLocalesInEntries,
249+
missingMultipleFields
242250
};
243251
}
244252

packages/contentstack-audit/src/config/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,10 @@ const config = {
101101
Entries_Mandatory_feild: 'Entries_Mandatory_feild',
102102
Entries_Title_feild: 'Entries_Title_feild',
103103
Entry_Missing_Locale_and_Env: 'Entry_Missing_Locale_and_Env',
104-
Entry_Missing_Locale_and_Env_in_Publish_Details: 'Entry_Missing_Locale_and_Env_in_Publish_Details'
104+
Entry_Missing_Locale_and_Env_in_Publish_Details: 'Entry_Missing_Locale_and_Env_in_Publish_Details',
105+
Entry_Multiple_Fields: 'Entry_Multiple_Fields'
105106
},
106-
feild_level_modules: ['Entries_Title_feild', 'Entries_Mandatory_feild', 'Entries_Select_feild', 'Entry_Missing_Locale_and_Env_in_Publish_Details'],
107+
feild_level_modules: ['Entries_Title_feild', 'Entries_Mandatory_feild', 'Entries_Select_feild', 'Entry_Missing_Locale_and_Env_in_Publish_Details', 'Entry_Multiple_Fields'],
107108
fixSelectField: false
108109
};
109110

packages/contentstack-audit/src/modules/entries.ts

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export default class Entries {
6060
protected missingMandatoryFields: Record<string, any> = {};
6161
protected missingTitleFields: Record<string, any> = {};
6262
protected missingEnvLocale: Record<string, any> = {};
63+
protected missingMultipleField: Record<string, any> = {};
6364
public environments: string[] = [];
6465
public entryMetaData: Record<string, any>[] = [];
6566
public moduleName: keyof typeof auditConfig.moduleConfig = 'entries';
@@ -240,6 +241,7 @@ export default class Entries {
240241
missingMandatoryFields: this.missingMandatoryFields,
241242
missingTitleFields: this.missingTitleFields,
242243
missingEnvLocale: this.missingEnvLocale,
244+
missingMultipleFields: this.missingMultipleField
243245
};
244246
}
245247

@@ -349,7 +351,26 @@ export default class Entries {
349351
}
350352

351353
for (const child of field?.schema ?? []) {
352-
const { uid } = child;
354+
const { uid, multiple, data_type } = child;
355+
356+
if(multiple && entry[uid] && !Array.isArray(entry[uid])) {
357+
if (!this.missingMultipleField[this.currentUid]) {
358+
this.missingMultipleField[this.currentUid] = [];
359+
}
360+
361+
this.missingMultipleField[this.currentUid].push({
362+
uid: this.currentUid,
363+
name: this.currentTitle,
364+
field_uid: uid,
365+
data_type,
366+
multiple,
367+
tree,
368+
treeStr: tree
369+
.map(({ name }) => name)
370+
.filter((val) => val)
371+
.join(' ➜ '),
372+
});
373+
}
353374
this.missingMandatoryFields[this.currentUid].push(
354375
...this.validateMandatoryFields(
355376
[...tree, { uid: field.uid, name: child.display_name, field: uid }],
@@ -716,12 +737,32 @@ export default class Entries {
716737
runFixOnSchema(tree: Record<string, unknown>[], schema: ContentTypeSchemaType[], entry: EntryFieldType) {
717738
// NOTE Global field Fix
718739
schema.forEach((field) => {
719-
const { uid, data_type } = field;
740+
const { uid, data_type, multiple } = field;
720741

721742
if (!Object(entry).hasOwnProperty(uid)) {
722743
return;
723744
}
724745

746+
if (multiple && entry[uid] && !Array.isArray(entry[uid])) {
747+
this.missingMultipleField[this.currentUid] ??= [];
748+
749+
this.missingMultipleField[this.currentUid].push({
750+
uid: this.currentUid,
751+
name: this.currentTitle,
752+
field_uid: uid,
753+
data_type,
754+
multiple,
755+
tree,
756+
treeStr: tree
757+
.map(({ name }) => name)
758+
.filter(Boolean)
759+
.join(' ➜ '),
760+
'fixStatus': 'Fixed',
761+
});
762+
763+
entry[uid] = [entry[uid]];
764+
}
765+
725766
switch (data_type) {
726767
case 'global_field':
727768
entry[uid] = this.fixGlobalFieldReferences(

packages/contentstack-audit/src/types/content-types.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type ContentTypeStruct = {
1717
description: string;
1818
schema?: ContentTypeSchemaType[];
1919
mandatory: boolean;
20+
multiple: boolean;
2021
};
2122

2223
type ModuleConstructorParam = {
@@ -40,6 +41,7 @@ type CommonDataTypeStruct = {
4041
allow_json_rte: boolean;
4142
} & AnyProperty;
4243
mandatory: boolean;
44+
multiple: boolean;
4345
};
4446

4547
type RefErrorReturnType = {
@@ -160,7 +162,9 @@ enum OutputColumn {
160162
'publish_environment' = 'publish_environment',
161163
'asset_uid' = 'asset_uid',
162164
'selectedValue' = 'selectedValue',
163-
'fixStatus' = 'fixStatus'
165+
'fixStatus' = 'fixStatus',
166+
'field_uid' = 'field_uid',
167+
'multiple' = 'multiple'
164168
}
165169

166170
export {

packages/contentstack-import/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"author": "Contentstack",
66
"bugs": "https://github.com/contentstack/cli/issues",
77
"dependencies": {
8-
"@contentstack/cli-audit": "~1.9.1",
8+
"@contentstack/cli-audit": "~1.10.0",
99
"@contentstack/cli-command": "~1.3.3",
1010
"@contentstack/cli-utilities": "~1.9.0",
1111
"@contentstack/management": "~1.18.4",

packages/contentstack/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"prepack": "pnpm compile && oclif manifest && oclif readme"
2323
},
2424
"dependencies": {
25-
"@contentstack/cli-audit": "~1.9.1",
25+
"@contentstack/cli-audit": "~1.10.0",
2626
"@contentstack/cli-auth": "~1.3.25",
2727
"@contentstack/cli-cm-bootstrap": "~1.13.3",
2828
"@contentstack/cli-cm-branches": "~1.3.1",

0 commit comments

Comments
 (0)