diff --git a/doc/search.md b/doc/search.md index b798b934d35..0a674808001 100644 --- a/doc/search.md +++ b/doc/search.md @@ -34,6 +34,12 @@ Returns a list of issues found by such criteria. $users = $client->api('search')->users('location:Amsterdam language:php'); ``` +### Search commits + +```php +$commits = $client->api('search')->commits('repo:octocat/Spoon-Knife+css'); +``` + Returns a list of users found by such criteria. ### Sorting results @@ -45,4 +51,5 @@ $repos = $client->api('search')->repositories('...', 'created', 'asc'); $files = $client->api('search')->code('...........', 'indexed', 'desc'); $issues = $client->api('search')->issues('.........', 'comments', 'asc'); $users = $client->api('search')->users('..........', 'followers', 'asc'); +$commits = $client->api('search')->commits('..........', 'author-date', 'desc'); ``` diff --git a/lib/Github/Api/Search.php b/lib/Github/Api/Search.php index 15e698ac970..857ef9eb7b9 100644 --- a/lib/Github/Api/Search.php +++ b/lib/Github/Api/Search.php @@ -10,6 +10,8 @@ */ class Search extends AbstractApi { + use AcceptHeaderTrait; + /** * Search repositories by filter (q). * @@ -73,4 +75,23 @@ public function users($q, $sort = 'updated', $order = 'desc') { return $this->get('/search/users', array('q' => $q, 'sort' => $sort, 'order' => $order)); } + + /** + * Search commits by filter (q). + * + * @link https://developer.github.com/v3/search/#search-commits + * + * @param string $q the filter + * @param string $sort the sort field + * @param string $order sort order. asc/desc + * + * @return array + */ + public function commits($q, $sort = null, $order = 'desc') + { + //This api is in preview mode, so set the correct accept-header + $this->acceptHeaderValue = 'application/vnd.github.cloak-preview'; + + return $this->get('/search/commits', array('q' => $q, 'sort' => $sort, 'order' => $order)); + } } diff --git a/test/Github/Tests/Api/SearchTest.php b/test/Github/Tests/Api/SearchTest.php index 69ec3e032eb..c67fc96171c 100644 --- a/test/Github/Tests/Api/SearchTest.php +++ b/test/Github/Tests/Api/SearchTest.php @@ -176,6 +176,26 @@ public function shouldSearchUsersRegardingSortAndOrder() ); } + /** + * @test + */ + public function shouldSearchCommitsRegardingSortAndOrder() + { + $expectedArray = ['total_count' => '0']; + + $api = $this->getApiMock(); + + $api->expects($this->once()) + ->method('get') + ->with('/search/commits', ['q' => 'query text', 'sort' => 'author-date', 'order' => 'asc']) + ->will($this->returnValue($expectedArray)); + + $this->assertEquals( + $expectedArray, + $api->commits('query text', 'author-date', 'asc') + ); + } + /** * @return string */