Skip to content
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

fix: minor bugs and doc update #1116

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
8 changes: 8 additions & 0 deletions doc/pull_requests.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
```
13 changes: 13 additions & 0 deletions doc/users.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,16 @@ $emails = $client->api('current_user')->emails()->remove(array('[email protected]
```

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.
30 changes: 22 additions & 8 deletions lib/Github/Api/CurrentUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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));
}

/**
Expand Down
33 changes: 24 additions & 9 deletions lib/Github/Api/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand Down
29 changes: 29 additions & 0 deletions test/Github/Tests/Api/CurrentUserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Github\Tests\Api;

use Github\Api\CurrentUser;
use PHPUnit\Framework\MockObject\MockObject;

class CurrentUserTest extends TestCase
{
/**
Expand Down Expand Up @@ -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
*/
Expand Down
15 changes: 13 additions & 2 deletions test/Github/Tests/Api/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Github\Tests\Api;

use Github\Api\User;
use PHPUnit\Framework\MockObject\MockObject;

class UserTest extends TestCase
{
/**
Expand Down Expand Up @@ -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,
],
]));
}

/**
Expand Down