Skip to content

Commit a8986ac

Browse files
authored
Added Api::addWorklog and Api::deleteWorklog methods (#219)
1 parent afa00f5 commit a8986ac

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
1818
- Added optional override for the filename in `Api::createAttachment` by [@betterphp]
1919
- Allow getting issue count back from `Walker` class by [@aik099].
2020
- Setup `.gitattributes` for better `CHANGELOG.md` merging by [@glensc].
21+
- Added `Api::addWorklog` and `Api::deleteWorklog` calls for more control over the work logs [@dumconstantin] and [@aik099].
2122

2223
### Changed
2324
- Classes/interfaces were renamed to use namespaces by [@chobie].
@@ -71,3 +72,4 @@ This project adheres to [Semantic Versioning](https://semver.org/).
7172
[@aik099]: https://github.com/aik099
7273
[@betterphp]: https://github.com/betterphp
7374
[@glensc]: https://github.com/glensc
75+
[@dumconstantin]: https://github.com/dumconstantin

src/Jira/Api.php

+48
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class Api
3838
const REQUEST_DELETE = 'DELETE';
3939

4040
const AUTOMAP_FIELDS = 0x01;
41+
const DATE_TIME_FORMAT = 'Y-m-d\TG:m:s.vO';
4142

4243
/**
4344
* Endpoint URL.
@@ -373,6 +374,33 @@ public function addComment($issue_key, $params)
373374
return $this->api(self::REQUEST_POST, sprintf('/rest/api/2/issue/%s/comment', $issue_key), $params);
374375
}
375376

377+
/**
378+
* Adds a worklog for an issue.
379+
*
380+
* @param string $issue_key Issue key should be "YOURPROJ-22".
381+
* @param string|integer $time_spent Either string in "2w 4d 6h 45m" format or second count as a number.
382+
* @param array $params Params.
383+
*
384+
* @return array
385+
* @since 2.0.0
386+
*/
387+
public function addWorklog($issue_key, $time_spent, array $params = array())
388+
{
389+
if ( is_int($time_spent) ) {
390+
$params['timeSpentSeconds'] = $time_spent;
391+
}
392+
else {
393+
$params['timeSpent'] = $time_spent;
394+
}
395+
396+
return $this->api(
397+
self::REQUEST_POST,
398+
sprintf('/rest/api/2/issue/%s/worklog', $issue_key),
399+
$params,
400+
true
401+
);
402+
}
403+
376404
/**
377405
* Get all worklogs for an issue.
378406
*
@@ -387,6 +415,26 @@ public function getWorklogs($issue_key, array $params)
387415
return $this->api(self::REQUEST_GET, sprintf('/rest/api/2/issue/%s/worklog', $issue_key), $params);
388416
}
389417

418+
/**
419+
* Deletes an issue worklog.
420+
*
421+
* @param string $issue_key Issue key should be "YOURPROJ-22".
422+
* @param integer $worklog_id Work Log ID.
423+
* @param array $params Params.
424+
*
425+
* @return array
426+
* @since 2.0.0
427+
*/
428+
public function deleteWorklog($issue_key, $worklog_id, array $params = array())
429+
{
430+
return $this->api(
431+
self::REQUEST_DELETE,
432+
sprintf('/rest/api/2/issue/%s/worklog/%s', $issue_key, $worklog_id),
433+
$params,
434+
true
435+
);
436+
}
437+
390438
/**
391439
* Get available transitions for a ticket.
392440
*

tests/Jira/ApiTest.php

+80
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,86 @@ public function testGetIssueTypes()
312312
$this->assertEquals($expected, $actual);
313313
}
314314

315+
/**
316+
* @param string|integer $time_spent Time spent.
317+
* @param array $expected_rest_params Expected rest params.
318+
*
319+
* @return void
320+
* @dataProvider addWorkLogWithoutCustomParamsDataProvider
321+
*/
322+
public function testAddWorkLogWithoutCustomParams($time_spent, array $expected_rest_params)
323+
{
324+
$response = '{}';
325+
326+
$this->expectClientCall(
327+
Api::REQUEST_POST,
328+
'/rest/api/2/issue/JRA-15/worklog',
329+
$expected_rest_params,
330+
$response
331+
);
332+
333+
$actual = $this->api->addWorklog('JRA-15', $time_spent);
334+
335+
$this->assertEquals(json_decode($response, true), $actual, 'The response is json-decoded.');
336+
}
337+
338+
public static function addWorkLogWithoutCustomParamsDataProvider()
339+
{
340+
return array(
341+
'integer time spent' => array(12, array('timeSpentSeconds' => 12)),
342+
'string time spent' => array('12m', array('timeSpent' => '12m')),
343+
);
344+
}
345+
346+
public function testAddWorklogWithCustomParams()
347+
{
348+
$response = '{}';
349+
350+
$started = date(Api::DATE_TIME_FORMAT, 1621026000);
351+
$this->expectClientCall(
352+
Api::REQUEST_POST,
353+
'/rest/api/2/issue/JRA-15/worklog',
354+
array('timeSpent' => '12m', 'started' => $started),
355+
$response
356+
);
357+
358+
$actual = $this->api->addWorklog('JRA-15', '12m', array('started' => $started));
359+
360+
$this->assertEquals(json_decode($response, true), $actual, 'The response is json-decoded.');
361+
}
362+
363+
public function testDeleteWorkLogWithoutCustomParams()
364+
{
365+
$response = '{}';
366+
367+
$this->expectClientCall(
368+
Api::REQUEST_DELETE,
369+
'/rest/api/2/issue/JRA-15/worklog/11256',
370+
array(),
371+
$response
372+
);
373+
374+
$actual = $this->api->deleteWorklog('JRA-15', 11256);
375+
376+
$this->assertEquals(json_decode($response, true), $actual, 'The response is json-decoded.');
377+
}
378+
379+
public function testDeleteWorkLogWithCustomParams()
380+
{
381+
$response = '{}';
382+
383+
$this->expectClientCall(
384+
Api::REQUEST_DELETE,
385+
'/rest/api/2/issue/JRA-15/worklog/11256',
386+
array('custom' => 'param'),
387+
$response
388+
);
389+
390+
$actual = $this->api->deleteWorklog('JRA-15', 11256, array('custom' => 'param'));
391+
392+
$this->assertEquals(json_decode($response, true), $actual, 'The response is json-decoded.');
393+
}
394+
315395
/**
316396
* Expects a particular client call.
317397
*

0 commit comments

Comments
 (0)