Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added environments #1103

Merged
merged 8 commits into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
28 changes: 28 additions & 0 deletions doc/repo/environments.md
Original file line number Diff line number Diff line change
@@ -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);
```
70 changes: 70 additions & 0 deletions lib/Github/Api/Environment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Github\Api;

/**
* Listing, creating and updating environments.
*
* @link https://docs.github.com/en/rest/deployments/environments?apiVersion=2022-11-28#
*/
class Environment extends AbstractApi
{
/**
* List environments for a particular repository.
*
* @link https://docs.github.com/en/rest/deployments/environments?apiVersion=2022-11-28##list-environments
*
* @param string $username the username of the user who owns the repository
* @param string $repository the name of the repository
* @param array $params query parameters to filter environments by (see link)
*
* @return array the environments requested
*/
public function all($username, $repository, array $params = [])
{
return $this->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);
}
}
5 changes: 5 additions & 0 deletions lib/Github/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
71 changes: 71 additions & 0 deletions test/Github/Tests/Api/EnvironmentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Github\Tests\Api;

class EnvironmentTest extends TestCase
{
/**
* @test
*/
public function shouldCreateOrUpdateEnvironment()
{
$api = $this->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;
}
}