Skip to content

Commit

Permalink
support new branch protection API
Browse files Browse the repository at this point in the history
  • Loading branch information
atmos committed Jul 28, 2016
1 parent fa1619b commit 261b97b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
18 changes: 8 additions & 10 deletions lib/octokit/client/repositories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -504,14 +504,13 @@ def branch(repo, branch, options = {})
# @example
# @client.protect_branch('octokit/octokit.rb', 'master', foo)
def protect_branch(repo, branch, required_status_checks = {}, options = {})
if !required_status_checks.empty?
required_status_checks = { :required_status_checks => required_status_checks }
end

protection = { :protection => { :enabled => true }.merge(required_status_checks) }
options = ensure_api_media_type(:branch_protection, options.merge(protection))
required_status_checks[:restrictions] ||= nil
required_status_checks[:required_status_checks] ||= {
:include_admins => true, :strict => false, :contexts => []
}

patch "#{Repository.path repo}/branches/#{branch}", options
options = ensure_api_media_type(:branch_protection, options.merge(required_status_checks))
put "#{Repository.path repo}/branches/#{branch}/protection", options
end

# Unlock a single branch from a repository
Expand All @@ -525,9 +524,8 @@ def protect_branch(repo, branch, required_status_checks = {}, options = {})
# @example
# @client.unprotect_branch('octokit/octokit.rb', 'master')
def unprotect_branch(repo, branch, options = {})
protection = { :protection => { :enabled => false } }
options = ensure_api_media_type(:branch_protection, options.merge(protection))
patch "#{Repository.path repo}/branches/#{branch}", options
options = ensure_api_media_type(:branch_protection, options)
delete "#{Repository.path repo}/branches/#{branch}/protection", options
end

# List users available for assigning to issues.
Expand Down
7 changes: 4 additions & 3 deletions spec/octokit/client/repositories_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -326,14 +326,15 @@

it "protects a single branch" do
branch = @client.protect_branch(@repo.full_name, "master")
expect(branch.protection.enabled).to be true
expect(branch.required_status_checks.include_admins).to be true
end

it "unprotects a single branch" do
branch = @client.protect_branch(@repo.full_name, "master")
expect(branch.protection.enabled).to be true
expect(branch.required_status_checks.include_admins).to be true

branch = @client.unprotect_branch(@repo.full_name, "master")
expect(branch.protection.enabled?).to be false
expect(branch).to be_empty
end
end
end # .branches
Expand Down

0 comments on commit 261b97b

Please sign in to comment.