-
Notifications
You must be signed in to change notification settings - Fork 182
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the ability to synch Protected Branches
- Loading branch information
Showing
4 changed files
with
173 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module.exports = class Branches { | ||
constructor (github, repo, settings) { | ||
this.github = github | ||
this.repo = repo | ||
this.branches = settings | ||
} | ||
|
||
sync () { | ||
return Promise.all( | ||
this.branches | ||
.filter(branch => branch.protection !== undefined) | ||
.map((branch) => { | ||
let params = Object.assign(this.repo, { branch: branch.name }) | ||
|
||
if (Object.keys(branch.protection).length === 0) { | ||
this.github.repos.removeBranchProtection(params) | ||
} else { | ||
Object.assign(params, branch.protection) | ||
|
||
this.github.repos.updateBranchProtection(params) | ||
} | ||
}) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
|
||
const Branches = require('../../../lib/plugins/branches') | ||
|
||
describe('Branches', () => { | ||
let github | ||
|
||
function configure (config) { | ||
return new Branches(github, {owner: 'bkeepers', repo: 'test'}, config) | ||
} | ||
|
||
beforeEach(() => { | ||
github = { | ||
repos: { | ||
updateBranchProtection: jest.fn().mockImplementation(() => Promise.resolve()), | ||
removeBranchProtection: jest.fn().mockImplementation(() => Promise.resolve()) | ||
} | ||
} | ||
}) | ||
|
||
describe('sync', () => { | ||
it('syncs branch protection settings', () => { | ||
const plugin = configure( | ||
[{ | ||
name: 'master', | ||
protection: { | ||
required_status_checks: { | ||
strict: true, | ||
contexts: ['travis-ci'] | ||
}, | ||
enforce_admins: true, | ||
required_pull_request_reviews: { | ||
require_code_owner_reviews: true | ||
} | ||
} | ||
}] | ||
) | ||
|
||
return plugin.sync().then(() => { | ||
expect(github.repos.updateBranchProtection).toHaveBeenCalledWith({ | ||
owner: 'bkeepers', | ||
repo: 'test', | ||
branch: 'master', | ||
required_status_checks: { | ||
strict: true, | ||
contexts: ['travis-ci'] | ||
}, | ||
enforce_admins: true, | ||
required_pull_request_reviews: { | ||
require_code_owner_reviews: true | ||
} | ||
}) | ||
}) | ||
}) | ||
|
||
describe('when the "protection" config is empty', () => { | ||
it('removes branch protection', () => { | ||
const plugin = configure( | ||
[{ | ||
name: 'master', | ||
protection: {} | ||
}] | ||
) | ||
|
||
return plugin.sync().then(() => { | ||
expect(github.repos.updateBranchProtection).not.toHaveBeenCalled() | ||
expect(github.repos.removeBranchProtection).toHaveBeenCalledWith({ | ||
owner: 'bkeepers', | ||
repo: 'test', | ||
branch: 'master' | ||
}) | ||
}) | ||
}) | ||
}) | ||
|
||
describe('when the "protection" key is not present', () => { | ||
it('makes no change to branch protection', () => { | ||
const plugin = configure( | ||
[{ | ||
name: 'master' | ||
}] | ||
) | ||
|
||
return plugin.sync().then(() => { | ||
expect(github.repos.updateBranchProtection).not.toHaveBeenCalled() | ||
expect(github.repos.removeBranchProtection).not.toHaveBeenCalled() | ||
}) | ||
}) | ||
}) | ||
|
||
describe('when multiple branches are configured', () => { | ||
it('updates them each appropriately', () => { | ||
const plugin = configure( | ||
[ | ||
{ | ||
name: 'master', | ||
protection: { enforce_admins: true } | ||
}, | ||
{ | ||
name: 'other', | ||
protection: { enforce_admins: false } | ||
} | ||
] | ||
) | ||
|
||
return plugin.sync().then(() => { | ||
expect(github.repos.updateBranchProtection).toHaveBeenCalledTimes(2) | ||
|
||
expect(github.repos.updateBranchProtection).toHaveBeenLastCalledWith({ | ||
owner: 'bkeepers', | ||
repo: 'test', | ||
branch: 'other', | ||
enforce_admins: false | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) |