Skip to content

Commit 7b7adf3

Browse files
committed
Merge pull request #82 from jpastoor/feature/updateVersion
Added updateVersion, releaseVersion and findVersionByName
2 parents d27ee8e + 6a39975 commit 7b7adf3

File tree

2 files changed

+168
-1
lines changed

2 files changed

+168
-1
lines changed

src/Jira/Api.php

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626

2727
use chobie\Jira\Api\Authentication\AuthenticationInterface;
2828
use chobie\Jira\Api\Client\ClientInterface;
29-
use chobie\Jira\Api\Result;
3029
use chobie\Jira\Api\Client\CurlClient;
30+
use chobie\Jira\Api\Result;
3131

3232
class Api
3333
{
@@ -222,6 +222,7 @@ public function getRoleDetails($projectKey, $roleId)
222222
* @param $issuetypeIds array Combined with issuetypeNames, lists the issue types with which to filter the results.
223223
* If null, all issue types are returned. Specifiying an issue type that does not exist is
224224
* not an error.
225+
*
225226
* @param $issuetypeNames array Combined with issuetypeIds, lists the issue types with which to filter the results.
226227
* If null, all issue types are returned. This parameter can be specified multiple times,
227228
* but is NOT interpreted as a comma-separated list. Specifiying an issue type that does
@@ -346,6 +347,33 @@ public function getVersions($projectKey)
346347
return $this->api(self::REQUEST_GET, "/rest/api/2/project/{$projectKey}/versions", array(), true);
347348
}
348349

350+
/**
351+
* Helper method to find a specific version based on the name of the version.
352+
*
353+
* @param string $projectKey Project Key
354+
* @param string $name The version name to match on
355+
*
356+
* @return int|null VersionId on match or null when there is no match
357+
*/
358+
public function findVersionByName($projectKey, $name)
359+
{
360+
// Fetch all versions of this project
361+
$versions = $this->getVersions($projectKey);
362+
363+
// Filter results on the name
364+
$matching_versions = array_filter($versions, function (array $version) use ($name) {
365+
return $version['name'] == $name;
366+
});
367+
368+
// Early out for no results
369+
if (empty($matching_versions)) {
370+
return null;
371+
}
372+
373+
// Multiple results should not happen since name is unique
374+
return reset($matching_versions);
375+
}
376+
349377
/**
350378
* Get available priorities
351379
*
@@ -485,6 +513,47 @@ public function createVersion($projectId, $name, $options = array())
485513
return $this->api(self::REQUEST_POST, '/rest/api/2/version', $options);
486514
}
487515

516+
/**
517+
* Update JIRA Version
518+
*
519+
* https://docs.atlassian.com/jira/REST/latest/#api/2/version-updateVersion
520+
*
521+
* @param int $versionId Version identifier
522+
* @param array $params Key->Value list to update the version with.
523+
*
524+
* @return Result|false
525+
*/
526+
public function updateVersion($versionId, $params = array())
527+
{
528+
return $this->api(self::REQUEST_PUT, sprintf('/rest/api/2/version/%d', $versionId), $params);
529+
}
530+
531+
/**
532+
* Shorthand to mark a version as Released
533+
*
534+
* @param int $versionId Version identifier
535+
* @param string|null $releaseDate Date in Y-m-d format. Defaults to today
536+
* @param array $params Optionally extra parameters.
537+
*
538+
* @return Result|false
539+
*/
540+
public function releaseVersion($versionId, $releaseDate = null, $params = array())
541+
{
542+
if(!$releaseDate) {
543+
$releaseDate = date('Y-m-d');
544+
}
545+
546+
$params = array_merge(
547+
array(
548+
'releaseDate' => $releaseDate,
549+
'released' => true
550+
),
551+
$params
552+
);
553+
554+
return $this->updateVersion($versionId, $params);
555+
}
556+
488557
/**
489558
* Create JIRA Attachment
490559
*

tests/Jira/ApiTest.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,102 @@ public function testSetEndpointTrailingSlash()
2525
$api->setEndPoint($url);
2626
$this->assertEquals($url, $api->getEndpoint());
2727
}
28+
29+
/**
30+
* Tests that the updateVersion call constructs the correct api call
31+
*/
32+
public function testUpdateVersion()
33+
{
34+
$params = array(
35+
'released' => true,
36+
'releaseDate' => '2010-07-06',
37+
);
38+
39+
// Stub the api method and keep the rest intact
40+
/** @var Api|\PHPUnit_Framework_MockObject_MockObject $api */
41+
$api = $this->getMockBuilder('\chobie\Jira\Api')->setMethods(array('api'))->disableOriginalConstructor()->getMock();
42+
$api->expects($this->once())->method('api')->with(
43+
$this->equalTo(Api::REQUEST_PUT),
44+
$this->equalTo('/rest/api/2/version/111000'),
45+
$this->equalTo($params)
46+
);
47+
48+
$api->updateVersion(111000, $params);
49+
}
50+
51+
/**
52+
* Tests that the releaseVersion call constructs the correct api call
53+
*/
54+
public function testReleaseVersion()
55+
{
56+
$params = array(
57+
'released' => true,
58+
'releaseDate' => date('Y-m-d'),
59+
);
60+
61+
// Stub the api method and keep the rest intact
62+
/** @var Api|\PHPUnit_Framework_MockObject_MockObject $api */
63+
$api = $this->getMockBuilder('\chobie\Jira\Api')->setMethods(array('api'))->disableOriginalConstructor()->getMock();
64+
$api->expects($this->once())->method('api')->with(
65+
$this->equalTo(Api::REQUEST_PUT),
66+
$this->equalTo('/rest/api/2/version/111000'),
67+
$this->equalTo($params)
68+
);
69+
70+
$api->releaseVersion(111000);
71+
}
72+
73+
/**
74+
* Tests that the releaseVersion call constructs the correct api call with overriden release data and params
75+
*/
76+
public function testReleaseVersionAdvanced()
77+
{
78+
$releaseDate = '2010-07-06';
79+
80+
$params = array(
81+
'released' => true,
82+
'releaseDate' => $releaseDate,
83+
'test' => 'extra'
84+
);
85+
86+
// Stub the api method and keep the rest intact
87+
/** @var Api|\PHPUnit_Framework_MockObject_MockObject $api */
88+
$api = $this->getMockBuilder('\chobie\Jira\Api')->setMethods(array('api'))->disableOriginalConstructor()->getMock();
89+
$api->expects($this->once())->method('api')->with(
90+
$this->equalTo(Api::REQUEST_PUT),
91+
$this->equalTo('/rest/api/2/version/111000'),
92+
$this->equalTo($params)
93+
);
94+
95+
$api->releaseVersion(111000, $releaseDate, array('test' => 'extra'));
96+
}
97+
98+
/**
99+
* Tests FindVersionByName
100+
*/
101+
public function testFindVersionByName()
102+
{
103+
$name = '3.36.0';
104+
$versionId = '14206';
105+
$projectKey = 'POR';
106+
107+
$versions = array(
108+
array('id' => '14205', 'name' => '3.62.0'),
109+
array('id' => $versionId, 'name' => $name),
110+
array('id' => '14207', 'name' => '3.66.0'),
111+
);
112+
113+
// Stub the getVersions method and keep the rest intact
114+
/** @var Api|\PHPUnit_Framework_MockObject_MockObject $api */
115+
$api = $this->getMockBuilder('\chobie\Jira\Api')->setMethods(array('getVersions'))->disableOriginalConstructor()->getMock();
116+
$api->expects($this->exactly(2))->method('getVersions')->with(
117+
$this->equalTo($projectKey)
118+
)->willReturn($versions);
119+
120+
// He should find this one
121+
$this->assertEquals(array('id' => $versionId, 'name' => $name), $api->findVersionByName($projectKey, $name));
122+
123+
// And there should be no result for this one
124+
$this->assertNull($api->findVersionByName($projectKey, 'i_do_not_exist'));
125+
}
28126
}

0 commit comments

Comments
 (0)