Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Primed solving of #13,

based on Github's Pull Request API documentation : http://develop.github.com/p/pulls.html
  • Loading branch information...
commit 3700336c506634675fb51ef583fb87cabd9dabab 1 parent da66e04
Nicolas Pastorino authored
1  README.markdown
View
@@ -531,5 +531,6 @@ This library borrows ideas, code and tests from [phptwitterbot](http://code.goog
- Thanks to [noloh](http://github.com/noloh) for his contribution on the Object API.
- Thanks to [bshaffer](http://github.com/bshaffer) for his contribution on the Repo API.
- Thanks to [Rolf van de Krol](http://github.com/rolfvandekrol) for his countless contributions.
+- Thanks to [Nicolas Pastorino](http://github.com/jeanvoye) for his contribution on the Pull Request API.
Thanks to GitHub for the high quality API and documentation.
95 lib/Github/Api/PullRequest.php
View
@@ -0,0 +1,95 @@
+<?php
+
+/**
+ * API for accessing Pull Requests from your Git/Github repositories.
+ *
+ * @link http://develop.github.com/p/pulls.html
+ * @author Nicolas Pastorino <nicolas.pastorino at gmail dot com>
+ * @license MIT License
+ */
+class Github_Api_PullRequest extends Github_Api
+{
+ /**
+ * Get a listing of a project's pull requests by the username, repo, and optionnally state.
+ *
+ * @link http://develop.github.com/p/pulls.html
+ * @param string $username the username
+ * @param string $repo the repo
+ * @param string $state the state of the fetched pull requests.
+ * The API seems to automatically default to 'open'
+ * @return array array of pull requests for the project
+ */
+ public function listPullRequests($username, $repo, $state = '')
+ {
+ $response = $this->get('pulls/'.urlencode($username).'/'.urlencode($repo).'/'.urlencode($state));
+ return $response['pulls'];
+ }
+
+ /**
+ * Show all details of a pull request, including the discussions.
+ *
+ * @link http://develop.github.com/p/pulls.html
+ * @param string $username the username
+ * @param string $repo the repo
+ * @param string $pullRequestId the ID of the pull request for which details are retrieved
+ * @return array array of pull requests for the project
+ */
+ public function show($username, $repo, $pullRequestId)
+ {
+ $response = $this->get('pulls/'.urlencode($username).'/'.urlencode($repo).'/'.urlencode($pullRequestId));
+ return $response['pulls'];
+ }
+
+ /**
+ * Create a pull request
+ *
+ * @link http://develop.github.com/p/pulls.html
+ * @param string $username the username
+ * @param string $repo the repo
+ * @param string $base A String of the branch or commit SHA that you want your changes to be pulled to.
+ * @param string $head 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".
+ * @param string $title The String title of the Pull Request. Used in pair with $body.
+ * @param string $body The String body of the Pull Request. Used in pair with $title.
+ * @param int $issueId If a pull-request is related to an issue, place issue ID here. The $title-$body pair and this are mutually exclusive.
+ * @return array array of pull requests for the project
+ */
+ public function create($username, $repo, $base, $head, $title = null, $body = null, $issueId = null)
+ {
+ $postParameters = array( 'pull[base]' => $base,
+ 'pull[head]' => $head
+ );
+
+ if ( $title !== null and $body !== null )
+ {
+ $postParameters = array_merge( $postParameters,
+ array(
+ 'pull[title]' => $title,
+ 'pull[body]' => $body
+ )
+ );
+ }
+ elseif ( $issueId !== null )
+ {
+ $postParameters = array_merge( $postParameters,
+ array(
+ 'pull[issue]' => $issueId
+ )
+ );
+ }
+ else
+ {
+ // @FIXME : Exception required here.
+ return null;
+ }
+
+ $response = $this->post('pulls/'.urlencode($username).'/'.urlencode($repo),
+ $postParameters
+ );
+
+ // @FIXME : Exception to be thrown when $response['error'] exists.
+ // Content of error can be : "{"error":["A pull request already exists for <username>:<branch>."]}"
+ return $response['pull'];
+ }
+}
84 test/Github/Tests/Api/PullRequestTest.php
View
@@ -0,0 +1,84 @@
+<?php
+
+class Github_Tests_Api_PullRequestTest extends Github_Tests_ApiTest
+{
+ public function testlistPullRequests()
+ {
+ $api = $this->getApiMock();
+
+ // 1. Test with no last parameter
+ $api->expects($this->once())
+ ->method('get')
+ ->with('pulls/ezsystems/ezpublish/');
+
+ $api->listPullRequests('ezsystems', 'ezpublish' );
+
+
+ $api = $this->getApiMock();
+ // 2. Test with last parameter set to 'open'
+ $api->expects($this->once())
+ ->method('get')
+ ->with('pulls/ezsystems/ezpublish/open');
+
+ $api->listPullRequests('ezsystems', 'ezpublish', 'open' );
+
+ $api = $this->getApiMock();
+ // 2. Test with last parameter set to 'closed'
+ $api->expects($this->once())
+ ->method('get')
+ ->with('pulls/ezsystems/ezpublish/closed');
+
+ $api->listPullRequests('ezsystems', 'ezpublish', 'closed' );
+ }
+
+ public function testshow()
+ {
+ $api = $this->getApiMock();
+
+ $api->expects($this->once())
+ ->method('get')
+ ->with('pulls/ezsystems/ezpublish/15');
+
+ $api->listPullRequests('ezsystems', 'ezpublish', '15' );
+ }
+
+ public function testcreate()
+ {
+ // 1. Testing with body & title
+ $api = $this->getApiMock();
+
+ $api->expects($this->once())
+ ->method('post')
+ ->with('pulls/ezsystems/ezpublish',
+ array( 'pull[base]' => 'master',
+ 'pull[head]' => 'virtualtestbranch',
+ 'pull[title]' => 'TITLE : Testing pull-request creation from PHP Gituhub API',
+ 'pull[body]' => 'BODY: Testing pull-request creation from PHP Gituhub API'
+ )
+ );
+
+ $api->create('ezsystems', 'ezpublish', 'master', 'virtualtestbranch',
+ 'TITLE : Testing pull-request creation from PHP Gituhub API',
+ 'BODY: Testing pull-request creation from PHP Gituhub API' );
+
+
+ // 2. Testing with issue ID
+ $api = $this->getApiMock();
+
+ $api->expects($this->once())
+ ->method('post')
+ ->with('pulls/ezsystems/ezpublish',
+ array( 'pull[base]' => 'master',
+ 'pull[head]' => 'virtualtestbranch',
+ 'pull[issue]' => 25
+ )
+ );
+
+ $api->create('ezsystems', 'ezpublish', 'master', 'virtualtestbranch', null, null, 25 );
+ }
+
+ protected function getApiClass()
+ {
+ return 'Github_Api_PullRequest';
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.