Skip to content

Commit a9dd509

Browse files
authored
Merge pull request piotrmurach#315 from kartikluke/branches
Add the new Branch Protection API
2 parents 4a9be11 + 55d7d35 commit a9dd509

File tree

11 files changed

+302
-64
lines changed

11 files changed

+302
-64
lines changed

features/repos.feature

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,6 @@ Feature: Repositories API
33
Background:
44
Given I have "Github::Client::Repos" instance
55

6-
Scenario: Branches
7-
8-
Given I want branches resource with the following params:
9-
| user | repo |
10-
| peter-murach | github |
11-
When I make request within a cassette named "repos/branches"
12-
Then the response status should be 200
13-
And the response type should be JSON
14-
And the response should not be empty
15-
16-
Scenario: Get Branch
17-
18-
Given I want branch resource with the following params:
19-
| user | repo | branch |
20-
| peter-murach | github | new_dsl |
21-
When I make request within a cassette named "repos/branch"
22-
Then the response status should be 200
23-
And the response type should be JSON
24-
And the response should not be empty
25-
26-
Scenario: Get Branch mutation (Issue #154)
27-
28-
Given I want branch resource with the following params:
29-
| user | repo | branch |
30-
| peter-murach | github | new_dsl |
31-
When I make request within a cassette named "repos/branch_mutation_one"
32-
Then the response status should be 200
33-
And the response type should be JSON
34-
And the response should not be empty
35-
When I make request within a cassette named "repos/branch_mutation_two"
36-
Then the response status should be 200
37-
And the response type should be JSON
38-
And the response should not be empty
39-
406
Scenario: Tags
417

428
Given I want tags resource with the following params:

lib/github_api/client/repos.rb

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module Github
44
class Client::Repos < API
55
# Load all the modules after initializing Repos to avoid superclass mismatch
66
require_all 'github_api/client/repos',
7+
'branches',
78
'collaborators',
89
'comments',
910
'commits',
@@ -87,6 +88,9 @@ class Client::Repos < API
8788
# Access to Repos::Statuses API
8889
namespace :statuses
8990

91+
# Access to Repos::Branches API
92+
namespace :branches
93+
9094
# List repositories for the authenticated user
9195
#
9296
# @example
@@ -356,14 +360,14 @@ def delete(*args)
356360
# repos.branches 'user-name', 'repo-name'
357361
#
358362
# @api public
359-
def branches(*args)
360-
arguments(args, required: [:user, :repo])
363+
# def branches(*args)
364+
# arguments(args, required: [:user, :repo])
361365

362-
response = get_request("/repos/#{arguments.user}/#{arguments.repo}/branches", arguments.params)
363-
return response unless block_given?
364-
response.each { |el| yield el }
365-
end
366-
alias :list_branches :branches
366+
# response = get_request("/repos/#{arguments.user}/#{arguments.repo}/branches", arguments.params)
367+
# return response unless block_given?
368+
# response.each { |el| yield el }
369+
# end
370+
# alias :list_branches :branches
367371

368372
# Get branch
369373
#
@@ -373,11 +377,11 @@ def branches(*args)
373377
# github.repos.branch user: 'user-name', repo: 'repo-name', branch: 'branch-name'
374378
# github.repos(user: 'user-name', repo: 'repo-name', branch: 'branch-name').branch
375379
# @api public
376-
def branch(*args)
377-
arguments(args, required: [:user, :repo, :branch])
380+
# def branch(*args)
381+
# arguments(args, required: [:user, :repo, :branch])
378382

379-
get_request("/repos/#{arguments.user}/#{arguments.repo}/branches/#{arguments.branch}", arguments.params)
380-
end
383+
# get_request("/repos/#{arguments.user}/#{arguments.repo}/branches/#{arguments.branch}", arguments.params)
384+
# end
381385

382386
# List contributors
383387
#
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# encoding: utf-8
2+
3+
module Github
4+
class Client::Repos::Branches < API
5+
require_all 'github_api/client/repos/branches', 'protections'
6+
7+
# Access to Repos::Branches::Protections API
8+
namespace :protections
9+
10+
# List branches
11+
#
12+
# @example
13+
# github = Github.new
14+
# github.repos.branches.list 'user-name', 'repo-name'
15+
# github.repos(user: 'user-name', repo: 'repo-name').branches.list
16+
#
17+
# @example
18+
# repos = Github::Repos.new
19+
# repos.branches.list 'user-name', 'repo-name'
20+
#
21+
# @api public
22+
def list(*args)
23+
arguments(args, required: [:user, :repo])
24+
25+
response = get_request("/repos/#{arguments.user}/#{arguments.repo}/branches", arguments.params)
26+
return response unless block_given?
27+
response.each { |el| yield el }
28+
end
29+
alias :all :list
30+
31+
# Get branch
32+
#
33+
# @example
34+
# github = Github.new
35+
# github.repos.branches.get 'user-name', 'repo-name', 'branch-name'
36+
# github.repos.branches.get user: 'user-name', repo: 'repo-name', branch: 'branch-name'
37+
# github.repos(user: 'user-name', repo: 'repo-name', branch: 'branch-name').branches.get
38+
# @api public
39+
def get(*args)
40+
arguments(args, required: [:user, :repo, :branch])
41+
42+
get_request("/repos/#{arguments.user}/#{arguments.repo}/branches/#{arguments.branch}", arguments.params)
43+
end
44+
alias :find :get
45+
end # Client::Repos::Branches
46+
end # Github
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# encoding: utf-8
2+
3+
module Github
4+
# The Branch Protections API
5+
class Client::Repos::Branches::Protections < API
6+
VALID_PROTECTION_PARAM_NAMES = %w[
7+
required_status_checks
8+
required_pull_request_reviews
9+
enforce_admins
10+
restrictions
11+
accept
12+
].freeze
13+
14+
# Get a single branch's protection
15+
#
16+
# @example
17+
# github = Github.new
18+
# github.repos.branches.protections.get 'user', 'repo', 'branch'
19+
#
20+
# @api public
21+
def get(*args)
22+
arguments(args, required: [:user, :repo, :branch])
23+
24+
get_request("/repos/#{arguments.user}/#{arguments.repo}/branches/#{arguments.branch}/protection", arguments.params)
25+
end
26+
alias :find :get
27+
28+
# Edit a branch protection
29+
#
30+
# Users with push access to the repository can edit a branch protection.
31+
#
32+
# @param [Hash] params
33+
# @input params [String] :required_status_checks
34+
# Required.
35+
# @input params [String] :enforce_admins
36+
# Required.
37+
# @input params [String] :restrictions
38+
# Required.
39+
# @input params [String] :required_pull_request_reviews
40+
# Required.
41+
# Look to the branch protection API t see how to use these
42+
# https://developer.github.com/v3/repos/branches/#update-branch-protection
43+
#
44+
# @example
45+
# github = Github.new
46+
# github.repos.branches.protections.edit 'user', 'repo', 'branch',
47+
# required_pull_request_reviews: {dismiss_stale_reviews: false}
48+
#
49+
# @api public
50+
def edit(*args)
51+
arguments(args, required: [:user, :repo, :branch]) do
52+
permit VALID_PROTECTION_PARAM_NAMES
53+
end
54+
55+
put_request("/repos/#{arguments.user}/#{arguments.repo}/branches/#{arguments.branch}/protection", arguments.params)
56+
end
57+
alias :update :edit
58+
59+
# Delete a branch protection
60+
#
61+
# @example
62+
# github = Github.new
63+
# github.repos.branches.protections.delete 'user', 'repo', 'branch'
64+
#
65+
# @api public
66+
def delete(*args)
67+
arguments(args, required: [:user, :repo, :branch])
68+
69+
delete_request("/repos/#{arguments.user}/#{arguments.repo}/branches/#{arguments.branch}/protection", arguments.params)
70+
end
71+
alias :remove :delete
72+
end # Client::Repos::Branches::Protections
73+
end # Github
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"url": "https://api.github.com/repos/peter-murach/github/branches/master/protection",
3+
"required_pull_request_reviews": {
4+
"url": "https://api.github.com/repos/peter-murach/github/branches/master/protection/required_pull_request_reviews",
5+
"dismiss_stale_reviews": false
6+
},
7+
"enforce_admins": {
8+
"url":"https://api.github.com/repos/peter-murach/github/branches/master/protection/enforce_admins",
9+
"enabled": false
10+
}
11+
}

spec/github/client/repos/branch_spec.rb renamed to spec/github/client/repos/branches/get_spec.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
require 'spec_helper'
44

5-
describe Github::Client::Repos, '#branch' do
5+
describe Github::Client::Repos::Branches, '#get' do
66
let(:user) { 'peter-murach' }
77
let(:repo) { 'github' }
88
let(:request_path) { "/repos/#{user}/#{repo}/branches/#{branch}" }
@@ -20,22 +20,22 @@
2020
let(:status) { 200 }
2121

2222
it "should find resources" do
23-
subject.branch user, repo, branch
23+
subject.get user, repo, branch
2424
a_get(request_path).should have_been_made
2525
end
2626

2727
it "should return repository mash" do
28-
repo_branch = subject.branch user, repo, branch
28+
repo_branch = subject.get user, repo, branch
2929
repo_branch.should be_a Github::ResponseWrapper
3030
end
3131

3232
it "should get repository branch information" do
33-
repo_branch = subject.branch user, repo, branch
33+
repo_branch = subject.get user, repo, branch
3434
repo_branch.name.should == 'master'
3535
end
3636
end
3737

3838
it_should_behave_like 'request failure' do
39-
let(:requestable) { subject.branch user, repo, branch }
39+
let(:requestable) { subject.get user, repo, branch }
4040
end
41-
end # branch
41+
end # get

spec/github/client/repos/branches_spec.rb renamed to spec/github/client/repos/branches/list_spec.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
require 'spec_helper'
44

5-
describe Github::Client::Repos, '#branches' do
5+
describe Github::Client::Repos::Branches, '#list' do
66
let(:user) { 'peter-murach' }
77
let(:repo) { 'github' }
88
let(:request_path) { "/repos/#{user}/#{repo}/branches" }
@@ -19,35 +19,35 @@
1919
let(:status) { 200 }
2020

2121
it "should raise error when no user/repo parameters" do
22-
expect { subject.branches nil, repo }.to raise_error(ArgumentError)
22+
expect { subject.list nil, repo }.to raise_error(ArgumentError)
2323
end
2424

2525
it "should raise error when no repository" do
26-
expect { subject.branches user, nil }.to raise_error(ArgumentError)
26+
expect { subject.list user, nil }.to raise_error(ArgumentError)
2727
end
2828

2929
it "should find resources" do
30-
subject.branches user, repo
30+
subject.list user, repo
3131
a_get(request_path).should have_been_made
3232
end
3333

3434
it_should_behave_like 'an array of resources' do
35-
let(:requestable) { subject.branches user, repo }
35+
let(:requestable) { subject.list user, repo }
3636
end
3737

3838
it "should get branch information" do
39-
branches = subject.branches user, repo
39+
branches = subject.list user, repo
4040
branches.first.name.should == 'master'
4141
end
4242

4343
it "should yield to a block" do
4444
block = lambda { |el| repo }
45-
subject.should_receive(:branches).with(user, repo).and_yield repo
46-
subject.branches(user, repo, &block)
45+
subject.should_receive(:list).with(user, repo).and_yield repo
46+
subject.list(user, repo, &block)
4747
end
4848
end
4949

5050
it_should_behave_like 'request failure' do
51-
let(:requestable) { subject.branches user, repo }
51+
let(:requestable) { subject.list user, repo }
5252
end
5353
end # branches
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# encoding: utf-8
2+
3+
require 'spec_helper'
4+
5+
describe Github::Client::Repos::Branches::Protections, '#delete' do
6+
let(:user) { 'peter-murach' }
7+
let(:repo) { 'github' }
8+
let(:request_path) { "/repos/#{user}/#{repo}/branches/#{branch}/protection" }
9+
let(:branch) { 'master' }
10+
let(:body) { '' }
11+
let(:status) { 204 }
12+
13+
before {
14+
stub_delete(request_path).to_return(:body => body, :status => status,
15+
:headers => { :content_type => "application/json; charset=utf-8"})
16+
}
17+
18+
after { reset_authentication_for subject }
19+
20+
it { should respond_to :remove }
21+
22+
it "should delete the resource successfully" do
23+
subject.delete user, repo, branch
24+
a_delete(request_path).should have_been_made
25+
end
26+
27+
it "should fail to delete resource without 'user' parameter" do
28+
expect { subject.delete nil, repo, branch }.to raise_error(ArgumentError)
29+
end
30+
31+
it "should fail to delete resource without 'repo' parameter" do
32+
expect { subject.delete user, nil, branch }.to raise_error(ArgumentError)
33+
end
34+
35+
it_should_behave_like 'request failure' do
36+
let(:requestable) { subject.delete user, repo, branch }
37+
end
38+
end # delete

0 commit comments

Comments
 (0)