diff --git a/doc/repos.md b/doc/repos.md index ca8a8b4da74..95cc76a310a 100644 --- a/doc/repos.md +++ b/doc/repos.md @@ -4,6 +4,20 @@ Searching repositories, getting repository information and managing repository information for authenticated users. Wrap [GitHub Repo API](http://developer.github.com/v3/repos/). All methods are described on that page. +### List all repositories + +#### Simple call + +```php +$repos = $client->api('repo')->all(); +``` + +#### Start from a specific repository id + +```php +$repos = $client->api('repo')->all(1337); +``` + ### Search repos by keyword #### Simple search diff --git a/lib/Github/Api/Repo.php b/lib/Github/Api/Repo.php index 34d9dcbb25e..3e398fe1c98 100644 --- a/lib/Github/Api/Repo.php +++ b/lib/Github/Api/Repo.php @@ -39,6 +39,23 @@ public function find($keyword, array $params = array()) return $this->get('legacy/repos/search/'.rawurlencode($keyword), array_merge(array('start_page' => 1), $params)); } + /** + * List all public repositories. + * + * @link https://developer.github.com/v3/repos/#list-all-public-repositories + * + * @param int|null $id The integer ID of the last Repository that you’ve seen. + * + * @return array list of users found + */ + public function all($id = null) + { + if (!is_int($id)) { + return $this->get('repositories'); + } + return $this->get('repositories?since=' . rawurldecode($id)); + } + /** * Get the last year of commit activity for a repository grouped by week. * diff --git a/test/Github/Tests/Api/RepoTest.php b/test/Github/Tests/Api/RepoTest.php index 200b9cbf447..849bfe35d10 100644 --- a/test/Github/Tests/Api/RepoTest.php +++ b/test/Github/Tests/Api/RepoTest.php @@ -58,6 +58,48 @@ public function shouldPaginateFoundRepositories() $this->assertEquals($expectedArray, $api->find('php', array('start_page' => 2))); } + /** + * @test + */ + public function shouldGetAllRepositories() + { + $expectedArray = array( + array('id' => 1, 'name' => 'dummy project'), + array('id' => 2, 'name' => 'awesome another project'), + array('id' => 3, 'name' => 'fork of php'), + array('id' => 4, 'name' => 'fork of php-cs'), + ); + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('get') + ->with('repositories') + ->will($this->returnValue($expectedArray)); + + $this->assertEquals($expectedArray, $api->all()); + } + + /** + * @test + */ + public function shouldGetAllRepositoriesStartingIndex() + { + $expectedArray = array( + array('id' => 1, 'name' => 'dummy project'), + array('id' => 2, 'name' => 'awesome another project'), + array('id' => 3, 'name' => 'fork of php'), + array('id' => 4, 'name' => 'fork of php-cs'), + ); + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('get') + ->with('repositories?since=2') + ->will($this->returnValue($expectedArray)); + + $this->assertEquals($expectedArray, $api->all(2)); + } + /** * @test */