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
46 changes: 35 additions & 11 deletions lib/plugins/custom_properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment on lines +20 to +28
})

return normalizedEntries
}, [])
}

async find () {
Expand All @@ -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) {
Expand Down
25 changes: 24 additions & 1 deletion test/unit/lib/plugins/custom_properties.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand All @@ -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' }
]
Comment on lines +66 to +71

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' },
Expand Down
Loading