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

CIPassingCheck::getKey()   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
    public function getKey()
10
    {
11
        return 'ci_passing';
12
    }
13
14
    public function getDescription()
15
    {
16
        return 'Has Travis CI or CircleCI configured and the last build passed successfully (requires slug)';
17
    }
18
19
    public function run()
20
    {
21
        // This check requires a repository slug to be provided
22
        $slug = $this->getSuite()->getRepositorySlug();
23
        if (!$slug) {
24
            return;
25
        }
26
27
        $result = $this->checkTravisBuild($slug) || $this->checkCircleCiBuild($slug);
28
        $this->setSuccessful((bool) $result);
29
    }
30
31
    /**
32
     * Uses the Travis API to check whether the latest CI build passed
33
     *
34
     * @param string $slug
35
     * @return bool
36
     */
37
    protected function checkTravisBuild($slug)
38
    {
39
        $result = $this->getRequestClient()
40
            ->get('https://api.travis-ci.org/repositories/' . $slug . '.json')
41
            ->getBody();
42
        $response = json_decode($result, true);
43
44
        // Fetch failure
45
        if (!$response) {
46
            return false;
47
        }
48
49
        // Not set up (404)
50
        if (isset($response['file']) && $response['file'] === 'not found') {
51
            return false;
52
        }
53
54
        // Passing?
55
        if (isset($response['last_build_result']) && (int) $response['last_build_result'] === 0) {
56
            return true;
57
        }
58
        return false;
59
    }
60
61
    /**
62
     * Uses the CircleCI API to check whether the latest CI build passed
63
     *
64
     * @param string $slug
65
     * @return bool
66
     */
67
    protected function checkCircleCiBuild($slug)
68
    {
69
        $result = $this->getRequestClient()
70
            ->get('https://circleci.com/api/v1.1/project/github/' . $slug, [
71
                'headers' => ['Accept' => 'application/json'],
72
            ])
73
            ->getBody();
74
        $response = json_decode($result, true);
75
76
        // Fetch failure
77
        if (!$response) {
78
            return false;
79
        }
80
81
        // 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...
82
        if (isset($response['message'])) {
83
            return false;
84
        }
85
86
        // Latest build passing?
87
        if (isset($response[0]['failed']) && (bool) $response[0]['failed'] === false) {
88
            return true;
89
        }
90
        return false;
91
    }
92
}
93