Completed
Push — master ( 7847da...67b8c1 )
by Robbie
02:20
created

CIPassingCheck::getOptions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 11
rs 10
c 0
b 0
f 0
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
            $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
     * Return Guzzle options that are specific to Travis CI, if available
69
     *
70
     * @return array
71
     */
72
    protected function getOptions()
73
    {
74
        $options = [];
75
        if (defined('TRAVIS_CI_TOKEN')) {
76
            // Note: if you've defined this constant in _ss_environment.php then it will not be available
77
            // when running this as a Composer plugin
78
            $options['headers'] = [
79
                'Authorization' => 'token ' . TRAVIS_CI_TOKEN,
0 ignored issues
show
Bug introduced by
The constant SilverStripe\ModuleRatings\Check\TRAVIS_CI_TOKEN was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
80
            ];
81
        }
82
        return $options;
83
    }
84
85
    /**
86
     * Uses the CircleCI API to check whether the latest CI build passed
87
     *
88
     * @param string $slug
89
     * @return bool
90
     */
91
    protected function checkCircleCiBuild($slug)
92
    {
93
        try {
94
            $result = $this->getRequestClient()
95
                ->get('https://circleci.com/api/v1.1/project/github/' . $slug, [
96
                    'headers' => ['Accept' => 'application/json'],
97
                ])
98
                ->getBody();
99
        } catch (Exception $ex) {
100
            $result = '';
101
        }
102
        $response = json_decode($result, true);
103
104
        // Fetch failure
105
        if (!$response) {
106
            return false;
107
        }
108
109
        // Not set up (404), e.g. {"message": "Project not found"}
0 ignored issues
show
Unused Code Comprehensibility introduced by
40% of this comment could be valid code. Did you maybe forget this after debugging?

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.

Loading history...
110
        if (isset($response['message'])) {
111
            return false;
112
        }
113
114
        // Latest build passing?
115
        if (isset($response[0]['failed']) && (bool) $response[0]['failed'] === false) {
116
            return true;
117
        }
118
        return false;
119
    }
120
}
121