From 1a740323118a06e6e077ee50358f57f000bb6ad2 Mon Sep 17 00:00:00 2001 From: Constantin Dumitrescu Date: Sun, 18 Jan 2015 06:51:27 -0500 Subject: [PATCH 01/13] Initial commit. --- src/Jira/Api.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/Jira/Api.php b/src/Jira/Api.php index 8bf3cee..dfa980f 100644 --- a/src/Jira/Api.php +++ b/src/Jira/Api.php @@ -37,6 +37,8 @@ class Api const REQUEST_PUT = 'PUT'; const REQUEST_DELETE = 'DELETE'; + const WORKLOG_COMMENT = 'transition:'; + const AUTOMAP_FIELDS = 0x01; /** @@ -853,6 +855,25 @@ public function closeIssue($issue_key) return $result; } + /** + * create JIRA Worklog + * + * @param $issue + * @param $filename + * @param array $options + * @return mixed + */ + public function createWorklogFromTransition($issue, $transitionId, $time, $startDate) + { + $options = array( + "timeSpent" => $time, + "started" => "2013-09-01T10:30:18.932+0530", + "comment" => self::WORKLOG_COMMENT . $transitionId + ); + + return $this->api(self::REQUEST_POST, "/rest/api/2/issue/" . $issue . "/worklog", $options); + } + /** * Returns project components. * From 197ef60ac0925af9666eaeb3ff607e8ef3eccd6b Mon Sep 17 00:00:00 2001 From: Constantin Dumitrescu Date: Sun, 18 Jan 2015 06:59:55 -0500 Subject: [PATCH 02/13] Changed date to the one provided by the arguments function. --- src/Jira/Api.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Jira/Api.php b/src/Jira/Api.php index dfa980f..1d7ad3b 100644 --- a/src/Jira/Api.php +++ b/src/Jira/Api.php @@ -867,7 +867,7 @@ public function createWorklogFromTransition($issue, $transitionId, $time, $start { $options = array( "timeSpent" => $time, - "started" => "2013-09-01T10:30:18.932+0530", + "started" => $startDate, "comment" => self::WORKLOG_COMMENT . $transitionId ); From ddd30e173da86f3db4e72f7a0d75a5faf1db08cf Mon Sep 17 00:00:00 2001 From: Constantin Dumitrescu Date: Sun, 18 Jan 2015 07:31:07 -0500 Subject: [PATCH 03/13] Changed const name for TRANSITION_DELIMITER text that will appear in the worklog comment. --- src/Jira/Api.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Jira/Api.php b/src/Jira/Api.php index 1d7ad3b..9d40e52 100644 --- a/src/Jira/Api.php +++ b/src/Jira/Api.php @@ -37,7 +37,7 @@ class Api const REQUEST_PUT = 'PUT'; const REQUEST_DELETE = 'DELETE'; - const WORKLOG_COMMENT = 'transition:'; + const TRANSITION_DELIMITER = 'transition:'; const AUTOMAP_FIELDS = 0x01; @@ -868,7 +868,7 @@ public function createWorklogFromTransition($issue, $transitionId, $time, $start $options = array( "timeSpent" => $time, "started" => $startDate, - "comment" => self::WORKLOG_COMMENT . $transitionId + "comment" => self::TRANSITION_DELIMITER . $transitionId ); return $this->api(self::REQUEST_POST, "/rest/api/2/issue/" . $issue . "/worklog", $options); From 5ee586d1bc19ef1f4ff282825e74b350150f4935 Mon Sep 17 00:00:00 2001 From: Constantin Dumitrescu Date: Mon, 19 Jan 2015 10:12:47 -0500 Subject: [PATCH 04/13] Abstracted the createWorkload method. --- src/Jira/Api.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Jira/Api.php b/src/Jira/Api.php index 9d40e52..cd81d5b 100644 --- a/src/Jira/Api.php +++ b/src/Jira/Api.php @@ -863,15 +863,17 @@ public function closeIssue($issue_key) * @param array $options * @return mixed */ - public function createWorklogFromTransition($issue, $transitionId, $time, $startDate) + public function createWorklog($issueKey, $time, $options = array()) { - $options = array( - "timeSpent" => $time, - "started" => $startDate, - "comment" => self::TRANSITION_DELIMITER . $transitionId + + $options = array_merge( + array( + "timeSpent" => $time, + ), + $options ); - return $this->api(self::REQUEST_POST, "/rest/api/2/issue/" . $issue . "/worklog", $options); + return $this->api(self::REQUEST_POST, "/rest/api/2/issue/" . $issueKey . "/worklog", $options); } /** From 3d05ba97b681604f147322f957cdb97937a645a1 Mon Sep 17 00:00:00 2001 From: Constantin Dumitrescu Date: Thu, 29 Jan 2015 02:40:18 -0500 Subject: [PATCH 05/13] Add removeWorkflow method. Refactor api call using sprintf. --- src/Jira/Api.php | 57 ++++++++++++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 21 deletions(-) diff --git a/src/Jira/Api.php b/src/Jira/Api.php index cd81d5b..b901145 100644 --- a/src/Jira/Api.php +++ b/src/Jira/Api.php @@ -375,6 +375,27 @@ public function addComment($issue_key, $params) return $this->api(self::REQUEST_POST, sprintf('/rest/api/2/issue/%s/comment', $issue_key), $params); } + /** + * create JIRA Worklog + * + * @param $issue + * @param $filename + * @param array $options + * @return mixed + */ + public function createWorklog($issueKey, $time, $options = array()) + { + + $options = array_merge( + array( + "timeSpent" => $time, + ), + $options + ); + + return $this->api(self::REQUEST_POST, sprintf("/rest/api/2/issue/%s/worklog", $issueKey), $options); + } + /** * Get all worklogs for an issue. * @@ -389,6 +410,21 @@ public function getWorklogs($issue_key, array $params) return $this->api(self::REQUEST_GET, sprintf('/rest/api/2/issue/%s/worklog', $issue_key), $params); } + /** + * remove a JIRA Worklog + * + * @param $issueKey + * @return mixed + */ + public function removeWorklog($issueKey, $worklogId) + { + $options = array( + "adjustEstimate" => "auto" + ); + + return $this->api(self::REQUEST_DELETE, sprintf("/rest/api/2/issue/%s/worklog/%s", $issueKey, $worklogId), $options); + } + /** * Get available transitions for a ticket. * @@ -855,27 +891,6 @@ public function closeIssue($issue_key) return $result; } - /** - * create JIRA Worklog - * - * @param $issue - * @param $filename - * @param array $options - * @return mixed - */ - public function createWorklog($issueKey, $time, $options = array()) - { - - $options = array_merge( - array( - "timeSpent" => $time, - ), - $options - ); - - return $this->api(self::REQUEST_POST, "/rest/api/2/issue/" . $issueKey . "/worklog", $options); - } - /** * Returns project components. * From 42e73b743c17c0b83ba156ce0a41cc1ea037de1f Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 31 Dec 2024 17:03:55 +0200 Subject: [PATCH 06/13] CS fixes --- src/Jira/Api.php | 74 ++++++++++++++++++++++++++---------------------- 1 file changed, 40 insertions(+), 34 deletions(-) diff --git a/src/Jira/Api.php b/src/Jira/Api.php index b901145..adb55f1 100644 --- a/src/Jira/Api.php +++ b/src/Jira/Api.php @@ -375,26 +375,26 @@ public function addComment($issue_key, $params) return $this->api(self::REQUEST_POST, sprintf('/rest/api/2/issue/%s/comment', $issue_key), $params); } - /** - * create JIRA Worklog - * - * @param $issue - * @param $filename - * @param array $options - * @return mixed - */ - public function createWorklog($issueKey, $time, $options = array()) - { - - $options = array_merge( - array( - "timeSpent" => $time, - ), - $options - ); - - return $this->api(self::REQUEST_POST, sprintf("/rest/api/2/issue/%s/worklog", $issueKey), $options); - } + /** + * Creates worklog for na issue. + * + * @param string $issue_key Issue key should be "YOURPROJ-22". + * @param string $time Time. + * @param array $options Options. + * + * @return mixed + */ + public function createWorklog($issue_key, $time, array $options = array()) + { + $options = array_merge( + array( + 'timeSpent' => $time, + ), + $options + ); + + return $this->api(self::REQUEST_POST, sprintf('/rest/api/2/issue/%s/worklog', $issue_key), $options); + } /** * Get all worklogs for an issue. @@ -410,20 +410,26 @@ public function getWorklogs($issue_key, array $params) return $this->api(self::REQUEST_GET, sprintf('/rest/api/2/issue/%s/worklog', $issue_key), $params); } - /** - * remove a JIRA Worklog - * - * @param $issueKey - * @return mixed - */ - public function removeWorklog($issueKey, $worklogId) - { - $options = array( - "adjustEstimate" => "auto" - ); - - return $this->api(self::REQUEST_DELETE, sprintf("/rest/api/2/issue/%s/worklog/%s", $issueKey, $worklogId), $options); - } + /** + * Remove an issue worklog. + * + * @param string $issue_key Issue key should be "YOURPROJ-22". + * @param integer $worklog_id Work Log ID. + * + * @return mixed + */ + public function removeWorklog($issue_key, $worklog_id) + { + $options = array( + 'adjustEstimate' => 'auto', + ); + + return $this->api( + self::REQUEST_DELETE, + sprintf('/rest/api/2/issue/%s/worklog/%s', $issue_key, $worklog_id), + $options + ); + } /** * Get available transitions for a ticket. From f78c424f21f3171ea77cefee22a2aed43f979ba0 Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 31 Dec 2024 17:05:21 +0200 Subject: [PATCH 07/13] Removed unused constant --- src/Jira/Api.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Jira/Api.php b/src/Jira/Api.php index adb55f1..815f21c 100644 --- a/src/Jira/Api.php +++ b/src/Jira/Api.php @@ -37,7 +37,6 @@ class Api const REQUEST_PUT = 'PUT'; const REQUEST_DELETE = 'DELETE'; - const TRANSITION_DELIMITER = 'transition:'; const AUTOMAP_FIELDS = 0x01; From c17d1ba9b57f8d19eb562001af57acd2c85170c4 Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 31 Dec 2024 17:47:59 +0200 Subject: [PATCH 08/13] Added versions and changes return types of previously added methods --- src/Jira/Api.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Jira/Api.php b/src/Jira/Api.php index 815f21c..898025c 100644 --- a/src/Jira/Api.php +++ b/src/Jira/Api.php @@ -37,7 +37,6 @@ class Api const REQUEST_PUT = 'PUT'; const REQUEST_DELETE = 'DELETE'; - const AUTOMAP_FIELDS = 0x01; /** @@ -381,7 +380,8 @@ public function addComment($issue_key, $params) * @param string $time Time. * @param array $options Options. * - * @return mixed + * @return Result|false + * @since 2.0.0 */ public function createWorklog($issue_key, $time, array $options = array()) { @@ -415,7 +415,8 @@ public function getWorklogs($issue_key, array $params) * @param string $issue_key Issue key should be "YOURPROJ-22". * @param integer $worklog_id Work Log ID. * - * @return mixed + * @return Result|false + * @since 2.0.0 */ public function removeWorklog($issue_key, $worklog_id) { From 27cbc2800ba4f00c793cae6f5cd972506e5ba690 Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 31 Dec 2024 22:03:36 +0200 Subject: [PATCH 09/13] Made "Api::removeWorklog" more configurable --- src/Jira/Api.php | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/Jira/Api.php b/src/Jira/Api.php index 898025c..d724bfc 100644 --- a/src/Jira/Api.php +++ b/src/Jira/Api.php @@ -414,20 +414,17 @@ public function getWorklogs($issue_key, array $params) * * @param string $issue_key Issue key should be "YOURPROJ-22". * @param integer $worklog_id Work Log ID. + * @param array $params Params. * * @return Result|false * @since 2.0.0 */ - public function removeWorklog($issue_key, $worklog_id) + public function removeWorklog($issue_key, $worklog_id, array $params = array()) { - $options = array( - 'adjustEstimate' => 'auto', - ); - return $this->api( self::REQUEST_DELETE, sprintf('/rest/api/2/issue/%s/worklog/%s', $issue_key, $worklog_id), - $options + $params ); } From 9fec70d252ee0ca1ebe4a350f340125b8269b331 Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 31 Dec 2024 22:04:03 +0200 Subject: [PATCH 10/13] Improved usability of the "Api::createWorklog" method --- src/Jira/Api.php | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/Jira/Api.php b/src/Jira/Api.php index d724bfc..5f51b2d 100644 --- a/src/Jira/Api.php +++ b/src/Jira/Api.php @@ -376,23 +376,26 @@ public function addComment($issue_key, $params) /** * Creates worklog for na issue. * - * @param string $issue_key Issue key should be "YOURPROJ-22". - * @param string $time Time. - * @param array $options Options. + * @param string $issue_key Issue key should be "YOURPROJ-22". + * @param integer $started Start time (unix timestamp). + * @param string|integer $time_spent Either string in "2w 4d 6h 45m" format or second count as a number. + * @param array $params Params. * * @return Result|false * @since 2.0.0 */ - public function createWorklog($issue_key, $time, array $options = array()) + public function createWorklog($issue_key, $started, $time_spent, array $params = array()) { - $options = array_merge( - array( - 'timeSpent' => $time, - ), - $options - ); + $params['started'] = date('Y-m-dTG:m:s.vO', $started); + + if ( is_int($time_spent) ) { + $params['timeSpentSeconds'] = $time_spent; + } + else { + $params['timeSpent'] = $time_spent; + } - return $this->api(self::REQUEST_POST, sprintf('/rest/api/2/issue/%s/worklog', $issue_key), $options); + return $this->api(self::REQUEST_POST, sprintf('/rest/api/2/issue/%s/worklog', $issue_key), $params); } /** From 365d4e7ef9da9f9dbcc7f2a5d3e22d619b08f480 Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 31 Dec 2024 22:09:35 +0200 Subject: [PATCH 11/13] Use terminology from API docs in new method naming --- src/Jira/Api.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Jira/Api.php b/src/Jira/Api.php index 5f51b2d..9dcc939 100644 --- a/src/Jira/Api.php +++ b/src/Jira/Api.php @@ -374,7 +374,7 @@ public function addComment($issue_key, $params) } /** - * Creates worklog for na issue. + * Adds a worklog for an issue. * * @param string $issue_key Issue key should be "YOURPROJ-22". * @param integer $started Start time (unix timestamp). @@ -384,7 +384,7 @@ public function addComment($issue_key, $params) * @return Result|false * @since 2.0.0 */ - public function createWorklog($issue_key, $started, $time_spent, array $params = array()) + public function addWorklog($issue_key, $started, $time_spent, array $params = array()) { $params['started'] = date('Y-m-dTG:m:s.vO', $started); @@ -413,7 +413,7 @@ public function getWorklogs($issue_key, array $params) } /** - * Remove an issue worklog. + * Deletes an issue worklog. * * @param string $issue_key Issue key should be "YOURPROJ-22". * @param integer $worklog_id Work Log ID. @@ -422,7 +422,7 @@ public function getWorklogs($issue_key, array $params) * @return Result|false * @since 2.0.0 */ - public function removeWorklog($issue_key, $worklog_id, array $params = array()) + public function deleteWorklog($issue_key, $worklog_id, array $params = array()) { return $this->api( self::REQUEST_DELETE, From 7cc565f2e2c14acc1d5cc1de52adc14c0b9ca71a Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 1 Jan 2025 18:36:54 +0200 Subject: [PATCH 12/13] Added tests. Both methods now return an array. --- src/Jira/Api.php | 20 ++++++----- tests/Jira/ApiTest.php | 80 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 8 deletions(-) diff --git a/src/Jira/Api.php b/src/Jira/Api.php index 9dcc939..1426710 100644 --- a/src/Jira/Api.php +++ b/src/Jira/Api.php @@ -38,6 +38,7 @@ class Api const REQUEST_DELETE = 'DELETE'; const AUTOMAP_FIELDS = 0x01; + const DATE_TIME_FORMAT = 'Y-m-d\TG:m:s.vO'; /** * Endpoint URL. @@ -377,17 +378,14 @@ public function addComment($issue_key, $params) * Adds a worklog for an issue. * * @param string $issue_key Issue key should be "YOURPROJ-22". - * @param integer $started Start time (unix timestamp). * @param string|integer $time_spent Either string in "2w 4d 6h 45m" format or second count as a number. * @param array $params Params. * - * @return Result|false + * @return array * @since 2.0.0 */ - public function addWorklog($issue_key, $started, $time_spent, array $params = array()) + public function addWorklog($issue_key, $time_spent, array $params = array()) { - $params['started'] = date('Y-m-dTG:m:s.vO', $started); - if ( is_int($time_spent) ) { $params['timeSpentSeconds'] = $time_spent; } @@ -395,7 +393,12 @@ public function addWorklog($issue_key, $started, $time_spent, array $params = ar $params['timeSpent'] = $time_spent; } - return $this->api(self::REQUEST_POST, sprintf('/rest/api/2/issue/%s/worklog', $issue_key), $params); + return $this->api( + self::REQUEST_POST, + sprintf('/rest/api/2/issue/%s/worklog', $issue_key), + $params, + true + ); } /** @@ -419,7 +422,7 @@ public function getWorklogs($issue_key, array $params) * @param integer $worklog_id Work Log ID. * @param array $params Params. * - * @return Result|false + * @return array * @since 2.0.0 */ public function deleteWorklog($issue_key, $worklog_id, array $params = array()) @@ -427,7 +430,8 @@ public function deleteWorklog($issue_key, $worklog_id, array $params = array()) return $this->api( self::REQUEST_DELETE, sprintf('/rest/api/2/issue/%s/worklog/%s', $issue_key, $worklog_id), - $params + $params, + true ); } diff --git a/tests/Jira/ApiTest.php b/tests/Jira/ApiTest.php index 31c69a4..caed679 100644 --- a/tests/Jira/ApiTest.php +++ b/tests/Jira/ApiTest.php @@ -312,6 +312,86 @@ public function testGetIssueTypes() $this->assertEquals($expected, $actual); } + /** + * @param string|integer $time_spent Time spent. + * @param array $expected_rest_params Expected rest params. + * + * @return void + * @dataProvider addWorkLogWithoutCustomParamsDataProvider + */ + public function testAddWorkLogWithoutCustomParams($time_spent, array $expected_rest_params) + { + $response = '{}'; + + $this->expectClientCall( + Api::REQUEST_POST, + '/rest/api/2/issue/JRA-15/worklog', + $expected_rest_params, + $response + ); + + $actual = $this->api->addWorklog('JRA-15', $time_spent); + + $this->assertEquals(json_decode($response, true), $actual, 'The response is json-decoded.'); + } + + public static function addWorkLogWithoutCustomParamsDataProvider() + { + return array( + 'integer time spent' => array(12, array('timeSpentSeconds' => 12)), + 'string time spent' => array('12m', array('timeSpent' => '12m')), + ); + } + + public function testAddWorklogWithCustomParams() + { + $response = '{}'; + + $started = date(Api::DATE_TIME_FORMAT, 1621026000); + $this->expectClientCall( + Api::REQUEST_POST, + '/rest/api/2/issue/JRA-15/worklog', + array('timeSpent' => '12m', 'started' => $started), + $response + ); + + $actual = $this->api->addWorklog('JRA-15', '12m', array('started' => $started)); + + $this->assertEquals(json_decode($response, true), $actual, 'The response is json-decoded.'); + } + + public function testDeleteWorkLogWithoutCustomParams() + { + $response = '{}'; + + $this->expectClientCall( + Api::REQUEST_DELETE, + '/rest/api/2/issue/JRA-15/worklog/11256', + array(), + $response + ); + + $actual = $this->api->deleteWorklog('JRA-15', 11256); + + $this->assertEquals(json_decode($response, true), $actual, 'The response is json-decoded.'); + } + + public function testDeleteWorkLogWithCustomParams() + { + $response = '{}'; + + $this->expectClientCall( + Api::REQUEST_DELETE, + '/rest/api/2/issue/JRA-15/worklog/11256', + array('custom' => 'param'), + $response + ); + + $actual = $this->api->deleteWorklog('JRA-15', 11256, array('custom' => 'param')); + + $this->assertEquals(json_decode($response, true), $actual, 'The response is json-decoded.'); + } + /** * Expects a particular client call. * From e181fe23285b06f2504d584fa4fdbf74792789cd Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 1 Jan 2025 18:39:40 +0200 Subject: [PATCH 13/13] Updated the CHANGELOG --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 62fd651..c5e7c62 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ This project adheres to [Semantic Versioning](https://semver.org/). - Added optional override for the filename in `Api::createAttachment` by [@betterphp] - Allow getting issue count back from `Walker` class by [@aik099]. - Setup `.gitattributes` for better `CHANGELOG.md` merging by [@glensc]. +- Added `Api::addWorklog` and `Api::deleteWorklog` calls for more control over the work logs [@dumconstantin] and [@aik099]. ### Changed - Classes/interfaces were renamed to use namespaces by [@chobie]. @@ -71,3 +72,4 @@ This project adheres to [Semantic Versioning](https://semver.org/). [@aik099]: https://github.com/aik099 [@betterphp]: https://github.com/betterphp [@glensc]: https://github.com/glensc +[@dumconstantin]: https://github.com/dumconstantin