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
|
|
|
$result = $this->getRequestClient() |
43
|
|
|
->get('https://codecov.io/api/gh/' . $slug . '/branches') |
44
|
|
|
->getBody(); |
45
|
|
|
} catch (Exception $ex) { |
46
|
|
|
if ($logger = $this->getSuite()->getLogger()) { |
47
|
|
|
$logger->debug($ex->getMessage()); |
48
|
|
|
} |
49
|
|
|
$result = ''; |
50
|
|
|
} |
51
|
|
|
$response = json_decode($result, true); |
52
|
|
|
|
53
|
|
|
// Fetch failure |
54
|
|
|
if (!$response) { |
55
|
|
|
return false; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// Not set up (404) |
59
|
|
|
if (isset($response['meta']['status']) && (int) $response['meta']['status'] !== 200) { |
60
|
|
|
return false; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$defaultBranch = 'master'; |
64
|
|
|
if (isset($response['repo']['branch'])) { |
65
|
|
|
$defaultBranch = $response['repo']['branch']; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
try { |
69
|
|
|
$result = $this->getRequestClient() |
70
|
|
|
->get('https://codecov.io/api/gh/' . $slug . '/branch/' . $defaultBranch) |
71
|
|
|
->getBody(); |
72
|
|
|
} catch (Exception $ex) { |
73
|
|
|
if ($logger = $this->getSuite()->getLogger()) { |
74
|
|
|
$logger->debug($ex->getMessage()); |
75
|
|
|
} |
76
|
|
|
$result = ''; |
77
|
|
|
} |
78
|
|
|
$response = json_decode($result, true); |
79
|
|
|
|
80
|
|
|
// Fetch failure |
81
|
|
|
if (!$response) { |
82
|
|
|
return false; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// Not set up (404) |
86
|
|
|
if (isset($response['meta']['status']) && (int) $response['meta']['status'] !== 200) { |
87
|
|
|
return false; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
// Get coverage result |
91
|
|
|
if (isset($response['commit']['totals']['c'])) { |
92
|
|
|
return $response['commit']['totals']['c']; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return 0; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Gets test coverage level from Scrutinizer, returns false if not found or missing |
100
|
|
|
* |
101
|
|
|
* @return bool|float|int |
102
|
|
|
*/ |
103
|
|
|
public function getScrutinizerCoverage() |
104
|
|
|
{ |
105
|
|
|
$slug = $this->getSuite()->getRepositorySlug(); |
106
|
|
|
// Note: assume everyone uses the master branch |
107
|
|
|
try { |
108
|
|
|
$result = $this->getRequestClient() |
109
|
|
|
->get('https://scrutinizer-ci.com/api/repositories/g/' . $slug) |
110
|
|
|
->getBody(); |
111
|
|
|
} catch (Exception $ex) { |
112
|
|
|
if ($logger = $this->getSuite()->getLogger()) { |
113
|
|
|
$logger->debug($ex->getMessage()); |
114
|
|
|
} |
115
|
|
|
$result = ''; |
116
|
|
|
} |
117
|
|
|
$response = json_decode($result, true); |
118
|
|
|
|
119
|
|
|
// Fetch failure |
120
|
|
|
if (!$response) { |
121
|
|
|
return false; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$defaultBranch = 'master'; |
125
|
|
|
if (isset($response['default_branch'])) { |
126
|
|
|
$defaultBranch = $response['default_branch']; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
if (!isset($response['applications'][$defaultBranch]['index']['_embedded']['project']['metric_values'])) { |
130
|
|
|
return false; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$metrics = $response['applications'][$defaultBranch]['index']['_embedded']['project']['metric_values']; |
134
|
|
|
if (isset($metrics['scrutinizer.quality'])) { |
135
|
|
|
return $metrics['scrutinizer.quality'] * 100; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return 0; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Get the threshold for measuring code coverage |
143
|
|
|
* |
144
|
|
|
* @return int |
145
|
|
|
*/ |
146
|
|
|
public function getThreshold() |
147
|
|
|
{ |
148
|
|
|
return $this->threshold; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Set the threshold for measuring code coverage |
153
|
|
|
* |
154
|
|
|
* @param int $threshold |
155
|
|
|
* @return $this |
156
|
|
|
*/ |
157
|
|
|
public function setThreshold($threshold) |
158
|
|
|
{ |
159
|
|
|
$this->threshold = $threshold; |
160
|
|
|
return $this; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|