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..057d1edf0d8 --- /dev/null +++ b/lib/Github/Api/Environment.php @@ -0,0 +1,70 @@ +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/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); diff --git a/test/Github/Tests/Api/EnvironmentTest.php b/test/Github/Tests/Api/EnvironmentTest.php new file mode 100644 index 00000000000..56efbc0afc8 --- /dev/null +++ b/test/Github/Tests/Api/EnvironmentTest.php @@ -0,0 +1,71 @@ +getApiMock(); + + $api->expects($this->once()) + ->method('put') + ->with('/repos/KnpLabs/php-github-api/environments'); + + $api->createOrUpdate('KnpLabs', 'php-github-api', 'production'); + } + + /** + * @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 = 'production'; + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('get') + ->with('/repos/KnpLabs/php-github-api/environments/production') + ->will($this->returnValue($expectedValue)); + + $this->assertEquals($expectedValue, $api->show('KnpLabs', 'php-github-api', 'production')); + } + + /** + * @test + */ + public function shouldDeleteEnvironment() + { + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('delete') + ->with('/repos/KnpLabs/php-github-api/environments/production') + ->will($this->returnValue(null)); + + $this->assertNull($api->remove('KnpLabs', 'php-github-api', 'production')); + } + + /** + * @return string + */ + protected function getApiClass() + { + return \Github\Api\Environment::class; + } +}