1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\ModuleRatings\Check; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use SilverStripe\ModuleRatings\Check; |
7
|
|
|
|
8
|
|
|
abstract class AbstractCodeCoverageCheck extends Check |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Percent threshold for the coverage to be assessed at a certain level, e.g. good or great |
12
|
|
|
* @var int |
13
|
|
|
*/ |
14
|
|
|
protected $threshold = 0; |
15
|
|
|
|
16
|
|
|
public function getCoverage() |
17
|
|
|
{ |
18
|
|
|
// This check requires a repository slug to be provided |
19
|
|
|
$slug = $this->getSuite()->getRepositorySlug(); |
20
|
|
|
if (!$slug) { |
21
|
|
|
return 0; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
// Priority: codecov.io |
25
|
|
|
$coverage = $this->getCodecovCoverage(); |
26
|
|
|
if ($coverage === false) { |
27
|
|
|
// Fallback: scrutinizer-ci |
28
|
|
|
$coverage = $this->getScrutinizerCoverage(); |
29
|
|
|
} |
30
|
|
|
return $coverage; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Gets test coverage level from Codecov.io, returns false if not found or missing |
35
|
|
|
* |
36
|
|
|
* @return bool|float|int |
37
|
|
|
*/ |
38
|
|
|
public function getCodecovCoverage() |
39
|
|
|
{ |
40
|
|
|
$slug = $this->getSuite()->getRepositorySlug(); |
41
|
|
|
try { |
42
|
|
|
// Note: assume everyone uses the master branch |
43
|
|
|
$result = $this->getRequestClient() |
44
|
|
|
->get('https://codecov.io/api/gh/' . $slug . '/branch/master') |
45
|
|
|
->getBody(); |
46
|
|
|
} catch (Exception $ex) { |
47
|
|
|
if ($logger = $this->getSuite()->getLogger()) { |
48
|
|
|
$logger->debug($ex->getMessage()); |
49
|
|
|
} |
50
|
|
|
$result = ''; |
51
|
|
|
} |
52
|
|
|
$response = json_decode($result, true); |
53
|
|
|
|
54
|
|
|
// Fetch failure |
55
|
|
|
if (!$response) { |
56
|
|
|
return false; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// Not set up (404) |
60
|
|
|
if (isset($response['meta']['status']) && (int) $response['meta']['status'] !== 200) { |
61
|
|
|
return false; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// Get coverage result |
65
|
|
|
if (isset($response['commit']['totals']['c'])) { |
66
|
|
|
return $response['commit']['totals']['c']; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return 0; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Gets test coverage level from Scrutinizer, returns false if not found or missing |
74
|
|
|
* |
75
|
|
|
* @return bool|float|int |
76
|
|
|
*/ |
77
|
|
|
public function getScrutinizerCoverage() |
78
|
|
|
{ |
79
|
|
|
$slug = $this->getSuite()->getRepositorySlug(); |
80
|
|
|
// Note: assume everyone uses the master branch |
81
|
|
|
try { |
82
|
|
|
$result = $this->getRequestClient() |
83
|
|
|
->get('https://scrutinizer-ci.com/api/repositories/g/' . $slug) |
84
|
|
|
->getBody(); |
85
|
|
|
} catch (Exception $ex) { |
86
|
|
|
if ($logger = $this->getSuite()->getLogger()) { |
87
|
|
|
$logger->debug($ex->getMessage()); |
88
|
|
|
} |
89
|
|
|
$result = ''; |
90
|
|
|
} |
91
|
|
|
$response = json_decode($result, true); |
92
|
|
|
|
93
|
|
|
// Fetch failure |
94
|
|
|
if (!$response) { |
95
|
|
|
return false; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
// Not set up (404) |
99
|
|
|
if (!isset($response['applications']['master'])) { |
100
|
|
|
return false; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
// Get coverage result |
104
|
|
|
$metrics = $response['applications']['master']['index']['_embedded']['project']['metric_values']; |
105
|
|
|
if (isset($metrics['scrutinizer.test_coverage'])) { |
106
|
|
|
return $metrics['scrutinizer.test_coverage'] * 100; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return 0; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Get the threshold for measuring code coverage |
114
|
|
|
* |
115
|
|
|
* @return int |
116
|
|
|
*/ |
117
|
|
|
public function getThreshold() |
118
|
|
|
{ |
119
|
|
|
return $this->threshold; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Set the threshold for measuring code coverage |
124
|
|
|
* |
125
|
|
|
* @param int $threshold |
126
|
|
|
* @return $this |
127
|
|
|
*/ |
128
|
|
|
public function setThreshold($threshold) |
129
|
|
|
{ |
130
|
|
|
$this->threshold = $threshold; |
131
|
|
|
return $this; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|