Skip to content

Commit d561376

Browse files
committed
Merge pull request #174 from ericduran/deploymentapi
Adding Deployment API - For Review
2 parents 3e777b4 + 5efad64 commit d561376

File tree

5 files changed

+223
-0
lines changed

5 files changed

+223
-0
lines changed

doc/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ APIs:
1818
* [Contents](repo/contents.md)
1919
* [Releases](repo/releases.md)
2020
* [Assets](repo/assets.md)
21+
* [Deployments](repo/deployments.md)
2122
* [Users](users.md)
2223
* [Meta](meta.md)
2324
* [Activity](activity.md)

doc/repo/deployments.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
## Repo / Deployments API
2+
[Back to the "Repos API"](../repos.md) | [Back to the navigation](../index.md)
3+
4+
Provides information about deployments for a repository. Wraps [GitHub Deployments API](https://developer.github.com/v3/repos/deployments/).
5+
6+
#### List all deployments.
7+
8+
```php
9+
$deployments = $client->api('deployment')->all('KnpLabs', 'php-github-api');
10+
```
11+
12+
You can also filter the returned results (see [the documentation](https://developer.github.com/v3/repos/deployments/#list-deployments) for more information):
13+
14+
```php
15+
$deployments = $client->api('deployment')->all('KnpLabs', 'php-github-api', array('environment' => 'production'));
16+
```
17+
18+
#### Create a new deployments.
19+
20+
The `ref` parameter is required.
21+
22+
```php
23+
$data = $client->api('deployment')->create('KnpLabs', 'php-github-api', array('ref' => 'fd6a5f9e5a430dddae8d6a8ea378f913d3a766f9'));
24+
```
25+
26+
Please note that once a deployment is created it cannot be edited. Only status updates can be created.
27+
28+
#### Create a new status update.
29+
30+
The `state` parameter is required. At the time of writing, this must be pending, success, error, or failure.
31+
32+
```php
33+
$data = $client->api('deployment')->updateStatus('KnpLabs', 'php-github-api', 1, array('state' => 'error', 'description' => 'syntax error'));
34+
```
35+
36+
#### Get all status updates for a deployment.
37+
38+
```php
39+
$statusUpdates = $client->api('deployment')->getStatuses('KnpLabs', 'php-github-api', 1);
40+
```

lib/Github/Api/Deployment.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
namespace Github\Api;
4+
5+
use Github\Exception\MissingArgumentException;
6+
7+
/**
8+
* Listing, creating and updating deployments.
9+
*
10+
* @link https://developer.github.com/v3/repos/deployments/
11+
*/
12+
class Deployment extends AbstractApi
13+
{
14+
/**
15+
* List deployments for a particular repository
16+
* @link https://developer.github.com/v3/repos/deployments/#list-deployments
17+
*
18+
* @param string $username the username of the user who owns the repository
19+
* @param string $repository the name of the repository
20+
* @param array $params query parameters to filter deployments by (see link)
21+
* @return array the deployments requested
22+
*/
23+
public function all($username, $repository, array $params = array())
24+
{
25+
return $this->get('repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/deployments', $params);
26+
}
27+
28+
/**
29+
* Create a new deployment for the given username and repo.
30+
* @link https://developer.github.com/v3/repos/deployments/#create-a-deployment
31+
*
32+
* Important: Once a deployment is created, it cannot be updated. Changes are indicated by creating new statuses.
33+
* @see updateStatus
34+
*
35+
* @param string $username the username
36+
* @param string $repository the repository
37+
* @param array $params the new deployment data
38+
* @return array information about the deployment
39+
*
40+
* @throws MissingArgumentException
41+
*/
42+
public function create($username, $repository, array $params)
43+
{
44+
if (!isset($params['ref'])) {
45+
throw new MissingArgumentException(array('ref'));
46+
}
47+
48+
return $this->post('repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/deployments', $params);
49+
}
50+
51+
/**
52+
* Updates a deployment by creating a new status update.
53+
* @link https://developer.github.com/v3/repos/deployments/#create-a-deployment-status
54+
*
55+
* @param string $username the username
56+
* @param string $repository the repository
57+
* @param string $id the deployment number
58+
* @param array $params The information about the deployment update.
59+
* Must include a "state" field of pending, success, error, or failure.
60+
* May also be given a target_url and description, ßee link for more details.
61+
* @return array information about the deployment
62+
*
63+
* @throws MissingArgumentException
64+
*/
65+
public function updateStatus($username, $repository, $id, array $params)
66+
{
67+
if (!isset($params['state'])) {
68+
throw new MissingArgumentException(array('state'));
69+
}
70+
return $this->post('repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/deployments/'.rawurlencode($id).'/statuses', $params);
71+
}
72+
73+
/**
74+
* Gets all of the status updates tied to a given deployment.
75+
*
76+
* @param string $username the username
77+
* @param string $repository the repository
78+
* @param int $id the deployment identifier
79+
* @return array the deployment statuses
80+
*/
81+
public function getStatuses($username, $repository, $id) {
82+
return $this->get('repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/deployments/'.rawurlencode($id).'/statuses');
83+
}
84+
}

lib/Github/Client.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ public function api($name)
120120
$api = new Api\CurrentUser($this);
121121
break;
122122

123+
case 'deployment':
124+
case 'deployments':
125+
$api = new Api\Deployment($this);
126+
break;
127+
123128
case 'ent':
124129
case 'enterprise':
125130
$api = new Api\Enterprise($this);
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
namespace Github\Tests\Api;
4+
5+
class DeploymentTest extends TestCase
6+
{
7+
/**
8+
* @test
9+
*/
10+
public function shouldCreateDeployment()
11+
{
12+
$api = $this->getApiMock();
13+
$deploymentData = array('ref' => 'fd6a5f9e5a430dddae8d6a8ea378f913d3a766f9');
14+
$api->expects($this->once())
15+
->method('post')
16+
->with('repos/KnpLabs/php-github-api/deployments', $deploymentData);
17+
18+
$api->create('KnpLabs', 'php-github-api', $deploymentData);
19+
}
20+
21+
/**
22+
* @test
23+
*/
24+
public function shouldGetAllDeployments()
25+
{
26+
$api = $this->getApiMock();
27+
$api->expects($this->once())
28+
->method('get')
29+
->with('repos/KnpLabs/php-github-api/deployments');
30+
31+
$api->all('KnpLabs', 'php-github-api');
32+
}
33+
34+
/**
35+
* @test
36+
*/
37+
public function shouldGetAllDeploymentsWithFilterParameters()
38+
{
39+
$api = $this->getApiMock();
40+
$filterData = array('foo' => 'bar', 'bar' => 'foo');
41+
42+
$api->expects($this->once())
43+
->method('get')
44+
->with('repos/KnpLabs/php-github-api/deployments', $filterData);
45+
46+
$api->all('KnpLabs', 'php-github-api', $filterData);
47+
}
48+
49+
/**
50+
* @test
51+
*/
52+
public function shouldCreateStatusUpdate()
53+
{
54+
$api = $this->getApiMock();
55+
$statusData = array('state' => 'pending', 'description' => 'waiting to start');
56+
57+
$api->expects($this->once())
58+
->method('post')
59+
->with('repos/KnpLabs/php-github-api/deployments/1/statuses', $statusData);
60+
61+
$api->updateStatus('KnpLabs', 'php-github-api', 1, $statusData);
62+
}
63+
64+
/**
65+
* @test
66+
* @expectedException GitHub\Exception\MissingArgumentException
67+
*/
68+
public function shouldRejectStatusUpdateWithoutStateField()
69+
{
70+
$api = $this->getApiMock();
71+
$statusData = array('description' => 'waiting to start');
72+
73+
$api->updateStatus('KnpLabs', 'php-github-api', 1, $statusData);
74+
}
75+
76+
/**
77+
* @test
78+
*/
79+
public function shouldGetAllStatuses()
80+
{
81+
$api = $this->getApiMock();
82+
$api->expects($this->once())
83+
->method('get')
84+
->with('repos/KnpLabs/php-github-api/deployments/1/statuses');
85+
86+
$api->getStatuses('KnpLabs', 'php-github-api', 1);
87+
}
88+
89+
protected function getApiClass()
90+
{
91+
return 'Github\Api\Deployment';
92+
}
93+
}

0 commit comments

Comments
 (0)