Skip to content

Commit 9734d23

Browse files
committed
Deployment branch policies
* added Deployment branch policies * Moved environments under deployment
1 parent fcc99dd commit 9734d23

File tree

10 files changed

+288
-13
lines changed

10 files changed

+288
-13
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+
* [Policies](repo/policies.md)
6263
* [Environments](repo/environments.md)
6364
* [Labels](repo/labels.md)
6465
* [Protection](repo/protection.md)

doc/repo/environments.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,23 @@ Provides information about environments for a repository. Wraps [GitHub Environm
66
#### List all environments.
77

88
```php
9-
$environments = $client->api('environment')->all('KnpLabs', 'php-github-api');
9+
$environments = $client->deployment()->environment()->all('KnpLabs', 'php-github-api');
1010
```
1111

1212
### Get one environment.
1313

1414
```php
15-
$environment = $client->api('environment')->show('KnpLabs', 'php-github-api', $name);
15+
$environment = $client->deployment()->environment()->show('KnpLabs', 'php-github-api', $name);
1616
```
1717

1818
#### Create or update environment.
1919

2020
```php
21-
$data = $client->api('environment')->createOrUpdate('KnpLabs', 'php-github-api', $name);
21+
$data = $client->deployment()->environment()->createOrUpdate('KnpLabs', 'php-github-api', $name);
2222
```
2323

2424
#### Delete a existing environment.
2525

2626
```php
27-
$environment = $client->api('environment')->remove('KnpLabs', 'php-github-api', $name);
27+
$environment = $client->deployment()->environment()->remove('KnpLabs', 'php-github-api', $name);
2828
```

doc/repo/policies.md

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

lib/Github/Api/Deployment.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Github\Api;
44

5+
use Github\Api\Deployment\Policies;
6+
use Github\Api\Deployment\Environments;
57
use Github\Exception\MissingArgumentException;
68

79
/**
@@ -130,4 +132,20 @@ public function getStatuses($username, $repository, $id)
130132
{
131133
return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/deployments/'.$id.'/statuses');
132134
}
135+
136+
/**
137+
* @return Environments
138+
*/
139+
public function environments()
140+
{
141+
return new Environments($this->getClient());
142+
}
143+
144+
/**
145+
* @return Policies
146+
*/
147+
public function policies()
148+
{
149+
return new Policies($this->getClient());
150+
}
133151
}

lib/Github/Api/Environment.php renamed to lib/Github/Api/Deployment/Environments.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
<?php
22

3-
namespace Github\Api;
3+
namespace Github\Api\Deployment;
4+
5+
use Github\Api\AbstractApi;
46

57
/**
68
* Listing, creating and updating environments.
79
*
810
* @link https://docs.github.com/en/rest/deployments/environments?apiVersion=2022-11-28#
911
*/
10-
class Environment extends AbstractApi
12+
class Environments extends AbstractApi
1113
{
1214
/**
1315
* List environments for a particular repository.
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
namespace Github\Api\Deployment;
4+
5+
use Github\Api\AbstractApi;
6+
use Github\Exception\MissingArgumentException;
7+
8+
/**
9+
* Listing, creating and updating deployments.
10+
*
11+
* @link https://docs.github.com/en/rest/deployments/branch-policies?apiVersion=2022-11-28#list-deployment-branch-policies
12+
*/
13+
class Policies extends AbstractApi
14+
{
15+
/**
16+
* List deployment branch policies
17+
*
18+
* @link https://docs.github.com/en/rest/deployments/branch-policies?apiVersion=2022-11-28#list-deployment-branch-policies
19+
*
20+
* @param string $username the username of the user who owns the repository
21+
* @param string $repository the name of the repository
22+
* @param string $environment the name of the environment.
23+
* @param array $params query parameters to filter deployments by (see link)
24+
*
25+
* @return array the deployments requested
26+
*/
27+
public function all(string $username, string $repository, string $environment, array $params = [])
28+
{
29+
return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/environments/'.rawurlencode($environment).'/deployment-branch-policies', $params);
30+
}
31+
32+
/**
33+
* Get a deployment branch policy
34+
*
35+
* @link https://docs.github.com/en/rest/deployments/branch-policies?apiVersion=2022-11-28#get-a-deployment-branch-policy
36+
*
37+
* @param string $username the username of the user who owns the repository
38+
* @param string $repository the name of the repository
39+
* @param string $environment the name of the environment.
40+
* @param int $id The unique identifier of the branch policy.
41+
*
42+
* @return array
43+
*/
44+
public function show(string $username, string $repository, string $environment, int $id)
45+
{
46+
return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/environments/'.rawurlencode($environment).'/deployment-branch-policies/'.$id);
47+
}
48+
49+
/**
50+
* Creates a deployment branch policy for an environment.
51+
*
52+
* @link https://docs.github.com/en/rest/deployments/branch-policies?apiVersion=2022-11-28#create-a-deployment-branch-policy
53+
*
54+
* @param string $username the username of the user who owns the repository
55+
* @param string $repository the name of the repository
56+
* @param string $environment the name of the environment.
57+
*
58+
* @throws MissingArgumentException
59+
*
60+
* @return array information about the deployment branch policy
61+
*/
62+
public function create(string $username, string $repository, string $environment, array $params)
63+
{
64+
if (!isset($params['name'])) {
65+
throw new MissingArgumentException(['name']);
66+
}
67+
68+
return $this->post('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/environments/'.rawurlencode($environment).'/deployment-branch-policies', $params);
69+
}
70+
71+
/**
72+
* Updates a deployment branch policy for an environment.
73+
*
74+
* @link https://docs.github.com/en/rest/deployments/branch-policies?apiVersion=2022-11-28#update-a-deployment-branch-policy
75+
*
76+
* @param string $username the username of the user who owns the repository
77+
* @param string $repository the name of the repository
78+
* @param string $environment the name of the environment.
79+
* @param int $id The unique identifier of the branch policy.
80+
*
81+
* @return array information about the deployment branch policy
82+
*/
83+
public function update(string $username, string $repository, string $environment, int $id, array $params)
84+
{
85+
if (!isset($params['name'])) {
86+
throw new MissingArgumentException(['name']);
87+
}
88+
89+
return $this->put('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/environments/'.rawurlencode($environment).'/deployment-branch-policies/'.$id, $params);
90+
}
91+
92+
/**
93+
* Delete a deployment branch policy
94+
*
95+
* @link https://docs.github.com/en/rest/deployments/branch-policies?apiVersion=2022-11-28#delete-a-deployment-branch-policy
96+
*
97+
* @param string $username the username of the user who owns the repository
98+
* @param string $repository the name of the repository
99+
* @param string $environment the name of the environment.
100+
* @param int $id The unique identifier of the branch policy.
101+
*
102+
* @return mixed null on success, array on error with 'message'
103+
*/
104+
public function remove(string $username, string $repository, string $environment, int $id)
105+
{
106+
return $this->delete('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/environments/'.rawurlencode($environment).'/deployment-branch-policies/'.$id);
107+
}
108+
}

lib/Github/Client.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,6 @@ 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-
189184
case 'ent':
190185
case 'enterprise':
191186
$api = new Api\Enterprise($this);

test/Github/Tests/Api/EnvironmentTest.php renamed to test/Github/Tests/Api/Deployment/EnvironmentsTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22

3-
namespace Github\Tests\Api;
3+
namespace Github\Tests\Api\Deployment;
4+
5+
use Github\Tests\Api\TestCase;
46

57
class EnvironmentTest extends TestCase
68
{
@@ -66,6 +68,6 @@ public function shouldDeleteEnvironment()
6668
*/
6769
protected function getApiClass()
6870
{
69-
return \Github\Api\Environment::class;
71+
return \Github\Api\Deployment\Environments::class;
7072
}
7173
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
namespace Github\Tests\Api\Deployment;
4+
5+
use Github\Tests\Api\TestCase;
6+
7+
class PoliciesTest extends TestCase
8+
{
9+
/**
10+
* @test
11+
*/
12+
public function shouldCreatePolicy()
13+
{
14+
$api = $this->getApiMock();
15+
16+
$api->expects($this->once())
17+
->method('post')
18+
->with('/repos/KnpLabs/php-github-api/environments/production/deployment-branch-policies');
19+
20+
$api->create('KnpLabs', 'php-github-api', 'production', [
21+
'name' => 'name'
22+
]);
23+
}
24+
25+
/**
26+
* @test
27+
*/
28+
public function shouldUpdatePolicy()
29+
{
30+
$api = $this->getApiMock();
31+
32+
$api->expects($this->once())
33+
->method('put')
34+
->with('/repos/KnpLabs/php-github-api/environments/production/deployment-branch-policies/1');
35+
36+
$api->update('KnpLabs', 'php-github-api', 'production', 1, [
37+
'name' => 'name'
38+
]);
39+
}
40+
41+
/**
42+
* @test
43+
*/
44+
public function shouldGetAllPolicies()
45+
{
46+
$api = $this->getApiMock();
47+
$api->expects($this->once())
48+
->method('get')
49+
->with('/repos/KnpLabs/php-github-api/environments/production/deployment-branch-policies');
50+
51+
$api->all('KnpLabs', 'php-github-api', 'production');
52+
}
53+
54+
/**
55+
* @test
56+
*/
57+
public function shouldShowPolicy()
58+
{
59+
$expectedValue = 'production';
60+
61+
$api = $this->getApiMock();
62+
$api->expects($this->once())
63+
->method('get')
64+
->with('/repos/KnpLabs/php-github-api/environments/production/deployment-branch-policies/1')
65+
->will($this->returnValue($expectedValue));
66+
67+
$this->assertEquals($expectedValue, $api->show('KnpLabs', 'php-github-api', 'production', 1));
68+
}
69+
70+
/**
71+
* @test
72+
*/
73+
public function shouldDeletePolicy()
74+
{
75+
$api = $this->getApiMock();
76+
$api->expects($this->once())
77+
->method('delete')
78+
->with('/repos/KnpLabs/php-github-api/environments/production/deployment-branch-policies/1')
79+
->will($this->returnValue(null));
80+
81+
$this->assertNull($api->remove('KnpLabs', 'php-github-api', 'production', 1));
82+
}
83+
84+
/**
85+
* @return string
86+
*/
87+
protected function getApiClass()
88+
{
89+
return \Github\Api\Deployment\Policies::class;
90+
}
91+
}

test/Github/Tests/Api/DeploymentTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,26 @@ public function shouldGetAllStatuses()
118118
$api->getStatuses('KnpLabs', 'php-github-api', 1);
119119
}
120120

121+
/**
122+
* @test
123+
*/
124+
public function shouldGetEnvironmentsApiObject()
125+
{
126+
$api = $this->getApiMock();
127+
128+
$this->assertInstanceOf(\Github\Api\Deployment\Environments::class, $api->environments());
129+
}
130+
131+
/**
132+
* @test
133+
*/
134+
public function shouldGetPoliciesApiObject()
135+
{
136+
$api = $this->getApiMock();
137+
138+
$this->assertInstanceOf(\Github\Api\Deployment\Policies::class, $api->policies());
139+
}
140+
121141
/**
122142
* @return string
123143
*/

0 commit comments

Comments
 (0)