Skip to content

Commit dfb8aa3

Browse files
committed
Change organization teams api to use new arguments parser, add ability to list user teams.
1 parent 5f25b11 commit dfb8aa3

4 files changed

Lines changed: 171 additions & 100 deletions

File tree

lib/github_api/client/orgs/teams.rb

Lines changed: 136 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -11,147 +11,192 @@ class Client::Orgs::Teams < API
1111
'permission' => %w[ pull push admin ].freeze
1212
}
1313

14+
15+
# List user teams
16+
#
17+
# List all of the teams across all of the organizations
18+
# to which the authenticated user belongs. This method
19+
# requires user or repo scope when authenticating via OAuth.
20+
#
21+
# @example
22+
# github = Github.new oauth_token: '...'
23+
# github.orgs.teams.list
24+
#
1425
# List teams
1526
#
16-
# = Examples
17-
# github = Github.new :oauth_token => '...'
18-
# github.orgs.teams.list 'org-name'
27+
# @example
28+
# github = Github.new oauth_token: '...'
29+
# github.orgs.teams.list org: 'org-name'
1930
#
31+
# @api public
2032
def list(*args)
21-
arguments(args, :required => [:org_name])
33+
params = arguments(args).params
2234

23-
response = get_request("/orgs/#{org_name}/teams", arguments.params)
35+
response = if org = params.delete('org')
36+
get_request("/orgs/#{org}/teams", params)
37+
else
38+
get_request("/user/teams", params)
39+
end
2440
return response unless block_given?
2541
response.each { |el| yield el }
2642
end
2743
alias :all :list
2844

2945
# Get a team
3046
#
31-
# = Examples
32-
# github = Github.new :oauth_token => '...'
33-
# github.orgs.teams.get 'team-id'
47+
# @example
48+
# github = Github.new oauth_token: '...'
49+
# github.orgs.teams.get 'team-id'
3450
#
51+
# @api public
3552
def get(*args)
36-
arguments(args, :required => [:team_id])
53+
arguments(args, required: [:id])
3754

38-
get_request("/teams/#{team_id}", arguments.params)
55+
get_request("/teams/#{arguments.id}", arguments.params)
3956
end
4057
alias :find :get
4158

4259
# Create a team
4360
#
44-
# In order to create a team, the authenticated user must be an owner of<tt>:org</tt>.
45-
# = Inputs
46-
# <tt>:name</tt> - Required string
47-
# <tt>:repo_names</tt> - Optional array of strings
48-
# <tt>:permission</tt> - Optional string
49-
# * <tt>pull</tt> - team members can pull, but not push or administor this repositories. Default
50-
# * <tt>push</tt> - team members can pull and push, but not administor this repositores.
51-
# * <tt>admin</tt> - team members can pull, push and administor these repositories.
52-
#
53-
# = Examples
54-
# github = Github.new :oauth_token => '...'
55-
# github.orgs.teams.create 'org-name',
56-
# "name" => "new team",
57-
# "permission" => "push",
58-
# "repo_names" => [
59-
# "github/dotfiles"
61+
# In order to create a team, the authenticated user must be an owner of :org
62+
#
63+
# @param [Hash] params
64+
# @input params [String] :name
65+
# Required. The name of the team
66+
# @input params [Array[String]] :repo_names
67+
# The repositories to add the team to.
68+
# @input params [String] :permission
69+
# The permission to grant the team. Can be one of:
70+
# * pull - team members can pull, but not push or
71+
# administor this repositories.
72+
# * push - team members can pull and push,
73+
# but not administor this repositores.
74+
# * admin - team members can pull, push and
75+
# administor these repositories.
76+
# Default: pull
77+
#
78+
# @example
79+
# github = Github.new oauth_token: '...'
80+
# github.orgs.teams.create 'org-name',
81+
# name: "new team",
82+
# permission: "push",
83+
# repo_names: [
84+
# "github/dotfiles"
6085
# ]
6186
#
87+
# @api public
6288
def create(*args)
63-
arguments(args, :required => [:org_name]) do
64-
sift VALID_TEAM_PARAM_NAMES
89+
arguments(args, required: [:org]) do
90+
permit VALID_TEAM_PARAM_NAMES
6591
assert_values VALID_TEAM_PARAM_VALUES
6692
assert_required %w[name]
6793
end
6894

69-
post_request("/orgs/#{org_name}/teams", arguments.params)
95+
post_request("/orgs/#{arguments.org}/teams", arguments.params)
7096
end
7197

7298
# Edit a team
73-
# In order to edit a team, the authenticated user must be an owner of the org that the team is associated with.
7499
#
75-
# = Inputs
76-
# <tt>:name</tt> - Required string
77-
# <tt>:permission</tt> - Optional string
78-
# * <tt>pull</tt> - team members can pull, but not push or administor this repositories. Default
79-
# * <tt>push</tt> - team members can pull and push, but not administor this repositores.
80-
# * <tt>admin</tt> - team members can pull, push and administor these repositories.
100+
# In order to edit a team, the authenticated user must be an owner
101+
# of the org that the team is associated with.
102+
#
103+
# @param [Hash] params
104+
# @input params [String] :name
105+
# The repositories to add the team to.
106+
# @input params [String] :permission
107+
# The permission to grant the team. Can be one of:
108+
# * pull - team members can pull, but not push or
109+
# administor this repositories.
110+
# * push - team members can pull and push,
111+
# but not administor this repositores.
112+
# * admin - team members can pull, push and
113+
# administor these repositories.
114+
# Default: pull
81115
#
82-
# = Examples
83-
# github = Github.new :oauth_token => '...'
116+
# @example
117+
# github = Github.new oauth_token: '...'
84118
# github.orgs.teams.edit 'team-id',
85-
# "name" => "new team name",
86-
# "permission" => "push"
119+
# name: "new team name",
120+
# permission: "push"
87121
#
122+
# @api public
88123
def edit(*args)
89-
arguments(args, :required => [:team_id]) do
90-
sift VALID_TEAM_PARAM_NAMES
124+
arguments(args, required: [:id]) do
125+
permit VALID_TEAM_PARAM_NAMES
91126
assert_values VALID_TEAM_PARAM_VALUES
92127
assert_required %w[name]
93128
end
94129

95-
patch_request("/teams/#{team_id}", arguments.params)
130+
patch_request("/teams/#{arguments.id}", arguments.params)
96131
end
97132

98133
# Delete a team
99-
# In order to delete a team, the authenticated user must be an owner of the org that the team is associated with
100134
#
101-
# = Examples
102-
# github = Github.new :oauth_token => '...'
103-
# github.orgs.teams.delete 'team-id'
135+
# In order to delete a team, the authenticated user must be an owner
136+
# of the org that the team is associated with
137+
#
138+
# @example
139+
# github = Github.new oauth_token: '...'
140+
# github.orgs.teams.delete 'team-id'
104141
#
105142
def delete(*args)
106-
arguments(args, :required => [:team_id])
143+
arguments(args, required: [:id])
107144

108-
delete_request("/teams/#{team_id}", arguments.params)
145+
delete_request("/teams/#{arguments.id}", arguments.params)
109146
end
110147
alias :remove :delete
111148

112149
# List team members
113-
# In order to list members in a team, the authenticated user must be a member of the team.
114150
#
115-
# = Examples
116-
# github = Github.new :oauth_token => '...'
117-
# github.orgs.teams.list_members 'team-id'
118-
# github.orgs.teams.list_members 'team-id' { |member| ... }
151+
# In order to list members in a team, the authenticated user
152+
# must be a member of the team.
119153
#
154+
# @example
155+
# github = Github.new oauth_token: '...'
156+
# github.orgs.teams.list_members 'team-id'
157+
# github.orgs.teams.list_members 'team-id' { |member| ... }
158+
#
159+
# @api public
120160
def list_members(*args)
121-
arguments(args, :required => [:team_id])
161+
arguments(args, required: [:id])
122162

123-
response = get_request("/teams/#{team_id}/members", arguments.params)
163+
response = get_request("/teams/#{arguments.id}/members", arguments.params)
124164
return response unless block_given?
125165
response.each { |el| yield el }
126166
end
127167
alias :all_members :list_members
128168

129169
# Check if a user is a member of a team
130170
#
131-
# = Examples
132-
# github = Github.new :oauth_token => '...'
133-
# github.orgs.teams.team_member? 'team-id', 'user-name'
171+
# @example
172+
# github = Github.new oauth_token: '...'
173+
# github.orgs.teams.team_member? 'team-id', 'user-name'
134174
#
175+
# @api public
135176
def team_member?(*args)
136-
arguments(args, :required => [:team_id, :user])
177+
arguments(args, required: [:id, :user])
137178

138-
response = get_request("/teams/#{team_id}/members/#{user}", arguments.params)
179+
response = get_request("/teams/#{arguments.id}/members/#{arguments.user}", arguments.params)
139180
response.status == 204
140181
rescue Github::Error::NotFound
141182
false
142183
end
143184

144185
# Add a team member
145-
# In order to add a user to a team, the authenticated user must have ‘admin’ permissions to the team or be an owner of the org that the team is associated with.
146186
#
147-
# = Examples
148-
# github = Github.new :oauth_token => '...'
187+
# In order to add a user to a team, the authenticated user must
188+
# have ‘admin’ permissions to the team or be an owner of the org
189+
# that the team is associated with.
190+
#
191+
# @example
192+
# github = Github.new oauth_token: '...'
149193
# github.orgs.teams.add_member 'team-id', 'user-name'
150194
#
195+
# @api public
151196
def add_member(*args)
152-
arguments(args, :required => [:team_id, :user])
197+
arguments(args, required: [:id, :user])
153198

154-
put_request("/teams/#{team_id}/members/#{user}", arguments.params)
199+
put_request("/teams/#{arguments.id}/members/#{arguments.user}", arguments.params)
155200
end
156201
alias :add_team_member :add_member
157202

@@ -162,42 +207,45 @@ def add_member(*args)
162207
# the team is associated with.
163208
# note: This does not delete the user, it just remove them from the team.
164209
#
165-
# = Examples
166-
# github = Github.new :oauth_token => '...'
210+
# @example
211+
# github = Github.new oauth_token: '...'
167212
# github.orgs.teams.remove_member 'team-id', 'user-name'
168213
#
214+
# @api public
169215
def remove_member(*args)
170-
arguments(args, :required => [:team_id, :user])
216+
arguments(args, required: [:id, :user])
171217

172-
delete_request("/teams/#{team_id}/members/#{user}", arguments.params)
218+
delete_request("/teams/#{arguments.id}/members/#{arguments.user}", arguments.params)
173219
end
174220
alias :remove_team_member :remove_member
175221

176222
# List team repositories
177223
#
178-
# = Examples
179-
# github = Github.new :oauth_token => '...'
224+
# @example
225+
# github = Github.new oauth_token: '...'
180226
# github.orgs.teams.list_repos 'team-id'
181227
#
228+
# @api public
182229
def list_repos(*args)
183-
arguments(args, :required => [:team_id])
230+
arguments(args, required: [:id])
184231

185-
response = get_request("/teams/#{team_id}/repos", arguments.params)
232+
response = get_request("/teams/#{arguments.id}/repos", arguments.params)
186233
return response unless block_given?
187234
response.each { |el| yield el }
188235
end
189236
alias :repos :list_repos
190237

191238
# Check if a repository belongs to a team
192239
#
193-
# = Examples
194-
# github = Github.new :oauth_token => '...'
240+
# @example
241+
# github = Github.new oauth_token: '...'
195242
# github.orgs.teams.team_repo? 'team-id', 'user-name', 'repo-name'
196243
#
244+
# @api public
197245
def team_repo?(*args)
198-
arguments(args, :required => [:team_id, :user, :repo])
246+
arguments(args, required: [:id, :user, :repo])
199247

200-
response = get_request("/teams/#{team_id}/repos/#{user}/#{repo}", arguments.params)
248+
response = get_request("/teams/#{arguments.id}/repos/#{arguments.user}/#{arguments.repo}", arguments.params)
201249
response.status == 204
202250
rescue Github::Error::NotFound
203251
false
@@ -211,14 +259,15 @@ def team_repo?(*args)
211259
# must be owned by the organization, or a direct for of a repo owned
212260
# by the organization.
213261
#
214-
# = Examples
215-
# github = Github.new :oauth_token => '...'
262+
# @example
263+
# github = Github.new oauth_token: '...'
216264
# github.orgs.teams.add_repo 'team-id', 'user-name', 'repo-name'
217265
#
266+
# @api public
218267
def add_repo(*args)
219-
arguments(args, :required => [:team_id, :user, :repo])
268+
arguments(args, required: [:id, :user, :repo])
220269

221-
put_request("/teams/#{team_id}/repos/#{user}/#{repo}", arguments.params)
270+
put_request("/teams/#{arguments.id}/repos/#{arguments.user}/#{arguments.repo}", arguments.params)
222271
end
223272
alias :add_repository :add_repo
224273

@@ -228,14 +277,15 @@ def add_repo(*args)
228277
# an owner of the org that the team is associated with.
229278
# note: This does not delete the repo, it just removes it from the team.
230279
#
231-
# = Examples
232-
# github = Github.new :oauth_token => '...'
280+
# @example
281+
# github = Github.new oauth_token: '...'
233282
# github.orgs.teams.remove_repo 'team-id', 'user-name', 'repo-name'
234283
#
284+
# @api public
235285
def remove_repo(*args)
236-
arguments(args, :required => [:team_id, :user, :repo])
286+
arguments(args, required: [:id, :user, :repo])
237287

238-
delete_request("/teams/#{team_id}/repos/#{user}/#{repo}", arguments.params)
288+
delete_request("/teams/#{arguments.id}/repos/#{arguments.user}/#{arguments.repo}", arguments.params)
239289
end
240290
alias :remove_repository :remove_repo
241291
end # Client::Orgs::Teams

spec/github/client/orgs/teams/delete_spec.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,4 @@
3232
it_should_behave_like 'request failure' do
3333
let(:requestable) { subject.delete team_id }
3434
end
35-
3635
end # delete

spec/github/client/orgs/teams/get_spec.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,4 @@
4141
it_should_behave_like 'request failure' do
4242
let(:requestable) { subject.get team }
4343
end
44-
4544
end # get

0 commit comments

Comments
 (0)