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 Activity class for the Joomla Platform.
 14  *
 15  * @since       3.3 (CMS)
 16  * @deprecated  4.0  Use the `joomla/github` package via Composer instead
 17  *
 18  * @documentation  https://developer.github.com/v3/repos
 19  *
 20  * @property-read  JGithubPackageRepositoriesCollaborators  $collaborators  GitHub API object for collaborators.
 21  * @property-read  JGithubPackageRepositoriesComments       $comments       GitHub API object for comments.
 22  * @property-read  JGithubPackageRepositoriesCommits        $commits        GitHub API object for commits.
 23  * @property-read  JGithubPackageRepositoriesContents       $contents       GitHub API object for contents.
 24  * @property-read  JGithubPackageRepositoriesDownloads      $downloads      GitHub API object for downloads.
 25  * @property-read  JGithubPackageRepositoriesForks          $forks          GitHub API object for forks.
 26  * @property-read  JGithubPackageRepositoriesHooks          $hooks          GitHub API object for hooks.
 27  * @property-read  JGithubPackageRepositoriesKeys           $keys           GitHub API object for keys.
 28  * @property-read  JGithubPackageRepositoriesMerging        $merging        GitHub API object for merging.
 29  * @property-read  JGithubPackageRepositoriesStatuses       $statuses       GitHub API object for statuses.
 30  */
 31 class JGithubPackageRepositories extends JGithubPackage
 32 {
 33     protected $name = 'Repositories';
 34 
 35     protected $packages = array('collaborators', 'comments', 'commits', 'contents', 'downloads', 'forks', 'hooks', 'keys', 'merging', 'statuses');
 36 
 37     /**
 38      * List your repositories.
 39      *
 40      * List repositories for the authenticated user.
 41      *
 42      * @param   string  $type       Sort type. all, owner, public, private, member. Default: all.
 43      * @param   string  $sort       Sort field. created, updated, pushed, full_name, default: full_name.
 44      * @param   string  $direction  Sort direction. asc or desc, default: when using full_name: asc, otherwise desc.
 45      *
 46      * @throws RuntimeException
 47      *
 48      * @return object
 49      */
 50     public function getListOwn($type = 'all', $sort = 'full_name', $direction = '')
 51     {
 52         if (false == in_array($type, array('all', 'owner', 'public', 'private', 'member')))
 53         {
 54             throw new RuntimeException('Invalid type');
 55         }
 56 
 57         if (false == in_array($sort, array('created', 'updated', 'pushed', 'full_name')))
 58         {
 59             throw new RuntimeException('Invalid sort field');
 60         }
 61 
 62         // Sort direction default: when using full_name: asc, otherwise desc.
 63         $direction = ($direction) ? : (('full_name' == $sort) ? 'asc' : 'desc');
 64 
 65         if (false == in_array($direction, array('asc', 'desc')))
 66         {
 67             throw new RuntimeException('Invalid sort order');
 68         }
 69 
 70         // Build the request path.
 71         $path = '/user/repos'
 72             . '?type=' . $type
 73             . '&sort=' . $sort
 74             . '&direction=' . $direction;
 75 
 76         // Send the request.
 77         return $this->processResponse(
 78             $this->client->get($this->fetchUrl($path))
 79         );
 80     }
 81 
 82     /**
 83      * List user repositories.
 84      *
 85      * List public repositories for the specified user.
 86      *
 87      * @param   string  $user       The user name.
 88      * @param   string  $type       Sort type. all, owner, member. Default: all.
 89      * @param   string  $sort       Sort field. created, updated, pushed, full_name, default: full_name.
 90      * @param   string  $direction  Sort direction. asc or desc, default: when using full_name: asc, otherwise desc.
 91      *
 92      * @throws RuntimeException
 93      *
 94      * @return object
 95      */
 96     public function getListUser($user, $type = 'all', $sort = 'full_name', $direction = '')
 97     {
 98         if (false == in_array($type, array('all', 'owner', 'member')))
 99         {
100             throw new RuntimeException('Invalid type');
101         }
102 
103         if (false == in_array($sort, array('created', 'updated', 'pushed', 'full_name')))
104         {
105             throw new RuntimeException('Invalid sort field');
106         }
107 
108         // Sort direction default: when using full_name: asc, otherwise desc.
109         $direction = ($direction) ? : (('full_name' == $sort) ? 'asc' : 'desc');
110 
111         if (false == in_array($direction, array('asc', 'desc')))
112         {
113             throw new RuntimeException('Invalid sort order');
114         }
115 
116         // Build the request path.
117         $path = '/users/' . $user . '/repos'
118             . '?type=' . $type
119             . '&sort=' . $sort
120             . '&direction=' . $direction;
121 
122         // Send the request.
123         return $this->processResponse(
124             $this->client->get($this->fetchUrl($path))
125         );
126     }
127 
128     /**
129      * List organization repositories.
130      *
131      * List repositories for the specified org.
132      *
133      * @param   string  $org   The name of the organization.
134      * @param   string  $type  Sort type. all, public, private, forks, sources, member. Default: all.
135      *
136      * @throws RuntimeException
137      *
138      * @return object
139      */
140     public function getListOrg($org, $type = 'all')
141     {
142         if (false == in_array($type, array('all', 'public', 'private', 'forks', 'sources', 'member')))
143         {
144             throw new RuntimeException('Invalid type');
145         }
146 
147         // Build the request path.
148         $path = '/orgs/' . $org . '/repos'
149             . '?type=' . $type;
150 
151         // Send the request.
152         return $this->processResponse(
153             $this->client->get($this->fetchUrl($path))
154         );
155     }
156 
157     /**
158      * List all repositories.
159      *
160      * This provides a dump of every repository, in the order that they were created.
161      *
162      * @param   integer  $id  The integer ID of the last Repository that you’ve seen.
163      *
164      * @throws RuntimeException
165      *
166      * @return object
167      */
168     public function getList($id = 0)
169     {
170         // Build the request path.
171         $path = '/repositories';
172         $path .= ($id) ? '?since=' . (int) $id : '';
173 
174         // Send the request.
175         return $this->processResponse(
176             $this->client->get($this->fetchUrl($path))
177         );
178     }
179 
180     /**
181      * Create a new repository for the authenticated user or an organization.
182      * OAuth users must supply repo scope.
183      *
184      * @param   string   $name                The repository name.
185      * @param   string   $org                 The organization name (if needed).
186      * @param   string   $description         The repository description.
187      * @param   string   $homepage            The repository homepage.
188      * @param   boolean  $private             Set true to create a private repository, false to create a public one.
189      *                                          Creating private repositories requires a paid GitHub account.
190      * @param   boolean  $has_issues          Set true to enable issues for this repository, false to disable them.
191      * @param   boolean  $has_wiki            Set true to enable the wiki for this repository, false to disable it.
192      * @param   boolean  $has_downloads       Set true to enable downloads for this repository, false to disable them.
193      * @param   integer  $team_id             The id of the team that will be granted access to this repository.
194      *                                        This is only valid when creating a repo in an organization.
195      * @param   boolean  $auto_init           true to create an initial commit with empty README.
196      * @param   string   $gitignore_template  Desired language or platform .gitignore template to apply.
197      *                                         Use the name of the template without the extension. For example,
198      *                                        “Haskell” Ignored if auto_init parameter is not provided.
199      *
200      * @return object
201      */
202     public function create($name, $org = '', $description = '', $homepage = '', $private = false, $has_issues = false,
203         $has_wiki = false, $has_downloads = false, $team_id = 0, $auto_init = false, $gitignore_template = '')
204     {
205         $path = ($org)
206             // Create a repository for an organization
207             ? '/orgs/' . $org . '/repos'
208             // Create a repository for a user
209             : '/user/repos';
210 
211         $data = array(
212             'name'               => $name,
213             'description'        => $description,
214             'homepage'           => $homepage,
215             'private'            => $private,
216             'has_issues'         => $has_issues,
217             'has_wiki'           => $has_wiki,
218             'has_downloads'      => $has_downloads,
219             'team_id'            => $team_id,
220             'auto_init'          => $auto_init,
221             'gitignore_template' => $gitignore_template,
222         );
223 
224         // Send the request.
225         return $this->processResponse(
226             $this->client->post($this->fetchUrl($path), json_encode($data)),
227             201
228         );
229     }
230 
231     /**
232      * Get a repository.
233      *
234      * @param   string  $owner  Repository owner.
235      * @param   string  $repo   Repository name.
236      *
237      * @return object
238      */
239     public function get($owner, $repo)
240     {
241         // Build the request path.
242         $path = '/repos/' . $owner . '/' . $repo;
243 
244         // Send the request.
245         return $this->processResponse(
246             $this->client->get($this->fetchUrl($path))
247         );
248     }
249 
250     /**
251      * Edit a repository.
252      *
253      * @param   string   $owner           Repository owner.
254      * @param   string   $repo            Repository name.
255      * @param   string   $name            The repository name.
256      * @param   string   $description     The repository description.
257      * @param   string   $homepage        The repository homepage.
258      * @param   boolean  $private         Set true to create a private repository, false to create a public one.
259      *                                    Creating private repositories requires a paid GitHub account.
260      * @param   boolean  $has_issues      Set true to enable issues for this repository, false to disable them.
261      * @param   boolean  $has_wiki        Set true to enable the wiki for this repository, false to disable it.
262      * @param   boolean  $has_downloads   Set true to enable downloads for this repository, false to disable them.
263      * @param   string   $default_branch  Update the default branch for this repository
264      *
265      * @return object
266      */
267     public function edit($owner, $repo, $name, $description = '', $homepage = '', $private = false, $has_issues = false,
268         $has_wiki = false, $has_downloads = false, $default_branch = '')
269     {
270         $path = '/repos/' . $owner . '/' . $repo;
271 
272         $data = array(
273             'name'           => $name,
274             'description'    => $description,
275             'homepage'       => $homepage,
276             'private'        => $private,
277             'has_issues'     => $has_issues,
278             'has_wiki'       => $has_wiki,
279             'has_downloads'  => $has_downloads,
280             'default_branch' => $default_branch,
281         );
282 
283         // Send the request.
284         return $this->processResponse(
285             $this->client->patch($this->fetchUrl($path), json_encode($data))
286         );
287     }
288 
289     /**
290      * List contributors.
291      *
292      * @param   string   $owner  Repository owner.
293      * @param   string   $repo   Repository name.
294      * @param   boolean  $anon   Set to 1 or true to include anonymous contributors in results.
295      *
296      * @return object
297      */
298     public function getListContributors($owner, $repo, $anon = false)
299     {
300         // Build the request path.
301         $path = '/repos/' . $owner . '/' . $repo . '/contributors';
302 
303         $path .= ($anon) ? '?anon=true' : '';
304 
305         // Send the request.
306         return $this->processResponse(
307             $this->client->get($this->fetchUrl($path))
308         );
309     }
310 
311     /**
312      * List languages.
313      *
314      * List languages for the specified repository. The value on the right of a language is the number of bytes of code
315      * written in that language.
316      *
317      * @param   string  $owner  Repository owner.
318      * @param   string  $repo   Repository name.
319      *
320      * @return object
321      */
322     public function getListLanguages($owner, $repo)
323     {
324         // Build the request path.
325         $path = '/repos/' . $owner . '/' . $repo . '/languages';
326 
327         // Send the request.
328         return $this->processResponse(
329             $this->client->get($this->fetchUrl($path))
330         );
331     }
332 
333     /**
334      * List Teams
335      *
336      * @param   string  $owner  Repository owner.
337      * @param   string  $repo   Repository name.
338      *
339      * @return object
340      */
341     public function getListTeams($owner, $repo)
342     {
343         // Build the request path.
344         $path = '/repos/' . $owner . '/' . $repo . '/teams';
345 
346         // Send the request.
347         return $this->processResponse(
348             $this->client->get($this->fetchUrl($path))
349         );
350     }
351 
352     /**
353      * List Tags.
354      *
355      * @param   string  $owner  Repository owner.
356      * @param   string  $repo   Repository name.
357      *
358      * @return object
359      */
360     public function getListTags($owner, $repo)
361     {
362         // Build the request path.
363         $path = '/repos/' . $owner . '/' . $repo . '/tags';
364 
365         // Send the request.
366         return $this->processResponse(
367             $this->client->get($this->fetchUrl($path))
368         );
369     }
370 
371     /**
372      * List Branches.
373      *
374      * @param   string  $owner  Repository owner.
375      * @param   string  $repo   Repository name.
376      *
377      * @return object
378      */
379     public function getListBranches($owner, $repo)
380     {
381         // Build the request path.
382         $path = '/repos/' . $owner . '/' . $repo . '/branches';
383 
384         // Send the request.
385         return $this->processResponse(
386             $this->client->get($this->fetchUrl($path))
387         );
388     }
389 
390     /**
391      * Get a Branch.
392      *
393      * @param   string  $owner   Repository owner.
394      * @param   string  $repo    Repository name.
395      * @param   string  $branch  Branch name.
396      *
397      * @return object
398      */
399     public function getBranch($owner, $repo, $branch)
400     {
401         // Build the request path.
402         $path = '/repos/' . $owner . '/' . $repo . '/branches/' . $branch;
403 
404         // Send the request.
405         return $this->processResponse(
406             $this->client->get($this->fetchUrl($path))
407         );
408     }
409 
410     /**
411      * Delete a Repository.
412      *
413      * Deleting a repository requires admin access. If OAuth is used, the delete_repo scope is required.
414      *
415      * @param   string  $owner  Repository owner.
416      * @param   string  $repo   Repository name.
417      *
418      * @return object
419      */
420     public function delete($owner, $repo)
421     {
422         // Build the request path.
423         $path = '/repos/' . $owner . '/' . $repo;
424 
425         // Send the request.
426         return $this->processResponse(
427             $this->client->delete($this->fetchUrl($path))
428         );
429     }
430 }
431