1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the bitbucket-api package. |
5
|
|
|
* |
6
|
|
|
* (c) Alexandru G. <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Bitbucket\API\Repositories; |
13
|
|
|
|
14
|
|
|
use Bitbucket\API; |
15
|
|
|
use Buzz\Message\MessageInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Manage the comments on pull requests. |
19
|
|
|
* |
20
|
|
|
* @author Alexandru G. <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class PullRequests extends API\Api |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Get comments |
26
|
|
|
* |
27
|
|
|
* @access public |
28
|
|
|
* @return PullRequests\Comments |
29
|
|
|
* @codeCoverageIgnore |
30
|
|
|
*/ |
31
|
|
|
public function comments() |
32
|
|
|
{ |
33
|
|
|
return $this->childFactory('Repositories\\PullRequests\\Comments'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Get a list of pull requests |
38
|
|
|
* |
39
|
|
|
* @access public |
40
|
|
|
* @param string $account The team or individual account owning the repository. |
41
|
|
|
* @param string $repo The repository identifier. |
42
|
|
|
* @param array $params Additional parameters |
43
|
|
|
* @return MessageInterface |
44
|
|
|
* |
45
|
|
|
* @throws \InvalidArgumentException |
46
|
|
|
*/ |
47
|
1 |
|
public function all($account, $repo, array $params = array()) |
48
|
|
|
{ |
49
|
1 |
|
$states = array('OPEN', 'MERGED', 'DECLINED'); |
50
|
1 |
|
$params = array_merge( |
51
|
|
|
array( |
52
|
|
|
'state' => 'OPEN' |
53
|
1 |
|
), |
54
|
|
|
$params |
55
|
1 |
|
); |
56
|
|
|
|
57
|
1 |
|
if (!is_array($params['state'])) { |
58
|
1 |
|
$params['state'] = array($params['state']); |
59
|
1 |
|
} |
60
|
|
|
|
61
|
1 |
|
array_walk( |
62
|
1 |
|
$params['state'], |
63
|
|
|
function ($state) use ($states) { |
64
|
1 |
|
if (!in_array($state, $states)) { |
65
|
|
|
throw new \InvalidArgumentException(sprintf('Unknown `state` %s', $state)); |
66
|
|
|
} |
67
|
1 |
|
} |
68
|
1 |
|
); |
69
|
|
|
|
70
|
1 |
|
return $this->getClient()->setApiVersion('2.0')->get( |
71
|
1 |
|
sprintf('repositories/%s/%s/pullrequests', $account, $repo), |
72
|
|
|
$params |
73
|
1 |
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Create a new pull request |
78
|
|
|
* |
79
|
|
|
* @access public |
80
|
|
|
* @param string $account The team or individual account owning the repository. |
81
|
|
|
* @param string $repo The repository identifier. |
82
|
|
|
* @param array|string $params Additional parameters as array or JSON string |
83
|
|
|
* @return MessageInterface |
84
|
|
|
* |
85
|
|
|
* @throws \InvalidArgumentException |
86
|
|
|
* @see https://confluence.atlassian.com/x/XAZAGQ |
87
|
|
|
*/ |
88
|
3 |
|
public function create($account, $repo, $params = array()) |
89
|
|
|
{ |
90
|
|
|
$defaults = array( |
91
|
3 |
|
'title' => 'New pull request', |
92
|
|
|
'source' => array( |
93
|
|
|
'branch' => array( |
94
|
|
|
'name' => 'develop' |
95
|
3 |
|
) |
96
|
3 |
|
) |
97
|
3 |
|
); |
98
|
|
|
|
99
|
|
|
// allow developer to directly specify params as json if (s)he wants. |
100
|
3 |
|
if ('array' !== gettype($params)) { |
101
|
2 |
|
if (empty($params)) { |
102
|
1 |
|
throw new \InvalidArgumentException('Invalid JSON provided.'); |
103
|
|
|
} |
104
|
|
|
|
105
|
1 |
|
$params = $this->decodeJSON($params); |
106
|
1 |
|
} |
107
|
|
|
|
108
|
2 |
|
$params = array_merge($defaults, $params); |
109
|
|
|
|
110
|
2 |
|
if (empty($params['title'])) { |
111
|
|
|
throw new \InvalidArgumentException('Pull request\'s title must be specified.'); |
112
|
|
|
} |
113
|
|
|
|
114
|
2 |
|
if (empty($params['source']['branch']['name'])) { |
115
|
|
|
throw new \InvalidArgumentException('Pull request\'s source branch name must be specified.'); |
116
|
|
|
} |
117
|
|
|
|
118
|
2 |
|
return $this->getClient()->setApiVersion('2.0')->post( |
119
|
2 |
|
sprintf('repositories/%s/%s/pullrequests', $account, $repo), |
120
|
2 |
|
json_encode($params), |
121
|
2 |
|
array('Content-Type' => 'application/json') |
122
|
2 |
|
); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Update a pull request |
127
|
|
|
* |
128
|
|
|
* @access public |
129
|
|
|
* @param string $account The team or individual account owning the repository. |
130
|
|
|
* @param string $repo The repository identifier. |
131
|
|
|
* @param int $id ID of the pull request that will be updated |
132
|
|
|
* @param array|string $params Additional parameters as array or JSON string |
133
|
|
|
* @return MessageInterface |
134
|
|
|
* |
135
|
|
|
* @throws \InvalidArgumentException |
136
|
|
|
*/ |
137
|
3 |
|
public function update($account, $repo, $id, $params = array()) |
138
|
|
|
{ |
139
|
|
|
$defaults = array( |
140
|
3 |
|
'title' => 'Updated pull request', |
141
|
|
|
'destination' => array( |
142
|
|
|
'branch' => array( |
143
|
|
|
'name' => 'develop' |
144
|
3 |
|
) |
145
|
3 |
|
) |
146
|
3 |
|
); |
147
|
|
|
|
148
|
|
|
// allow developer to directly specify params as json if (s)he wants. |
149
|
3 |
|
if ('array' !== gettype($params)) { |
150
|
2 |
|
if (empty($params)) { |
151
|
1 |
|
throw new \InvalidArgumentException('Invalid JSON provided.'); |
152
|
|
|
} |
153
|
|
|
|
154
|
1 |
|
$params = $this->decodeJSON($params); |
155
|
1 |
|
} |
156
|
|
|
|
157
|
2 |
|
$params = array_merge($defaults, $params); |
158
|
|
|
|
159
|
2 |
|
if (empty($params['title'])) { |
160
|
|
|
throw new \InvalidArgumentException('Pull request\'s title must be specified.'); |
161
|
|
|
} |
162
|
|
|
|
163
|
2 |
|
if (empty($params['destination']['branch']['name'])) { |
164
|
|
|
throw new \InvalidArgumentException('Pull request\'s destination branch name must be specified.'); |
165
|
|
|
} |
166
|
|
|
|
167
|
2 |
|
return $this->getClient()->setApiVersion('2.0')->put( |
168
|
2 |
|
sprintf('repositories/%s/%s/pullrequests/%d', $account, $repo, $id), |
169
|
2 |
|
json_encode($params), |
170
|
2 |
|
array('Content-Type' => 'application/json') |
171
|
2 |
|
); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Get a specific pull request |
176
|
|
|
* |
177
|
|
|
* @access public |
178
|
|
|
* @param string $account The team or individual account owning the repository. |
179
|
|
|
* @param string $repo The repository identifier. |
180
|
|
|
* @param int $id ID of the pull request |
181
|
|
|
* @return MessageInterface |
182
|
|
|
*/ |
183
|
1 |
|
public function get($account, $repo, $id) |
184
|
|
|
{ |
185
|
1 |
|
return $this->getClient()->setApiVersion('2.0')->get( |
186
|
1 |
|
sprintf('repositories/%s/%s/pullrequests/%d', $account, $repo, $id) |
187
|
1 |
|
); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Get the commits for a pull request |
192
|
|
|
* |
193
|
|
|
* @access public |
194
|
|
|
* @param string $account The team or individual account owning the repository. |
195
|
|
|
* @param string $repo The repository identifier. |
196
|
|
|
* @param int $id ID of the pull request |
197
|
|
|
* @return MessageInterface |
198
|
|
|
*/ |
199
|
1 |
|
public function commits($account, $repo, $id) |
200
|
|
|
{ |
201
|
1 |
|
return $this->getClient()->setApiVersion('2.0')->get( |
202
|
1 |
|
sprintf('repositories/%s/%s/pullrequests/%d/commits', $account, $repo, $id) |
203
|
1 |
|
); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Approve a pull request |
208
|
|
|
* |
209
|
|
|
* @access public |
210
|
|
|
* @param string $account The team or individual account owning the repository. |
211
|
|
|
* @param string $repo The repository identifier. |
212
|
|
|
* @param int $id ID of the pull request |
213
|
|
|
* @return MessageInterface |
214
|
|
|
*/ |
215
|
1 |
|
public function approve($account, $repo, $id) |
216
|
|
|
{ |
217
|
1 |
|
return $this->getClient()->setApiVersion('2.0')->post( |
218
|
1 |
|
sprintf('repositories/%s/%s/pullrequests/%d/approve', $account, $repo, $id) |
219
|
1 |
|
); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Delete a pull request approval |
224
|
|
|
* |
225
|
|
|
* NOTE: On success returns `HTTP/1.1 204 NO CONTENT` |
226
|
|
|
* |
227
|
|
|
* @access public |
228
|
|
|
* @param string $account The team or individual account owning the repository. |
229
|
|
|
* @param string $repo The repository identifier. |
230
|
|
|
* @param int $id ID of the pull request |
231
|
|
|
* @return MessageInterface |
232
|
|
|
*/ |
233
|
1 |
|
public function deleteApproval($account, $repo, $id) |
234
|
|
|
{ |
235
|
1 |
|
return $this->getClient()->setApiVersion('2.0')->delete( |
236
|
1 |
|
sprintf('repositories/%s/%s/pullrequests/%d/approve', $account, $repo, $id) |
237
|
1 |
|
); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Get the diff for a pull request |
242
|
|
|
* |
243
|
|
|
* @access public |
244
|
|
|
* @param string $account The team or individual account owning the repository. |
245
|
|
|
* @param string $repo The repository identifier. |
246
|
|
|
* @param int $id ID of the pull request |
247
|
|
|
* @return MessageInterface |
248
|
|
|
*/ |
249
|
1 |
|
public function diff($account, $repo, $id) |
250
|
|
|
{ |
251
|
1 |
|
return $this->getClient()->setApiVersion('2.0')->get( |
252
|
1 |
|
sprintf('repositories/%s/%s/pullrequests/%d/diff', $account, $repo, $id) |
253
|
1 |
|
); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Get the log of all of a repository's pull request activity |
258
|
|
|
* |
259
|
|
|
* If `$id` is omitted the repository's pull request activity is returned. |
260
|
|
|
* If `$id` is not omitted the pull request activity is returned. |
261
|
|
|
* |
262
|
|
|
* @access public |
263
|
|
|
* @param string $account The team or individual account owning the repository. |
264
|
|
|
* @param string $repo The repository identifier. |
265
|
|
|
* @param int $id (Optional) ID of the pull request |
266
|
|
|
* @return MessageInterface |
267
|
|
|
*/ |
268
|
2 |
|
public function activity($account, $repo, $id = 0) |
269
|
|
|
{ |
270
|
2 |
|
$endpoint = sprintf('repositories/%s/%s/pullrequests/', $account, $repo); |
271
|
|
|
|
272
|
2 |
|
if ($id === 0) { |
273
|
1 |
|
$endpoint .= 'activity'; |
274
|
1 |
|
} else { |
275
|
1 |
|
$endpoint .= $id.'/activity'; |
276
|
|
|
} |
277
|
|
|
|
278
|
2 |
|
return $this->getClient()->setApiVersion('2.0')->get($endpoint); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Accept and merge a pull request |
283
|
|
|
* |
284
|
|
|
* @access public |
285
|
|
|
* @param string $account The team or individual account owning the repository. |
286
|
|
|
* @param string $repo The repository identifier. |
287
|
|
|
* @param int $id (Optional) ID of the pull request |
288
|
|
|
* @param array $params Additional parameters. |
289
|
|
|
* @return MessageInterface |
290
|
|
|
*/ |
291
|
2 |
|
public function accept($account, $repo, $id, $params = array()) |
292
|
|
|
{ |
293
|
1 |
|
return $this->getClient()->setApiVersion('2.0')->post( |
294
|
1 |
|
sprintf('repositories/%s/%s/pullrequests/%d/merge', $account, $repo, $id), |
295
|
1 |
|
json_encode($params), |
296
|
1 |
|
array('Content-Type' => 'application/json') |
297
|
2 |
|
); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Decline a pull request |
302
|
|
|
* |
303
|
|
|
* @access public |
304
|
|
|
* @param string $account The team or individual account owning the repository. |
305
|
|
|
* @param string $repo The repository identifier. |
306
|
|
|
* @param int $id (Optional) ID of the pull request |
307
|
|
|
* @param array $params Additional parameters. |
308
|
|
|
* @return MessageInterface |
309
|
|
|
*/ |
310
|
2 |
|
public function decline($account, $repo, $id, $params = array()) |
311
|
|
|
{ |
312
|
2 |
|
if (false === array_key_exists('message', $params)) { |
313
|
1 |
|
$params['message'] = ''; |
314
|
1 |
|
} |
315
|
|
|
|
316
|
2 |
|
return $this->getClient()->setApiVersion('2.0')->post( |
317
|
2 |
|
sprintf('repositories/%s/%s/pullrequests/%d/decline', $account, $repo, $id), |
318
|
2 |
|
json_encode($params), |
319
|
2 |
|
array('Content-Type' => 'application/json') |
320
|
2 |
|
); |
321
|
|
|
} |
322
|
|
|
} |
323
|
|
|
|