|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* PHPCI - Continuous Integration for PHP. |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright 2014, Block 8 Limited. |
|
6
|
|
|
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md |
|
7
|
|
|
* |
|
8
|
|
|
* @link https://www.phptesting.org/ |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace PHPCI\Service; |
|
12
|
|
|
|
|
13
|
|
|
use PHPCI\Model\Project; |
|
14
|
|
|
use PHPCI\Model\Build; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class BuildStatusService. |
|
18
|
|
|
*/ |
|
19
|
|
|
class BuildStatusService |
|
20
|
|
|
{ |
|
21
|
|
|
/* @var BuildStatusService */ |
|
22
|
|
|
protected $prevService = null; |
|
23
|
|
|
|
|
24
|
|
|
/* @var Project */ |
|
25
|
|
|
protected $project; |
|
26
|
|
|
|
|
27
|
|
|
/** @var string */ |
|
28
|
|
|
protected $branch; |
|
29
|
|
|
|
|
30
|
|
|
/* @var Build */ |
|
31
|
|
|
protected $build; |
|
32
|
|
|
|
|
33
|
|
|
/** @var string */ |
|
34
|
|
|
protected $url; |
|
35
|
|
|
|
|
36
|
|
|
/** @var array */ |
|
37
|
|
|
protected $finishedStatusIds = array( |
|
38
|
|
|
Build::STATUS_SUCCESS, |
|
39
|
|
|
Build::STATUS_FAILED, |
|
40
|
|
|
); |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param $branch |
|
44
|
|
|
* @param Project $project |
|
45
|
|
|
* @param Build $build |
|
46
|
|
|
* @param bool $isParent |
|
47
|
|
|
*/ |
|
48
|
5 |
|
public function __construct( |
|
49
|
|
|
$branch, |
|
50
|
|
|
Project $project, |
|
51
|
|
|
Build $build = null, |
|
52
|
|
|
$isParent = false |
|
53
|
|
|
) { |
|
54
|
5 |
|
$this->project = $project; |
|
55
|
5 |
|
$this->branch = $branch; |
|
56
|
5 |
|
$this->build = $build; |
|
57
|
5 |
|
if ($this->build) { |
|
58
|
5 |
|
$this->loadParentBuild($isParent); |
|
59
|
5 |
|
} |
|
60
|
5 |
|
if (defined('PHPCI_URL')) { |
|
61
|
5 |
|
$this->setUrl(PHPCI_URL); |
|
62
|
5 |
|
} |
|
63
|
5 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param $url |
|
67
|
|
|
*/ |
|
68
|
5 |
|
public function setUrl($url) |
|
69
|
|
|
{ |
|
70
|
5 |
|
$this->url = $url; |
|
71
|
5 |
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @return Build |
|
75
|
|
|
*/ |
|
76
|
2 |
|
public function getBuild() |
|
77
|
|
|
{ |
|
78
|
2 |
|
return $this->build; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param bool $isParent |
|
83
|
|
|
* |
|
84
|
|
|
* @throws \Exception |
|
85
|
|
|
*/ |
|
86
|
5 |
|
protected function loadParentBuild($isParent = true) |
|
87
|
|
|
{ |
|
88
|
5 |
|
if ($isParent === false && !$this->isFinished()) { |
|
89
|
3 |
|
$lastFinishedBuild = $this->project->getLatestBuild($this->branch, $this->finishedStatusIds); |
|
90
|
|
|
|
|
91
|
3 |
|
if ($lastFinishedBuild) { |
|
92
|
2 |
|
$this->prevService = new self( |
|
93
|
2 |
|
$this->branch, |
|
94
|
2 |
|
$this->project, |
|
95
|
2 |
|
$lastFinishedBuild, |
|
96
|
|
|
true |
|
97
|
2 |
|
); |
|
98
|
2 |
|
} |
|
99
|
3 |
|
} |
|
100
|
5 |
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @return string |
|
104
|
|
|
*/ |
|
105
|
5 |
|
public function getActivity() |
|
106
|
|
|
{ |
|
107
|
5 |
|
if (in_array($this->build->getStatus(), $this->finishedStatusIds)) { |
|
108
|
2 |
|
return 'Sleeping'; |
|
109
|
3 |
|
} elseif ($this->build->getStatus() == Build::STATUS_NEW) { |
|
110
|
1 |
|
return 'Pending'; |
|
111
|
2 |
|
} elseif ($this->build->getStatus() == Build::STATUS_RUNNING) { |
|
112
|
2 |
|
return 'Building'; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return 'Unknown'; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @return string |
|
120
|
|
|
*/ |
|
121
|
5 |
|
public function getName() |
|
122
|
|
|
{ |
|
123
|
5 |
|
return $this->project->getTitle().' / '.$this->branch; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @return bool |
|
128
|
|
|
*/ |
|
129
|
5 |
|
public function isFinished() |
|
130
|
|
|
{ |
|
131
|
5 |
|
if (in_array($this->build->getStatus(), $this->finishedStatusIds)) { |
|
132
|
2 |
|
return true; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
3 |
|
return false; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @return null|Build |
|
140
|
|
|
*/ |
|
141
|
5 |
|
public function getFinishedBuildInfo() |
|
142
|
|
|
{ |
|
143
|
5 |
|
if ($this->isFinished()) { |
|
144
|
2 |
|
return $this->build; |
|
145
|
3 |
|
} elseif ($this->prevService) { |
|
146
|
2 |
|
return $this->prevService->getBuild(); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
1 |
|
return; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @return int|string |
|
154
|
|
|
*/ |
|
155
|
5 |
|
public function getLastBuildLabel() |
|
156
|
|
|
{ |
|
157
|
5 |
|
if ($buildInfo = $this->getFinishedBuildInfo()) { |
|
158
|
4 |
|
return $buildInfo->getId(); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
1 |
|
return ''; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @return string |
|
166
|
|
|
*/ |
|
167
|
5 |
|
public function getLastBuildTime() |
|
168
|
|
|
{ |
|
169
|
5 |
|
$dateFormat = 'Y-m-d\\TH:i:sO'; |
|
170
|
5 |
|
if ($buildInfo = $this->getFinishedBuildInfo()) { |
|
171
|
4 |
|
return ($buildInfo->getFinished()) ? $buildInfo->getFinished()->format($dateFormat) : ''; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
1 |
|
return ''; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @param Build $build |
|
179
|
|
|
* |
|
180
|
|
|
* @return string |
|
181
|
|
|
*/ |
|
182
|
4 |
|
public function getBuildStatus(Build $build) |
|
183
|
|
|
{ |
|
184
|
4 |
|
switch ($build->getStatus()) { |
|
185
|
4 |
|
case Build::STATUS_SUCCESS: |
|
186
|
2 |
|
return 'Success'; |
|
187
|
2 |
|
case Build::STATUS_FAILED: |
|
188
|
2 |
|
return 'Failure'; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
return 'Unknown'; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* @return string |
|
196
|
|
|
*/ |
|
197
|
5 |
|
public function getLastBuildStatus() |
|
198
|
|
|
{ |
|
199
|
5 |
|
if ($build = $this->getFinishedBuildInfo()) { |
|
200
|
4 |
|
return $this->getBuildStatus($build); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
1 |
|
return ''; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* @return string |
|
208
|
|
|
*/ |
|
209
|
5 |
|
public function getBuildUrl() |
|
210
|
|
|
{ |
|
211
|
5 |
|
return $this->url.'build/view/'.$this->build->getId(); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* @return array |
|
216
|
|
|
*/ |
|
217
|
5 |
|
public function toArray() |
|
218
|
|
|
{ |
|
219
|
5 |
|
if (!$this->build) { |
|
220
|
|
|
return array(); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
return array( |
|
224
|
5 |
|
'name' => $this->getName(), |
|
225
|
5 |
|
'activity' => $this->getActivity(), |
|
226
|
5 |
|
'lastBuildLabel' => $this->getLastBuildLabel(), |
|
227
|
5 |
|
'lastBuildStatus' => $this->getLastBuildStatus(), |
|
228
|
5 |
|
'lastBuildTime' => $this->getLastBuildTime(), |
|
229
|
5 |
|
'webUrl' => $this->getBuildUrl(), |
|
230
|
5 |
|
); |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
|