Completed
Push — master ( 6054f8...3b037a )
by Matthias
24:24
created

All::execute()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 32
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 32
rs 8.8571
cc 2
eloc 16
nc 2
nop 0
1
<?php
2
3
namespace Cauditor\Runners;
4
5
use Cauditor\Analyzers\AnalyzerInterface;
6
use Cauditor\Api;
7
use Cauditor\Config;
8
use Cauditor\Exception;
9
use MatthiasMullie\CI\Factory as CIFactory;
10
11
/**
12
 * @author Matthias Mullie <[email protected]>
13
 * @copyright Copyright (c) 2016, Matthias Mullie. All rights reserved.
14
 * @license LICENSE MIT
15
 */
16
class All extends Current implements RunnerInterface
17
{
18
    /**
19
     * @var string
20
     */
21
    protected $slug;
22
23
    /**
24
     * @var string
25
     */
26
    protected $branch;
27
28
    /**
29
     * @param Config $config
30
     * @param Api $api
31
     * @param AnalyzerInterface $analyzer
32
     */
33
    public function __construct(Config $config, Api $api, AnalyzerInterface $analyzer)
34
    {
35
        parent::__construct($config, $api, $analyzer);
36
37
        // figure out current repo & branch
38
        $this->slug = $this->getRepo();
39
        $this->branch = $this->getDefaultBranch();
40
    }
41
42
    /**
43
     * @throws Exception
44
     */
45
    public function execute()
46
    {
47
        // find build folder
48
        $path = $this->config['path'];
49
        $build = $path.DIRECTORY_SEPARATOR.$this->config['build_path'];
50
51
        // don't want to mess with current repo; copy it to build folder instead
52
        exec("rm -rf $build/repo && mkdir -p $build/repo && cp -r $path/.git $build/repo/.git");
53
        chdir("$build/repo");
54
55
        // checkout default branch & get list of all commits
56
        exec("git checkout {$this->branch} && git reset --hard && git pull");
57
        exec("git log --pretty=format:'%H'", $commits);
58
59
        // compare with already imported commits & figure out which are missing
60
        $imported = $this->getImportedCommits($this->slug, $this->branch);
61
        $missing = array_diff($commits, $imported);
62
63
        // now analyze all missing commits
64
        foreach ($missing as $commit) {
65
            exec("git checkout $commit && git reset --hard");
66
67
            $config = new Config(getcwd(), getcwd().DIRECTORY_SEPARATOR.'.cauditor.yml');
68
            $this->analyzer->setConfig($config);
69
70
            parent::execute();
71
        }
72
73
        // cleanup
74
        chdir('../..');
75
        exec("rm -rf $build/repo");
76
    }
77
78
    /**
79
     * @return string
80
     */
81
    protected function getRepo()
82
    {
83
        $factory = new CIFactory();
84
        $ci = $factory->getCurrent();
85
86
        return $ci->getSlug();
87
    }
88
89
    /**
90
     * @return string
91
     */
92
    protected function getDefaultBranch()
93
    {
94
        $config = shell_exec("cat .git/config");
95
        preg_match('/\[branch "(.+)"\]/', $config, $match);
96
97
        return isset($match[1]) ? $match[1] : 'master';
98
    }
99
100
    /**
101
     * @param string $slug
102
     * @param string $branch
103
     * @return string[]
104
     * @throws Exception
105
     */
106
    protected function getImportedCommits($slug, $branch)
107
    {
108
        $imported = $this->api->get("/api/v1/$slug/$branch");
109
        if ($imported === false) {
110
            throw new Exception('Failed to reach API.');
111
        }
112
113
        return json_decode($imported);
114
    }
115
116
    /**
117
     * @return array
118
     */
119
    protected function getEnvironment()
120
    {
121
        $data = parent::getEnvironment();
122
123
        // override branch: ci-sniffer's data is unreliable here because we're
124
        // not really on the branch we're importing, but on detached commits
125
        $data['branch'] = $this->branch;
126
127
        return $data;
128
    }
129
}
130