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

Deployment branch policies #1108

Merged
merged 7 commits into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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)
* [Policies](repo/policies.md)
* [Environments](repo/environments.md)
* [Labels](repo/labels.md)
* [Protection](repo/protection.md)
Expand Down
8 changes: 4 additions & 4 deletions doc/repo/environments.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ Provides information about environments for a repository. Wraps [GitHub Environm
#### List all environments.

```php
$environments = $client->api('environment')->all('KnpLabs', 'php-github-api');
$environments = $client->deployment()->environment()->all('KnpLabs', 'php-github-api');
```

### Get one environment.

```php
$environment = $client->api('environment')->show('KnpLabs', 'php-github-api', $name);
$environment = $client->deployment()->environment()->show('KnpLabs', 'php-github-api', $name);
```

#### Create or update environment.

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

#### Delete a existing environment.

```php
$environment = $client->api('environment')->remove('KnpLabs', 'php-github-api', $name);
$environment = $client->deployment()->environment()->remove('KnpLabs', 'php-github-api', $name);
```
38 changes: 38 additions & 0 deletions doc/repo/policies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
## Repo / Deployment branch policies API
[Back to the "Repos API"](../repos.md) | [Back to the navigation](../index.md)

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).

#### List deployment branch policies.

```php
$policies = $client->deployment()->policies()->all('KnpLabs', 'php-github-api', 'production');
```

### Get one environment.

```php
$policy = $client->deployment()->policies()->show('KnpLabs', 'php-github-api', 'production', $branchPolicyId);
```

#### Create policy.

```php
$data = $client->deployment()->policies()->create('KnpLabs', 'php-github-api', 'production', [
'name' => 'name'
]);
```

#### Update policy.

```php
$data = $client->deployment()->policies()->update('KnpLabs', 'php-github-api', 'production', $branchPolicyId, [
'name' => 'name'
]);
```

#### Delete a existing policy.

```php
$policy = $client->deployment()->policies()->remove('KnpLabs', 'php-github-api', 'production', $branchPolicyId);
```
18 changes: 18 additions & 0 deletions lib/Github/Api/Deployment.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Github\Api;

use Github\Api\Deployment\Policies;
use Github\Api\Deployment\Environments;
use Github\Exception\MissingArgumentException;

/**
Expand Down Expand Up @@ -130,4 +132,20 @@ public function getStatuses($username, $repository, $id)
{
return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/deployments/'.$id.'/statuses');
}

/**
* @return Environments
*/
public function environments()
{
return new Environments($this->getClient());
}

/**
* @return Policies
*/
public function policies()
{
return new Policies($this->getClient());
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?php

namespace Github\Api;
namespace Github\Api\Deployment;

use Github\Api\AbstractApi;

/**
* Listing, creating and updating environments.
*
* @link https://docs.github.com/en/rest/deployments/environments?apiVersion=2022-11-28#
*/
class Environment extends AbstractApi
class Environments extends AbstractApi
SergkeiM marked this conversation as resolved.
Show resolved Hide resolved
{
/**
* List environments for a particular repository.
Expand Down
108 changes: 108 additions & 0 deletions lib/Github/Api/Deployment/Policies.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

namespace Github\Api\Deployment;

use Github\Api\AbstractApi;
use Github\Exception\MissingArgumentException;

/**
* Listing, creating and updating deployments.
*
* @link https://docs.github.com/en/rest/deployments/branch-policies?apiVersion=2022-11-28#list-deployment-branch-policies
*/
class Policies extends AbstractApi
{
/**
* List deployment branch policies
*
* @link https://docs.github.com/en/rest/deployments/branch-policies?apiVersion=2022-11-28#list-deployment-branch-policies
*
* @param string $username the username of the user who owns the repository
* @param string $repository the name of the repository
* @param string $environment the name of the environment.
* @param array $params query parameters to filter deployments by (see link)
*
* @return array the deployments requested
*/
public function all(string $username, string $repository, string $environment, array $params = [])
{
return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/environments/'.rawurlencode($environment).'/deployment-branch-policies', $params);
}

/**
* Get a deployment branch policy
*
* @link https://docs.github.com/en/rest/deployments/branch-policies?apiVersion=2022-11-28#get-a-deployment-branch-policy
*
* @param string $username the username of the user who owns the repository
* @param string $repository the name of the repository
* @param string $environment the name of the environment.
* @param int $id The unique identifier of the branch policy.
*
* @return array
*/
public function show(string $username, string $repository, string $environment, int $id)
{
return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/environments/'.rawurlencode($environment).'/deployment-branch-policies/'.$id);
}

/**
* Creates a deployment branch policy for an environment.
*
* @link https://docs.github.com/en/rest/deployments/branch-policies?apiVersion=2022-11-28#create-a-deployment-branch-policy
*
* @param string $username the username of the user who owns the repository
* @param string $repository the name of the repository
* @param string $environment the name of the environment.
*
* @throws MissingArgumentException
*
* @return array information about the deployment branch policy
*/
public function create(string $username, string $repository, string $environment, array $params)
{
if (!isset($params['name'])) {
throw new MissingArgumentException(['name']);
}
SergkeiM marked this conversation as resolved.
Show resolved Hide resolved

return $this->post('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/environments/'.rawurlencode($environment).'/deployment-branch-policies', $params);
}

/**
* Updates a deployment branch policy for an environment.
*
* @link https://docs.github.com/en/rest/deployments/branch-policies?apiVersion=2022-11-28#update-a-deployment-branch-policy
*
* @param string $username the username of the user who owns the repository
* @param string $repository the name of the repository
* @param string $environment the name of the environment.
* @param int $id The unique identifier of the branch policy.
*
* @return array information about the deployment branch policy
*/
public function update(string $username, string $repository, string $environment, int $id, array $params)
{
if (!isset($params['name'])) {
throw new MissingArgumentException(['name']);
}
SergkeiM marked this conversation as resolved.
Show resolved Hide resolved

return $this->put('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/environments/'.rawurlencode($environment).'/deployment-branch-policies/'.$id, $params);
}

/**
* Delete a deployment branch policy
*
* @link https://docs.github.com/en/rest/deployments/branch-policies?apiVersion=2022-11-28#delete-a-deployment-branch-policy
*
* @param string $username the username of the user who owns the repository
* @param string $repository the name of the repository
* @param string $environment the name of the environment.
* @param int $id The unique identifier of the branch policy.
*
* @return mixed null on success, array on error with 'message'
*/
public function remove(string $username, string $repository, string $environment, int $id)
{
return $this->delete('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/environments/'.rawurlencode($environment).'/deployment-branch-policies/'.$id);
}
}
5 changes: 0 additions & 5 deletions lib/Github/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,6 @@ 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
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

namespace Github\Tests\Api;
namespace Github\Tests\Api\Deployment;

use Github\Tests\Api\TestCase;

class EnvironmentTest extends TestCase
{
Expand Down Expand Up @@ -66,6 +68,6 @@ public function shouldDeleteEnvironment()
*/
protected function getApiClass()
{
return \Github\Api\Environment::class;
return \Github\Api\Deployment\Environments::class;
}
}
91 changes: 91 additions & 0 deletions test/Github/Tests/Api/Deployment/PoliciesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace Github\Tests\Api\Deployment;

use Github\Tests\Api\TestCase;

class PoliciesTest extends TestCase
{
/**
* @test
*/
public function shouldCreatePolicy()
{
$api = $this->getApiMock();

$api->expects($this->once())
->method('post')
->with('/repos/KnpLabs/php-github-api/environments/production/deployment-branch-policies');

$api->create('KnpLabs', 'php-github-api', 'production', [
'name' => 'name'
]);
}

/**
* @test
*/
public function shouldUpdatePolicy()
{
$api = $this->getApiMock();

$api->expects($this->once())
->method('put')
->with('/repos/KnpLabs/php-github-api/environments/production/deployment-branch-policies/1');

$api->update('KnpLabs', 'php-github-api', 'production', 1, [
'name' => 'name'
]);
}

/**
* @test
*/
public function shouldGetAllPolicies()
{
$api = $this->getApiMock();
$api->expects($this->once())
->method('get')
->with('/repos/KnpLabs/php-github-api/environments/production/deployment-branch-policies');

$api->all('KnpLabs', 'php-github-api', 'production');
}

/**
* @test
*/
public function shouldShowPolicy()
{
$expectedValue = 'production';

$api = $this->getApiMock();
$api->expects($this->once())
->method('get')
->with('/repos/KnpLabs/php-github-api/environments/production/deployment-branch-policies/1')
->will($this->returnValue($expectedValue));

$this->assertEquals($expectedValue, $api->show('KnpLabs', 'php-github-api', 'production', 1));
}

/**
* @test
*/
public function shouldDeletePolicy()
{
$api = $this->getApiMock();
$api->expects($this->once())
->method('delete')
->with('/repos/KnpLabs/php-github-api/environments/production/deployment-branch-policies/1')
->will($this->returnValue(null));

$this->assertNull($api->remove('KnpLabs', 'php-github-api', 'production', 1));
}

/**
* @return string
*/
protected function getApiClass()
{
return \Github\Api\Deployment\Policies::class;
}
}
20 changes: 20 additions & 0 deletions test/Github/Tests/Api/DeploymentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,26 @@ public function shouldGetAllStatuses()
$api->getStatuses('KnpLabs', 'php-github-api', 1);
}

/**
* @test
*/
public function shouldGetEnvironmentsApiObject()
{
$api = $this->getApiMock();

$this->assertInstanceOf(\Github\Api\Deployment\Environments::class, $api->environments());
}

/**
* @test
*/
public function shouldGetPoliciesApiObject()
{
$api = $this->getApiMock();

$this->assertInstanceOf(\Github\Api\Deployment\Policies::class, $api->policies());
}

/**
* @return string
*/
Expand Down