Skip to content

Commit

Permalink
feature #1000 Events list per authenticated user for all repos (richa…
Browse files Browse the repository at this point in the history
…rd015ar)

This PR was squashed before being merged into the 3.2.x-dev branch.

Discussion
----------

This change add a list of events for all repos for an authenticated user: https://docs.github.com/en/rest/reference/activity#list-events-for-the-authenticated-user.
It is pretty useful if you want to get a list of different [types of events](https://docs.github.com/en/developers/webhooks-and-events/github-event-types) without specify any repo.

Commits
-------

b565d6d Add list events for an authenticated user method
10a85ea Add docs and tests for List Events by Authenticated User method
3938a42 Fix lint for comment block
5534e25 Fix lint for comment block
c520754 Add API unit test. Remove perPage and page parameters for events method
42f1f42 Add unit test for API
  • Loading branch information
richard015ar authored Apr 23, 2021
1 parent 26a838b commit 54ddd41
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 1 deletion.
9 changes: 8 additions & 1 deletion doc/activity.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ $activity = $client->api('current_user')->starring()->all();
```
Returns an array of starred repos.

### Get list of private and public events for an authenticated user for all repos

```php
$activity = $client->api('user')->events('ornicar');
```
Returns an array of private and public events created for all repos related to the user.

### Get repos that a authenticated user has starred with creation date

Support for getting the star creation timestamp in the response, using the custom `Accept: application/vnd.github.v3.star+json` header.
Expand Down Expand Up @@ -100,4 +107,4 @@ $owner = "KnpLabs";
$repo = "php-github-api";
$activity = $client->api('current_user')->watchers()->unwatch($owner, $repo);
```
Throws an Exception in case of failure or NULL in case of success.
Throws an Exception in case of failure or NULL in case of success.
11 changes: 11 additions & 0 deletions doc/users.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,17 @@ $users = $client->api('current_user')->starring()->all();

Returns an array of starred repos.

### Get the authenticated user activity

> Requires [authentication](security.md).
```php
$activity = $client->api('user')->events('ornicar');
```

Returns an array of private and public events created for all repos related to the user.
> See [more](activity.md).
### Get the authenticated user emails

> Requires [authentication](security.md).
Expand Down
12 changes: 12 additions & 0 deletions lib/Github/Api/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,16 @@ public function publicEvents($username)
{
return $this->get('/users/'.rawurlencode($username).'/events/public');
}

/**
* List events performed by an authenticated user.
*
* @link https://docs.github.com/en/rest/reference/activity#list-events-for-the-authenticated-user
*
* @return array
*/
public function events(string $username)
{
return $this->get('/users/'.rawurlencode($username).'/events');
}
}
23 changes: 23 additions & 0 deletions test/Github/Tests/Api/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,29 @@ public function shouldGetUserGists()
$this->assertEquals($expectedArray, $api->gists('l3l0'));
}

/**
* @test
*/
public function shouldGetAuthorizedUserEvents()
{
$expectedArray = [
[
'id' => 1,
'actor' => [
'id' => 1,
'login' => 'l3l0',
],
],
];
$api = $this->getApiMock();
$api->expects($this->once())
->method('get')
->with('/users/l3l0/events')
->will($this->returnValue($expectedArray));

$this->assertEquals($expectedArray, $api->events('l3l0'));
}

/**
* @return string
*/
Expand Down
21 changes: 21 additions & 0 deletions test/Github/Tests/Integration/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,25 @@ public function shouldGetReposBeingStarred()
$this->assertArrayHasKey('git_url', $repo);
$this->assertArrayHasKey('svn_url', $repo);
}

/**
* @test
*/
public function shouldGetEventsForAuthenticatedUserBeignWatched()
{
$username = 'l3l0';

$events = $this->client->api('user')->events($username);
$event = array_pop($events);

$this->assertArrayHasKey('id', $event);
$this->assertArrayHasKey('type', $event);
$this->assertArrayHasKey('actor', $event);
$this->assertArrayHasKey('login', $event['actor']);
$this->assertArrayHasKey('repo', $event);
$this->assertArrayHasKey('name', $event['repo']);
$this->assertArrayHasKey('payload', $event);
$this->assertArrayHasKey('public', $event);
$this->assertArrayHasKey('created_at', $event);
}
}

0 comments on commit 54ddd41

Please sign in to comment.