From b565d6d7122f7e8c1edc80a4b249e9be99978d23 Mon Sep 17 00:00:00 2001 From: Ricardo Aragon Date: Wed, 21 Apr 2021 08:54:50 -0400 Subject: [PATCH 1/6] Add list events for an authenticated user method --- lib/Github/Api/User.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lib/Github/Api/User.php b/lib/Github/Api/User.php index b36ae47123b..da89d0b9dad 100644 --- a/lib/Github/Api/User.php +++ b/lib/Github/Api/User.php @@ -234,4 +234,23 @@ 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 + * + * @param string $username + * @param int $page the page number of the paginated result set + * @param int $perPage the number of results per page + * + * @return array + */ + public function events($username, $page = 1, $perPage = 30) + { + return $this->get('/users/'.rawurlencode($username).'/events', [ + 'page' => $page, + 'per_page' => $perPage, + ]); + } } From 10a85ea86fd61d2228b4d272d590b1d8aa9a6a31 Mon Sep 17 00:00:00 2001 From: Ricardo Aragon Date: Wed, 21 Apr 2021 09:26:03 -0400 Subject: [PATCH 2/6] Add docs and tests for List Events by Authenticated User method --- doc/activity.md | 9 ++++++++- doc/users.md | 11 +++++++++++ test/Github/Tests/Integration/UserTest.php | 21 +++++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) 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/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); + } } From 3938a4220420a5919be545ffd055ea8ffaa69e53 Mon Sep 17 00:00:00 2001 From: Ricardo Aragon Date: Wed, 21 Apr 2021 09:32:17 -0400 Subject: [PATCH 3/6] Fix lint for comment block --- lib/Github/Api/User.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Github/Api/User.php b/lib/Github/Api/User.php index da89d0b9dad..eaa11278264 100644 --- a/lib/Github/Api/User.php +++ b/lib/Github/Api/User.php @@ -241,8 +241,8 @@ public function publicEvents($username) * @link https://docs.github.com/en/rest/reference/activity#list-events-for-the-authenticated-user * * @param string $username - * @param int $page the page number of the paginated result set - * @param int $perPage the number of results per page + * @param int $page the page number of the paginated result set + * @param int $perPage the number of results per page * * @return array */ From 5534e255ad103fda2ce4b2b83eab9d72a04093e1 Mon Sep 17 00:00:00 2001 From: Ricardo Aragon Date: Wed, 21 Apr 2021 09:38:51 -0400 Subject: [PATCH 4/6] Fix lint for comment block --- lib/Github/Api/User.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Github/Api/User.php b/lib/Github/Api/User.php index eaa11278264..79af5dc92d8 100644 --- a/lib/Github/Api/User.php +++ b/lib/Github/Api/User.php @@ -241,8 +241,8 @@ public function publicEvents($username) * @link https://docs.github.com/en/rest/reference/activity#list-events-for-the-authenticated-user * * @param string $username - * @param int $page the page number of the paginated result set - * @param int $perPage the number of results per page + * @param int $page the page number of the paginated result set + * @param int $perPage the number of results per page * * @return array */ From c520754fc31d5e16f4cec84afa708e7d1c7412cb Mon Sep 17 00:00:00 2001 From: Ricardo Aragon Date: Wed, 21 Apr 2021 13:16:08 -0400 Subject: [PATCH 5/6] Add API unit test. Remove perPage and page parameters for events method --- lib/Github/Api/User.php | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/lib/Github/Api/User.php b/lib/Github/Api/User.php index 79af5dc92d8..c1ccc89e8c1 100644 --- a/lib/Github/Api/User.php +++ b/lib/Github/Api/User.php @@ -240,17 +240,10 @@ public function publicEvents($username) * * @link https://docs.github.com/en/rest/reference/activity#list-events-for-the-authenticated-user * - * @param string $username - * @param int $page the page number of the paginated result set - * @param int $perPage the number of results per page - * * @return array */ - public function events($username, $page = 1, $perPage = 30) + public function events(string $username) { - return $this->get('/users/'.rawurlencode($username).'/events', [ - 'page' => $page, - 'per_page' => $perPage, - ]); + return $this->get('/users/'.rawurlencode($username).'/events'); } } From 42f1f423fa4737e69c58a3c438eb6f030a2eb1d7 Mon Sep 17 00:00:00 2001 From: Ricardo Aragon Date: Thu, 22 Apr 2021 12:54:41 -0400 Subject: [PATCH 6/6] Add unit test for API --- test/Github/Tests/Api/UserTest.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) 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 */