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', $this->getOptions()) |
||
43 | ->getBody(); |
||
44 | } catch (Exception $ex) { |
||
45 | if ($logger = $this->getSuite()->getLogger()) { |
||
46 | $logger->debug($ex->getMessage()); |
||
47 | } |
||
48 | $result = ''; |
||
49 | } |
||
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['file']) && $response['file'] === 'not found') { |
||
60 | return false; |
||
61 | } |
||
62 | |||
63 | // Passing? |
||
64 | if (isset($response['last_build_result']) && (int) $response['last_build_result'] === 0) { |
||
65 | return true; |
||
66 | } |
||
67 | return false; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Return Guzzle options that are specific to Travis CI, if available |
||
72 | * |
||
73 | * @return array |
||
74 | */ |
||
75 | protected function getOptions() |
||
76 | { |
||
77 | $options = []; |
||
78 | if (defined('TRAVIS_CI_TOKEN')) { |
||
79 | // Note: if you've defined this constant in _ss_environment.php then it will not be available |
||
80 | // when running this as a Composer plugin |
||
81 | $options['headers'] = [ |
||
82 | 'Authorization' => 'token ' . TRAVIS_CI_TOKEN, |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
83 | ]; |
||
84 | } |
||
85 | return $options; |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * Uses the CircleCI API to check whether the latest CI build passed |
||
90 | * |
||
91 | * @param string $slug |
||
92 | * @return bool |
||
93 | */ |
||
94 | protected function checkCircleCiBuild($slug) |
||
95 | { |
||
96 | try { |
||
97 | $result = $this->getRequestClient() |
||
98 | ->get('https://circleci.com/api/v1.1/project/github/' . $slug, [ |
||
99 | 'headers' => ['Accept' => 'application/json'], |
||
100 | ]) |
||
101 | ->getBody(); |
||
102 | } catch (Exception $ex) { |
||
103 | if ($logger = $this->getSuite()->getLogger()) { |
||
104 | $logger->debug($ex->getMessage()); |
||
105 | } |
||
106 | $result = ''; |
||
107 | } |
||
108 | $response = json_decode($result, true); |
||
109 | |||
110 | // Fetch failure |
||
111 | if (!$response) { |
||
112 | return false; |
||
113 | } |
||
114 | |||
115 | // Not set up (404), e.g. {"message": "Project not found"} |
||
116 | if (isset($response['message'])) { |
||
117 | return false; |
||
118 | } |
||
119 | |||
120 | // Latest build passing? |
||
121 | if (isset($response[0]['failed']) && (bool) $response[0]['failed'] === false) { |
||
122 | return true; |
||
123 | } |
||
124 | return false; |
||
125 | } |
||
126 | } |
||
127 |