From 1f066866a032451655cccacd1f510e4071d57696 Mon Sep 17 00:00:00 2001 From: "Eric J. Duran" Date: Wed, 13 Aug 2014 02:15:59 -0400 Subject: [PATCH 1/8] Adding basic deployment api support --- lib/Github/Api/Deployment.php | 53 +++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 lib/Github/Api/Deployment.php diff --git a/lib/Github/Api/Deployment.php b/lib/Github/Api/Deployment.php new file mode 100644 index 00000000000..6fbfff4041f --- /dev/null +++ b/lib/Github/Api/Deployment.php @@ -0,0 +1,53 @@ +get('repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/deployments', array(), + ['Accept' => 'application/vnd.github.cannonball-preview+json'] + ); + } + + + + /** + * Create a new deployment for the given username and repo. + * + * @param string $username the username + * @param string $repository the repository + * @param array $params the new deployment data + * @return array information about the deployment + * + * @throws MissingArgumentException + */ + public function create($username, $repository, array $params) + { + if (!isset($params['ref'])) { + throw new MissingArgumentException(array('ref')); + } + + return $this->post('repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/deployments', $params, ['Accept' => 'application/vnd.github.cannonball-preview+json']); + } + + /** + * Update deployment information's by username, repo and deployment number. Requires authentication. + * + * @param string $username the username + * @param string $repository the repository + * @param string $id the deployment number + * @return array information about the deployment + */ + public function update($username, $repository, $id, array $params) + { + if (!isset($params['state'])) { + throw new MissingArgumentException(array('state')); + } + return $this->post('repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/deployments/'.rawurlencode($id).'/statuses', $params, ['Accept' => 'application/vnd.github.cannonball-preview+json']); + } +} From 8d48ea5a3ec056f7e6478dec59a372d9502866b5 Mon Sep 17 00:00:00 2001 From: "Eric J. Duran" Date: Wed, 13 Aug 2014 19:14:35 -0400 Subject: [PATCH 2/8] Update Client.php --- lib/Github/Client.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/Github/Client.php b/lib/Github/Client.php index e63af775507..964481aad58 100644 --- a/lib/Github/Client.php +++ b/lib/Github/Client.php @@ -120,6 +120,11 @@ public function api($name) $api = new Api\CurrentUser($this); break; + case 'deployment': + case 'deployments': + $api = new Api\Deployment($this); + break; + case 'ent': case 'enterprise': $api = new Api\Enterprise($this); From 7c74ebe4a823e6b12ba2a28492f6f5316de6b4cc Mon Sep 17 00:00:00 2001 From: "Eric J. Duran" Date: Tue, 27 Jan 2015 17:44:05 -0500 Subject: [PATCH 3/8] updating to fix latest comment. --- lib/Github/Api/Deployment.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Github/Api/Deployment.php b/lib/Github/Api/Deployment.php index 6fbfff4041f..51343a88585 100644 --- a/lib/Github/Api/Deployment.php +++ b/lib/Github/Api/Deployment.php @@ -10,7 +10,7 @@ class Deployment extends AbstractApi public function all($username, $repository, array $params = array()) { return $this->get('repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/deployments', array(), - ['Accept' => 'application/vnd.github.cannonball-preview+json'] + array('Accept' => 'application/vnd.github.cannonball-preview+json') ); } From 88c83378ba167074c052394ed8cbf021cdeab8aa Mon Sep 17 00:00:00 2001 From: lol768 Date: Fri, 20 Feb 2015 20:35:59 +0000 Subject: [PATCH 4/8] No need for a special header on deployment API requests --- lib/Github/Api/Deployment.php | 55 ++++++++++++++++++------ test/Github/Tests/Api/DeploymentTest.php | 26 +++++++++++ 2 files changed, 69 insertions(+), 12 deletions(-) create mode 100644 test/Github/Tests/Api/DeploymentTest.php diff --git a/lib/Github/Api/Deployment.php b/lib/Github/Api/Deployment.php index 51343a88585..f8f3dbac953 100644 --- a/lib/Github/Api/Deployment.php +++ b/lib/Github/Api/Deployment.php @@ -4,20 +4,33 @@ use Github\Exception\MissingArgumentException; +/** + * Listing, creating and updating deployments. + * + * @link https://developer.github.com/v3/repos/deployments/ + */ class Deployment extends AbstractApi { - + /** + * List deployments for a particular repository + * @link https://developer.github.com/v3/repos/deployments/#list-deployments + * + * @param string $username the username of the user who owns the repository + * @param string $repository the name of the repository + * @param array $params query parameters to filter deployments by (see link) + * @return array the deployments requested + */ public function all($username, $repository, array $params = array()) { - return $this->get('repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/deployments', array(), - array('Accept' => 'application/vnd.github.cannonball-preview+json') - ); + return $this->get('repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/deployments', $params); } - - /** * Create a new deployment for the given username and repo. + * @link https://developer.github.com/v3/repos/deployments/#create-a-deployment + * + * Important: Once a deployment is created, it cannot be updated. Changes are indicated by creating new statuses. + * @see updateStatus * * @param string $username the username * @param string $repository the repository @@ -32,22 +45,40 @@ public function create($username, $repository, array $params) throw new MissingArgumentException(array('ref')); } - return $this->post('repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/deployments', $params, ['Accept' => 'application/vnd.github.cannonball-preview+json']); + return $this->post('repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/deployments', $params); } /** - * Update deployment information's by username, repo and deployment number. Requires authentication. + * Updates a deployment by creating a new status update. + * @link https://developer.github.com/v3/repos/deployments/#create-a-deployment-status * - * @param string $username the username + * @param string $username the username * @param string $repository the repository - * @param string $id the deployment number + * @param string $id the deployment number + * @param array $params The information about the deployment update. + * Must include a "state" field of pending, success, error, or failure. + * May also be given a target_url and description, ßee link for more details. * @return array information about the deployment + * + * @throws MissingArgumentException */ - public function update($username, $repository, $id, array $params) + public function updateStatus($username, $repository, $id, array $params) { if (!isset($params['state'])) { throw new MissingArgumentException(array('state')); } - return $this->post('repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/deployments/'.rawurlencode($id).'/statuses', $params, ['Accept' => 'application/vnd.github.cannonball-preview+json']); + return $this->post('repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/deployments/'.rawurlencode($id).'/statuses', $params); + } + + /** + * Gets all of the status updates tied to a given deployment. + * + * @param string $username the username + * @param string $repository the repository + * @param int $id the deployment identifier + * @return array the deployment statuses + */ + public function getStatuses($username, $repository, $id) { + return $this->get('repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/deployments/'.rawurlencode($id).'/statuses'); } } diff --git a/test/Github/Tests/Api/DeploymentTest.php b/test/Github/Tests/Api/DeploymentTest.php new file mode 100644 index 00000000000..a9c15c3bb71 --- /dev/null +++ b/test/Github/Tests/Api/DeploymentTest.php @@ -0,0 +1,26 @@ +getApiMock(); + $deploymentData = array("ref" => "fd6a5f9e5a430dddae8d6a8ea378f913d3a766f9"); + $api->expects($this->once()) + ->method('post') + ->with('/repos/KnpLabs/php-github-api/deployments', $deploymentData); + + $api->create("KnpLabs", "php-github-api", $deploymentData); + } + + protected function getApiClass() + { + return 'Github\Api\Deployment'; + } +} From 5d94a88f0b4233aa176948b22528df3ab42a2d38 Mon Sep 17 00:00:00 2001 From: lol768 Date: Fri, 20 Feb 2015 20:42:52 +0000 Subject: [PATCH 5/8] Add some more tests --- test/Github/Tests/Api/DeploymentTest.php | 71 +++++++++++++++++++++++- 1 file changed, 69 insertions(+), 2 deletions(-) diff --git a/test/Github/Tests/Api/DeploymentTest.php b/test/Github/Tests/Api/DeploymentTest.php index a9c15c3bb71..8334f8fd3ea 100644 --- a/test/Github/Tests/Api/DeploymentTest.php +++ b/test/Github/Tests/Api/DeploymentTest.php @@ -9,16 +9,83 @@ class DeploymentTest extends TestCase */ public function shouldCreateDeployment() { - /** @var \Github\Api\Deployment $api */ $api = $this->getApiMock(); $deploymentData = array("ref" => "fd6a5f9e5a430dddae8d6a8ea378f913d3a766f9"); $api->expects($this->once()) ->method('post') - ->with('/repos/KnpLabs/php-github-api/deployments', $deploymentData); + ->with('repos/KnpLabs/php-github-api/deployments', $deploymentData); $api->create("KnpLabs", "php-github-api", $deploymentData); } + /** + * @test + */ + public function shouldGetAllDeployments() + { + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('get') + ->with('repos/KnpLabs/php-github-api/deployments'); + + $api->all("KnpLabs", "php-github-api"); + } + + /** + * @test + */ + public function shouldGetAllDeploymentsWithFilterParameters() + { + $api = $this->getApiMock(); + $filterData = ["foo" => "bar", "bar" => "foo"]; + + $api->expects($this->once()) + ->method('get') + ->with('repos/KnpLabs/php-github-api/deployments', $filterData); + + $api->all("KnpLabs", "php-github-api", $filterData); + } + + /** + * @test + */ + public function shouldCreateStatusUpdate() + { + $api = $this->getApiMock(); + $statusData = ["state" => "pending", "description" => "waiting to start"]; + + $api->expects($this->once()) + ->method('post') + ->with('repos/KnpLabs/php-github-api/deployments/1/statuses', $statusData); + + $api->updateStatus("KnpLabs", "php-github-api", 1, $statusData); + } + + /** + * @test + * @expectedException GitHub\Exception\MissingArgumentException + */ + public function shouldRejectStatusUpdateWithoutStateField() + { + $api = $this->getApiMock(); + $statusData = [ "description" => "waiting to start"]; + + $api->updateStatus("KnpLabs", "php-github-api", 1, $statusData); + } + + /** + * @test + */ + public function shouldGetAllStatuses() + { + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('get') + ->with('repos/KnpLabs/php-github-api/deployments/1/statuses'); + + $api->getStatuses("KnpLabs", "php-github-api", 1); + } + protected function getApiClass() { return 'Github\Api\Deployment'; From 77fe71131b4c8a2d2edaf1520acf79339c160f98 Mon Sep 17 00:00:00 2001 From: lol768 Date: Fri, 20 Feb 2015 20:55:50 +0000 Subject: [PATCH 6/8] Add documentation for deployments API --- doc/README.md | 1 + doc/repo/deployments.md | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 doc/repo/deployments.md diff --git a/doc/README.md b/doc/README.md index 176e5a2b51d..5989a7d3edc 100644 --- a/doc/README.md +++ b/doc/README.md @@ -18,6 +18,7 @@ APIs: * [Contents](repo/contents.md) * [Releases](repo/releases.md) * [Assets](repo/assets.md) + * [Deployments](repo/deployments.md) * [Users](users.md) * [Meta](meta.md) * [Activity](activity.md) diff --git a/doc/repo/deployments.md b/doc/repo/deployments.md new file mode 100644 index 00000000000..386025d936f --- /dev/null +++ b/doc/repo/deployments.md @@ -0,0 +1,40 @@ +## Repo / Deployments API +[Back to the "Repos API"](../repos.md) | [Back to the navigation](../index.md) + +Provides information about deployments for a repository. Wraps [GitHub Deployments API](https://developer.github.com/v3/repos/deployments/). + +#### List all deployments. + +```php +$deployments = $client->api('deployment')->all('KnpLabs', 'php-github-api'); +``` + +You can also filter the returned results (see [the documentation](https://developer.github.com/v3/repos/deployments/#list-deployments) for more information): + +```php +$deployments = $client->api('deployment')->all('KnpLabs', 'php-github-api', array('environment' => 'production')); +``` + +#### Create a new deployments. + +The `ref` parameter is required. + +```php +$data = $client->api('deployment')->create('KnpLabs', 'php-github-api', array('ref' => 'fd6a5f9e5a430dddae8d6a8ea378f913d3a766f9')); +``` + +Please note that once a deployment is created it cannot be edited. Only status updates can be created. + +#### Create a new status update. + +The `state` parameter is required. At the time of writing, this must be pending, success, error, or failure. + +```php +$data = $client->api('deployment')->updateStatus('KnpLabs', 'php-github-api', 1, array('state' => 'error', 'description' => 'syntax error')); +``` + +#### Get all status updates for a deployment. + +```php +$statusUpdates = $client->api('deployment')->getStatuses('KnpLabs', 'php-github-api', 1); +``` From 4e62664a506e9664c4fc25075cc9c5fdad7f4104 Mon Sep 17 00:00:00 2001 From: "Eric J. Duran" Date: Sat, 21 Feb 2015 17:31:43 -0500 Subject: [PATCH 7/8] Making DeploymentTest php 5.3 compatible --- test/Github/Tests/Api/DeploymentTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/Github/Tests/Api/DeploymentTest.php b/test/Github/Tests/Api/DeploymentTest.php index 8334f8fd3ea..cfc4712de18 100644 --- a/test/Github/Tests/Api/DeploymentTest.php +++ b/test/Github/Tests/Api/DeploymentTest.php @@ -37,7 +37,7 @@ public function shouldGetAllDeployments() public function shouldGetAllDeploymentsWithFilterParameters() { $api = $this->getApiMock(); - $filterData = ["foo" => "bar", "bar" => "foo"]; + $filterData = array("foo" => "bar", "bar" => "foo"); $api->expects($this->once()) ->method('get') @@ -52,7 +52,7 @@ public function shouldGetAllDeploymentsWithFilterParameters() public function shouldCreateStatusUpdate() { $api = $this->getApiMock(); - $statusData = ["state" => "pending", "description" => "waiting to start"]; + $statusData = array("state" => "pending", "description" => "waiting to start"); $api->expects($this->once()) ->method('post') @@ -68,7 +68,7 @@ public function shouldCreateStatusUpdate() public function shouldRejectStatusUpdateWithoutStateField() { $api = $this->getApiMock(); - $statusData = [ "description" => "waiting to start"]; + $statusData = array("description" => "waiting to start"); $api->updateStatus("KnpLabs", "php-github-api", 1, $statusData); } From 5efad64be360d97f701129b8a583cfe90fd10340 Mon Sep 17 00:00:00 2001 From: lol768 Date: Fri, 27 Mar 2015 23:30:50 +0000 Subject: [PATCH 8/8] Use single quotes for strings --- test/Github/Tests/Api/DeploymentTest.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/test/Github/Tests/Api/DeploymentTest.php b/test/Github/Tests/Api/DeploymentTest.php index cfc4712de18..2bcbb4d5d25 100644 --- a/test/Github/Tests/Api/DeploymentTest.php +++ b/test/Github/Tests/Api/DeploymentTest.php @@ -10,12 +10,12 @@ class DeploymentTest extends TestCase public function shouldCreateDeployment() { $api = $this->getApiMock(); - $deploymentData = array("ref" => "fd6a5f9e5a430dddae8d6a8ea378f913d3a766f9"); + $deploymentData = array('ref' => 'fd6a5f9e5a430dddae8d6a8ea378f913d3a766f9'); $api->expects($this->once()) ->method('post') ->with('repos/KnpLabs/php-github-api/deployments', $deploymentData); - $api->create("KnpLabs", "php-github-api", $deploymentData); + $api->create('KnpLabs', 'php-github-api', $deploymentData); } /** @@ -28,7 +28,7 @@ public function shouldGetAllDeployments() ->method('get') ->with('repos/KnpLabs/php-github-api/deployments'); - $api->all("KnpLabs", "php-github-api"); + $api->all('KnpLabs', 'php-github-api'); } /** @@ -37,13 +37,13 @@ public function shouldGetAllDeployments() public function shouldGetAllDeploymentsWithFilterParameters() { $api = $this->getApiMock(); - $filterData = array("foo" => "bar", "bar" => "foo"); + $filterData = array('foo' => 'bar', 'bar' => 'foo'); $api->expects($this->once()) ->method('get') ->with('repos/KnpLabs/php-github-api/deployments', $filterData); - $api->all("KnpLabs", "php-github-api", $filterData); + $api->all('KnpLabs', 'php-github-api', $filterData); } /** @@ -52,13 +52,13 @@ public function shouldGetAllDeploymentsWithFilterParameters() public function shouldCreateStatusUpdate() { $api = $this->getApiMock(); - $statusData = array("state" => "pending", "description" => "waiting to start"); + $statusData = array('state' => 'pending', 'description' => 'waiting to start'); $api->expects($this->once()) ->method('post') ->with('repos/KnpLabs/php-github-api/deployments/1/statuses', $statusData); - $api->updateStatus("KnpLabs", "php-github-api", 1, $statusData); + $api->updateStatus('KnpLabs', 'php-github-api', 1, $statusData); } /** @@ -68,9 +68,9 @@ public function shouldCreateStatusUpdate() public function shouldRejectStatusUpdateWithoutStateField() { $api = $this->getApiMock(); - $statusData = array("description" => "waiting to start"); + $statusData = array('description' => 'waiting to start'); - $api->updateStatus("KnpLabs", "php-github-api", 1, $statusData); + $api->updateStatus('KnpLabs', 'php-github-api', 1, $statusData); } /** @@ -83,7 +83,7 @@ public function shouldGetAllStatuses() ->method('get') ->with('repos/KnpLabs/php-github-api/deployments/1/statuses'); - $api->getStatuses("KnpLabs", "php-github-api", 1); + $api->getStatuses('KnpLabs', 'php-github-api', 1); } protected function getApiClass()