-
-
Notifications
You must be signed in to change notification settings - Fork 600
/
PullRequest.php
203 lines (173 loc) · 6.79 KB
/
PullRequest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
namespace Github\Api;
use Github\Api\PullRequest\Comments;
use Github\Api\PullRequest\Review;
use Github\Api\PullRequest\ReviewRequest;
use Github\Exception\InvalidArgumentException;
use Github\Exception\MissingArgumentException;
/**
* API for accessing Pull Requests from your Git/Github repositories.
*
* @see http://developer.github.com/v3/pulls/
*
* @author Joseph Bielawski <[email protected]>
*/
class PullRequest extends AbstractApi
{
use AcceptHeaderTrait;
/**
* Configure the body type.
*
* @link https://developer.github.com/v3/pulls/#custom-media-types
*
* @param string|null $bodyType
* @param string|null $apiVersion
*
* @return self
*/
public function configure($bodyType = null, $apiVersion = null)
{
if (null === $apiVersion) {
$apiVersion = $this->client->getApiVersion();
}
if (!in_array($bodyType, ['text', 'html', 'full', 'diff', 'patch'])) {
$bodyType = 'raw';
}
if (!in_array($bodyType, ['diff', 'patch'])) {
$bodyType .= '+json';
}
$this->acceptHeaderValue = sprintf('application/vnd.github.%s.%s', $apiVersion, $bodyType);
return $this;
}
/**
* Get a listing of a project's pull requests by the username, repository and (optionally) state.
*
* @link http://developer.github.com/v3/pulls/
*
* @param string $username the username
* @param string $repository the repository
* @param array $parameters a list of extra parameters.
*
* @return array array of pull requests for the project
*/
public function all($username, $repository, array $parameters = [])
{
return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/pulls', $parameters);
}
/**
* Show all details of a pull request, including the discussions.
*
* @link http://developer.github.com/v3/pulls/
*
* @param string $username the username
* @param string $repository the repository
* @param int $id the ID of the pull request for which details are retrieved
*
* @return array|string pull request details
*/
public function show($username, $repository, $id)
{
return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/pulls/'.rawurlencode($id));
}
public function commits($username, $repository, $id)
{
return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/pulls/'.rawurlencode($id).'/commits');
}
public function files($username, $repository, $id, array $parameters = [])
{
return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/pulls/'.rawurlencode($id).'/files', $parameters);
}
/**
* All statuses which are the statuses of its head branch.
*
* @see http://developer.github.com/v3/pulls/
*
* @param string $username the username
* @param string $repository the repository
* @param int $id the ID of the pull request for which statuses are retrieved
*
* @return array array of statuses for the project
*/
public function status($username, $repository, $id)
{
$link = $this->show($username, $repository, $id)['_links']['statuses']['href'];
return $this->get($link);
}
public function comments()
{
return new Comments($this->client);
}
public function reviews()
{
return new Review($this->client);
}
public function reviewRequests()
{
return new ReviewRequest($this->client);
}
/**
* Create a pull request.
*
* @link http://developer.github.com/v3/pulls/
*
* @param string $username the username
* @param string $repository the repository
* @param array $params A String of the branch or commit SHA that you want your changes to be pulled to.
* A String of the branch or commit SHA of your changes. Typically this will be a branch.
* If the branch is in a fork of the original repository, specify the username first:
* "my-user:some-branch". The String title of the Pull Request. The String body of
* the Pull Request. The issue number. Used when title and body is not set.
*
* @throws MissingArgumentException
*
* @return array
*/
public function create($username, $repository, array $params)
{
// Two ways to create PR, using issue or title
if (!isset($params['issue']) && !isset($params['title'])) {
throw new MissingArgumentException(['issue', 'title']);
}
if (!isset($params['base'], $params['head'])) {
throw new MissingArgumentException(['base', 'head']);
}
// If `issue` is not sent, then `body` must be sent
if (!isset($params['issue']) && !isset($params['body'])) {
throw new MissingArgumentException(['issue', 'body']);
}
if (isset($params['draft']) && $params['draft'] === true) {
//This feature is in preview mode, so set the correct accept-header
$this->acceptHeaderValue = 'application/vnd.github.shadow-cat-preview+json';
}
return $this->post('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/pulls', $params);
}
public function update($username, $repository, $id, array $params)
{
if (isset($params['state']) && !in_array($params['state'], ['open', 'closed'])) {
$params['state'] = 'open';
}
return $this->patch('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/pulls/'.rawurlencode($id), $params);
}
public function merged($username, $repository, $id)
{
return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/pulls/'.rawurlencode($id).'/merge');
}
public function merge($username, $repository, $id, $message, $sha, $mergeMethod = 'merge', $title = null)
{
if (is_bool($mergeMethod)) {
$mergeMethod = $mergeMethod ? 'squash' : 'merge';
}
if (!in_array($mergeMethod, ['merge', 'squash', 'rebase'], true)) {
throw new InvalidArgumentException(sprintf('"$mergeMethod" must be one of ["merge", "squash", "rebase"] ("%s" given).', $mergeMethod));
}
$params = [
'commit_message' => $message,
'sha' => $sha,
'merge_method' => $mergeMethod,
];
if (is_string($title)) {
$params['commit_title'] = $title;
}
return $this->put('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/pulls/'.rawurlencode($id).'/merge', $params);
}
}