From 75c79306f76844d0d1916df1ea2890f81f76c539 Mon Sep 17 00:00:00 2001 From: Froxz Date: Thu, 9 Mar 2023 10:59:33 +0200 Subject: [PATCH 1/8] Added environments --- doc/README.md | 1 + doc/repo/environments.md | 28 +++++++++ lib/Github/Api/Environment.php | 73 +++++++++++++++++++++++ test/Github/Tests/Api/EnvironmentTest.php | 72 ++++++++++++++++++++++ 4 files changed, 174 insertions(+) create mode 100644 doc/repo/environments.md create mode 100644 lib/Github/Api/Environment.php create mode 100644 test/Github/Tests/Api/EnvironmentTest.php diff --git a/doc/README.md b/doc/README.md index acae0d3a441..3c1579f3269 100644 --- a/doc/README.md +++ b/doc/README.md @@ -59,6 +59,7 @@ v3 APIs: * [Check Suites](repo/check_suites.md) * [Contents](repo/contents.md) * [Deployments](repo/deployments.md) + * [Environments](repo/environments.md) * [Labels](repo/labels.md) * [Protection](repo/protection.md) * [Releases](repo/releases.md) diff --git a/doc/repo/environments.md b/doc/repo/environments.md new file mode 100644 index 00000000000..56387ef8bfc --- /dev/null +++ b/doc/repo/environments.md @@ -0,0 +1,28 @@ +## Repo / Environments API +[Back to the "Repos API"](../repos.md) | [Back to the navigation](../index.md) + +Provides information about environments for a repository. Wraps [GitHub Environments API](https://docs.github.com/en/rest/deployments/environments?apiVersion=2022-11-28). + +#### List all environments. + +```php +$environments = $client->api('environment')->all('KnpLabs', 'php-github-api'); +``` + +### Get one environment. + +```php +$environment = $client->api('environment')->show('KnpLabs', 'php-github-api', $name); +``` + +#### Create or update environment. + +```php +$data = $client->api('environment')->createOrUpdate('KnpLabs', 'php-github-api', $name); +``` + +#### Delete a existing environment. + +```php +$environment = $client->api('environment')->remove('KnpLabs', 'php-github-api', $name); +``` diff --git a/lib/Github/Api/Environment.php b/lib/Github/Api/Environment.php new file mode 100644 index 00000000000..cec0c2b567e --- /dev/null +++ b/lib/Github/Api/Environment.php @@ -0,0 +1,73 @@ +get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/environments', $params); + } + + /** + * Get a environment in selected repository. + * + * @param string $username the user who owns the repo + * @param string $repository the name of the repo + * @param string $name the name of the environment + * + * @return array + */ + public function show($username, $repository, $name) + { + return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/environments/'.$name); + } + + /** + * Create or update a environment for the given username and repo. + * + * @link https://docs.github.com/en/rest/deployments/environments?apiVersion=2022-11-28#create-or-update-an-environment + * + * @param string $username the username + * @param string $repository the repository + * @param string $name the name of the environment + * @param array $params the new environment data + * + * @return array information about the environment + */ + public function createOrUpdate($username, $repository, $name, array $params = []) + { + + return $this->put('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/environments', $params); + } + + /** + * Delete a environment for the given username and repo. + * + * @link https://docs.github.com/en/rest/deployments/environments?apiVersion=2022-11-28#delete-an-environment + * + * @return mixed null on success, array on error with 'message' + */ + public function remove(string $username, string $repository, string $name) + { + return $this->delete('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/environments/'.$name); + } +} diff --git a/test/Github/Tests/Api/EnvironmentTest.php b/test/Github/Tests/Api/EnvironmentTest.php new file mode 100644 index 00000000000..dcd7a38451f --- /dev/null +++ b/test/Github/Tests/Api/EnvironmentTest.php @@ -0,0 +1,72 @@ +getApiMock(); + + $api->expects($this->once()) + ->method('put') + ->with('/repos/KnpLabs/php-github-api/environments'); + + $api->create('KnpLabs', 'php-github-api'); + } + + /** + * @test + */ + public function shouldGetAllEnvironments() + { + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('get') + ->with('/repos/KnpLabs/php-github-api/environments'); + + $api->all('KnpLabs', 'php-github-api'); + } + /** + * @test + */ + public function shouldShowEnvironment() + { + $expectedValue = ['name' => 123]; + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('get') + ->with('/repos/KnpLabs/php-github-api/environments/123') + ->will($this->returnValue($expectedValue)); + + $this->assertEquals($expectedValue, $api->show('KnpLabs', 'php-github-api', 123)); + } + + /** + * @test + */ + public function shouldDeleteEnvironment() + { + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('delete') + ->with('/repos/KnpLabs/php-github-api/environments/123') + ->will($this->returnValue(null)); + + $this->assertNull($api->remove('KnpLabs', 'php-github-api', 123)); + } + + /** + * @return string + */ + protected function getApiClass() + { + return \Github\Api\Environment::class; + } +} From 329c72fe2cb4b4a5f5e8bc78b830007d7b7be8c2 Mon Sep 17 00:00:00 2001 From: Froxz Date: Thu, 9 Mar 2023 11:10:45 +0200 Subject: [PATCH 2/8] environment --- lib/Github/Client.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/Github/Client.php b/lib/Github/Client.php index 56d68d59cec..c1eaa501faf 100644 --- a/lib/Github/Client.php +++ b/lib/Github/Client.php @@ -181,6 +181,11 @@ public function api($name): AbstractApi $api = new Api\Deployment($this); break; + case 'environment': + case 'environments': + $api = new Api\Environment($this); + break; + case 'ent': case 'enterprise': $api = new Api\Enterprise($this); From b163de2daab9d490bca111eb22671c3b8f960750 Mon Sep 17 00:00:00 2001 From: Froxz Date: Thu, 9 Mar 2023 11:15:00 +0200 Subject: [PATCH 3/8] fixes --- lib/Github/Api/Environment.php | 1 - test/Github/Tests/Api/EnvironmentTest.php | 3 +-- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/Github/Api/Environment.php b/lib/Github/Api/Environment.php index cec0c2b567e..b4327823ce4 100644 --- a/lib/Github/Api/Environment.php +++ b/lib/Github/Api/Environment.php @@ -55,7 +55,6 @@ public function show($username, $repository, $name) */ public function createOrUpdate($username, $repository, $name, array $params = []) { - return $this->put('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/environments', $params); } diff --git a/test/Github/Tests/Api/EnvironmentTest.php b/test/Github/Tests/Api/EnvironmentTest.php index dcd7a38451f..c9cdcea7b30 100644 --- a/test/Github/Tests/Api/EnvironmentTest.php +++ b/test/Github/Tests/Api/EnvironmentTest.php @@ -2,8 +2,6 @@ namespace Github\Tests\Api; -use Github\Exception\MissingArgumentException; - class EnvironmentTest extends TestCase { /** @@ -32,6 +30,7 @@ public function shouldGetAllEnvironments() $api->all('KnpLabs', 'php-github-api'); } + /** * @test */ From e9da726b127d03f012348c074cc42dc5e12a30c4 Mon Sep 17 00:00:00 2001 From: Froxz Date: Thu, 9 Mar 2023 11:16:03 +0200 Subject: [PATCH 4/8] fixes --- test/Github/Tests/Api/EnvironmentTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Github/Tests/Api/EnvironmentTest.php b/test/Github/Tests/Api/EnvironmentTest.php index c9cdcea7b30..7ce42dce3d0 100644 --- a/test/Github/Tests/Api/EnvironmentTest.php +++ b/test/Github/Tests/Api/EnvironmentTest.php @@ -30,7 +30,7 @@ public function shouldGetAllEnvironments() $api->all('KnpLabs', 'php-github-api'); } - + /** * @test */ From 536ac080141039b5b4082c4084942e419e92eb59 Mon Sep 17 00:00:00 2001 From: Froxz Date: Thu, 9 Mar 2023 11:42:52 +0200 Subject: [PATCH 5/8] Fixed Tests --- test/Github/Tests/Api/EnvironmentTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Github/Tests/Api/EnvironmentTest.php b/test/Github/Tests/Api/EnvironmentTest.php index 7ce42dce3d0..1a4426560ad 100644 --- a/test/Github/Tests/Api/EnvironmentTest.php +++ b/test/Github/Tests/Api/EnvironmentTest.php @@ -15,7 +15,7 @@ public function shouldCreateOrUpdateEnvironment() ->method('put') ->with('/repos/KnpLabs/php-github-api/environments'); - $api->create('KnpLabs', 'php-github-api'); + $api->createOrUpdate('KnpLabs', 'php-github-api'); } /** From 48da33703f488680d5a0985e8d0817f380eb282f Mon Sep 17 00:00:00 2001 From: Froxz Date: Thu, 9 Mar 2023 12:00:59 +0200 Subject: [PATCH 6/8] Fixed tests Tested locally all passed --- test/Github/Tests/Api/EnvironmentTest.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/Github/Tests/Api/EnvironmentTest.php b/test/Github/Tests/Api/EnvironmentTest.php index 1a4426560ad..56efbc0afc8 100644 --- a/test/Github/Tests/Api/EnvironmentTest.php +++ b/test/Github/Tests/Api/EnvironmentTest.php @@ -15,7 +15,7 @@ public function shouldCreateOrUpdateEnvironment() ->method('put') ->with('/repos/KnpLabs/php-github-api/environments'); - $api->createOrUpdate('KnpLabs', 'php-github-api'); + $api->createOrUpdate('KnpLabs', 'php-github-api', 'production'); } /** @@ -36,15 +36,15 @@ public function shouldGetAllEnvironments() */ public function shouldShowEnvironment() { - $expectedValue = ['name' => 123]; + $expectedValue = 'production'; $api = $this->getApiMock(); $api->expects($this->once()) ->method('get') - ->with('/repos/KnpLabs/php-github-api/environments/123') + ->with('/repos/KnpLabs/php-github-api/environments/production') ->will($this->returnValue($expectedValue)); - $this->assertEquals($expectedValue, $api->show('KnpLabs', 'php-github-api', 123)); + $this->assertEquals($expectedValue, $api->show('KnpLabs', 'php-github-api', 'production')); } /** @@ -55,10 +55,10 @@ public function shouldDeleteEnvironment() $api = $this->getApiMock(); $api->expects($this->once()) ->method('delete') - ->with('/repos/KnpLabs/php-github-api/environments/123') + ->with('/repos/KnpLabs/php-github-api/environments/production') ->will($this->returnValue(null)); - $this->assertNull($api->remove('KnpLabs', 'php-github-api', 123)); + $this->assertNull($api->remove('KnpLabs', 'php-github-api', 'production')); } /** From 6c79b05de665277081fcae8e2d9cf825f12c0646 Mon Sep 17 00:00:00 2001 From: Froxz Date: Thu, 9 Mar 2023 12:04:35 +0200 Subject: [PATCH 7/8] Removed trait for custom beta accept header, This api doesn't require a custom beta accept header, so the trait usage can be removed --- lib/Github/Api/Environment.php | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/Github/Api/Environment.php b/lib/Github/Api/Environment.php index b4327823ce4..514c71e0990 100644 --- a/lib/Github/Api/Environment.php +++ b/lib/Github/Api/Environment.php @@ -9,8 +9,6 @@ */ class Environment extends AbstractApi { - use AcceptHeaderTrait; - /** * List environments for a particular repository. * @@ -69,4 +67,20 @@ public function remove(string $username, string $repository, string $name) { return $this->delete('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/environments/'.$name); } + + /** + * @link https://docs.github.com/en/rest/reference/actions#secrets + */ + public function secrets(): Secrets + { + return new Secrets($this->getClient()); + } + + /** + * @link https://docs.github.com/en/rest/reference/actions#secrets + */ + public function variables(): Variables + { + return new Variables($this->getClient()); + } } From 7c97a2c7a4d68a47d6f052e6ec93d73036c60cb2 Mon Sep 17 00:00:00 2001 From: Froxz Date: Thu, 9 Mar 2023 12:08:43 +0200 Subject: [PATCH 8/8] removed unready changes --- lib/Github/Api/Environment.php | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/lib/Github/Api/Environment.php b/lib/Github/Api/Environment.php index 514c71e0990..057d1edf0d8 100644 --- a/lib/Github/Api/Environment.php +++ b/lib/Github/Api/Environment.php @@ -67,20 +67,4 @@ public function remove(string $username, string $repository, string $name) { return $this->delete('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/environments/'.$name); } - - /** - * @link https://docs.github.com/en/rest/reference/actions#secrets - */ - public function secrets(): Secrets - { - return new Secrets($this->getClient()); - } - - /** - * @link https://docs.github.com/en/rest/reference/actions#secrets - */ - public function variables(): Variables - { - return new Variables($this->getClient()); - } }