diff --git a/doc/activity.md b/doc/activity.md index 7cad30ef5f7..c15690e9fe3 100644 --- a/doc/activity.md +++ b/doc/activity.md @@ -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. @@ -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. \ No newline at end of file +Throws an Exception in case of failure or NULL in case of success. diff --git a/doc/users.md b/doc/users.md index 3bc20c39bc3..5bac98a7d16 100644 --- a/doc/users.md +++ b/doc/users.md @@ -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). diff --git a/lib/Github/Api/User.php b/lib/Github/Api/User.php index b36ae47123b..c1ccc89e8c1 100644 --- a/lib/Github/Api/User.php +++ b/lib/Github/Api/User.php @@ -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'); + } } diff --git a/test/Github/Tests/Api/UserTest.php b/test/Github/Tests/Api/UserTest.php index 491a58f8602..0be80a28f01 100644 --- a/test/Github/Tests/Api/UserTest.php +++ b/test/Github/Tests/Api/UserTest.php @@ -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 */ diff --git a/test/Github/Tests/Integration/UserTest.php b/test/Github/Tests/Integration/UserTest.php index 57dbeceeefc..7b8f9f0c364 100644 --- a/test/Github/Tests/Integration/UserTest.php +++ b/test/Github/Tests/Integration/UserTest.php @@ -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); + } }