Skip to content

Commit a89bb30

Browse files
committed
add the functions for branches and protections
1 parent 4a9be11 commit a89bb30

File tree

3 files changed

+128
-11
lines changed

3 files changed

+128
-11
lines changed

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: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# encoding: utf-8
2+
3+
module Github
4+
# The Branch Protections API
5+
class Client::Repos::Branches::Protections < API
6+
7+
VALID_PROTECTION_PARAM_NAMES = %w[
8+
required_status_checks
9+
enforce_admins
10+
restrictions
11+
].freeze
12+
13+
# Get a single branch protection
14+
#
15+
# @example
16+
# github = Github.new
17+
# github.repos.branches.protections.get 'user', 'repo', 'id'
18+
#
19+
# @api public
20+
def get(*args)
21+
arguments(args, required: [:user, :repo, :branch])
22+
23+
get_request("/repos/#{arguments.user}/#{arguments.repo}/branches/#{arguments.branch}/protection", arguments.params)
24+
end
25+
alias :find :get
26+
27+
# Edit a branch protection
28+
#
29+
# Users with push access to the repository can edit a branch protection.
30+
#
31+
# @param [Hash] params
32+
# @input params [String] :name
33+
# Required. The file name of the asset.
34+
# @input params [String] :label
35+
# An alternate short description of the asset.
36+
# Used in place of the filename.
37+
#
38+
# @example
39+
# github = Github.new
40+
# github.repos.branches.protections.edit 'user', 'repo', 'id',
41+
# name: "foo-1.0.0-osx.zip",
42+
# label: "Mac binary"
43+
#
44+
# @api public
45+
def edit(*args)
46+
arguments(args, required: [:user, :repo, :branch]) do
47+
permit VALID_PROTECTION_PARAM_NAMES
48+
end
49+
50+
patch_request("/repos/#{arguments.user}/#{arguments.repo}/branches/#{arguments.branch}/protection", arguments.params)
51+
end
52+
alias :update :edit
53+
54+
# Delete a branch protection
55+
#
56+
# @example
57+
# github = Github.new
58+
# github.repos.branches.protections.delete 'user', 'repo', 'id'
59+
#
60+
# @api public
61+
def delete(*args)
62+
arguments(args, required: [:user, :repo, :branch])
63+
64+
delete_request("/repos/#{arguments.user}/#{arguments.repo}/branches/#{arguments.branch}/protection", arguments.params)
65+
end
66+
end # Client::Repos::Branches::Protections
67+
end # Github

0 commit comments

Comments
 (0)