1 <?php
2 /**
3 * @package Joomla.Platform
4 * @subpackage GitHub
5 *
6 * @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
7 * @license GNU General Public License version 2 or later; see LICENSE
8 */
9
10 defined('JPATH_PLATFORM') or die;
11
12 /**
13 * GitHub API Orgs Teams class for the Joomla Platform.
14 *
15 * All actions against teams require at a minimum an authenticated user who is a member
16 * of the owner’s team in the :org being managed. Additionally, OAuth users require “user” scope.
17 *
18 * @documentation https://developer.github.com/v3/orgs/teams/
19 *
20 * @since 12.3
21 * @deprecated 4.0 Use the `joomla/github` package via Composer instead
22 */
23 class JGithubPackageOrgsTeams extends JGithubPackage
24 {
25 /**
26 * List teams.
27 *
28 * @param string $org The name of the organization.
29 *
30 * @since 3.3 (CMS)
31 *
32 * @return object
33 */
34 public function getList($org)
35 {
36 // Build the request path.
37 $path = '/orgs/' . $org . '/teams';
38
39 return $this->processResponse(
40 $this->client->get($this->fetchUrl($path))
41 );
42 }
43
44 /**
45 * Get team.
46 *
47 * @param integer $id The team id.
48 *
49 * @since 3.3 (CMS)
50 *
51 * @return object
52 */
53 public function get($id)
54 {
55 // Build the request path.
56 $path = '/teams/' . (int) $id;
57
58 return $this->processResponse(
59 $this->client->get($this->fetchUrl($path))
60 );
61 }
62
63 /**
64 * Create team.
65 *
66 * In order to create a team, the authenticated user must be an owner of the organization.
67 *
68 * @param string $org The name of the organization.
69 * @param string $name The name of the team.
70 * @param array $repoNames Repository names.
71 * @param string $permission The permission.
72 * pull - team members can pull, but not push to or administer these repositories. Default
73 * push - team members can pull and push, but not administer these repositories.
74 * admin - team members can pull, push and administer these repositories.
75 *
76 * @throws UnexpectedValueException
77 *
78 * @since 3.3 (CMS)
79 *
80 * @return object
81 */
82 public function create($org, $name, array $repoNames = array(), $permission = '')
83 {
84 // Build the request path.
85 $path = '/orgs/' . $org . '/teams';
86
87 $data = array(
88 'name' => $name,
89 );
90
91 if ($repoNames)
92 {
93 $data['repo_names'] = $repoNames;
94 }
95
96 if ($permission)
97 {
98 if (false == in_array($permission, array('pull', 'push', 'admin')))
99 {
100 throw new UnexpectedValueException('Permissions must be either "pull", "push", or "admin".');
101 }
102
103 $data['permission'] = $permission;
104 }
105
106 return $this->processResponse(
107 $this->client->post($this->fetchUrl($path), $data),
108 201
109 );
110 }
111
112 /**
113 * Edit team.
114 *
115 * In order to edit a team, the authenticated user must be an owner of the org that the team is associated with.
116 *
117 * @param integer $id The team id.
118 * @param string $name The name of the team.
119 * @param string $permission The permission.
120 * pull - team members can pull, but not push to or administer these repositories. Default
121 * push - team members can pull and push, but not administer these repositories.
122 * admin - team members can pull, push and administer these repositories.
123 *
124 * @throws UnexpectedValueException
125 * @since 3.3 (CMS)
126 *
127 * @return object
128 */
129 public function edit($id, $name, $permission = '')
130 {
131 // Build the request path.
132 $path = '/teams/' . (int) $id;
133
134 $data = array(
135 'name' => $name,
136 );
137
138 if ($permission)
139 {
140 if (false == in_array($permission, array('pull', 'push', 'admin')))
141 {
142 throw new UnexpectedValueException('Permissions must be either "pull", "push", or "admin".');
143 }
144
145 $data['permission'] = $permission;
146 }
147
148 return $this->processResponse(
149 $this->client->patch($this->fetchUrl($path), $data)
150 );
151 }
152
153 /**
154 * Delete team.
155 *
156 * In order to delete a team, the authenticated user must be an owner of the org that the team is associated with.
157 *
158 * @param integer $id The team id.
159 *
160 * @since 3.3 (CMS)
161 *
162 * @return object
163 */
164 public function delete($id)
165 {
166 // Build the request path.
167 $path = '/teams/' . $id;
168
169 return $this->processResponse(
170 $this->client->delete($this->fetchUrl($path)),
171 204
172 );
173 }
174
175 /**
176 * List team members.
177 *
178 * In order to list members in a team, the authenticated user must be a member of the team.
179 *
180 * @param integer $id The team id.
181 *
182 * @since 3.3 (CMS)
183 *
184 * @return object
185 */
186 public function getListMembers($id)
187 {
188 // Build the request path.
189 $path = '/teams/' . $id . '/members';
190
191 return $this->processResponse(
192 $this->client->get($this->fetchUrl($path))
193 );
194 }
195
196 /**
197 * Get team member.
198 *
199 * In order to get if a user is a member of a team, the authenticated user must be a member of the team.
200 *
201 * @param integer $id The team id.
202 * @param string $user The name of the user.
203 *
204 * @throws UnexpectedValueException
205 * @since 3.3 (CMS)
206 *
207 * @return object
208 */
209 public function isMember($id, $user)
210 {
211 // Build the request path.
212 $path = '/teams/' . $id . '/members/' . $user;
213
214 $response = $this->client->get($this->fetchUrl($path));
215
216 switch ($response->code)
217 {
218 case 204 :
219 // Response if user is a member
220 return true;
221 break;
222
223 case 404 :
224 // Response if user is not a member
225 return false;
226 break;
227
228 default :
229 throw new UnexpectedValueException('Unexpected response code: ' . $response->code);
230 break;
231 }
232 }
233
234 /**
235 * Add team member.
236 *
237 * In order to add a user to a team, the authenticated user must have ‘admin’ permissions
238 * to the team or be an owner of the org that the team is associated with.
239 *
240 * @param integer $id The team id.
241 * @param string $user The name of the user.
242 *
243 * @since 3.3 (CMS)
244 *
245 * @return object
246 */
247 public function addMember($id, $user)
248 {
249 // Build the request path.
250 $path = '/teams/' . $id . '/members/' . $user;
251
252 return $this->processResponse(
253 $this->client->put($this->fetchUrl($path), ''),
254 204
255 );
256 }
257
258 /**
259 * Remove team member.
260 *
261 * In order to remove a user from a team, the authenticated user must have ‘admin’ permissions
262 * to the team or be an owner of the org that the team is associated with.
263 * NOTE: This does not delete the user, it just remove them from the team.
264 *
265 * @param integer $id The team id.
266 * @param string $user The name of the user.
267 *
268 * @since 3.3 (CMS)
269 *
270 * @return object
271 */
272 public function removeMember($id, $user)
273 {
274 // Build the request path.
275 $path = '/teams/' . $id . '/members/' . $user;
276
277 return $this->processResponse(
278 $this->client->delete($this->fetchUrl($path)),
279 204
280 );
281 }
282
283 /**
284 * List team repos.
285 *
286 * @param integer $id The team id.
287 *
288 * @since 3.3 (CMS)
289 *
290 * @return object
291 */
292 public function getListRepos($id)
293 {
294 // Build the request path.
295 $path = '/teams/' . $id . '/repos';
296
297 return $this->processResponse(
298 $this->client->get($this->fetchUrl($path))
299 );
300 }
301
302 /**
303 * Check if the repo is managed by this team.
304 *
305 * @param integer $id The team id.
306 * @param string $repo The name of the GitHub repository.
307 *
308 * @throws UnexpectedValueException
309 * @since 3.3 (CMS)
310 *
311 * @return object
312 */
313 public function checkRepo($id, $repo)
314 {
315 // Build the request path.
316 $path = '/teams/' . $id . '/repos/' . $repo;
317
318 $response = $this->client->get($this->fetchUrl($path));
319
320 switch ($response->code)
321 {
322 case 204 :
323 // Response if repo is managed by this team.
324 return true;
325 break;
326
327 case 404 :
328 // Response if repo is not managed by this team.
329 return false;
330 break;
331
332 default :
333 throw new UnexpectedValueException('Unexpected response code: ' . $response->code);
334 break;
335 }
336 }
337
338 /**
339 * Add team repo.
340 *
341 * In order to add a repo to a team, the authenticated user must be an owner of the
342 * org that the team is associated with. Also, the repo must be owned by the organization,
343 * or a direct form of a repo owned by the organization.
344 *
345 * If you attempt to add a repo to a team that is not owned by the organization, you get:
346 * Status: 422 Unprocessable Entity
347 *
348 * @param integer $id The team id.
349 * @param string $owner The name of the owner of the GitHub repository.
350 * @param string $repo The name of the GitHub repository.
351 *
352 * @since 3.3 (CMS)
353 *
354 * @return object
355 */
356 public function addRepo($id, $owner, $repo)
357 {
358 // Build the request path.
359 $path = '/teams/' . $id . '/repos/' . $owner . '/' . $repo;
360
361 return $this->processResponse(
362 $this->client->put($this->fetchUrl($path), ''),
363 204
364 );
365 }
366
367 /**
368 * Remove team repo.
369 *
370 * In order to remove a repo from a team, the authenticated user must be an owner
371 * of the org that the team is associated with. NOTE: This does not delete the
372 * repo, it just removes it from the team.
373 *
374 * @param integer $id The team id.
375 * @param string $owner The name of the owner of the GitHub repository.
376 * @param string $repo The name of the GitHub repository.
377 *
378 * @since 3.3 (CMS)
379 *
380 * @return object
381 */
382 public function removeRepo($id, $owner, $repo)
383 {
384 // Build the request path.
385 $path = '/teams/' . (int) $id . '/repos/' . $owner . '/' . $repo;
386
387 return $this->processResponse(
388 $this->client->delete($this->fetchUrl($path)),
389 204
390 );
391 }
392 }
393