Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 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
13 changes: 9 additions & 4 deletions lib/mergeDeep.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const mergeBy = require('./mergeArrayBy')
const DeploymentConfig = require('./deploymentConfig')

const NAME_FIELDS = ['name', 'username', 'actor_id', 'login', 'type', 'key_prefix', 'context']
const NAME_FIELDS = ['slug', 'name', 'username', 'actor_id', 'login', 'type', 'key_prefix', 'context', 'title']
const NAME_USERNAME_PROPERTY = item => NAME_FIELDS.find(prop => Object.prototype.hasOwnProperty.call(item, prop))
const GET_NAME_USERNAME_PROPERTY = item => { if (NAME_USERNAME_PROPERTY(item)) return item[NAME_USERNAME_PROPERTY(item)] }

Expand Down Expand Up @@ -248,8 +248,13 @@ class MergeDeep {
modifications.push({})
additions.push({})
deletions.push({})
if (visited[id]) {
Comment thread
fgenois-coveo marked this conversation as resolved.
this.compareDeep(a, visited[id], additions[additions.length - 1], modifications[modifications.length - 1], deletions[deletions.length - 1])
if (this.isObjectNotArray(visited[id]) && this.isObjectNotArray(a)) {
// Strip the identity field before comparing — it was already used for matching
const idPropA = NAME_USERNAME_PROPERTY(a)
const idPropV = NAME_USERNAME_PROPERTY(visited[id])
const targetObj = idPropA ? Object.fromEntries(Object.entries(a).filter(([k]) => k !== idPropA)) : a
const sourceObj = idPropV ? Object.fromEntries(Object.entries(visited[id]).filter(([k]) => k !== idPropV)) : visited[id]
this.compareDeep(targetObj, sourceObj, additions[additions.length - 1], modifications[modifications.length - 1], deletions[deletions.length - 1])
}
// Any addtions for the matching key must be moved to modifications
const lastAddition = additions[additions.length - 1]
Expand All @@ -270,7 +275,7 @@ class MergeDeep {
}
// Add name attribute to the modifications to make it look better ; it won't be added otherwise as it would be the same
if (!this.isEmpty(modifications[modifications.length - 1])) {
if (visited[id]) {
if (this.isObjectNotArray(visited[id])) {
modifications[modifications.length - 1][NAME_USERNAME_PROPERTY(a)] = id
}
}
Expand Down
51 changes: 51 additions & 0 deletions test/unit/lib/mergeDeep.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1882,4 +1882,55 @@ branches:
expect(same.additions).toEqual({})
expect(same.modifications).toEqual({})
})

it('Teams: slug-based matching produces no diff when permissions match', () => {
const ignorableFields = []
const mockReturnGitHubContext = jest.fn().mockReturnValue({
request: () => {}
})
const mergeDeep = new MergeDeep(
log,
mockReturnGitHubContext,
ignorableFields
)

// API returns display name + slug; YAML uses slug as name
const target = [
{ name: 'Dev Tooling', slug: 'dev-tooling', permission: 'admin' }
]
const source = [
{ name: 'dev-tooling', permission: 'admin' }
]

const result = mergeDeep.compareDeep(target, source)
expect(result.hasChanges).toBeFalsy()
})

it('Teams: slug-based matching detects permission change as modification', () => {
const ignorableFields = []
const mockReturnGitHubContext = jest.fn().mockReturnValue({
request: () => {}
})
const mergeDeep = new MergeDeep(
log,
mockReturnGitHubContext,
ignorableFields
)

const target = [
{ name: 'Dev Tooling', slug: 'dev-tooling', permission: 'admin' }
]
const source = [
{ name: 'dev-tooling', permission: 'push' }
]

const result = mergeDeep.compareDeep(target, source)
expect(result.hasChanges).toBeTruthy()
// Should be a modification, not an addition+deletion
expect(result.modifications).toEqual(
expect.arrayContaining([
expect.objectContaining({ permission: 'push' })
])
)
})
})