From 6a39975c4e918ef028c3cb8c477e4167618db102 Mon Sep 17 00:00:00 2001 From: Joost Pastoor Date: Mon, 2 May 2016 10:30:28 +0200 Subject: [PATCH] Added the updateVersion, releaseVersion and findVersionByName methods --- src/Jira/Api.php | 71 +++++++++++++++++++++++++++++- tests/Jira/ApiTest.php | 98 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 168 insertions(+), 1 deletion(-) diff --git a/src/Jira/Api.php b/src/Jira/Api.php index 5741d96..cbe09db 100644 --- a/src/Jira/Api.php +++ b/src/Jira/Api.php @@ -26,8 +26,8 @@ use chobie\Jira\Api\Authentication\AuthenticationInterface; use chobie\Jira\Api\Client\ClientInterface; -use chobie\Jira\Api\Result; use chobie\Jira\Api\Client\CurlClient; +use chobie\Jira\Api\Result; class Api { @@ -222,6 +222,7 @@ public function getRoleDetails($projectKey, $roleId) * @param $issuetypeIds array Combined with issuetypeNames, lists the issue types with which to filter the results. * If null, all issue types are returned. Specifiying an issue type that does not exist is * not an error. + * * @param $issuetypeNames array Combined with issuetypeIds, lists the issue types with which to filter the results. * If null, all issue types are returned. This parameter can be specified multiple times, * but is NOT interpreted as a comma-separated list. Specifiying an issue type that does @@ -346,6 +347,33 @@ public function getVersions($projectKey) return $this->api(self::REQUEST_GET, "/rest/api/2/project/{$projectKey}/versions", array(), true); } + /** + * Helper method to find a specific version based on the name of the version. + * + * @param string $projectKey Project Key + * @param string $name The version name to match on + * + * @return int|null VersionId on match or null when there is no match + */ + public function findVersionByName($projectKey, $name) + { + // Fetch all versions of this project + $versions = $this->getVersions($projectKey); + + // Filter results on the name + $matching_versions = array_filter($versions, function (array $version) use ($name) { + return $version['name'] == $name; + }); + + // Early out for no results + if (empty($matching_versions)) { + return null; + } + + // Multiple results should not happen since name is unique + return reset($matching_versions); + } + /** * Get available priorities * @@ -485,6 +513,47 @@ public function createVersion($projectId, $name, $options = array()) return $this->api(self::REQUEST_POST, '/rest/api/2/version', $options); } + /** + * Update JIRA Version + * + * https://docs.atlassian.com/jira/REST/latest/#api/2/version-updateVersion + * + * @param int $versionId Version identifier + * @param array $params Key->Value list to update the version with. + * + * @return Result|false + */ + public function updateVersion($versionId, $params = array()) + { + return $this->api(self::REQUEST_PUT, sprintf('/rest/api/2/version/%d', $versionId), $params); + } + + /** + * Shorthand to mark a version as Released + * + * @param int $versionId Version identifier + * @param string|null $releaseDate Date in Y-m-d format. Defaults to today + * @param array $params Optionally extra parameters. + * + * @return Result|false + */ + public function releaseVersion($versionId, $releaseDate = null, $params = array()) + { + if(!$releaseDate) { + $releaseDate = date('Y-m-d'); + } + + $params = array_merge( + array( + 'releaseDate' => $releaseDate, + 'released' => true + ), + $params + ); + + return $this->updateVersion($versionId, $params); + } + /** * Create JIRA Attachment * diff --git a/tests/Jira/ApiTest.php b/tests/Jira/ApiTest.php index f26dce7..ac0c739 100644 --- a/tests/Jira/ApiTest.php +++ b/tests/Jira/ApiTest.php @@ -25,4 +25,102 @@ public function testSetEndpointTrailingSlash() $api->setEndPoint($url); $this->assertEquals($url, $api->getEndpoint()); } + + /** + * Tests that the updateVersion call constructs the correct api call + */ + public function testUpdateVersion() + { + $params = array( + 'released' => true, + 'releaseDate' => '2010-07-06', + ); + + // Stub the api method and keep the rest intact + /** @var Api|\PHPUnit_Framework_MockObject_MockObject $api */ + $api = $this->getMockBuilder('\chobie\Jira\Api')->setMethods(array('api'))->disableOriginalConstructor()->getMock(); + $api->expects($this->once())->method('api')->with( + $this->equalTo(Api::REQUEST_PUT), + $this->equalTo('/rest/api/2/version/111000'), + $this->equalTo($params) + ); + + $api->updateVersion(111000, $params); + } + + /** + * Tests that the releaseVersion call constructs the correct api call + */ + public function testReleaseVersion() + { + $params = array( + 'released' => true, + 'releaseDate' => date('Y-m-d'), + ); + + // Stub the api method and keep the rest intact + /** @var Api|\PHPUnit_Framework_MockObject_MockObject $api */ + $api = $this->getMockBuilder('\chobie\Jira\Api')->setMethods(array('api'))->disableOriginalConstructor()->getMock(); + $api->expects($this->once())->method('api')->with( + $this->equalTo(Api::REQUEST_PUT), + $this->equalTo('/rest/api/2/version/111000'), + $this->equalTo($params) + ); + + $api->releaseVersion(111000); + } + + /** + * Tests that the releaseVersion call constructs the correct api call with overriden release data and params + */ + public function testReleaseVersionAdvanced() + { + $releaseDate = '2010-07-06'; + + $params = array( + 'released' => true, + 'releaseDate' => $releaseDate, + 'test' => 'extra' + ); + + // Stub the api method and keep the rest intact + /** @var Api|\PHPUnit_Framework_MockObject_MockObject $api */ + $api = $this->getMockBuilder('\chobie\Jira\Api')->setMethods(array('api'))->disableOriginalConstructor()->getMock(); + $api->expects($this->once())->method('api')->with( + $this->equalTo(Api::REQUEST_PUT), + $this->equalTo('/rest/api/2/version/111000'), + $this->equalTo($params) + ); + + $api->releaseVersion(111000, $releaseDate, array('test' => 'extra')); + } + + /** + * Tests FindVersionByName + */ + public function testFindVersionByName() + { + $name = '3.36.0'; + $versionId = '14206'; + $projectKey = 'POR'; + + $versions = array( + array('id' => '14205', 'name' => '3.62.0'), + array('id' => $versionId, 'name' => $name), + array('id' => '14207', 'name' => '3.66.0'), + ); + + // Stub the getVersions method and keep the rest intact + /** @var Api|\PHPUnit_Framework_MockObject_MockObject $api */ + $api = $this->getMockBuilder('\chobie\Jira\Api')->setMethods(array('getVersions'))->disableOriginalConstructor()->getMock(); + $api->expects($this->exactly(2))->method('getVersions')->with( + $this->equalTo($projectKey) + )->willReturn($versions); + + // He should find this one + $this->assertEquals(array('id' => $versionId, 'name' => $name), $api->findVersionByName($projectKey, $name)); + + // And there should be no result for this one + $this->assertNull($api->findVersionByName($projectKey, 'i_do_not_exist')); + } }