Skip to content

Commit 20eb870

Browse files
authored
Merge pull request #517 from xabbuh/review-api
add support for the pull request reviews API
2 parents bd3318d + 6e02425 commit 20eb870

File tree

8 files changed

+1093
-3
lines changed

8 files changed

+1093
-3
lines changed

lib/Github/Api/PullRequest.php

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Github\Api;
44

55
use Github\Api\PullRequest\Comments;
6+
use Github\Api\PullRequest\Review;
67
use Github\Exception\MissingArgumentException;
78

89
/**
@@ -65,6 +66,11 @@ public function comments()
6566
return new Comments($this->client);
6667
}
6768

69+
public function reviews()
70+
{
71+
return new Review($this->client);
72+
}
73+
6874
/**
6975
* Create a pull request.
7076
*

lib/Github/Api/PullRequest/Comments.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@
1111
*/
1212
class Comments extends AbstractApi
1313
{
14-
public function all($username, $repository, $pullRequest)
14+
public function all($username, $repository, $pullRequest = null)
1515
{
16-
return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/pulls/'.rawurlencode($pullRequest).'/comments');
16+
if (null !== $pullRequest) {
17+
return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/pulls/'.rawurlencode($pullRequest).'/comments');
18+
}
19+
20+
return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/pulls/comments');
1721
}
1822

1923
public function show($username, $repository, $comment)

lib/Github/Api/PullRequest/Review.php

+161
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
<?php
2+
3+
namespace Github\Api\PullRequest;
4+
5+
use Github\Api\AbstractApi;
6+
use Github\Exception\InvalidArgumentException;
7+
use Github\Exception\MissingArgumentException;
8+
9+
/**
10+
* API for accessing Pull Request Reviews from your Git/Github repositories.
11+
*
12+
* @link https://developer.github.com/v3/pulls/reviews/
13+
* @author Christian Flothmann <[email protected]>
14+
*/
15+
class Review extends AbstractApi
16+
{
17+
/**
18+
* Get a listing of a pull request's reviews by the username, repository and pull request number.
19+
*
20+
* @link https://developer.github.com/v3/pulls/reviews/#list-reviews-on-a-pull-request
21+
*
22+
* @param string $username the username
23+
* @param string $repository the repository
24+
* @param int $pullRequest the pull request number
25+
* @param array $params a list of extra parameters.
26+
*
27+
* @return array array of pull request reviews for the pull request
28+
*/
29+
public function all($username, $repository, $pullRequest, array $params = [])
30+
{
31+
$parameters = array_merge([
32+
'page' => 1,
33+
'per_page' => 30
34+
], $params);
35+
36+
return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/pulls/'.$pullRequest.'/reviews', $parameters);
37+
}
38+
39+
/**
40+
* Get a single pull request review by the username, repository, pull request number and the review id.
41+
*
42+
* @link https://developer.github.com/v3/pulls/reviews/#get-a-single-review
43+
*
44+
* @param string $username the username
45+
* @param string $repository the repository
46+
* @param int $pullRequest the pull request number
47+
* @param int $id the review id
48+
*
49+
* @return array the pull request review
50+
*/
51+
public function show($username, $repository, $pullRequest, $id)
52+
{
53+
return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/pulls/'.$pullRequest.'/reviews/'.$id);
54+
}
55+
56+
/**
57+
* Delete a single pull request review by the username, repository, pull request number and the review id.
58+
*
59+
* @link https://developer.github.com/v3/pulls/reviews/#delete-a-pending-review
60+
*
61+
* @param string $username the username
62+
* @param string $repository the repository
63+
* @param int $pullRequest the pull request number
64+
* @param int $id the review id
65+
*
66+
* @return array|string
67+
*/
68+
public function remove($username, $repository, $pullRequest, $id)
69+
{
70+
return $this->delete('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/pulls/'.$pullRequest.'/reviews/'.$id);
71+
}
72+
73+
/**
74+
* Get comments for a single pull request review.
75+
*
76+
* @link https://developer.github.com/v3/pulls/reviews/#get-comments-for-a-single-review
77+
*
78+
* @param string $username the username
79+
* @param string $repository the repository
80+
* @param int $pullRequest the pull request number
81+
* @param int $id the review id
82+
*
83+
* @return array|string
84+
*/
85+
public function comments($username, $repository, $pullRequest, $id)
86+
{
87+
return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/pulls/'.rawurlencode($pullRequest).'/reviews/'.rawurlencode($id).'/comments');
88+
}
89+
90+
/**
91+
* Create a pull request review by the username, repository and pull request number.
92+
*
93+
* @link https://developer.github.com/v3/pulls/reviews/#create-a-pull-request-review
94+
*
95+
* @param string $username the username
96+
* @param string $repository the repository
97+
* @param int $pullRequest the pull request number
98+
* @param array $params a list of extra parameters.
99+
*
100+
* @throws MissingArgumentException
101+
*
102+
* @return array the pull request review
103+
*/
104+
public function create($username, $repository, $pullRequest, array $params = [])
105+
{
106+
if (!isset($params['event'])) {
107+
throw new MissingArgumentException('event');
108+
}
109+
110+
if (!in_array($params['event'], ["APPROVE", "REQUEST_CHANGES", "COMMENT"], true)) {
111+
throw new InvalidArgumentException(sprintf('"event" must be one of ["APPROVE", "REQUEST_CHANGES", "COMMENT"] ("%s" given).', $params['event']));
112+
}
113+
114+
return $this->post('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/pulls/'.$pullRequest.'/reviews', $params);
115+
}
116+
117+
/**
118+
* Submit a pull request review by the username, repository, pull request number and the review id.
119+
*
120+
* @link https://developer.github.com/v3/pulls/reviews/#submit-a-pull-request-review
121+
*
122+
* @param string $username the username
123+
* @param string $repository the repository
124+
* @param int $pullRequest the pull request number
125+
* @param int $id the review id
126+
* @param array $params a list of extra parameters.
127+
*
128+
* @throws MissingArgumentException
129+
*
130+
* @return array the pull request review
131+
*/
132+
public function submit($username, $repository, $pullRequest, $id, array $params = [])
133+
{
134+
if (!isset($params['event'])) {
135+
throw new MissingArgumentException('event');
136+
}
137+
138+
if (!in_array($params['event'], ["APPROVE", "REQUEST_CHANGES", "COMMENT"], true)) {
139+
throw new InvalidArgumentException(sprintf('"event" must be one of ["APPROVE", "REQUEST_CHANGES", "COMMENT"] ("%s" given).', $params['event']));
140+
}
141+
142+
return $this->post('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/pulls/'.$pullRequest.'/reviews/'.$id.'/events', $params);
143+
}
144+
145+
/**
146+
* Dismiss a pull request review by the username, repository, pull request number and the review id.
147+
*
148+
* @link https://developer.github.com/v3/pulls/reviews/#dismiss-a-pull-request-review
149+
*
150+
* @param string $username the username
151+
* @param string $repository the repository
152+
* @param int $pullRequest the pull request number
153+
* @param int $id the review id
154+
*
155+
* @return array|string
156+
*/
157+
public function dismiss($username, $repository, $pullRequest, $id)
158+
{
159+
return $this->put('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/pulls/'.$pullRequest.'/reviews/'.$id.'/dismissals');
160+
}
161+
}

lib/Github/Client.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public function __construct(Builder $httpClientBuilder = null, $apiVersion = nul
128128
)));
129129

130130
$this->apiVersion = $apiVersion ?: 'v3';
131-
$builder->addHeaders(['Accept' => sprintf('application/vnd.github.%s+json', $this->apiVersion)]);
131+
$builder->addHeaderValue('Accept', sprintf('application/vnd.github.%s+json', $this->apiVersion));
132132

133133
if ($enterpriseUrl) {
134134
$this->setEnterpriseUrl($enterpriseUrl);

lib/Github/HttpClient/Builder.php

+13
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,19 @@ public function addHeaders(array $headers)
157157
$this->addPlugin(new Plugin\HeaderAppendPlugin($this->headers));
158158
}
159159

160+
/**
161+
* @param string $header
162+
* @param string $headerValue
163+
*/
164+
public function addHeaderValue($header, $headerValue)
165+
{
166+
if (!isset($this->headers[$header])) {
167+
$this->headers[$header] = $headerValue;
168+
} else {
169+
$this->headers[$header] = array_merge((array)$this->headers[$header], array($headerValue));
170+
}
171+
}
172+
160173
/**
161174
* Add a cache plugin to cache responses locally.
162175
*

0 commit comments

Comments
 (0)