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 Commits class for the Joomla Platform.
14 *
15 * @since 12.1
16 * @deprecated 4.0 Use the `joomla/github` package via Composer instead
17 */
18 class JGithubCommits extends JGithubObject
19 {
20 /**
21 * Method to create a commit.
22 *
23 * @param string $user The name of the owner of the GitHub repository.
24 * @param string $repo The name of the GitHub repository.
25 * @param string $message The commit message.
26 * @param string $tree SHA of the tree object this commit points to.
27 * @param array $parents Array of the SHAs of the commits that were the parents of this commit.
28 * If omitted or empty, the commit will be written as a root commit.
29 * For a single parent, an array of one SHA should be provided.
30 * For a merge commit, an array of more than one should be provided.
31 *
32 * @deprecated use data->commits->create()
33 *
34 * @return object
35 *
36 * @since 12.1
37 */
38 public function create($user, $repo, $message, $tree, array $parents = array())
39 {
40 // Build the request path.
41 $path = '/repos/' . $user . '/' . $repo . '/git/commits';
42
43 $data = json_encode(
44 array('message' => $message, 'tree' => $tree, 'parents' => $parents)
45 );
46
47 // Send the request.
48 $response = $this->client->post($this->fetchUrl($path), $data);
49
50 // Validate the response code.
51 if ($response->code != 201)
52 {
53 // Decode the error response and throw an exception.
54 $error = json_decode($response->body);
55 throw new DomainException($error->message, $response->code);
56 }
57
58 return json_decode($response->body);
59 }
60
61 /**
62 * Method to create a comment on a commit.
63 *
64 * @param string $user The name of the owner of the GitHub repository.
65 * @param string $repo The name of the GitHub repository.
66 * @param string $sha The SHA of the commit to comment on.
67 * @param string $comment The text of the comment.
68 * @param integer $line The line number of the commit to comment on.
69 * @param string $filepath A relative path to the file to comment on within the commit.
70 * @param integer $position Line index in the diff to comment on.
71 *
72 * @deprecated use repositories->comments->create()
73 *
74 * @return object
75 *
76 * @since 12.1
77 */
78 public function createCommitComment($user, $repo, $sha, $comment, $line, $filepath, $position)
79 {
80 // Build the request path.
81 $path = '/repos/' . $user . '/' . $repo . '/commits/' . $sha . '/comments';
82
83 $data = json_encode(
84 array(
85 'body' => $comment,
86 'commit_id' => $sha,
87 'line' => (int) $line,
88 'path' => $filepath,
89 'position' => (int) $position,
90 )
91 );
92
93 // Send the request.
94 $response = $this->client->post($this->fetchUrl($path), $data);
95
96 // Validate the response code.
97 if ($response->code != 201)
98 {
99 // Decode the error response and throw an exception.
100 $error = json_decode($response->body);
101 throw new DomainException($error->message, $response->code);
102 }
103
104 return json_decode($response->body);
105 }
106
107 /**
108 * Method to delete a comment on a commit.
109 *
110 * @param string $user The name of the owner of the GitHub repository.
111 * @param string $repo The name of the GitHub repository.
112 * @param string $id The ID of the comment to edit.
113 *
114 * @deprecated use repositories->comments->delete()
115 *
116 * @return object
117 *
118 * @since 12.1
119 */
120 public function deleteCommitComment($user, $repo, $id)
121 {
122 // Build the request path.
123 $path = '/repos/' . $user . '/' . $repo . '/comments/' . $id;
124
125 // Send the request.
126 $response = $this->client->delete($this->fetchUrl($path));
127
128 // Validate the response code.
129 if ($response->code != 204)
130 {
131 // Decode the error response and throw an exception.
132 $error = json_decode($response->body);
133 throw new DomainException($error->message, $response->code);
134 }
135
136 return json_decode($response->body);
137 }
138
139 /**
140 * Method to edit a comment on a commit.
141 *
142 * @param string $user The name of the owner of the GitHub repository.
143 * @param string $repo The name of the GitHub repository.
144 * @param string $id The ID of the comment to edit.
145 * @param string $comment The text of the comment.
146 *
147 * @deprecated use repositories->comments->edit()
148 *
149 * @return object
150 *
151 * @since 12.1
152 */
153 public function editCommitComment($user, $repo, $id, $comment)
154 {
155 // Build the request path.
156 $path = '/repos/' . $user . '/' . $repo . '/comments/' . $id;
157
158 $data = json_encode(
159 array(
160 'body' => $comment,
161 )
162 );
163
164 // Send the request.
165 $response = $this->client->patch($this->fetchUrl($path), $data);
166
167 // Validate the response code.
168 if ($response->code != 200)
169 {
170 // Decode the error response and throw an exception.
171 $error = json_decode($response->body);
172 throw new DomainException($error->message, $response->code);
173 }
174
175 return json_decode($response->body);
176 }
177
178 /**
179 * Method to get a single commit for a repository.
180 *
181 * @param string $user The name of the owner of the GitHub repository.
182 * @param string $repo The name of the GitHub repository.
183 * @param string $sha The SHA of the commit to retrieve.
184 * @param integer $page Page to request
185 * @param integer $limit Number of results to return per page
186 *
187 * @deprecated use repositories->commits->get()
188 *
189 * @return array
190 *
191 * @since 12.1
192 */
193 public function getCommit($user, $repo, $sha, $page = 0, $limit = 0)
194 {
195 // Build the request path.
196 $path = '/repos/' . $user . '/' . $repo . '/commits/' . $sha;
197
198 // Send the request.
199 $response = $this->client->get($this->fetchUrl($path, $page, $limit));
200
201 // Validate the response code.
202 if ($response->code != 200)
203 {
204 // Decode the error response and throw an exception.
205 $error = json_decode($response->body);
206 throw new DomainException($error->message, $response->code);
207 }
208
209 return json_decode($response->body);
210 }
211
212 /**
213 * Method to get a single comment on a commit.
214 *
215 * @param string $user The name of the owner of the GitHub repository.
216 * @param string $repo The name of the GitHub repository.
217 * @param integer $id ID of the comment to retrieve
218 *
219 * @deprecated use repositories->comments->get()
220 *
221 * @return array
222 *
223 * @since 12.1
224 */
225 public function getCommitComment($user, $repo, $id)
226 {
227 // Build the request path.
228 $path = '/repos/' . $user . '/' . $repo . '/comments/' . $id;
229
230 // Send the request.
231 $response = $this->client->get($this->fetchUrl($path));
232
233 // Validate the response code.
234 if ($response->code != 200)
235 {
236 // Decode the error response and throw an exception.
237 $error = json_decode($response->body);
238 throw new DomainException($error->message, $response->code);
239 }
240
241 return json_decode($response->body);
242 }
243
244 /**
245 * Method to get a list of comments for a single commit for a repository.
246 *
247 * @param string $user The name of the owner of the GitHub repository.
248 * @param string $repo The name of the GitHub repository.
249 * @param string $sha The SHA of the commit to retrieve.
250 * @param integer $page Page to request
251 * @param integer $limit Number of results to return per page
252 *
253 * @deprecated use repositories->comments->getList()
254 *
255 * @return array
256 *
257 * @since 12.1
258 */
259 public function getCommitComments($user, $repo, $sha, $page = 0, $limit = 0)
260 {
261 // Build the request path.
262 $path = '/repos/' . $user . '/' . $repo . '/commits/' . $sha . '/comments';
263
264 // Send the request.
265 $response = $this->client->get($this->fetchUrl($path, $page, $limit));
266
267 // Validate the response code.
268 if ($response->code != 200)
269 {
270 // Decode the error response and throw an exception.
271 $error = json_decode($response->body);
272 throw new DomainException($error->message, $response->code);
273 }
274
275 return json_decode($response->body);
276 }
277
278 /**
279 * Method to get a diff for two commits.
280 *
281 * @param string $user The name of the owner of the GitHub repository.
282 * @param string $repo The name of the GitHub repository.
283 * @param string $base The base of the diff, either a commit SHA or branch.
284 * @param string $head The head of the diff, either a commit SHA or branch.
285 *
286 * @deprecated use repositories->commits->compare()
287 *
288 * @return array
289 *
290 * @since 12.1
291 */
292 public function getDiff($user, $repo, $base, $head)
293 {
294 // Build the request path.
295 $path = '/repos/' . $user . '/' . $repo . '/compare/' . $base . '...' . $head;
296
297 // Send the request.
298 $response = $this->client->get($this->fetchUrl($path));
299
300 // Validate the response code.
301 if ($response->code != 200)
302 {
303 // Decode the error response and throw an exception.
304 $error = json_decode($response->body);
305 throw new DomainException($error->message, $response->code);
306 }
307
308 return json_decode($response->body);
309 }
310
311 /**
312 * Method to list commits for a repository.
313 *
314 * @param string $user The name of the owner of the GitHub repository.
315 * @param string $repo The name of the GitHub repository.
316 * @param integer $page Page to request
317 * @param integer $limit Number of results to return per page
318 *
319 * @deprecated use repositories->commits->getList()
320 *
321 * @return array
322 *
323 * @since 12.1
324 */
325 public function getList($user, $repo, $page = 0, $limit = 0)
326 {
327 // Build the request path.
328 $path = '/repos/' . $user . '/' . $repo . '/commits';
329
330 // Send the request.
331 $response = $this->client->get($this->fetchUrl($path, $page, $limit));
332
333 // Validate the response code.
334 if ($response->code != 200)
335 {
336 // Decode the error response and throw an exception.
337 $error = json_decode($response->body);
338 throw new DomainException($error->message, $response->code);
339 }
340
341 return json_decode($response->body);
342 }
343
344 /**
345 * Method to get a list of commit comments for a repository.
346 *
347 * @param string $user The name of the owner of the GitHub repository.
348 * @param string $repo The name of the GitHub repository.
349 * @param integer $page Page to request
350 * @param integer $limit Number of results to return per page
351 *
352 * @deprecated use repositories->comments->getListRepository()
353 *
354 * @return array
355 *
356 * @since 12.1
357 */
358 public function getListComments($user, $repo, $page = 0, $limit = 0)
359 {
360 // Build the request path.
361 $path = '/repos/' . $user . '/' . $repo . '/comments';
362
363 // Send the request.
364 $response = $this->client->get($this->fetchUrl($path, $page, $limit));
365
366 // Validate the response code.
367 if ($response->code != 200)
368 {
369 // Decode the error response and throw an exception.
370 $error = json_decode($response->body);
371 throw new DomainException($error->message, $response->code);
372 }
373
374 return json_decode($response->body);
375 }
376 }
377