Skip to content
Open
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
8 changes: 4 additions & 4 deletions lib/plugins/branches.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ 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 }
this.log.debug(`Result of compareDeep = ${results}`)
const results = { msg: `The following changes will be applied to the branch protection for ${params.branch} branch`, additions: changes.additions, modifications: changes.modifications, deletions: changes.deletions }
this.log.debug(`Result of compareDeep = ${JSON.stringify(results)}`)

if (!changes.hasChanges) {
this.log.debug(`There are no changes for branch ${JSON.stringify(params)}. Skipping branch protection changes`)
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
56 changes: 54 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,60 @@ 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.includes('will be applied to the branch protection'))
expect(diffMessage).toBeDefined()
expect(diffMessage).toContain('for master branch')
expect(diffMessage).not.toContain('undefined')
})
})
})

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