Passed
Pull Request — master (#9)
by Robbie
01:51
created

CIPassingCheck::getDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\ModuleRatings\Check;
4
5
use SilverStripe\ModuleRatings\Check;
6
7
class CIPassingCheck extends Check
8
{
9
    /**
10
     * HTTP client request options
11
     *
12
     * @var array
13
     */
14
    protected $requestOptions = [
15
        'headers' => ['Accept' => 'application/json'],
16
    ];
17
18
    public function getKey()
19
    {
20
        return 'ci_passing';
21
    }
22
23
    public function getDescription()
24
    {
25
        return 'Has Travis CI or CircleCI configured and the last build passed successfully (requires slug)';
26
    }
27
28
    public function run()
29
    {
30
        // This check requires a repository slug to be provided
31
        $slug = $this->getSuite()->getRepositorySlug();
32
        if (!$slug) {
33
            return;
34
        }
35
36
        $result = $this->checkTravisBuild($slug) || $this->checkCircleCiBuild($slug);
37
        $this->setSuccessful((bool) $result);
38
    }
39
40
    /**
41
     * Uses the Travis API to check whether the latest CI build passed
42
     *
43
     * @param string $slug
44
     * @return bool
45
     */
46
    protected function checkTravisBuild($slug)
47
    {
48
        $result = $this->getRequestClient()
49
            ->get('https://api.travis-ci.org/repositories/' . $slug . '.json')
50
            ->getBody();
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
     * Uses the CircleCI API to check whether the latest CI build passed
72
     *
73
     * @param string $slug
74
     * @return bool
75
     */
76
    protected function checkCircleCiBuild($slug)
77
    {
78
        $result = $this->getRequestClient()
79
            ->get('https://circleci.com/api/v1.1/project/github/' . $slug, $this->requestOptions)
80
            ->getBody();
81
        $response = json_decode($result, true);
82
83
        // Fetch failure
84
        if (!$response) {
85
            return false;
86
        }
87
88
        // 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...
89
        if (isset($response['message'])) {
90
            return false;
91
        }
92
93
        // Latest build passing?
94
        if (isset($response[0]['failed']) && (bool) $response[0]['failed'] === false) {
95
            return true;
96
        }
97
        return false;
98
    }
99
}
100