1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace TogglJira\Jira; |
5
|
|
|
|
6
|
|
|
use chobie\Jira\Api as BaseApi; |
7
|
|
|
use Exception; |
8
|
|
|
|
9
|
|
|
class Api extends BaseApi |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @param string $username |
13
|
|
|
* @return array |
14
|
|
|
*/ |
15
|
1 |
|
public function getUser(string $username): array |
16
|
|
|
{ |
17
|
1 |
|
$userDetails = $this->api(self::REQUEST_GET, "/rest/api/2/user", ['username' => $username]); |
18
|
|
|
|
19
|
1 |
|
return $userDetails->getResult(); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param string $issueID |
24
|
|
|
* @param int $seconds |
25
|
|
|
* @param string $accountId |
26
|
|
|
* @param string $comment |
27
|
|
|
* @param string $created |
28
|
|
|
* @param bool $overwrite |
29
|
|
|
* @param bool $notifyUsers |
30
|
|
|
* |
31
|
|
|
* @return array|BaseApi\Result|false |
32
|
|
|
* @throws Exception |
33
|
|
|
*/ |
34
|
2 |
|
public function addWorkLogEntry( |
35
|
|
|
string $issueID, |
36
|
|
|
int $seconds, |
37
|
|
|
string $accountId, |
38
|
|
|
string $comment, |
39
|
|
|
string $created, |
40
|
|
|
bool $overwrite, |
41
|
|
|
bool $notifyUsers = true |
42
|
|
|
) { |
43
|
2 |
|
$notify = $notifyUsers ? 'true' : 'false'; |
44
|
|
|
$params = [ |
45
|
2 |
|
'timeSpentSeconds' => $seconds, |
46
|
|
|
'author' => [ |
47
|
2 |
|
'accountId' => $accountId, |
48
|
|
|
], |
49
|
2 |
|
'comment' => $comment, |
50
|
2 |
|
'started' => $created, |
51
|
|
|
]; |
52
|
|
|
|
53
|
2 |
|
$worklogResponse = $this->api(self::REQUEST_GET, "/rest/api/2/issue/{$issueID}/worklog"); |
54
|
2 |
|
$workLogResult = $worklogResponse->getResult(); |
55
|
|
|
|
56
|
2 |
|
$startedDay = (new \DateTimeImmutable($params['started']))->format('Y-m-d'); |
57
|
|
|
|
58
|
2 |
|
if (isset($workLogResult['worklogs'])) { |
59
|
1 |
|
foreach ($workLogResult['worklogs'] as $workLog) { |
60
|
1 |
|
$workLogStartedDay = (new \DateTimeImmutable($workLog['started']))->format('Y-m-d'); |
61
|
|
|
|
62
|
1 |
|
if ($startedDay !== $workLogStartedDay || |
63
|
1 |
|
$workLog['author']['accountId'] !== $accountId) { |
64
|
|
|
continue; |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
if (!$overwrite) { |
68
|
1 |
|
return $this->api( |
69
|
1 |
|
self::REQUEST_PUT, |
70
|
1 |
|
"/rest/api/2/issue/{$issueID}/worklog/{$workLog['id']}?adjustEstimate=auto¬ifyUsers={$notify}", |
71
|
1 |
|
$params |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if ($overwrite) { |
76
|
|
|
/** |
77
|
|
|
* When overwriting the worklogs, delete the existing worklogs first before recreating. |
78
|
|
|
*/ |
79
|
|
|
$this->api(self::REQUEST_DELETE, "/rest/api/2/issue/{$issueID}/worklog/{$workLog['id']}"); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
1 |
|
return $this->api(self::REQUEST_POST, "/rest/api/2/issue/{$issueID}/worklog?adjustEstimate=auto¬ifyUsers={$notify}", $params); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|