Skip to content

Commit 75c7930

Browse files
committed
Added environments
1 parent 31cd5b1 commit 75c7930

File tree

4 files changed

+174
-0
lines changed

4 files changed

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

0 commit comments

Comments
 (0)