diff --git a/lib/plugins/custom_properties.js b/lib/plugins/custom_properties.js index 95b82501..35f0144d 100644 --- a/lib/plugins/custom_properties.js +++ b/lib/plugins/custom_properties.js @@ -12,12 +12,24 @@ module.exports = class CustomProperties extends Diffable { // Force all names to lowercase to avoid comparison issues. normalizeEntries () { - this.entries = this.entries - .filter(({ name }) => name != null) - .map(({ name, value }) => ({ - name: name.toLowerCase(), - value - })) + this.entries = this.entries.reduce((normalizedEntries, entry) => { + if (!entry || typeof entry !== 'object') { + return normalizedEntries + } + + const entryName = entry.name || entry.property_name + + if (typeof entryName !== 'string') { + return normalizedEntries + } + + normalizedEntries.push({ + name: entryName.toLowerCase(), + value: entry.value + }) + + return normalizedEntries + }, []) } async find () { @@ -40,12 +52,24 @@ module.exports = class CustomProperties extends Diffable { // Force all names to lowercase to avoid comparison issues. normalize (properties) { - return properties - .filter(({ property_name: propertyName }) => propertyName != null) - .map(({ property_name: propertyName, value }) => ({ + return properties.reduce((normalizedProperties, property) => { + if (!property || typeof property !== 'object') { + return normalizedProperties + } + + const propertyName = property.property_name || property.name + + if (typeof propertyName !== 'string') { + return normalizedProperties + } + + normalizedProperties.push({ name: propertyName.toLowerCase(), - value - })) + value: property.value + }) + + return normalizedProperties + }, []) } comparator (existing, attrs) { diff --git a/test/unit/lib/plugins/custom_properties.test.js b/test/unit/lib/plugins/custom_properties.test.js index 6afc8228..a9e878d5 100644 --- a/test/unit/lib/plugins/custom_properties.test.js +++ b/test/unit/lib/plugins/custom_properties.test.js @@ -27,11 +27,16 @@ describe('CustomProperties', () => { }) describe('Custom Properties plugin', () => { - it('should normalize entries when be instantiated', () => { + it('should normalize entries when instantiated', () => { const plugin = configure([{ name: 'Test', value: 'test' }]) expect(plugin.entries).toEqual([{ name: 'test', value: 'test' }]) }) + it('should normalize entries with property_name when instantiated', () => { + const plugin = configure([{ property_name: 'ent-ownership', value: 'expert-services' }]) + expect(plugin.entries).toEqual([{ name: 'ent-ownership', value: 'expert-services' }]) + }) + it('should fetch and normalize custom properties successfully', async () => { const mockResponse = [ { property_name: 'Test1', value: 'value1' }, @@ -58,6 +63,24 @@ describe('CustomProperties', () => { ]) }) + it('should normalize paginated custom properties when property name shape differs', async () => { + const mockResponse = [ + { name: 'Owner', value: 'My Team' }, + { property_name: 'Criticality', value: 'High' }, + { value: 'ignored' } + ] + + github.paginate.mockResolvedValue(mockResponse) + + const plugin = configure() + const result = await plugin.find() + + expect(result).toEqual([ + { name: 'owner', value: 'My Team' }, + { name: 'criticality', value: 'High' } + ]) + }) + it('should sync', async () => { const mockResponse = [ { property_name: 'no-change', value: 'no-change' },