Skip to content

add support for the pull request reviews API #517

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/Github/Api/PullRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Github\Api;

use Github\Api\PullRequest\Comments;
use Github\Api\PullRequest\Review;
use Github\Exception\MissingArgumentException;

/**
Expand Down Expand Up @@ -65,6 +66,11 @@ public function comments()
return new Comments($this->client);
}

public function reviews()
{
return new Review($this->client);
}

/**
* Create a pull request.
*
Expand Down
8 changes: 6 additions & 2 deletions lib/Github/Api/PullRequest/Comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@
*/
class Comments extends AbstractApi
{
public function all($username, $repository, $pullRequest)
public function all($username, $repository, $pullRequest = null)
{
return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/pulls/'.rawurlencode($pullRequest).'/comments');
if (null !== $pullRequest) {
return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/pulls/'.rawurlencode($pullRequest).'/comments');
}

return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/pulls/comments');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is not really a part of the PR, right?

Let's keep it anyways (for this time)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, it's there to allow https://developer.github.com/v3/pulls/comments/#list-comments-in-a-repository which is not directly related to the reviews, but needed if you want to fully leverage the reviews API. But I can move it to its own PR nonetheless. What do you think?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okey, I stand corrected.

Lets keep it.

}

public function show($username, $repository, $comment)
Expand Down
161 changes: 161 additions & 0 deletions lib/Github/Api/PullRequest/Review.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
<?php

namespace Github\Api\PullRequest;

use Github\Api\AbstractApi;
use Github\Exception\InvalidArgumentException;
use Github\Exception\MissingArgumentException;

/**
* API for accessing Pull Request Reviews from your Git/Github repositories.
*
* @link https://developer.github.com/v3/pulls/reviews/
* @author Christian Flothmann <christian.flothmann@sensiolabs.de>
*/
class Review extends AbstractApi
{
/**
* Get a listing of a pull request's reviews by the username, repository and pull request number.
*
* @link https://developer.github.com/v3/pulls/reviews/#list-reviews-on-a-pull-request
*
* @param string $username the username
* @param string $repository the repository
* @param int $pullRequest the pull request number
* @param array $params a list of extra parameters.
*
* @return array array of pull request reviews for the pull request
*/
public function all($username, $repository, $pullRequest, array $params = [])
{
$parameters = array_merge([
'page' => 1,
'per_page' => 30
], $params);

return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/pulls/'.$pullRequest.'/reviews', $parameters);
}

/**
* Get a single pull request review by the username, repository, pull request number and the review id.
*
* @link https://developer.github.com/v3/pulls/reviews/#get-a-single-review
*
* @param string $username the username
* @param string $repository the repository
* @param int $pullRequest the pull request number
* @param int $id the review id
*
* @return array the pull request review
*/
public function show($username, $repository, $pullRequest, $id)
{
return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/pulls/'.$pullRequest.'/reviews/'.$id);
}

/**
* Delete a single pull request review by the username, repository, pull request number and the review id.
*
* @link https://developer.github.com/v3/pulls/reviews/#delete-a-pending-review
*
* @param string $username the username
* @param string $repository the repository
* @param int $pullRequest the pull request number
* @param int $id the review id
*
* @return array|string
*/
public function remove($username, $repository, $pullRequest, $id)
{
return $this->delete('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/pulls/'.$pullRequest.'/reviews/'.$id);
}

/**
* Get comments for a single pull request review.
*
* @link https://developer.github.com/v3/pulls/reviews/#get-comments-for-a-single-review
*
* @param string $username the username
* @param string $repository the repository
* @param int $pullRequest the pull request number
* @param int $id the review id
*
* @return array|string
*/
public function comments($username, $repository, $pullRequest, $id)
{
return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/pulls/'.rawurlencode($pullRequest).'/reviews/'.rawurlencode($id).'/comments');
}

/**
* Create a pull request review by the username, repository and pull request number.
*
* @link https://developer.github.com/v3/pulls/reviews/#create-a-pull-request-review
*
* @param string $username the username
* @param string $repository the repository
* @param int $pullRequest the pull request number
* @param array $params a list of extra parameters.
*
* @throws MissingArgumentException
*
* @return array the pull request review
*/
public function create($username, $repository, $pullRequest, array $params = [])
{
if (!isset($params['event'])) {
throw new MissingArgumentException('event');
}

if (!in_array($params['event'], ["APPROVE", "REQUEST_CHANGES", "COMMENT"], true)) {
throw new InvalidArgumentException(sprintf('"event" must be one of ["APPROVE", "REQUEST_CHANGES", "COMMENT"] ("%s" given).', $params['event']));
}

return $this->post('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/pulls/'.$pullRequest.'/reviews', $params);
}

/**
* Submit a pull request review by the username, repository, pull request number and the review id.
*
* @link https://developer.github.com/v3/pulls/reviews/#submit-a-pull-request-review
*
* @param string $username the username
* @param string $repository the repository
* @param int $pullRequest the pull request number
* @param int $id the review id
* @param array $params a list of extra parameters.
*
* @throws MissingArgumentException
*
* @return array the pull request review
*/
public function submit($username, $repository, $pullRequest, $id, array $params = [])
{
if (!isset($params['event'])) {
throw new MissingArgumentException('event');
}

if (!in_array($params['event'], ["APPROVE", "REQUEST_CHANGES", "COMMENT"], true)) {
throw new InvalidArgumentException(sprintf('"event" must be one of ["APPROVE", "REQUEST_CHANGES", "COMMENT"] ("%s" given).', $params['event']));
}

return $this->post('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/pulls/'.$pullRequest.'/reviews/'.$id.'/events', $params);
}

/**
* Dismiss a pull request review by the username, repository, pull request number and the review id.
*
* @link https://developer.github.com/v3/pulls/reviews/#dismiss-a-pull-request-review
*
* @param string $username the username
* @param string $repository the repository
* @param int $pullRequest the pull request number
* @param int $id the review id
*
* @return array|string
*/
public function dismiss($username, $repository, $pullRequest, $id)
{
return $this->put('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/pulls/'.$pullRequest.'/reviews/'.$id.'/dismissals');
}
}
2 changes: 1 addition & 1 deletion lib/Github/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public function __construct(Builder $httpClientBuilder = null, $apiVersion = nul
)));

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise we wouldn't be able to add the custom Accept header value without having to repeat Accept header values passed by this builder.


if ($enterpriseUrl) {
$this->setEnterpriseUrl($enterpriseUrl);
Expand Down
13 changes: 13 additions & 0 deletions lib/Github/HttpClient/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,19 @@ public function addHeaders(array $headers)
$this->addPlugin(new Plugin\HeaderAppendPlugin($this->headers));
}

/**
* @param string $header
* @param string $headerValue
*/
public function addHeaderValue($header, $headerValue)
{
if (!isset($this->headers[$header])) {
$this->headers[$header] = $headerValue;
} else {
$this->headers[$header] = array_merge((array)$this->headers[$header], array($headerValue));
}
}

/**
* Add a cache plugin to cache responses locally.
*
Expand Down
Loading