Skip to content

Commit

Permalink
Added Api::addWorklog and Api::deleteWorklog methods (#219)
Browse files Browse the repository at this point in the history
  • Loading branch information
aik099 authored Jan 1, 2025
1 parent afa00f5 commit a8986ac
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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].
Expand Down Expand Up @@ -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
48 changes: 48 additions & 0 deletions src/Jira/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -373,6 +374,33 @@ public function addComment($issue_key, $params)
return $this->api(self::REQUEST_POST, sprintf('/rest/api/2/issue/%s/comment', $issue_key), $params);
}

/**
* Adds a worklog for an issue.
*
* @param string $issue_key Issue key should be "YOURPROJ-22".
* @param string|integer $time_spent Either string in "2w 4d 6h 45m" format or second count as a number.
* @param array $params Params.
*
* @return array
* @since 2.0.0
*/
public function addWorklog($issue_key, $time_spent, array $params = array())
{
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),
$params,
true
);
}

/**
* Get all worklogs for an issue.
*
Expand All @@ -387,6 +415,26 @@ public function getWorklogs($issue_key, array $params)
return $this->api(self::REQUEST_GET, sprintf('/rest/api/2/issue/%s/worklog', $issue_key), $params);
}

/**
* Deletes an issue worklog.
*
* @param string $issue_key Issue key should be "YOURPROJ-22".
* @param integer $worklog_id Work Log ID.
* @param array $params Params.
*
* @return array
* @since 2.0.0
*/
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,
true
);
}

/**
* Get available transitions for a ticket.
*
Expand Down
80 changes: 80 additions & 0 deletions tests/Jira/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down

0 comments on commit a8986ac

Please sign in to comment.