diff --git a/composer.json b/composer.json index ef8d0a84f38..aa183b888d3 100644 --- a/composer.json +++ b/composer.json @@ -58,7 +58,8 @@ "config": { "allow-plugins": { "phpstan/extension-installer": true, - "composer/package-versions-deprecated": true + "composer/package-versions-deprecated": true, + "php-http/discovery": true } } } diff --git a/doc/pull_requests.md b/doc/pull_requests.md index d87e6180043..af31e4430b0 100644 --- a/doc/pull_requests.md +++ b/doc/pull_requests.md @@ -90,3 +90,11 @@ $pullRequest = $client->api('pull_request')->create('ezsystems', 'ezpublish', ar ``` This returns the details of the pull request. + +### Merge a Pull Request + +> Requires [authentication](security.md) + +```php +$client->api('pull_request')->merge('KnpLabs', 'php-github-api', $pullNumber, $commitMessage, $sha, $mergeMethod, $commitTitle); +``` diff --git a/doc/users.md b/doc/users.md index 48bb0d7dc15..3cb748c0008 100644 --- a/doc/users.md +++ b/doc/users.md @@ -188,3 +188,16 @@ $emails = $client->api('current_user')->emails()->remove(array('first@provider.o ``` Return an array of the authenticated user emails. + +### List repositories for the user + +> Requires [authentication](security.md) for authenticated user + +```php +$repos = $client->api('current_user')->repositories(); +$repos = $client->api('users')->repositories(); +``` + +> Note: Following arguments `$type`, `$sort`, `$direction`, `$visibility` and `$affiliation` are deprecated and a new array argument is added `$parameters` which can be used to pass all these existing arguments as well as parameters like `per_page` which was not supported earlier. + +Return an array of the authenticated user repos. diff --git a/lib/Github/Api/CurrentUser.php b/lib/Github/Api/CurrentUser.php index b5cbc89a376..b14ce51897e 100644 --- a/lib/Github/Api/CurrentUser.php +++ b/lib/Github/Api/CurrentUser.php @@ -113,16 +113,30 @@ public function teams() /** * @link https://docs.github.com/en/rest/reference/repos#list-repositories-for-the-authenticated-user * - * @param string $type role in the repository - * @param string $sort sort by - * @param string $direction direction of sort, asc or desc - * @param string $visibility visibility of repository - * @param string $affiliation relationship to repository + * @param string $type role in the repository (Deprecated) + * @param string $sort sort by (Deprecated) + * @param string $direction direction of sort, asc or desc (Deprecated) + * @param string $visibility visibility of repository (Deprecated) + * @param string $affiliation relationship to repository (Deprecated) + * @param array $parameters e.g. [ + * 'type' => 'owner', + * 'sort' => 'full_name', + * 'direction'=> 'asc', + * 'visibility' => null, + * 'affiliation' => null, + * 'per_page' => 50, + * ] * * @return array */ - public function repositories($type = 'owner', $sort = 'full_name', $direction = 'asc', $visibility = null, $affiliation = null) - { + public function repositories( + $type = 'owner', + $sort = 'full_name', + $direction = 'asc', + $visibility = null, + $affiliation = null, + array $parameters = [] + ) { $params = [ 'type' => $type, 'sort' => $sort, @@ -139,7 +153,7 @@ public function repositories($type = 'owner', $sort = 'full_name', $direction = $params['affiliation'] = $affiliation; } - return $this->get('/user/repos', $params); + return $this->get('/user/repos', array_merge($params, $parameters)); } /** diff --git a/lib/Github/Api/User.php b/lib/Github/Api/User.php index c1ccc89e8c1..5608273d387 100644 --- a/lib/Github/Api/User.php +++ b/lib/Github/Api/User.php @@ -160,23 +160,38 @@ public function subscriptions($username) * @link https://developer.github.com/v3/repos/#list-user-repositories * * @param string $username the username - * @param string $type role in the repository - * @param string $sort sort by - * @param string $direction direction of sort, asc or desc - * @param string $visibility visibility of repository - * @param string $affiliation relationship to repository + * @param string $type role in the repository (Deprecated) + * @param string $sort sort by (Deprecated) + * @param string $direction direction of sort, asc or desc (Deprecated) + * @param string $visibility visibility of repository (Deprecated) + * @param string $affiliation relationship to repository (Deprecated) + * @param array $params e.g. e.g. [ + * 'type' => 'owner', + * 'sort' => 'full_name', + * 'direction'=> 'asc', + * 'visibility' => 'all', + * 'affiliation' => 'owner,collaborator,organization_member', + * 'per_page' => 50, + * ] * * @return array list of the user repositories */ - public function repositories($username, $type = 'owner', $sort = 'full_name', $direction = 'asc', $visibility = 'all', $affiliation = 'owner,collaborator,organization_member') - { - return $this->get('/users/'.rawurlencode($username).'/repos', [ + public function repositories( + string $username, + $type = 'owner', + $sort = 'full_name', + $direction = 'asc', + $visibility = 'all', + $affiliation = 'owner,collaborator,organization_member', + array $params = [] + ) { + return $this->get('/users/'.rawurlencode($username).'/repos', array_merge([ 'type' => $type, 'sort' => $sort, 'direction' => $direction, 'visibility' => $visibility, 'affiliation' => $affiliation, - ]); + ], $params)); } /** diff --git a/test/Github/Tests/Api/CurrentUserTest.php b/test/Github/Tests/Api/CurrentUserTest.php index 5de99f5c911..3e072e4ee32 100644 --- a/test/Github/Tests/Api/CurrentUserTest.php +++ b/test/Github/Tests/Api/CurrentUserTest.php @@ -2,6 +2,9 @@ namespace Github\Tests\Api; +use Github\Api\CurrentUser; +use PHPUnit\Framework\MockObject\MockObject; + class CurrentUserTest extends TestCase { /** @@ -160,6 +163,32 @@ public function shouldGetStarredApiObject() $this->assertInstanceOf(\Github\Api\CurrentUser\Starring::class, $api->starring()); } + /** + * @test + */ + public function shouldGetRepositories() + { + $expectedArray = [ + ['id' => 1, 'name' => 'dummy project'], + ['id' => 2, 'name' => 'awesome another project'], + ['id' => 3, 'name' => 'fork of php'], + ['id' => 4, 'name' => 'fork of php-cs'], + ]; + + /** @var CurrentUser|MockObject $api */ + $api = $this->getApiMock(); + + $api + ->expects($this->once()) + ->method('get') + ->with('/user/repos') + ->will($this->returnValue($expectedArray)); + + $this->assertEquals($expectedArray, $api->repositories('owner', 'full_name', 'asc', null, null, [ + 'per_page' => 10, + ])); + } + /** * @return string */ diff --git a/test/Github/Tests/Api/UserTest.php b/test/Github/Tests/Api/UserTest.php index 0be80a28f01..398a466dd43 100644 --- a/test/Github/Tests/Api/UserTest.php +++ b/test/Github/Tests/Api/UserTest.php @@ -2,6 +2,9 @@ namespace Github\Tests\Api; +use Github\Api\User; +use PHPUnit\Framework\MockObject\MockObject; + class UserTest extends TestCase { /** @@ -188,13 +191,21 @@ public function shouldGetUserRepositories() { $expectedArray = [['id' => 1, 'name' => 'l3l0repo']]; + /** @var User|MockObject $api */ $api = $this->getApiMock(); $api->expects($this->once()) ->method('get') - ->with('/users/l3l0/repos', ['type' => 'owner', 'sort' => 'full_name', 'direction' => 'asc', 'visibility' => 'all', 'affiliation' => 'owner,collaborator,organization_member']) + ->with('/users/l3l0/repos') ->will($this->returnValue($expectedArray)); - $this->assertEquals($expectedArray, $api->repositories('l3l0')); + $this->assertEquals($expectedArray, $api->repositories('l3l0', 'owner', 'full_name', '', '', '', [ + 'direction' => 'asc', + 'visibility' => 'all', + 'affiliation' => 'owner,collaborator,organization_member', + 'params' => [ + 'per_page' => 1, + ], + ])); } /**