|
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; |
|
10
|
|
|
|
|
11
|
|
|
use Joomla\Github\AbstractPackage; |
|
12
|
|
|
use Joomla\Http\Exception\UnexpectedResponseException; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* GitHub API Pull Requests class for the Joomla Framework. |
|
16
|
|
|
* |
|
17
|
|
|
* @documentation http://developer.github.com/v3/pulls |
|
18
|
|
|
* |
|
19
|
|
|
* @since 1.0 |
|
20
|
|
|
* |
|
21
|
|
|
* @property-read Pulls\Comments $comments GitHub API object for comments. |
|
22
|
|
|
*/ |
|
23
|
|
|
class Pulls extends AbstractPackage |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* Create a pull request. |
|
27
|
|
|
* |
|
28
|
|
|
* @param string $user The name of the owner of the GitHub repository. |
|
29
|
|
|
* @param string $repo The name of the GitHub repository. |
|
30
|
|
|
* @param string $title The title of the new pull request. |
|
31
|
|
|
* @param string $base The branch (or git ref) you want your changes pulled into. This |
|
32
|
|
|
* should be an existing branch on the current repository. You cannot |
|
33
|
|
|
* submit a pull request to one repo that requests a merge to a base |
|
34
|
|
|
* of another repo. |
|
35
|
|
|
* @param string $head The branch (or git ref) where your changes are implemented. |
|
36
|
|
|
* @param string $body The body text for the new pull request. |
|
37
|
|
|
* |
|
38
|
|
|
* @return object |
|
39
|
|
|
* |
|
40
|
|
|
* @since 1.0 |
|
41
|
|
|
* @throws \DomainException |
|
42
|
|
|
*/ |
|
43
|
|
View Code Duplication |
public function create($user, $repo, $title, $base, $head, $body = '') |
|
|
|
|
|
|
44
|
|
|
{ |
|
45
|
|
|
// Build the request path. |
|
46
|
|
|
$path = '/repos/' . $user . '/' . $repo . '/pulls'; |
|
47
|
|
|
|
|
48
|
|
|
// Build the request data. |
|
49
|
|
|
$data = json_encode( |
|
50
|
|
|
array( |
|
51
|
|
|
'title' => $title, |
|
52
|
|
|
'base' => $base, |
|
53
|
|
|
'head' => $head, |
|
54
|
|
|
'body' => $body |
|
55
|
|
|
) |
|
56
|
|
|
); |
|
57
|
|
|
|
|
58
|
|
|
// Send the request. |
|
59
|
|
|
return $this->processResponse($this->client->post($this->fetchUrl($path), $data), 201); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Method to create a pull request from an existing issue. |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $user The name of the owner of the GitHub repository. |
|
66
|
|
|
* @param string $repo The name of the GitHub repository. |
|
67
|
|
|
* @param integer $issueId The issue number for which to attach the new pull request. |
|
68
|
|
|
* @param string $base The branch (or git ref) you want your changes pulled into. This |
|
69
|
|
|
* should be an existing branch on the current repository. You cannot |
|
70
|
|
|
* submit a pull request to one repo that requests a merge to a base |
|
71
|
|
|
* of another repo. |
|
72
|
|
|
* @param string $head The branch (or git ref) where your changes are implemented. |
|
73
|
|
|
* |
|
74
|
|
|
* @return object |
|
75
|
|
|
* |
|
76
|
|
|
* @since 1.0 |
|
77
|
|
|
* @throws \DomainException |
|
78
|
|
|
*/ |
|
79
|
|
|
public function createFromIssue($user, $repo, $issueId, $base, $head) |
|
80
|
|
|
{ |
|
81
|
|
|
// Build the request path. |
|
82
|
|
|
$path = '/repos/' . $user . '/' . $repo . '/pulls'; |
|
83
|
|
|
|
|
84
|
|
|
// Build the request data. |
|
85
|
|
|
$data = json_encode( |
|
86
|
|
|
array( |
|
87
|
|
|
'issue' => (int) $issueId, |
|
88
|
|
|
'base' => $base, |
|
89
|
|
|
'head' => $head |
|
90
|
|
|
) |
|
91
|
|
|
); |
|
92
|
|
|
|
|
93
|
|
|
// Send the request. |
|
94
|
|
|
return $this->processResponse($this->client->post($this->fetchUrl($path), $data), 201); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Update a pull request. |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $user The name of the owner of the GitHub repository. |
|
101
|
|
|
* @param string $repo The name of the GitHub repository. |
|
102
|
|
|
* @param integer $pullId The pull request number. |
|
103
|
|
|
* @param string $title The optional new title for the pull request. |
|
104
|
|
|
* @param string $body The optional new body text for the pull request. |
|
105
|
|
|
* @param string $state The optional new state for the pull request. [open, closed] |
|
106
|
|
|
* @param string $base The optional new base branch for the pull request. |
|
107
|
|
|
* |
|
108
|
|
|
* @return object |
|
109
|
|
|
* |
|
110
|
|
|
* @since 1.0 |
|
111
|
|
|
* @throws \DomainException |
|
112
|
|
|
*/ |
|
113
|
|
|
public function edit($user, $repo, $pullId, $title = null, $body = null, $state = null, $base = null) |
|
114
|
|
|
{ |
|
115
|
|
|
// Build the request path. |
|
116
|
|
|
$path = '/repos/' . $user . '/' . $repo . '/pulls/' . (int) $pullId; |
|
117
|
|
|
|
|
118
|
|
|
// Create the data object. |
|
119
|
|
|
$data = new \stdClass; |
|
120
|
|
|
|
|
121
|
|
|
// If a title is set add it to the data object. |
|
122
|
|
|
if (isset($title)) |
|
123
|
|
|
{ |
|
124
|
|
|
$data->title = $title; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
// If a body is set add it to the data object. |
|
128
|
|
|
if (isset($body)) |
|
129
|
|
|
{ |
|
130
|
|
|
$data->body = $body; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
// If a state is set add it to the data object. |
|
134
|
|
|
if (isset($state)) |
|
135
|
|
|
{ |
|
136
|
|
|
$data->state = $state; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
// If a base branch is set add it to the data object. |
|
140
|
|
|
if (isset($base)) |
|
141
|
|
|
{ |
|
142
|
|
|
$data->base = $base; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
// Encode the request data. |
|
146
|
|
|
$data = json_encode($data); |
|
147
|
|
|
|
|
148
|
|
|
// Send the request. |
|
149
|
|
|
return $this->processResponse($this->client->patch($this->fetchUrl($path), $data)); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Get a single pull request. |
|
154
|
|
|
* |
|
155
|
|
|
* @param string $user The name of the owner of the GitHub repository. |
|
156
|
|
|
* @param string $repo The name of the GitHub repository. |
|
157
|
|
|
* @param integer $pullId The pull request number. |
|
158
|
|
|
* |
|
159
|
|
|
* @return object |
|
160
|
|
|
* |
|
161
|
|
|
* @since 1.0 |
|
162
|
|
|
* @throws \DomainException |
|
163
|
|
|
*/ |
|
164
|
|
|
public function get($user, $repo, $pullId) |
|
165
|
|
|
{ |
|
166
|
|
|
// Build the request path. |
|
167
|
|
|
$path = '/repos/' . $user . '/' . $repo . '/pulls/' . (int) $pullId; |
|
168
|
|
|
|
|
169
|
|
|
// Send the request. |
|
170
|
|
|
return $this->processResponse($this->client->get($this->fetchUrl($path))); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* List commits on a pull request. |
|
175
|
|
|
* |
|
176
|
|
|
* @param string $user The name of the owner of the GitHub repository. |
|
177
|
|
|
* @param string $repo The name of the GitHub repository. |
|
178
|
|
|
* @param integer $pullId The pull request number. |
|
179
|
|
|
* @param integer $page The page number from which to get items. |
|
180
|
|
|
* @param integer $limit The number of items on a page. |
|
181
|
|
|
* |
|
182
|
|
|
* @return object |
|
183
|
|
|
* |
|
184
|
|
|
* @since 1.0 |
|
185
|
|
|
* @throws \DomainException |
|
186
|
|
|
*/ |
|
187
|
|
|
public function getCommits($user, $repo, $pullId, $page = 0, $limit = 0) |
|
188
|
|
|
{ |
|
189
|
|
|
// Build the request path. |
|
190
|
|
|
$path = '/repos/' . $user . '/' . $repo . '/pulls/' . (int) $pullId . '/commits'; |
|
191
|
|
|
|
|
192
|
|
|
// Send the request. |
|
193
|
|
|
return $this->processResponse($this->client->get($this->fetchUrl($path, $page, $limit))); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* List pull requests files. |
|
198
|
|
|
* |
|
199
|
|
|
* @param string $user The name of the owner of the GitHub repository. |
|
200
|
|
|
* @param string $repo The name of the GitHub repository. |
|
201
|
|
|
* @param integer $pullId The pull request number. |
|
202
|
|
|
* @param integer $page The page number from which to get items. |
|
203
|
|
|
* @param integer $limit The number of items on a page. |
|
204
|
|
|
* |
|
205
|
|
|
* @return object |
|
206
|
|
|
* |
|
207
|
|
|
* @since 1.0 |
|
208
|
|
|
* @throws \DomainException |
|
209
|
|
|
*/ |
|
210
|
|
|
public function getFiles($user, $repo, $pullId, $page = 0, $limit = 0) |
|
211
|
|
|
{ |
|
212
|
|
|
// Build the request path. |
|
213
|
|
|
$path = '/repos/' . $user . '/' . $repo . '/pulls/' . (int) $pullId . '/files'; |
|
214
|
|
|
|
|
215
|
|
|
// Send the request. |
|
216
|
|
|
return $this->processResponse($this->client->get($this->fetchUrl($path, $page, $limit))); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* List pull requests. |
|
221
|
|
|
* |
|
222
|
|
|
* @param string $user The name of the owner of the GitHub repository. |
|
223
|
|
|
* @param string $repo The name of the GitHub repository. |
|
224
|
|
|
* @param string $state The optional state to filter requests by. [open, closed] |
|
225
|
|
|
* @param integer $page The page number from which to get items. |
|
226
|
|
|
* @param integer $limit The number of items on a page. |
|
227
|
|
|
* |
|
228
|
|
|
* @return array |
|
229
|
|
|
* |
|
230
|
|
|
* @since 1.0 |
|
231
|
|
|
* @throws \DomainException |
|
232
|
|
|
*/ |
|
233
|
|
View Code Duplication |
public function getList($user, $repo, $state = 'open', $page = 0, $limit = 0) |
|
|
|
|
|
|
234
|
|
|
{ |
|
235
|
|
|
// Build the request path. |
|
236
|
|
|
$path = '/repos/' . $user . '/' . $repo . '/pulls'; |
|
237
|
|
|
|
|
238
|
|
|
// If a state exists append it as an option. |
|
239
|
|
|
if ($state != 'open') |
|
240
|
|
|
{ |
|
241
|
|
|
$path .= '?state=' . $state; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
// Send the request. |
|
245
|
|
|
return $this->processResponse($this->client->get($this->fetchUrl($path, $page, $limit))); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* Get if a pull request has been merged. |
|
250
|
|
|
* |
|
251
|
|
|
* @param string $user The name of the owner of the GitHub repository. |
|
252
|
|
|
* @param string $repo The name of the GitHub repository. |
|
253
|
|
|
* @param integer $pullId The pull request number. The pull request number. |
|
254
|
|
|
* |
|
255
|
|
|
* @return boolean True if the pull request has been merged |
|
256
|
|
|
* |
|
257
|
|
|
* @since 1.0 |
|
258
|
|
|
* @throws UnexpectedResponseException |
|
259
|
|
|
*/ |
|
260
|
|
|
public function isMerged($user, $repo, $pullId) |
|
261
|
|
|
{ |
|
262
|
|
|
// Build the request path. |
|
263
|
|
|
$path = '/repos/' . $user . '/' . $repo . '/pulls/' . (int) $pullId . '/merge'; |
|
264
|
|
|
|
|
265
|
|
|
// Send the request. |
|
266
|
|
|
$response = $this->client->get($this->fetchUrl($path)); |
|
267
|
|
|
|
|
268
|
|
|
// Validate the response code. |
|
269
|
|
|
if ($response->code == 204) |
|
270
|
|
|
{ |
|
271
|
|
|
return true; |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
if ($response->code == 404) |
|
275
|
|
|
{ |
|
276
|
|
|
return false; |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
// Decode the error response and throw an exception. |
|
280
|
|
|
$error = json_decode($response->body); |
|
281
|
|
|
$message = isset($error->message) ? $error->message : 'Invalid response received from GitHub.'; |
|
282
|
|
|
throw new UnexpectedResponseException($response, $message, $response->code); |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
/** |
|
286
|
|
|
* Merge a pull request (Merge Button). |
|
287
|
|
|
* |
|
288
|
|
|
* @param string $user The name of the owner of the GitHub repository. |
|
289
|
|
|
* @param string $repo The name of the GitHub repository. |
|
290
|
|
|
* @param integer $pullId The pull request number. |
|
291
|
|
|
* @param string $message The message that will be used for the merge commit. |
|
292
|
|
|
* |
|
293
|
|
|
* @return object |
|
294
|
|
|
* |
|
295
|
|
|
* @since 1.0 |
|
296
|
|
|
* @throws \DomainException |
|
297
|
|
|
*/ |
|
298
|
|
|
public function merge($user, $repo, $pullId, $message = '') |
|
299
|
|
|
{ |
|
300
|
|
|
// Build the request path. |
|
301
|
|
|
$path = '/repos/' . $user . '/' . $repo . '/pulls/' . (int) $pullId . '/merge'; |
|
302
|
|
|
|
|
303
|
|
|
// Build the request data. |
|
304
|
|
|
$data = json_encode( |
|
305
|
|
|
array( |
|
306
|
|
|
'commit_message' => $message |
|
307
|
|
|
) |
|
308
|
|
|
); |
|
309
|
|
|
|
|
310
|
|
|
// Send the request. |
|
311
|
|
|
return $this->processResponse($this->client->put($this->fetchUrl($path), $data)); |
|
312
|
|
|
} |
|
313
|
|
|
} |
|
314
|
|
|
|
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.