Skip to content

Commit 32f79ea

Browse files
authored
feature #1103 Added environments (Froxz)
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
1 parent 31cd5b1 commit 32f79ea

File tree

5 files changed

+175
-0
lines changed

5 files changed

+175
-0
lines changed

doc/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ v3 APIs:
5959
* [Check Suites](repo/check_suites.md)
6060
* [Contents](repo/contents.md)
6161
* [Deployments](repo/deployments.md)
62+
* [Environments](repo/environments.md)
6263
* [Labels](repo/labels.md)
6364
* [Protection](repo/protection.md)
6465
* [Releases](repo/releases.md)

doc/repo/environments.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
## Repo / Environments API
2+
[Back to the "Repos API"](../repos.md) | [Back to the navigation](../index.md)
3+
4+
Provides information about environments for a repository. Wraps [GitHub Environments API](https://docs.github.com/en/rest/deployments/environments?apiVersion=2022-11-28).
5+
6+
#### List all environments.
7+
8+
```php
9+
$environments = $client->api('environment')->all('KnpLabs', 'php-github-api');
10+
```
11+
12+
### Get one environment.
13+
14+
```php
15+
$environment = $client->api('environment')->show('KnpLabs', 'php-github-api', $name);
16+
```
17+
18+
#### Create or update environment.
19+
20+
```php
21+
$data = $client->api('environment')->createOrUpdate('KnpLabs', 'php-github-api', $name);
22+
```
23+
24+
#### Delete a existing environment.
25+
26+
```php
27+
$environment = $client->api('environment')->remove('KnpLabs', 'php-github-api', $name);
28+
```

lib/Github/Api/Environment.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace Github\Api;
4+
5+
/**
6+
* Listing, creating and updating environments.
7+
*
8+
* @link https://docs.github.com/en/rest/deployments/environments?apiVersion=2022-11-28#
9+
*/
10+
class Environment extends AbstractApi
11+
{
12+
/**
13+
* List environments for a particular repository.
14+
*
15+
* @link https://docs.github.com/en/rest/deployments/environments?apiVersion=2022-11-28##list-environments
16+
*
17+
* @param string $username the username of the user who owns the repository
18+
* @param string $repository the name of the repository
19+
* @param array $params query parameters to filter environments by (see link)
20+
*
21+
* @return array the environments requested
22+
*/
23+
public function all($username, $repository, array $params = [])
24+
{
25+
return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/environments', $params);
26+
}
27+
28+
/**
29+
* Get a environment in selected repository.
30+
*
31+
* @param string $username the user who owns the repo
32+
* @param string $repository the name of the repo
33+
* @param string $name the name of the environment
34+
*
35+
* @return array
36+
*/
37+
public function show($username, $repository, $name)
38+
{
39+
return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/environments/'.$name);
40+
}
41+
42+
/**
43+
* Create or update a environment for the given username and repo.
44+
*
45+
* @link https://docs.github.com/en/rest/deployments/environments?apiVersion=2022-11-28#create-or-update-an-environment
46+
*
47+
* @param string $username the username
48+
* @param string $repository the repository
49+
* @param string $name the name of the environment
50+
* @param array $params the new environment data
51+
*
52+
* @return array information about the environment
53+
*/
54+
public function createOrUpdate($username, $repository, $name, array $params = [])
55+
{
56+
return $this->put('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/environments', $params);
57+
}
58+
59+
/**
60+
* Delete a environment for the given username and repo.
61+
*
62+
* @link https://docs.github.com/en/rest/deployments/environments?apiVersion=2022-11-28#delete-an-environment
63+
*
64+
* @return mixed null on success, array on error with 'message'
65+
*/
66+
public function remove(string $username, string $repository, string $name)
67+
{
68+
return $this->delete('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/environments/'.$name);
69+
}
70+
}

lib/Github/Client.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,11 @@ public function api($name): AbstractApi
181181
$api = new Api\Deployment($this);
182182
break;
183183

184+
case 'environment':
185+
case 'environments':
186+
$api = new Api\Environment($this);
187+
break;
188+
184189
case 'ent':
185190
case 'enterprise':
186191
$api = new Api\Enterprise($this);
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace Github\Tests\Api;
4+
5+
class EnvironmentTest extends TestCase
6+
{
7+
/**
8+
* @test
9+
*/
10+
public function shouldCreateOrUpdateEnvironment()
11+
{
12+
$api = $this->getApiMock();
13+
14+
$api->expects($this->once())
15+
->method('put')
16+
->with('/repos/KnpLabs/php-github-api/environments');
17+
18+
$api->createOrUpdate('KnpLabs', 'php-github-api', 'production');
19+
}
20+
21+
/**
22+
* @test
23+
*/
24+
public function shouldGetAllEnvironments()
25+
{
26+
$api = $this->getApiMock();
27+
$api->expects($this->once())
28+
->method('get')
29+
->with('/repos/KnpLabs/php-github-api/environments');
30+
31+
$api->all('KnpLabs', 'php-github-api');
32+
}
33+
34+
/**
35+
* @test
36+
*/
37+
public function shouldShowEnvironment()
38+
{
39+
$expectedValue = 'production';
40+
41+
$api = $this->getApiMock();
42+
$api->expects($this->once())
43+
->method('get')
44+
->with('/repos/KnpLabs/php-github-api/environments/production')
45+
->will($this->returnValue($expectedValue));
46+
47+
$this->assertEquals($expectedValue, $api->show('KnpLabs', 'php-github-api', 'production'));
48+
}
49+
50+
/**
51+
* @test
52+
*/
53+
public function shouldDeleteEnvironment()
54+
{
55+
$api = $this->getApiMock();
56+
$api->expects($this->once())
57+
->method('delete')
58+
->with('/repos/KnpLabs/php-github-api/environments/production')
59+
->will($this->returnValue(null));
60+
61+
$this->assertNull($api->remove('KnpLabs', 'php-github-api', 'production'));
62+
}
63+
64+
/**
65+
* @return string
66+
*/
67+
protected function getApiClass()
68+
{
69+
return \Github\Api\Environment::class;
70+
}
71+
}

0 commit comments

Comments
 (0)