1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Part of the Joomla Framework GitHub Package |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved. |
6
|
|
|
* @license GNU General Public License version 2 or later; see LICENSE |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Joomla\Github\Package\Repositories; |
10
|
|
|
|
11
|
|
|
use Joomla\Github\AbstractPackage; |
12
|
|
|
use Joomla\Http\Exception\UnexpectedResponseException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* GitHub API Repositories Commits class for the Joomla Framework. |
16
|
|
|
* |
17
|
|
|
* @documentation http://developer.github.com/v3/repos/commits |
18
|
|
|
* |
19
|
|
|
* @since 1.0 |
20
|
|
|
*/ |
21
|
|
|
class Commits extends AbstractPackage |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* List commits on a repository. |
25
|
|
|
* |
26
|
|
|
* A special note on pagination: Due to the way Git works, commits are paginated based on SHA |
27
|
|
|
* instead of page number. |
28
|
|
|
* Please follow the link headers as outlined in the pagination overview instead of constructing |
29
|
|
|
* page links yourself. |
30
|
|
|
* |
31
|
|
|
* @param string $user The name of the owner of the GitHub repository. |
32
|
|
|
* @param string $repo The name of the GitHub repository. |
33
|
|
|
* @param string $sha Sha or branch to start listing commits from. |
34
|
|
|
* @param string $path Only commits containing this file path will be returned. |
35
|
|
|
* @param string $author GitHub login, name, or email by which to filter by commit author. |
36
|
|
|
* @param \DateTime $since ISO 8601 Date - Only commits after this date will be returned. |
37
|
|
|
* @param \DateTime $until ISO 8601 Date - Only commits before this date will be returned. |
38
|
|
|
* |
39
|
|
|
* @return object |
40
|
|
|
* |
41
|
|
|
* @since 1.0 |
42
|
|
|
* @throws \DomainException |
43
|
|
|
*/ |
44
|
|
|
public function getList($user, $repo, $sha = '', $path = '', $author = '', \DateTime $since = null, \DateTime $until = null) |
45
|
|
|
{ |
46
|
|
|
// Build the request path. |
47
|
|
|
$rPath = '/repos/' . $user . '/' . $repo . '/commits?'; |
48
|
|
|
|
49
|
|
|
$rPath .= ($sha) ? '&sha=' . $sha : ''; |
50
|
|
|
$rPath .= ($path) ? '&path=' . $path : ''; |
51
|
|
|
$rPath .= ($author) ? '&author=' . $author : ''; |
52
|
|
|
$rPath .= ($since) ? '&since=' . $since->format(\DateTime::RFC3339) : ''; |
53
|
|
|
$rPath .= ($until) ? '&until=' . $until->format(\DateTime::RFC3339) : ''; |
54
|
|
|
|
55
|
|
|
// Send the request. |
56
|
|
|
return $this->processResponse($this->client->get($this->fetchUrl($rPath))); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get a single commit. |
61
|
|
|
* |
62
|
|
|
* @param string $user The name of the owner of the GitHub repository. |
63
|
|
|
* @param string $repo The name of the GitHub repository. |
64
|
|
|
* @param string $sha The SHA of the commit to retrieve. |
65
|
|
|
* |
66
|
|
|
* @return object |
67
|
|
|
* |
68
|
|
|
* @since 1.0 |
69
|
|
|
* @throws \DomainException |
70
|
|
|
*/ |
71
|
|
|
public function get($user, $repo, $sha) |
72
|
|
|
{ |
73
|
|
|
// Build the request path. |
74
|
|
|
$path = '/repos/' . $user . '/' . $repo . '/commits/' . $sha; |
75
|
|
|
|
76
|
|
|
// Send the request. |
77
|
|
|
return $this->processResponse($this->client->get($this->fetchUrl($path))); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get the SHA-1 of a commit reference. |
82
|
|
|
* |
83
|
|
|
* @param string $user The name of the owner of the GitHub repository. |
84
|
|
|
* @param string $repo The name of the GitHub repository. |
85
|
|
|
* @param string $ref The commit reference |
86
|
|
|
* |
87
|
|
|
* @return string |
88
|
|
|
* |
89
|
|
|
* @since 1.4.0 |
90
|
|
|
* @throws UnexpectedResponseException |
91
|
|
|
*/ |
92
|
|
|
public function getSha($user, $repo, $ref) |
93
|
|
|
{ |
94
|
|
|
// Build the request path. |
95
|
|
|
$path = '/repos/' . $user . '/' . $repo . '/commits/' . $ref; |
96
|
|
|
|
97
|
|
|
// Send the request. |
98
|
|
|
$response = $this->client->get($this->fetchUrl($path)); |
99
|
|
|
|
100
|
|
|
// Validate the response code. |
101
|
|
View Code Duplication |
if ($response->code != 200) |
|
|
|
|
102
|
|
|
{ |
103
|
|
|
// Decode the error response and throw an exception. |
104
|
|
|
$error = json_decode($response->body); |
105
|
|
|
$message = isset($error->message) ? $error->message : 'Invalid response received from GitHub.'; |
106
|
|
|
throw new UnexpectedResponseException($response, $message, $response->code); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $response->body; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Compare two commits. |
114
|
|
|
* |
115
|
|
|
* @param string $user The name of the owner of the GitHub repository. |
116
|
|
|
* @param string $repo The name of the GitHub repository. |
117
|
|
|
* @param string $base The base of the diff, either a commit SHA or branch. |
118
|
|
|
* @param string $head The head of the diff, either a commit SHA or branch. |
119
|
|
|
* |
120
|
|
|
* @return object |
121
|
|
|
* |
122
|
|
|
* @since 1.0 |
123
|
|
|
*/ |
124
|
|
|
public function compare($user, $repo, $base, $head) |
125
|
|
|
{ |
126
|
|
|
// Build the request path. |
127
|
|
|
$path = '/repos/' . $user . '/' . $repo . '/compare/' . $base . '...' . $head; |
128
|
|
|
|
129
|
|
|
// Send the request. |
130
|
|
|
return $this->processResponse( |
131
|
|
|
$this->client->get($this->fetchUrl($path)) |
132
|
|
|
); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.