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 Repositories Commits class for the Joomla Platform.
14 *
15 * @documentation https://developer.github.com/v3/repos/commits
16 *
17 * @since 11.3
18 * @deprecated 4.0 Use the `joomla/github` package via Composer instead
19 */
20 class JGithubPackageRepositoriesCommits extends JGithubPackage
21 {
22 /**
23 * Method to list commits for a repository.
24 *
25 * A special note on pagination: Due to the way Git works, commits are paginated based on SHA
26 * instead of page number.
27 * Please follow the link headers as outlined in the pagination overview instead of constructing
28 * page links yourself.
29 *
30 * @param string $user The name of the owner of the GitHub repository.
31 * @param string $repo The name of the GitHub repository.
32 * @param string $sha Sha or branch to start listing commits from.
33 * @param string $path Only commits containing this file path will be returned.
34 * @param string $author GitHub login, name, or email by which to filter by commit author.
35 * @param JDate $since ISO 8601 Date - Only commits after this date will be returned.
36 * @param JDate $until ISO 8601 Date - Only commits before this date will be returned.
37 *
38 * @throws DomainException
39 * @since 12.1
40 *
41 * @return array
42 */
43 public function getList($user, $repo, $sha = '', $path = '', $author = '', JDate $since = null, JDate $until = null)
44 {
45 // Build the request path.
46 $rPath = '/repos/' . $user . '/' . $repo . '/commits?';
47
48 $rPath .= ($sha) ? '&sha=' . $sha : '';
49 $rPath .= ($path) ? '&path=' . $path : '';
50 $rPath .= ($author) ? '&author=' . $author : '';
51 $rPath .= ($since) ? '&since=' . $since->toISO8601() : '';
52 $rPath .= ($until) ? '&until=' . $until->toISO8601() : '';
53
54 // Send the request.
55 $response = $this->client->get($this->fetchUrl($rPath));
56
57 // Validate the response code.
58 if ($response->code != 200)
59 {
60 // Decode the error response and throw an exception.
61 $error = json_decode($response->body);
62 throw new DomainException($error->message, $response->code);
63 }
64
65 return json_decode($response->body);
66 }
67
68 /**
69 * Method to get a single commit for a repository.
70 *
71 * @param string $user The name of the owner of the GitHub repository.
72 * @param string $repo The name of the GitHub repository.
73 * @param string $sha The SHA of the commit to retrieve.
74 *
75 * @throws DomainException
76 * @since 12.1
77 *
78 * @return array
79 */
80 public function get($user, $repo, $sha)
81 {
82 // Build the request path.
83 $path = '/repos/' . $user . '/' . $repo . '/commits/' . $sha;
84
85 // Send the request.
86 $response = $this->client->get($this->fetchUrl($path));
87
88 // Validate the response code.
89 if ($response->code != 200)
90 {
91 // Decode the error response and throw an exception.
92 $error = json_decode($response->body);
93 throw new DomainException($error->message, $response->code);
94 }
95
96 return json_decode($response->body);
97 }
98
99 /**
100 * Method to get a diff for two commits.
101 *
102 * @param string $user The name of the owner of the GitHub repository.
103 * @param string $repo The name of the GitHub repository.
104 * @param string $base The base of the diff, either a commit SHA or branch.
105 * @param string $head The head of the diff, either a commit SHA or branch.
106 *
107 * @return array
108 *
109 * @since 12.1
110 */
111 public function compare($user, $repo, $base, $head)
112 {
113 // Build the request path.
114 $path = '/repos/' . $user . '/' . $repo . '/compare/' . $base . '...' . $head;
115
116 // Send the request.
117 return $this->processResponse(
118 $this->client->get($this->fetchUrl($path))
119 );
120 }
121 }
122