From f4e822eceede81d302f63cf96e580a816e345824 Mon Sep 17 00:00:00 2001 From: Anthony Alaan Date: Tue, 3 Sep 2024 14:24:27 +0800 Subject: [PATCH 1/4] Add Copilot usage endpoints --- lib/Github/Api/Copilot/Usage.php | 28 ++++++++ lib/Github/Client.php | 5 ++ test/Github/Tests/Api/Copilot/UsageTest.php | 78 +++++++++++++++++++++ 3 files changed, 111 insertions(+) create mode 100644 lib/Github/Api/Copilot/Usage.php create mode 100644 test/Github/Tests/Api/Copilot/UsageTest.php diff --git a/lib/Github/Api/Copilot/Usage.php b/lib/Github/Api/Copilot/Usage.php new file mode 100644 index 00000000000..f738c697449 --- /dev/null +++ b/lib/Github/Api/Copilot/Usage.php @@ -0,0 +1,28 @@ +get('/orgs/' . rawurlencode($organization) . '/copilot/usage', $params); + } + + public function orgTeamUsageSummary(string $organization, string $teamSlug, array $params = []): array + { + return $this->get('/orgs/' . rawurlencode($organization) . '/teams/' . rawurlencode($teamSlug) . '/copilot/usage', $params); + } + + public function enterpriseUsageSummary(string $enterprise, array $params = []): array + { + return $this->get('/enterprises/' . rawurlencode($enterprise) . '/copilot/usage', $params); + } + + public function enterpriseTeamUsageSummary(string $enterprise, string $teamSlug, array $params = []): array + { + return $this->get('/enterprises/' . rawurlencode($enterprise) . '/teams/' . rawurlencode($teamSlug) . '/copilot/usage', $params); + } +} diff --git a/lib/Github/Client.php b/lib/Github/Client.php index 56d68d59cec..f765f2597fd 100644 --- a/lib/Github/Client.php +++ b/lib/Github/Client.php @@ -301,6 +301,11 @@ public function api($name): AbstractApi $api = new Api\Organization\OutsideCollaborators($this); break; + case 'copilotUsage': + case 'copilot_usage': + $api = new Api\Copilot\Usage($this); + break; + default: throw new InvalidArgumentException(sprintf('Undefined api instance called: "%s"', $name)); } diff --git a/test/Github/Tests/Api/Copilot/UsageTest.php b/test/Github/Tests/Api/Copilot/UsageTest.php new file mode 100644 index 00000000000..bf60313dd32 --- /dev/null +++ b/test/Github/Tests/Api/Copilot/UsageTest.php @@ -0,0 +1,78 @@ +getApiMock(); + $api->expects($this->once()) + ->method('get') + ->with('/orgs/KnpLabs/copilot/usage', []) + ->will($this->returnValue($expectedValue)); + + $this->assertEquals($expectedValue, $api->orgUsageSummary('KnpLabs')); + } + + /** + * @test + */ + public function shouldGetOrgTeamUsageSummary(): void + { + $expectedValue = ['usage1', 'usage2']; + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('get') + ->with('/orgs/KnpLabs/teams/php-github-api/copilot/usage', []) + ->will($this->returnValue($expectedValue)); + + $this->assertEquals($expectedValue, $api->orgTeamUsageSummary('KnpLabs', 'php-github-api')); + } + + /** + * @test + */ + public function shouldGetEnterpriseUsageSummary(): void + { + $expectedValue = ['usage1', 'usage2']; + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('get') + ->with('/enterprises/KnpLabs/copilot/usage', []) + ->will($this->returnValue($expectedValue)); + + $this->assertEquals($expectedValue, $api->enterpriseUsageSummary('KnpLabs')); + } + + /** + * @test + */ + public function shouldGetEnterpriseTeamUsageSummary(): void + { + $expectedValue = ['usage1', 'usage2']; + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('get') + ->with('/enterprises/KnpLabs/teams/php-github-api/copilot/usage', []) + ->will($this->returnValue($expectedValue)); + + $this->assertEquals($expectedValue, $api->enterpriseTeamUsageSummary('KnpLabs', 'php-github-api')); + } + + protected function getApiClass(): string + { + return Usage::class; + } +} From e16a14c94cc1dbabe4bc4b57e328cef24061d40d Mon Sep 17 00:00:00 2001 From: Anthony Alaan Date: Tue, 3 Sep 2024 14:45:44 +0800 Subject: [PATCH 2/4] Reformat code --- lib/Github/Api/Copilot/Usage.php | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/Github/Api/Copilot/Usage.php b/lib/Github/Api/Copilot/Usage.php index f738c697449..7ef246a9424 100644 --- a/lib/Github/Api/Copilot/Usage.php +++ b/lib/Github/Api/Copilot/Usage.php @@ -8,21 +8,27 @@ class Usage extends AbstractApi { public function orgUsageSummary(string $organization, array $params = []): array { - return $this->get('/orgs/' . rawurlencode($organization) . '/copilot/usage', $params); + return $this->get('/orgs/'.rawurlencode($organization).'/copilot/usage', $params); } public function orgTeamUsageSummary(string $organization, string $teamSlug, array $params = []): array { - return $this->get('/orgs/' . rawurlencode($organization) . '/teams/' . rawurlencode($teamSlug) . '/copilot/usage', $params); + return $this->get( + '/orgs/'.rawurlencode($organization).'/teams/'.rawurlencode($teamSlug).'/copilot/usage', + $params + ); } public function enterpriseUsageSummary(string $enterprise, array $params = []): array { - return $this->get('/enterprises/' . rawurlencode($enterprise) . '/copilot/usage', $params); + return $this->get('/enterprises/'.rawurlencode($enterprise).'/copilot/usage', $params); } public function enterpriseTeamUsageSummary(string $enterprise, string $teamSlug, array $params = []): array { - return $this->get('/enterprises/' . rawurlencode($enterprise) . '/teams/' . rawurlencode($teamSlug) . '/copilot/usage', $params); + return $this->get( + '/enterprises/'.rawurlencode($enterprise).'/teams/'.rawurlencode($teamSlug).'/copilot/usage', + $params + ); } } From 3698933a3246ecc363a986151cbed483c84b7999 Mon Sep 17 00:00:00 2001 From: Anthony Alaan Date: Tue, 3 Sep 2024 14:55:19 +0800 Subject: [PATCH 3/4] Fix incorrect team urls --- lib/Github/Api/Copilot/Usage.php | 4 ++-- test/Github/Tests/Api/Copilot/UsageTest.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/Github/Api/Copilot/Usage.php b/lib/Github/Api/Copilot/Usage.php index 7ef246a9424..0110a58bb40 100644 --- a/lib/Github/Api/Copilot/Usage.php +++ b/lib/Github/Api/Copilot/Usage.php @@ -14,7 +14,7 @@ public function orgUsageSummary(string $organization, array $params = []): array public function orgTeamUsageSummary(string $organization, string $teamSlug, array $params = []): array { return $this->get( - '/orgs/'.rawurlencode($organization).'/teams/'.rawurlencode($teamSlug).'/copilot/usage', + '/orgs/'.rawurlencode($organization).'/team/'.rawurlencode($teamSlug).'/copilot/usage', $params ); } @@ -27,7 +27,7 @@ public function enterpriseUsageSummary(string $enterprise, array $params = []): public function enterpriseTeamUsageSummary(string $enterprise, string $teamSlug, array $params = []): array { return $this->get( - '/enterprises/'.rawurlencode($enterprise).'/teams/'.rawurlencode($teamSlug).'/copilot/usage', + '/enterprises/'.rawurlencode($enterprise).'/team/'.rawurlencode($teamSlug).'/copilot/usage', $params ); } diff --git a/test/Github/Tests/Api/Copilot/UsageTest.php b/test/Github/Tests/Api/Copilot/UsageTest.php index bf60313dd32..c14c3e3ffa8 100644 --- a/test/Github/Tests/Api/Copilot/UsageTest.php +++ b/test/Github/Tests/Api/Copilot/UsageTest.php @@ -33,7 +33,7 @@ public function shouldGetOrgTeamUsageSummary(): void $api = $this->getApiMock(); $api->expects($this->once()) ->method('get') - ->with('/orgs/KnpLabs/teams/php-github-api/copilot/usage', []) + ->with('/orgs/KnpLabs/team/php-github-api/copilot/usage', []) ->will($this->returnValue($expectedValue)); $this->assertEquals($expectedValue, $api->orgTeamUsageSummary('KnpLabs', 'php-github-api')); @@ -65,7 +65,7 @@ public function shouldGetEnterpriseTeamUsageSummary(): void $api = $this->getApiMock(); $api->expects($this->once()) ->method('get') - ->with('/enterprises/KnpLabs/teams/php-github-api/copilot/usage', []) + ->with('/enterprises/KnpLabs/team/php-github-api/copilot/usage', []) ->will($this->returnValue($expectedValue)); $this->assertEquals($expectedValue, $api->enterpriseTeamUsageSummary('KnpLabs', 'php-github-api')); From 956fe9eb4d7596765710fd5914020fa2cf0068ac Mon Sep 17 00:00:00 2001 From: Anthony Alaan Date: Tue, 3 Sep 2024 15:36:12 +0800 Subject: [PATCH 4/4] Add Copilot Usage API Documentation detailing endpoints for retrieving usage summaries. --- doc/copilot/usage.md | 80 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 doc/copilot/usage.md diff --git a/doc/copilot/usage.md b/doc/copilot/usage.md new file mode 100644 index 00000000000..6005adc1600 --- /dev/null +++ b/doc/copilot/usage.md @@ -0,0 +1,80 @@ +# Copilot Usage API Documentation +[Back to the navigation](../README.md) + +## Overview + +The Copilot Usage API provides endpoints to retrieve usage summaries for organizations and enterprises. + +**Note**: This endpoint is in beta and is subject to change. + +## Endpoints + +### Organization Usage Summary + +Retrieve the usage summary for a specific organization. + +**Method:** `GET` + +**Endpoint:** `/orgs/{organization}/copilot/usage` + +**Parameters:** +- `organization` (string): The name of the organization. +- `params` (array, optional): Additional query parameters. + +**Example:** +```php +$usage = $client->api('copilotUsage')->orgUsageSummary('KnpLabs'); +``` + +### Organization Team Usage Summary + +Retrieve the usage summary for a specific team within an organization. + +**Method:** `GET` + +**Endpoint:** `/orgs/{organization}/team/{team}/copilot/usage` + +**Parameters:** +- `organization` (string): The name of the organization. +- `team` (string): The name of the team. +- `params` (array, optional): Additional query parameters. + +**Example:** +```php +$usage = $client->api('copilotUsage')->orgTeamUsageSummary('KnpLabs', 'developers'); +``` + +### Enterprise Usage Summary + +Retrieve the usage summary for a specific enterprise. + +**Method:** `GET` + +**Endpoint:** `/enterprises/{enterprise}/copilot/usage` + +**Parameters:** +- `enterprise` (string): The name of the enterprise. +- `params` (array, optional): Additional query parameters. + +**Example:** +```php +$usage = $client->api('copilotUsage')->enterpriseUsageSummary('KnpLabs'); +``` + +### Enterprise Team Usage Summary + +Retrieve the usage summary for a specific team within an enterprise. + +**Method:** `GET` + +**Endpoint:** `/enterprises/{enterprise}/team/{team}/copilot/usage` + +**Parameters:** +- `enterprise` (string): The name of the enterprise. +- `team` (string): The name of the team. +- `params` (array, optional): Additional query parameters. + +**Example:** +```php +$usage = $client->api('copilotUsage')->enterpriseTeamUsageSummary('KnpLabs', 'developers'); +```