Skip to content
Open
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 change: 1 addition & 0 deletions packages/contentstack-branches/src/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export interface CSVRow {
export interface AddCSVRowParams {
contentTypeName: string;
fieldName: string;
fieldPath?: string;
fieldType: string;
sourceValue: string;
srNo: number;
Expand Down
45 changes: 30 additions & 15 deletions packages/contentstack-branches/src/utils/branch-diff-utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ import {
ModifiedFieldsInput,
ModifiedFieldsType,
} from '../interfaces/index';
import { extractValueFromPath, generateCSVDataFromVerbose, getFieldDisplayName } from './csv-utility';
import {
extractValueFromPath,
generateCSVDataFromVerbose,
getFieldDisplayName,
getOptionDisplayName,
isContentTypeOptionPath,
} from './csv-utility';

/**
* Fetch differences between two branches
Expand Down Expand Up @@ -320,6 +326,9 @@ async function baseAndCompareBranchDiff(params: {
displayName = 'Description';
path = baseBranchFieldExists?.path;
field = 'metadata';
} else if (isContentTypeOptionPath(baseBranchFieldExists.path)) {
displayName = getOptionDisplayName(baseBranchFieldExists.path);
field = 'option';
}
params.listOfDeletedFields.push({
displayName: displayName,
Expand All @@ -335,6 +344,9 @@ async function baseAndCompareBranchDiff(params: {
displayName = 'Description';
path = compareBranchFieldExists?.path;
field = 'metadata';
} else if (isContentTypeOptionPath(compareBranchFieldExists.path)) {
displayName = getOptionDisplayName(compareBranchFieldExists.path);
field = 'option';
}
params.listOfAddedFields.push({
displayName: displayName,
Expand All @@ -353,22 +365,10 @@ async function prepareModifiedDiff(params: {
listOfModifiedFields: any[];
}) {
const { baseBranchFieldExists, compareBranchFieldExists } = params;
if (
compareBranchFieldExists.path === 'description' ||
compareBranchFieldExists.path === 'title' ||
compareBranchFieldExists.path === 'options.singleton'
) {
if (compareBranchFieldExists.path === 'description' || compareBranchFieldExists.path === 'title') {
let displayName: string;
let changeDetails = '';
if (baseBranchFieldExists.path === 'options.singleton') {
if (compareBranchFieldExists.value) {
displayName = 'Single';
changeDetails = `Changed from Multiple to Single`;
} else {
displayName = 'Multiple';
changeDetails = `Changed from Single to Multiple`;
}
} else if (baseBranchFieldExists.path === 'description') {
if (baseBranchFieldExists.path === 'description') {
displayName = 'Description';
const oldDesc = baseBranchFieldExists.value || 'undefined';
const newDesc = compareBranchFieldExists.value || 'undefined';
Expand All @@ -388,6 +388,21 @@ async function prepareModifiedDiff(params: {
path: '',
uid: baseBranchFieldExists.path,
});
} else if (isContentTypeOptionPath(compareBranchFieldExists.path)) {
// Content type options (is_page, singleton, title, url_pattern, url_prefix, ...) carry { path, value }
// and no uid/display_name, so they must not go through the schema field deep diff.
const optionPath: string = compareBranchFieldExists.path;
const oldValue = baseBranchFieldExists?.value;
const newValue = compareBranchFieldExists.value;
params.listOfModifiedFields.push({
changeDetails: `Changed from "${oldValue}" to "${newValue}"`,
displayName: getOptionDisplayName(optionPath),
field: 'option',
newValue,
oldValue,
path: optionPath,
uid: optionPath,
});
} else {
const fieldDisplayName = getFieldDisplayName(compareBranchFieldExists);

Expand Down
63 changes: 55 additions & 8 deletions packages/contentstack-branches/src/utils/csv-utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,39 @@ import { AddCSVRowParams, BranchDiffVerboseRes, CSV_HEADER, CSVRow, ContentTypeI
*/
export function getFieldDisplayName(field: Record<string, any>): string {
let fieldName = field?.displayName || field?.display_name || field?.title || field?.uid;

// Special handling for field_rules
if (!fieldName && field?.path === 'field_rules') {
fieldName = 'Field Rules';
} else if (!fieldName) {
fieldName = 'Unknown Field';

if (!fieldName) {
const path = typeof field?.path === 'string' ? field.path : '';
if (path === 'field_rules') {
fieldName = 'Field Rules';
} else if (isContentTypeOptionPath(path)) {
fieldName = getOptionDisplayName(path);
} else {
fieldName = path || 'Unknown Field';
}
}

return fieldName as string;
}

/**
* Whether a diff path points at a content type option (e.g. "options.is_page")
* @param path - The diff item path
* @returns {boolean}
*/
export function isContentTypeOptionPath(path: unknown): path is string {
return typeof path === 'string' && path.startsWith('options.') && path.length > 'options.'.length;
}

/**
* Readable label for a content type option path, e.g. "options.is_page" -> "Options → is_page"
* @param path - The option path
* @returns {string}
*/
export function getOptionDisplayName(path: string): string {
return `Options → ${path.slice('options.'.length).split('.').join(' → ')}`;
}

/**
* Format values for CSV display
* @param value - The value to format
Expand Down Expand Up @@ -230,7 +252,7 @@ function addCSVRow(csvRows: CSVRow[], params: AddCSVRowParams): void {
csvRows.push({
contentTypeName: params.contentTypeName,
fieldName: params.fieldName,
fieldPath: 'N/A',
fieldPath: params.fieldPath || 'N/A',
operation: params.fieldType,
sourceBranchValue: params.sourceValue,
srNo: params.srNo,
Expand Down Expand Up @@ -258,22 +280,36 @@ function addFieldChangesToCSV(csvRows: CSVRow[], params: {

fields.forEach(field => {
const fieldName = getFieldDisplayName(field);
const fieldPath = getCSVFieldPath(field);

if (field.propertyChanges?.length > 0) {
field.propertyChanges.forEach(propertyChange => {
addCSVRow(csvRows, {
contentTypeName: params.contentTypeName,
fieldName,
fieldPath,
fieldType,
sourceValue: formatValue(propertyChange.newValue),
srNo: srNo++,
targetValue: formatValue(propertyChange.oldValue)
});
});
} else if (fieldType === 'modified' && ('newValue' in field || 'oldValue' in field)) {
// Content type level changes (options, title, description) carry their values directly
addCSVRow(csvRows, {
contentTypeName: params.contentTypeName,
fieldName,
fieldPath,
fieldType,
sourceValue: formatValue(field.newValue),
srNo: srNo++,
targetValue: formatValue(field.oldValue)
});
} else {
addCSVRow(csvRows, {
contentTypeName: params.contentTypeName,
fieldName,
fieldPath,
fieldType,
sourceValue: fieldType === 'added' ? 'N/A' : formatValue(field),
srNo: srNo++,
Expand All @@ -284,6 +320,17 @@ function addFieldChangesToCSV(csvRows: CSVRow[], params: {
});
}

/**
* Field path shown in the CSV: the diff path (e.g. "group.title", "options.is_page"),
* falling back to the uid for content type level changes that carry an empty path.
* @param field - The diff item
* @returns {string} Field path, or 'N/A' when none is known
*/
function getCSVFieldPath(field: Record<string, any>): string {
const path = typeof field?.path === 'string' && field.path && field.path !== 'undefined' ? field.path : '';
return path || (typeof field?.uid === 'string' && field.uid) || 'N/A';
}

/**
* Get total field count for serial number calculation
* @param modifiedFields - Field changes data
Expand Down
137 changes: 137 additions & 0 deletions packages/contentstack-branches/test/unit/utils/csv-utility.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { stub } from 'sinon';
import { join } from 'path';
import { cliux } from '@contentstack/cli-utilities';
import * as csvUtility from '../../../src/utils/csv-utility';
import * as branchDiffUtility from '../../../src/utils/branch-diff-utility';
import { BranchDiffVerboseRes } from '../../../src/interfaces';

describe('CSV Utility Testcases', () => {
Expand Down Expand Up @@ -130,6 +131,142 @@ describe('CSV Utility Testcases', () => {
expect(deletedCTRow?.targetBranchValue).to.equal('N/A');
});

it('should fill Field Path from the diff path instead of always N/A', () => {
const result = csvUtility.generateCSVDataFromVerbose({
modified: [
{
moduleDetails: { title: 'Blog', uid: 'blog', status: 'modified', type: 'content_type' },
modifiedFields: {
modified: [
{
path: 'seo.meta_title',
displayName: 'Meta Title',
uid: 'meta_title',
field: 'text field',
propertyChanges: [{ property: 'mandatory', changeType: 'modified', oldValue: false, newValue: true }],
},
],
},
},
],
added: [],
deleted: [],
});

expect(result).to.have.length(1);
expect(result[0].fieldPath).to.equal('seo.meta_title');
});

it('DX-30314: should label content type option changes and use their path', async () => {
const branchDiff = {
diff: {
status: 'modified',
base_branch: {
differences: [
{ path: 'options.is_page', value: false },
{ path: 'options.url_prefix', value: '/' },
],
},
compare_branch: {
differences: [
{ path: 'options.is_page', value: true },
{ path: 'options.url_prefix', value: '/jobs/' },
{ path: 'options.url_pattern', value: '/:title' },
],
},
},
};

const { listOfAddedFields, listOfDeletedFields, listOfModifiedFields } =
await branchDiffUtility.prepareBranchVerboseRes(branchDiff);

expect(listOfModifiedFields.map((f) => f.displayName)).to.deep.equal([
'Options → is_page',
'Options → url_prefix',
]);
expect(listOfAddedFields).to.have.length(1);
expect(listOfAddedFields[0].displayName).to.equal('Options → url_pattern');
expect(listOfAddedFields[0].path).to.equal('options.url_pattern');
expect(listOfDeletedFields).to.have.length(0);

const result = csvUtility.generateCSVDataFromVerbose({
modified: [
{
moduleDetails: { title: 'Page - Employer', uid: 'page_employer', status: 'modified', type: 'content_type' },
modifiedFields: { added: listOfAddedFields, deleted: listOfDeletedFields, modified: listOfModifiedFields },
},
],
added: [],
deleted: [],
});

const isPageRow = result.find((row) => row.fieldPath === 'options.is_page');
expect(isPageRow).to.exist;
expect(isPageRow?.fieldName).to.equal('Options → is_page');
expect(isPageRow?.operation).to.equal('modified');
expect(isPageRow?.sourceBranchValue).to.equal('true');
expect(isPageRow?.targetBranchValue).to.equal('false');

const urlPrefixRow = result.find((row) => row.fieldPath === 'options.url_prefix');
expect(urlPrefixRow?.sourceBranchValue).to.equal('/jobs/');
expect(urlPrefixRow?.targetBranchValue).to.equal('/');

expect(result.some((row) => row.fieldName === 'Unknown Field')).to.be.false;
});

it('DX-30314: should label singleton like the other content type options', async () => {
const { listOfModifiedFields } = await branchDiffUtility.prepareBranchVerboseRes({
diff: {
status: 'modified',
base_branch: { differences: [{ path: 'options.singleton', value: false }] },
compare_branch: { differences: [{ path: 'options.singleton', value: true }] },
},
});

const result = csvUtility.generateCSVDataFromVerbose({
modified: [
{
moduleDetails: { title: 'Home', uid: 'home', status: 'modified', type: 'content_type' },
modifiedFields: { modified: listOfModifiedFields },
},
],
added: [],
deleted: [],
});

expect(result).to.have.length(1);
expect(result[0].fieldName).to.equal('Options → singleton');
expect(result[0].fieldPath).to.equal('options.singleton');
expect(result[0].sourceBranchValue).to.equal('true');
expect(result[0].targetBranchValue).to.equal('false');
});

it('should show values, not the uid, for a content type rename', async () => {
const { listOfModifiedFields } = await branchDiffUtility.prepareBranchVerboseRes({
diff: {
status: 'modified',
base_branch: { differences: [{ path: 'title', value: 'Home' }] },
compare_branch: { differences: [{ path: 'title', value: 'Home Page' }] },
},
});

const result = csvUtility.generateCSVDataFromVerbose({
modified: [
{
moduleDetails: { title: 'Home', uid: 'home', status: 'modified', type: 'content_type' },
modifiedFields: { modified: listOfModifiedFields },
},
],
added: [],
deleted: [],
});

expect(result[0].fieldName).to.equal('Display Name');
expect(result[0].fieldPath).to.equal('title');
expect(result[0].sourceBranchValue).to.equal('Home Page');
expect(result[0].targetBranchValue).to.equal('Home');
});

it('should handle empty verbose results gracefully', () => {
const emptyVerboseRes: BranchDiffVerboseRes = {
modified: [],
Expand Down
Loading