Skip to content

Commit

Permalink
feature #1103 Added environments (Froxz)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the 3.9.x-dev branch.

Discussion
----------

Added support for environments #1102

Commits
-------

75c7930 Added environments
329c72f environment
b163de2 fixes
e9da726 fixes
536ac08 Fixed Tests
48da337 Fixed tests
6c79b05 Removed trait for custom beta accept header,
7c97a2c removed unready changes
  • Loading branch information
SergkeiM authored Mar 9, 2023
1 parent 31cd5b1 commit 32f79ea
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 0 deletions.
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;
}
}

0 comments on commit 32f79ea

Please sign in to comment.