Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 3 additions & 3 deletions lib/plugins/branches.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ module.exports = class Branches extends ErrorStash {
return this.github.rest.repos.getBranchProtection(params).then((result) => {
const mergeDeep = new MergeDeep(this.log, this.github, ignorableFields)
const changes = mergeDeep.compareDeep({ branch: { protection: this.reformatAndReturnBranchProtection(structuredClone(result.data)) } }, { branch: { protection: Overrides.removeOverrides(overrides, branch.protection, result.data) } })
const results = { msg: `Followings changes will be applied to the branch protection for ${params.branch.name} branch`, additions: changes.additions, modifications: changes.modifications, deletions: changes.deletions }
const results = { msg: `Followings changes will be applied to the branch protection for ${params.branch} branch`, additions: changes.additions, modifications: changes.modifications, deletions: changes.deletions }
Comment thread
tdabasinskas marked this conversation as resolved.
Outdated
this.log.debug(`Result of compareDeep = ${results}`)
Comment thread
tdabasinskas marked this conversation as resolved.
Outdated

if (!changes.hasChanges) {
Expand All @@ -84,10 +84,10 @@ module.exports = class Branches extends ErrorStash {
Object.assign(params, requiredBranchProtectionDefaults, this.reformatAndReturnBranchProtection(structuredClone(result.data)), Overrides.removeOverrides(overrides, branch.protection, result.data), { headers: previewHeaders })

if (this.nop) {
resArray.push(new NopCommand(this.constructor.name, this.repo, this.github.rest.repos.updateBranchProtection.endpoint(params), 'Add Branch Protection'))
resArray.push(new NopCommand(this.constructor.name, this.repo, this.github.rest.repos.updateBranchProtection.endpoint(params), 'Update Branch Protection'))
return Promise.resolve(resArray)
}
this.log.debug(`Adding branch protection ${JSON.stringify(params)}`)
this.log.debug(`Updating branch protection ${JSON.stringify(params)}`)
return this.github.rest.repos.updateBranchProtection(params).then(res => this.log.debug(`Branch protection applied successfully ${JSON.stringify(res.url)}`)).catch(e => { this.logError(`Error applying branch protection ${JSON.stringify(e)}`); return [] })
}).catch((e) => {
if (e.status === 404) {
Expand Down
55 changes: 53 additions & 2 deletions test/unit/lib/plugins/branches.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@ describe('Branches', () => {
)

return plugin.sync().then(() => {

expect(github.rest.repos.updateBranchProtection).toHaveBeenCalledWith(expect.objectContaining({
owner: 'bkeepers',
repo: 'test',
Expand Down Expand Up @@ -305,7 +304,6 @@ describe('Branches', () => {
)

return plugin.sync().then(() => {

expect(github.rest.repos.updateBranchProtection).toHaveBeenCalledWith(expect.objectContaining({
owner: 'bkeepers',
repo: 'test',
Expand Down Expand Up @@ -363,6 +361,59 @@ describe('Branches', () => {
})
})

describe('in nop mode', () => {
function configureNop (config) {
return new Branches(true, github, { owner: 'bkeepers', repo: 'test' }, config, log, [])
}

beforeEach(() => {
github.rest.repos.updateBranchProtection.endpoint = jest.fn().mockImplementation(params => {
return { url: 'updateBranchProtection', body: params }
})
github.rest.repos.deleteBranchProtection.endpoint = jest.fn().mockImplementation(params => {
return { url: 'deleteBranchProtection', body: params }
})
})

describe('when branch protection already exists', () => {
it('labels the NopCommand as an update and names the branch in the diff message', () => {
const plugin = configureNop(
[{
name: 'master',
protection: { enforce_admins: true }
}]
)

return plugin.sync().then(res => {
const messages = res.map(nopCommand => nopCommand.action.msg)
expect(messages).toContain('Update Branch Protection')
expect(messages).not.toContain('Add Branch Protection')
const diffMessage = messages.find(msg => msg.startsWith('Followings changes'))
expect(diffMessage).toContain('for master branch')
expect(diffMessage).not.toContain('undefined')
Comment thread
tdabasinskas marked this conversation as resolved.
Outdated
})
})
})

describe('when branch protection does not exist yet', () => {
it('labels the NopCommand as an add', () => {
github.rest.repos.getBranchProtection = jest.fn().mockRejectedValue({ status: 404 })
const plugin = configureNop(
[{
name: 'master',
protection: { enforce_admins: true }
}]
)

return plugin.sync().then(res => {
const messages = res.map(nopCommand => nopCommand.action.msg)
expect(messages).toContain('Add Branch Protection')
expect(messages).not.toContain('Update Branch Protection')
})
})
})
})

describe.skip('return values', () => {
it('returns updateBranchProtection Promise', () => {
const plugin = configure(
Expand Down