-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathBuildStore.php
More file actions
556 lines (460 loc) · 18.2 KB
/
Copy pathBuildStore.php
File metadata and controls
556 lines (460 loc) · 18.2 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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
<?php
declare(strict_types=1);
namespace PHPCensor\Store;
use Exception;
use PDO;
use PHPCensor\Exception\HttpException;
use PHPCensor\Model\Build;
use PHPCensor\Model\BuildMeta;
use PHPCensor\Model\Project;
use PHPCensor\Store;
/**
* @package PHP Censor
* @subpackage Application
*
* @author Dan Cryer <dan@block8.co.uk>
* @author Dmitry Khomutov <poisoncorpsee@gmail.com>
*/
class BuildStore extends Store
{
protected string $tableName = 'builds';
protected string $modelName = Build::class;
/**
* Get multiple Build by ProjectId.
*
* @throws HttpException
*/
public function getByProjectId(int $projectId, int $limit = 1000, string $useConnection = 'read'): array
{
if (\is_null($projectId)) {
throw new HttpException('Value passed to ' . __FUNCTION__ . ' cannot be null.');
}
$query = 'SELECT * FROM {{' . $this->tableName . '}} WHERE {{project_id}} = :project_id LIMIT :limit';
$stmt = $this->databaseManager->getConnection($useConnection)->prepare($query);
$stmt->bindValue(':project_id', $projectId);
$stmt->bindValue(':limit', (int)$limit, PDO::PARAM_INT);
if ($stmt->execute()) {
$res = $stmt->fetchAll(PDO::FETCH_ASSOC);
$map = fn ($item) => new Build($this->storeRegistry, $item);
$rtn = \array_map($map, $res);
$count = \count($rtn);
return ['items' => $rtn, 'count' => $count];
} else {
return ['items' => [], 'count' => 0];
}
}
/**
* Get multiple Build by Status.
*
* @throws HttpException
*/
public function getByStatus(int $status, int $limit = 1000, string $useConnection = 'read'): array
{
if (\is_null($status)) {
throw new HttpException('Value passed to ' . __FUNCTION__ . ' cannot be null.');
}
$query = 'SELECT * FROM {{' . $this->tableName . '}} WHERE {{status}} = :status ORDER BY {{create_date}} ASC LIMIT :limit';
$stmt = $this->databaseManager->getConnection($useConnection)->prepare($query);
$stmt->bindValue(':status', $status);
$stmt->bindValue(':limit', (int)$limit, PDO::PARAM_INT);
if ($stmt->execute()) {
$res = $stmt->fetchAll(PDO::FETCH_ASSOC);
$map = fn ($item) => new Build($this->storeRegistry, $item);
$rtn = \array_map($map, $res);
$count = \count($rtn);
return ['items' => $rtn, 'count' => $count];
} else {
return ['items' => [], 'count' => 0];
}
}
public function getBuilds(int $limit = 5, int $offset = 0): array
{
$query = 'SELECT * FROM {{' . $this->tableName . '}} ORDER BY {{id}} DESC LIMIT :limit OFFSET :offset';
$stmt = $this->databaseManager->getConnection('read')->prepare($query);
$stmt->bindValue(':limit', $limit, PDO::PARAM_INT);
$stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
if ($stmt->execute()) {
$res = $stmt->fetchAll(PDO::FETCH_ASSOC);
$map = fn ($item) => new Build($this->storeRegistry, $item);
return \array_map($map, $res);
} else {
return [];
}
}
/**
* @throws Exception
*/
public function getLatestBuildByProjectAndBranch(int $projectId, string $branch): ?Build
{
$query = 'SELECT * FROM {{' . $this->tableName . '}} WHERE {{project_id}} = :project_id AND {{branch}} = :branch ORDER BY {{id}} DESC';
$stmt = $this->databaseManager->getConnection('read')->prepare($query);
$stmt->bindValue(':project_id', $projectId);
$stmt->bindValue(':branch', $branch);
if ($stmt->execute() && $data = $stmt->fetch(PDO::FETCH_ASSOC)) {
return new Build($this->storeRegistry, $data);
}
return null;
}
/**
* Return an array of the latest builds for a given project.
*
* @throws Exception
*/
public function getLatestBuilds(?int $projectId = null, int $limit = 5): array
{
if (!\is_null($projectId)) {
$query = 'SELECT * FROM {{' . $this->tableName . '}} WHERE {{project_id}} = :pid ORDER BY {{id}} DESC LIMIT :limit';
} else {
$query = 'SELECT * FROM {{' . $this->tableName . '}} ORDER BY {{id}} DESC LIMIT :limit';
}
$stmt = $this->databaseManager->getConnection('read')->prepare($query);
if (!\is_null($projectId)) {
$stmt->bindValue(':pid', $projectId);
}
$stmt->bindValue(':limit', $limit, PDO::PARAM_INT);
if ($stmt->execute()) {
$res = $stmt->fetchAll(PDO::FETCH_ASSOC);
$map = fn ($item) => new Build($this->storeRegistry, $item);
return \array_map($map, $res);
} else {
return [];
}
}
/**
* Return the latest build for a specific project, of a specific build status.
*/
public function getLastBuildByStatus(?int $projectId = null, int $status = Build::STATUS_SUCCESS): ?Build
{
$query = 'SELECT * FROM {{' . $this->tableName . '}} WHERE {{project_id}} = :pid AND {{status}} = :status ORDER BY {{id}} DESC LIMIT 1';
$stmt = $this->databaseManager->getConnection('read')->prepare($query);
$stmt->bindValue(':pid', $projectId);
$stmt->bindValue(':status', $status);
if ($stmt->execute()) {
if ($data = $stmt->fetch(PDO::FETCH_ASSOC)) {
return new Build($this->storeRegistry, $data);
}
}
return null;
}
/**
* Return an array of the latest builds for all projects.
*/
public function getAllProjectsLatestBuilds(int $limitByProject = 5, int $limitAll = 10): array
{
// don't fetch log field - contain many data
$query = '
SELECT
{{id}},
{{project_id}},
{{commit_id}},
{{status}},
{{branch}},
{{create_date}},
{{start_date}},
{{finish_date}},
{{committer_email}},
{{commit_message}},
{{extra}},
{{environment_id}},
{{tag}}
FROM {{' . $this->tableName . '}}
ORDER BY {{id}} DESC
LIMIT 10000
';
$stmt = $this->databaseManager->getConnection('read')->prepare($query);
if ($stmt->execute()) {
$res = $stmt->fetchAll(PDO::FETCH_ASSOC);
$projects = [];
$latest = [];
foreach ($res as $item) {
$projectId = $item['project_id'];
$environment = $item['environment_id'];
if (empty($projects[$projectId])) {
$projects[$projectId] = [];
}
if (empty($projects[$projectId][$environment])) {
$projects[$projectId][$environment] = [
'latest' => [],
'success' => null,
'failed' => null,
];
}
$build = null;
if (\count($projects[$projectId][$environment]['latest']) < $limitByProject) {
$build = new Build($this->storeRegistry, $item);
$projects[$projectId][$environment]['latest'][] = $build;
}
if (\count($latest) < $limitAll) {
if (\is_null($build)) {
$build = new Build($this->storeRegistry, $item);
}
$latest[] = $build;
}
if (empty($projects[$projectId][$environment]['success']) && Build::STATUS_SUCCESS === $item['status']) {
if (\is_null($build)) {
$build = new Build($this->storeRegistry, $item);
}
$projects[$projectId][$environment]['success'] = $build;
}
if (empty($projects[$projectId][$environment]['failed']) && Build::STATUS_FAILED === $item['status']) {
if (\is_null($build)) {
$build = new Build($this->storeRegistry, $item);
}
$projects[$projectId][$environment]['failed'] = $build;
}
}
foreach ($projects as $idx => $project) {
$projects[$idx] = \array_filter($project, fn ($val) => $val['latest'][0]->getStatus() !== Build::STATUS_SUCCESS);
}
$projects = \array_filter($projects);
return ['projects' => $projects, 'latest' => $latest];
} else {
return [];
}
}
/**
* Return an array of builds for a given project and commit ID.
*/
public function getByProjectAndCommit(int $projectId, string $commitId): array
{
$query = 'SELECT * FROM {{' . $this->tableName . '}} WHERE {{project_id}} = :project_id AND {{commit_id}} = :commit_id';
$stmt = $this->databaseManager->getConnection('read')->prepare($query);
$stmt->bindValue(':project_id', $projectId);
$stmt->bindValue(':commit_id', $commitId);
if ($stmt->execute()) {
$res = $stmt->fetchAll(PDO::FETCH_ASSOC);
$map = fn ($item) => new Build($this->storeRegistry, $item);
$rtn = \array_map($map, $res);
return ['items' => $rtn, 'count' => \count($rtn)];
} else {
return ['items' => [], 'count' => 0];
}
}
/**
* Returns all registered branches for project
*
* @throws Exception
*/
public function getBuildBranches(int $projectId): array
{
$query = 'SELECT DISTINCT {{branch}} FROM {{' . $this->tableName . '}} WHERE {{project_id}} = :project_id';
$stmt = $this->databaseManager->getConnection('read')->prepare($query);
$stmt->bindValue(':project_id', $projectId);
if ($stmt->execute()) {
return $stmt->fetchAll(PDO::FETCH_COLUMN);
} else {
return [];
}
}
/**
* Return build metadata by key, project and optionally build id.
*/
public function getMeta(string $key, ?int $projectId, ?int $buildId = null, ?string $branch = null, int $numResults = 1): ?array
{
$query = 'SELECT bm.build_id, bm.meta_key, bm.meta_value
FROM {{build_metas}} AS {{bm}}
LEFT JOIN {{' . $this->tableName . '}} AS {{b}} ON b.id = bm.build_id
WHERE bm.meta_key = :key AND b.project_id = :projectId';
// If we're getting comparative meta data, include previous builds
// otherwise just include the specified build ID:
if ($numResults > 1) {
$query .= ' AND bm.build_id <= :buildId ';
} else {
$query .= ' AND bm.build_id = :buildId ';
}
// Include specific branch information if required:
if (!\is_null($branch)) {
$query .= ' AND b.branch = :branch ';
}
$query .= ' ORDER BY bm.id DESC LIMIT :numResults';
$stmt = $this->databaseManager->getConnection('read')->prepare($query);
$stmt->bindValue(':key', $key, PDO::PARAM_STR);
$stmt->bindValue(':projectId', $projectId, PDO::PARAM_INT);
$stmt->bindValue(':buildId', $buildId, PDO::PARAM_INT);
$stmt->bindValue(':numResults', $numResults, PDO::PARAM_INT);
if (!\is_null($branch)) {
$stmt->bindValue(':branch', $branch, PDO::PARAM_STR);
}
if ($stmt->execute()) {
$rtn = $stmt->fetchAll(PDO::FETCH_ASSOC);
/** @var BuildErrorStore $errorStore */
$errorStore = $this->storeRegistry->get('BuildError');
$rtn = \array_reverse($rtn);
$rtn = \array_map(function ($item) use ($key, $errorStore, $buildId) {
$item['meta_value'] = \json_decode($item['meta_value'], true);
if ('plugin-summary' === $key) {
foreach ($item['meta_value'] as $stage => $stageData) {
foreach ($stageData as $plugin => $pluginData) {
$item['meta_value'][$stage][$plugin]['errors'] = $errorStore->getErrorTotalForBuild(
$buildId,
$plugin
);
}
}
}
return $item;
}, $rtn);
if (!\count($rtn)) {
return null;
} else {
return $rtn;
}
} else {
return null;
}
}
/**
* Set a metadata value for a given project and build ID.
*/
public function setMeta(int $buildId, string $key, string $value): void
{
/** @var BuildMetaStore $store */
$store = $this->storeRegistry->get('BuildMeta');
$meta = $store->getByKey($buildId, $key);
if (\is_null($meta)) {
$meta = new BuildMeta($this->storeRegistry);
$meta->setBuildId($buildId);
$meta->setMetaKey($key);
}
$meta->setMetaValue($value);
$store->save($meta);
}
public function deleteAllByProject(int $projectId): int
{
$q = $this->databaseManager->getConnection('write')
->prepare(
'DELETE FROM {{' . $this->tableName . '}} WHERE {{project_id}} = :project_id'
);
$q->bindValue(':project_id', $projectId);
$q->execute();
return $q->rowCount();
}
/**
* @throws HttpException
*/
public function getOldByProject(int $projectId, int $keep = 100): array
{
if (\is_null($projectId)) {
throw new HttpException('Value passed to ' . __FUNCTION__ . ' cannot be null.');
}
$query = 'SELECT * FROM {{' . $this->tableName . '}} WHERE {{project_id}} = :project_id ORDER BY {{create_date}} DESC LIMIT 1000000 OFFSET :keep';
$stmt = $this->databaseManager->getConnection('read')->prepare($query);
$stmt->bindValue(':project_id', $projectId);
$stmt->bindValue(':keep', (int)$keep, PDO::PARAM_INT);
if ($stmt->execute()) {
$res = $stmt->fetchAll(PDO::FETCH_ASSOC);
$map = fn ($item) => new Build($this->storeRegistry, $item);
$rtn = \array_map($map, $res);
$count = \count($rtn);
return ['items' => $rtn, 'count' => $count];
}
return ['items' => [], 'count' => 0];
}
/**
* @throws Exception
*/
public function getNewErrorsCount(int $buildId): int
{
$query = 'SELECT COUNT(*) AS {{total}} FROM {{build_errors}} WHERE {{build_id}} = :build_id AND {{is_new}} = true';
$stmt = $this->databaseManager->getConnection('read')->prepare($query);
$stmt->bindValue(':build_id', $buildId);
if ($stmt->execute()) {
$res = $stmt->fetch(PDO::FETCH_ASSOC);
return (int)$res['total'];
}
return 0;
}
/**
* @throws Exception
*/
public function getErrorsCount(int $buildId): int
{
$query = 'SELECT COUNT(*) AS {{total}} FROM {{build_errors}} WHERE {{build_id}} = :build_id';
$stmt = $this->databaseManager->getConnection('read')->prepare($query);
if ($stmt) {
$stmt->bindValue(':build_id', $buildId);
if ($stmt->execute()) {
$res = $stmt->fetch(PDO::FETCH_ASSOC);
return (int)$res['total'];
}
}
return 0;
}
/**
* @throws Exception
*/
public function getTestCoverage(int $buildId): string
{
$coverage = '0.00';
$type = 'lines';
try {
$build = $this->getById($buildId);
if (isset($build) && $build instanceof Build) {
$coverageMeta = $this->getMeta(
'php_unit-coverage',
$build->getProjectId(),
$build->getId(),
$build->getBranch()
);
if ($coverageMeta && isset($coverageMeta[0]['meta_value'][$type])) {
$coverage = $coverageMeta[0]['meta_value'][$type];
}
}
} catch (\Throwable $e) {
}
return $coverage;
}
/**
* @throws Exception
*/
public function getBuildErrorsTrend(int $buildId, int $projectId, string $branch): array
{
$query = '
SELECT b.id AS {{build_id}}, count(be.id) AS {{count}} FROM {{' . $this->tableName . '}} AS b
LEFT JOIN {{build_errors}} AS be
ON b.id = be.build_id
WHERE b.project_id = :project_id
AND b.branch = :branch
AND b.id < :build_id
AND b.status NOT IN (' . Build::STATUS_PENDING . ', ' . Build::STATUS_RUNNING . ')
GROUP BY b.id
ORDER BY b.id DESC
LIMIT 1';
$stmt = $this->databaseManager->getConnection('read')->prepare($query);
if ($stmt) {
$stmt->bindValue(':build_id', $buildId, PDO::PARAM_INT);
$stmt->bindValue(':project_id', $projectId, PDO::PARAM_INT);
$stmt->bindValue(':branch', $branch, PDO::PARAM_STR);
if ($stmt->execute()) {
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
}
return [];
}
/**
* @throws Exception
*/
public function getBuildTestCoverageTrend(int $buildId, int $projectId, string $branch): array
{
$query = '
SELECT b.id AS {{build_id}}, bm.coverage AS {{coverage}} FROM {{' . $this->tableName . '}} AS b
LEFT JOIN (SELECT {{build_id}}, {{meta_value}} AS {{coverage}} FROM {{build_metas}} WHERE {{meta_key}} = \'php_unit-coverage\') AS bm
ON b.id = bm.build_id
WHERE b.project_id = :project_id
AND b.branch = :branch
AND b.id < :build_id
AND b.status NOT IN (' . Build::STATUS_PENDING . ', ' . Build::STATUS_RUNNING . ')
ORDER BY b.id DESC
LIMIT 1';
$stmt = $this->databaseManager->getConnection('read')->prepare($query);
if ($stmt) {
$stmt->bindValue(':build_id', $buildId, PDO::PARAM_INT);
$stmt->bindValue(':project_id', $projectId, PDO::PARAM_INT);
$stmt->bindValue(':branch', $branch, PDO::PARAM_STR);
if ($stmt->execute()) {
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
}
return [];
}
}