-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathBuild.php
More file actions
443 lines (370 loc) · 12.1 KB
/
Copy pathBuild.php
File metadata and controls
443 lines (370 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
<?php
declare(strict_types=1);
namespace PHPCensor\Model\Base;
use DateTime;
use Exception;
use PHPCensor\Common\Exception\InvalidArgumentException;
use PHPCensor\Common\Exception\RuntimeException;
use PHPCensor\Model;
use PHPCensor\Store\BuildStore;
use PHPCensor\Traits\Model\HasCreateDateTrait;
use PHPCensor\Traits\Model\HasUserIdTrait;
/**
* @package PHP Censor
* @subpackage Application
*
* @author Dan Cryer <dan@block8.co.uk>
* @author Dmitry Khomutov <poisoncorpsee@gmail.com>
*/
class Build extends Model
{
use HasUserIdTrait;
use HasCreateDateTrait;
public const STATUS_PENDING = 0;
public const STATUS_RUNNING = 1;
public const STATUS_SUCCESS = 2;
public const STATUS_FAILED = 3;
public const SOURCE_UNKNOWN = 0;
public const SOURCE_MANUAL_WEB = 1;
public const SOURCE_MANUAL_CONSOLE = 2;
public const SOURCE_PERIODICAL = 3;
public const SOURCE_WEBHOOK_PUSH = 4;
public const SOURCE_WEBHOOK_PULL_REQUEST_CREATED = 5;
public const SOURCE_WEBHOOK_PULL_REQUEST_UPDATED = 6;
public const SOURCE_WEBHOOK_PULL_REQUEST_APPROVED = 7;
public const SOURCE_WEBHOOK_PULL_REQUEST_MERGED = 8;
public const SOURCE_MANUAL_REBUILD_WEB = 9;
public const SOURCE_MANUAL_REBUILD_CONSOLE = 10;
protected array $data = [
'id' => null,
'parent_id' => null,
'project_id' => null,
'commit_id' => null,
'status' => null,
'log' => null,
'branch' => null,
'tag' => null,
'create_date' => null,
'start_date' => null,
'finish_date' => null,
'committer_email' => null,
'commit_message' => null,
'extra' => [],
'environment_id' => null,
'source' => Build::SOURCE_UNKNOWN,
'user_id' => null,
'errors_total' => null,
'errors_total_previous' => null,
'errors_new' => null,
'test_coverage' => null,
'test_coverage_previous' => null,
];
protected array $dataTypes = [
'project_id' => 'integer',
'status' => 'integer',
'create_date' => 'datetime',
'start_date' => 'datetime',
'finish_date' => 'datetime',
'extra' => 'array',
'environment_id' => 'integer',
'source' => 'integer',
'user_id' => 'integer',
'errors_total' => 'integer',
'errors_total_previous' => 'integer',
'errors_new' => 'integer',
'parent_id' => 'integer',
];
protected array $allowedStatuses = [
self::STATUS_PENDING,
self::STATUS_RUNNING,
self::STATUS_SUCCESS,
self::STATUS_FAILED,
];
protected array $allowedSources = [
self::SOURCE_UNKNOWN,
self::SOURCE_MANUAL_WEB,
self::SOURCE_MANUAL_CONSOLE,
self::SOURCE_MANUAL_REBUILD_WEB,
self::SOURCE_MANUAL_REBUILD_CONSOLE,
self::SOURCE_PERIODICAL,
self::SOURCE_WEBHOOK_PUSH,
self::SOURCE_WEBHOOK_PULL_REQUEST_CREATED,
self::SOURCE_WEBHOOK_PULL_REQUEST_UPDATED,
self::SOURCE_WEBHOOK_PULL_REQUEST_APPROVED,
self::SOURCE_WEBHOOK_PULL_REQUEST_MERGED,
];
public function getParentId(): ?int
{
return $this->getDataItem('parent_id');
}
public function setParentId(?int $value): bool
{
return $this->setDataItem('parent_id', $value);
}
public function getProjectId(): ?int
{
return $this->getDataItem('project_id');
}
public function setProjectId(int $value): bool
{
return $this->setDataItem('project_id', $value);
}
public function getCommitId(): ?string
{
return $this->getDataItem('commit_id');
}
public function setCommitId(string $value): bool
{
return $this->setDataItem('commit_id', $value);
}
public function getStatus(): ?int
{
return $this->getDataItem('status');
}
/**
* @throws InvalidArgumentException
*/
public function setStatus(int $value): bool
{
if (!\in_array($value, $this->allowedStatuses, true)) {
throw new InvalidArgumentException(
'Column "status" must be one of: ' . \join(', ', $this->allowedStatuses) . '.'
);
}
return $this->setDataItem('status', $value);
}
public function setStatusPending(): bool
{
return $this->setDataItem('status', self::STATUS_PENDING);
}
public function setStatusRunning(): bool
{
return $this->setDataItem('status', self::STATUS_RUNNING);
}
public function setStatusSuccess(): bool
{
return $this->setDataItem('status', self::STATUS_SUCCESS);
}
public function setStatusFailed(): bool
{
return $this->setDataItem('status', self::STATUS_FAILED);
}
public function getLog(): ?string
{
return $this->getDataItem('log');
}
public function setLog(?string $value): bool
{
return $this->setDataItem('log', $value);
}
public function getBranch(): ?string
{
return $this->getDataItem('branch');
}
public function setBranch(string $value)
{
return $this->setDataItem('branch', $value);
}
public function getTag(): ?string
{
return $this->getDataItem('tag');
}
public function setTag(?string $value): bool
{
return $this->setDataItem('tag', $value);
}
public function getStartDate(): ?DateTime
{
return $this->getDataItem('start_date');
}
public function setStartDate(DateTime $value): bool
{
return $this->setDataItem('start_date', $value);
}
public function getFinishDate(): ?DateTime
{
return $this->getDataItem('finish_date');
}
public function setFinishDate(DateTime $value): bool
{
return $this->setDataItem('finish_date', $value);
}
public function getCommitterEmail(): ?string
{
return $this->getDataItem('committer_email');
}
public function setCommitterEmail(?string $value): bool
{
return $this->setDataItem('committer_email', $value);
}
public function getCommitMessage(): ?string
{
return $this->getDataItem('commit_message');
}
public function setCommitMessage(?string $value): bool
{
return $this->setDataItem('commit_message', $value);
}
/**
* @return mixed
*/
public function getExtra(string $key = null)
{
$data = $this->getDataItem('extra');
if ($key === null) {
return $data;
}
return $data[$key] ?? null;
}
public function setExtra(array $value): bool
{
return $this->setDataItem('extra', $value);
}
/**
* @param mixed $value
*/
public function addExtraValue(string $name, $value): bool
{
$extra = $this->getExtra();
if ($extra === null) {
$extra = [];
}
$extra[$name] = $value;
return $this->setExtra($extra);
}
public function removeExtraValue(string $name): bool
{
$extra = $this->getExtra();
if ($extra === null || !\array_key_exists($name, $extra)) {
return false;
}
unset($extra[$name]);
return $this->setExtra($extra);
}
public function getEnvironmentId(): ?int
{
return $this->getDataItem('environment_id');
}
public function setEnvironmentId(?int $value): bool
{
return $this->setDataItem('environment_id', $value);
}
public function getSource(): ?int
{
return $this->getDataItem('source');
}
/**
* @throws InvalidArgumentException
*/
public function setSource(?int $value): bool
{
if (!\in_array($value, $this->allowedSources, true)) {
throw new InvalidArgumentException(
'Column "source" must be one of: ' . \join(', ', $this->allowedSources) . '.'
);
}
return $this->setDataItem('source', $value);
}
/**
* @throws InvalidArgumentException|RuntimeException
*/
public function getErrorsTotal(): ?int
{
if ($this->getDataItem('errors_total') === null &&
!\in_array($this->getStatus(), [self::STATUS_PENDING, self::STATUS_RUNNING], true)) {
/** @var BuildStore $store */
$store = $this->storeRegistry->get('Build');
$this->setErrorsTotal($store->getErrorsCount($this->getId()));
$store->save($this);
}
return $this->getDataItem('errors_total');
}
public function setErrorsTotal(int $value): bool
{
return $this->setDataItem('errors_total', $value);
}
/**
* @throws Exception
*/
public function getErrorsTotalPrevious(): ?int
{
if ($this->getDataItem('errors_total_previous') === null) {
/** @var BuildStore $store */
$store = $this->storeRegistry->get('Build');
$trend = $store->getBuildErrorsTrend($this->getId(), $this->getProjectId(), $this->getBranch());
if (isset($trend[0])) {
$this->setErrorsTotalPrevious((int)$trend[0]['count']);
$store->save($this);
}
}
return $this->getDataItem('errors_total_previous');
}
public function setErrorsTotalPrevious(int $value): bool
{
return $this->setDataItem('errors_total_previous', $value);
}
/**
* @throws InvalidArgumentException|RuntimeException
*/
public function getTestCoverage(): ?string
{
if ($this->getDataItem('test_coverage') === null &&
!\in_array($this->getStatus(), [self::STATUS_PENDING, self::STATUS_RUNNING], true)) {
/** @var BuildStore $store */
$store = $this->storeRegistry->get('Build');
$this->setTestCoverage($store->getTestCoverage($this->getId()));
$store->save($this);
}
return $this->getDataItem('test_coverage');
}
public function setTestCoverage(string $value): bool
{
return $this->setDataItem('test_coverage', $value);
}
/**
* @throws Exception
*/
public function getTestCoveragePrevious(): ?string
{
if ($this->getDataItem('test_coverage_previous') === null) {
/** @var BuildStore $store */
$store = $this->storeRegistry->get('Build');
$trend = $store->getBuildTestCoverageTrend($this->getId(), $this->getProjectId(), $this->getBranch());
if (!empty($trend[0]) && !empty($trend[0]['coverage'])) {
$coverage = \json_decode($trend[0]['coverage'], true);
if (isset($coverage['lines'])) {
$this->setTestCoveragePrevious($coverage['lines']);
$store->save($this);
}
}
}
return $this->getDataItem('test_coverage_previous');
}
public function setTestCoveragePrevious(string $value): bool
{
return $this->setDataItem('test_coverage_previous', $value);
}
/**
* @throws InvalidArgumentException|RuntimeException
*/
public function getErrorsNew(): ?int
{
if ($this->getDataItem('errors_new') === null) {
/** @var BuildStore $errorStore */
$store = $this->storeRegistry->get('Build');
$this->setErrorsNew(
(int)$store->getNewErrorsCount($this->getId())
);
$store->save($this);
}
return $this->getDataItem('errors_new');
}
public function setErrorsNew(int $value): bool
{
return $this->setDataItem('errors_new', $value);
}
public function isDebug(): bool
{
return (\defined('DEBUG_MODE') && DEBUG_MODE) || $this->getExtra('debug');
}
}