|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* \Wicked\Timely\PushServices\Jira |
|
5
|
|
|
* |
|
6
|
|
|
* NOTICE OF LICENSE |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
|
9
|
|
|
* that is available through the world-wide-web at this URL: |
|
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
|
11
|
|
|
* |
|
12
|
|
|
* PHP version 5 |
|
13
|
|
|
* |
|
14
|
|
|
* @author wick-ed |
|
15
|
|
|
* @copyright 2020 Bernhard Wick |
|
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
17
|
|
|
* @link https://github.com/wick-ed/timely |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
namespace Wicked\Timely\PushServices; |
|
21
|
|
|
|
|
22
|
|
|
use JiraRestApi\Issue\IssueService; |
|
23
|
|
|
use JiraRestApi\Issue\Worklog; |
|
24
|
|
|
use Wicked\Timely\DotEnvConfiguration; |
|
25
|
|
|
use Wicked\Timely\Entities\Task; |
|
26
|
|
|
use Wicked\Timely\Helper\Date; |
|
27
|
|
|
use Wicked\Timely\PushServices\Authentication\PasswordRetrievalStrategyInterface; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Jira push service |
|
31
|
|
|
* |
|
32
|
|
|
* @author wick-ed |
|
33
|
|
|
* @copyright 2020 Bernhard Wick |
|
34
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
35
|
|
|
* @link https://github.com/wick-ed/timely |
|
36
|
|
|
*/ |
|
37
|
|
|
class Jira implements PushServiceInterface, AuthenticationAwarePushServiceInterface |
|
38
|
|
|
{ |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var IssueService |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $issueService; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var PasswordRetrievalStrategyInterface |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $passwordRetrievalStrategy; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Jira constructor. |
|
52
|
|
|
* |
|
53
|
|
|
* @param PasswordRetrievalStrategyInterface $passwordRetrievalStrategy |
|
54
|
|
|
* |
|
55
|
|
|
* @throws \JiraRestApi\JiraException |
|
56
|
|
|
*/ |
|
57
|
|
|
public function __construct(PasswordRetrievalStrategyInterface $passwordRetrievalStrategy) |
|
58
|
|
|
{ |
|
59
|
|
|
$this->passwordRetrievalStrategy = $passwordRetrievalStrategy; |
|
60
|
|
|
$this->init(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Initialize |
|
65
|
|
|
* |
|
66
|
|
|
* @throws \JiraRestApi\JiraException |
|
67
|
|
|
*/ |
|
68
|
|
|
protected function init() |
|
69
|
|
|
{ |
|
70
|
|
|
// retrieve configuration |
|
71
|
|
|
$configuration = new DotEnvConfiguration(); |
|
72
|
|
|
$password = $configuration->getJiraPassword(); |
|
73
|
|
|
if (empty($password)) { |
|
74
|
|
|
$password = $this->passwordRetrievalStrategy->getPassword(); |
|
75
|
|
|
//$password = $this->getPasswordFromKeychain($configuration); |
|
76
|
|
|
if (empty($password)) { |
|
77
|
|
|
return; |
|
78
|
|
|
} |
|
79
|
|
|
$configuration->setJiraPassword($password); |
|
80
|
|
|
} |
|
81
|
|
|
$this->issueService = new IssueService($configuration); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Push a given task to a (remote) service |
|
86
|
|
|
* |
|
87
|
|
|
* @param Task $task |
|
88
|
|
|
* |
|
89
|
|
|
* @return boolean |
|
90
|
|
|
* |
|
91
|
|
|
* @throws \Exception |
|
92
|
|
|
*/ |
|
93
|
|
|
public function push(Task $task) |
|
94
|
|
|
{ |
|
95
|
|
|
// create a worklog from the task |
|
96
|
|
|
$workLog = new Worklog(); |
|
97
|
|
|
$workLog->setComment($task->getComment()) |
|
98
|
|
|
->setStarted($task->getStartTime()) |
|
99
|
|
|
->setTimeSpent(Date::secondsToUnits(Date::roundByInterval($task->getDuration(), 900))); |
|
100
|
|
|
|
|
101
|
|
|
// check the sanity of our worklog and discard it if there is something missing |
|
102
|
|
|
if (!$task->getTicketId() || empty($workLog->timeSpent) || empty($workLog->comment)) { |
|
|
|
|
|
|
103
|
|
|
throw new \Exception('Not pushing one worklog as it misses vital information'); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
// push to remote |
|
107
|
|
|
$this->issueService->addWorklog($task->getTicketId(), $workLog); |
|
108
|
|
|
return true; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: