1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\ModuleRatings\Check; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use SilverStripe\ModuleRatings\Check; |
7
|
|
|
|
8
|
|
|
class CIPassingCheck extends Check |
9
|
|
|
{ |
10
|
|
|
public function getKey() |
11
|
|
|
{ |
12
|
|
|
return 'ci_passing'; |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
public function getDescription() |
16
|
|
|
{ |
17
|
|
|
return 'Has Travis CI or CircleCI configured and the last build passed successfully (requires slug)'; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function run() |
21
|
|
|
{ |
22
|
|
|
// This check requires a repository slug to be provided |
23
|
|
|
$slug = $this->getSuite()->getRepositorySlug(); |
24
|
|
|
if (!$slug) { |
25
|
|
|
return; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
$result = $this->checkTravisBuild($slug) || $this->checkCircleCiBuild($slug); |
29
|
|
|
$this->setSuccessful((bool) $result); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Uses the Travis API to check whether the latest CI build passed |
34
|
|
|
* |
35
|
|
|
* @param string $slug |
36
|
|
|
* @return bool |
37
|
|
|
*/ |
38
|
|
|
protected function checkTravisBuild($slug) |
39
|
|
|
{ |
40
|
|
|
try { |
41
|
|
|
$result = $this->getRequestClient() |
42
|
|
|
->get('https://api.travis-ci.org/repositories/' . $slug . '.json') |
43
|
|
|
->getBody(); |
44
|
|
|
} catch (Exception $ex) { |
45
|
|
|
$result = ''; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$response = json_decode($result, true); |
49
|
|
|
|
50
|
|
|
// Fetch failure |
51
|
|
|
if (!$response) { |
52
|
|
|
return false; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
// Not set up (404) |
56
|
|
|
if (isset($response['file']) && $response['file'] === 'not found') { |
57
|
|
|
return false; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// Passing? |
61
|
|
|
if (isset($response['last_build_result']) && (int) $response['last_build_result'] === 0) { |
62
|
|
|
return true; |
63
|
|
|
} |
64
|
|
|
return false; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Uses the CircleCI API to check whether the latest CI build passed |
69
|
|
|
* |
70
|
|
|
* @param string $slug |
71
|
|
|
* @return bool |
72
|
|
|
*/ |
73
|
|
|
protected function checkCircleCiBuild($slug) |
74
|
|
|
{ |
75
|
|
|
try { |
76
|
|
|
$result = $this->getRequestClient() |
77
|
|
|
->get('https://circleci.com/api/v1.1/project/github/' . $slug, [ |
78
|
|
|
'headers' => ['Accept' => 'application/json'], |
79
|
|
|
]) |
80
|
|
|
->getBody(); |
81
|
|
|
} catch (Exception $ex) { |
82
|
|
|
$result = ''; |
83
|
|
|
} |
84
|
|
|
$response = json_decode($result, true); |
85
|
|
|
|
86
|
|
|
// Fetch failure |
87
|
|
|
if (!$response) { |
88
|
|
|
return false; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
// Not set up (404), e.g. {"message": "Project not found"} |
|
|
|
|
92
|
|
|
if (isset($response['message'])) { |
93
|
|
|
return false; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
// Latest build passing? |
97
|
|
|
if (isset($response[0]['failed']) && (bool) $response[0]['failed'] === false) { |
98
|
|
|
return true; |
99
|
|
|
} |
100
|
|
|
return false; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.