Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Remove summary for teams/collaborators if only (noop) deletions #18

Merged
merged 1 commit into from
Feb 14, 2024
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
15 changes: 13 additions & 2 deletions lib/plugins/diffable.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ module.exports = class Diffable extends ErrorStash {
this.log.debug(` ${JSON.stringify(existingRecords, null, 2)} \n\n ${JSON.stringify(filteredEntries, null, 2)} `)

const mergeDeep = new MergeDeep(this.log, this.github, ignorableFields)
const compare = this._removeDeletionsIfPartialSync(mergeDeep.compareDeep(existingRecords, filteredEntries))
const compare = this._fixComparisonIfPartialSync(mergeDeep.compareDeep(existingRecords, filteredEntries))
const results = { msg: 'Changes found', additions: compare.additions, modifications: compare.modifications, deletions: compare.deletions }
this.log.debug(`Results of comparing ${this.constructor.name} diffable target ${JSON.stringify(existingRecords)} with source ${JSON.stringify(filteredEntries)} is ${results}`)
if (!compare.hasChanges) {
Expand Down Expand Up @@ -170,8 +170,17 @@ module.exports = class Diffable extends ErrorStash {

// Remove deletions from the summaries if the plugin is overridden
// to implement isPartialSync
_removeDeletionsIfPartialSync (comparison) {
_fixComparisonIfPartialSync (comparison) {
if (this.isPartialSync) {
if (isFalsyOrEmpty(comparison.additions) && isFalsyOrEmpty(comparison.modifications)) {
return {
additions: {},
modifications: {},
deletions: {},
hasChanges: false
}
}

return {
...comparison,
deletions: {}
Expand All @@ -181,3 +190,5 @@ module.exports = class Diffable extends ErrorStash {
return comparison
}
}

const isFalsyOrEmpty = (changes) => !changes || !Object.keys(changes).length
2 changes: 1 addition & 1 deletion test/unit/lib/plugins/branches.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ describe('Branches', () => {
})

describe.skip('return values', () => {
it.only('returns updateBranchProtection Promise', () => {
it('returns updateBranchProtection Promise', () => {
const plugin = configure(
[{
name: 'master',
Expand Down
15 changes: 15 additions & 0 deletions test/unit/lib/plugins/collaboratorsPartial.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,19 @@ describe('CollaboratorsPartial', () => {
);
})
})

it('doesnt add summary if no additions/modifications', async () => {
const plugin = configure([
{ username: 'bkeepers', permission: 'admin' },
], true)

github.repos.listCollaborators.mockResolvedValueOnce({
data: [
{ login: 'bkeepers', permissions: { admin: true, push: true, pull: true } },
{ login: 'removed-user', permissions: { admin: false, push: true, pull: true } },
]
})

expect(await plugin.sync()).toBe(undefined)
})
})
2 changes: 1 addition & 1 deletion test/unit/lib/plugins/repository.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe('Repository', () => {
})
})

it.only('syncs topics', () => {
it('syncs topics', () => {
const plugin = configure({
topics: ['foo', 'bar']
})
Expand Down
21 changes: 15 additions & 6 deletions test/unit/lib/plugins/teamsPartial.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ describe('TeamsPartial', () => {
repos: {
listTeams: jest.fn().mockResolvedValue({
data: [
{ id: unchangedTeamId, slug: unchangedTeamName, permission: 'push' },
{ id: removedTeamId, slug: removedTeamName, permission: 'push' },
{ id: updatedTeamId, slug: updatedTeamName, permission: 'pull' }
{ id: unchangedTeamId, slug: unchangedTeamName, name: unchangedTeamName, permission: 'push' },
{ id: removedTeamId, slug: removedTeamName, name: removedTeamName, permission: 'push' },
{ id: updatedTeamId, slug: updatedTeamName, name: updatedTeamName, permission: 'pull' }
]
})
},
Expand Down Expand Up @@ -104,10 +104,10 @@ describe('TeamsPartial', () => {
jest.clearAllMocks()
})

it.only('doesnt add deletions in summaries', async () => {
it('doesnt add deletions in summaries', async () => {
const plugin = configure([
{ name: unchangedTeamName, permission: 'push' },
{ name: updatedTeamName, permission: 'admin' },
{ name: updatedTeamName, permission: 'pull' },
{ name: addedTeamName, permission: 'pull' }
], true)

Expand All @@ -118,10 +118,19 @@ describe('TeamsPartial', () => {
{ owner: 'bkeepers', repo: 'test' },
null,
expect.objectContaining({
deletions: {}
deletions: {},
}),
'INFO'
);
})

it('doesnt add summary if no additions/modifications', async () => {
const plugin = configure([
{ name: unchangedTeamName, permission: 'push' },
{ name: updatedTeamName, permission: 'pull' }
], true)

expect(await plugin.sync()).toBe(undefined)
})
})
})
Loading