GitChangeInspector::getGitLogFromDeployment()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 21
rs 9.0534
cc 4
eloc 12
nc 6
nop 1
1
<?php
2
3
namespace ParityBit\DeploymentNotifier\ChangeInspectors;
4
5
use ParityBit\DeploymentNotifier\Change;
6
use ParityBit\DeploymentNotifier\Deployment;
7
8
use Symfony\Component\Process\ProcessBuilder;
9
use Symfony\Component\Process\Exception\ProcessFailedException;
10
11
class GitChangeInspector implements ChangeInspector
12
{
13
    protected $repoDirectory;
14
    protected $processBuilder;
15
16
    public function __construct(ProcessBuilder $builder, $repoDirectory)
17
    {
18
        $this->processBuilder = $builder;
19
        $this->repoDirectory = $repoDirectory;
20
21
        // folder exists
22
23
        // check is a git repo
24
    }
25
26
    public function getChangesFromDeployment(Deployment $deployment)
27
    {
28
        $gitLog = $this->getGitLogFromDeployment($deployment);
29
30
        return $this->convertGitLogIntoChanges($gitLog);
31
    }
32
33
    public function getGitLogFromDeployment(Deployment $deployment)
34
    {
35
        $builder = clone $this->processBuilder;
36
        $builder->setPrefix('git log');
37
38
        if (!is_null($deployment->getPreviousVersion())) {
39
            if (!is_null($deployment->getCurrentVersion())) {
40
                $revisionRange = $deployment->getPreviousVersion() . '..' . $deployment->getCurrentVersion();
41
                $builder->add($revisionRange);
42
            }
43
        }
44
45
        $process = $builder->getProcess();
46
        $process->run();
47
48
        if (!$process->isSuccessful()) {
49
            throw new ProcessFailedException($process);
50
        }
51
52
        return $process->getOutput();
53
    }
54
55
    public function convertGitLogIntoChanges($gitLog)
56
    {
57
        $changes = [];
58
59
        $gitLog = explode(\PHP_EOL, $gitLog);
60
61
        $change = new Change();
62
        foreach ($gitLog as $line) {
63
            if(strpos($line, 'commit') === 0) {
64
                if (!is_null($change->reference)) {
65
                    $change->fullDescription = trim($change->fullDescription);
66
                    $changes[] = $change;
67
                    $change = new Change();
68
                }
69
                $change->reference = trim(substr($line, 6));
70
            } elseif (strpos($line, 'Author:') === 0) {
71
                $change->author = trim(substr($line, 7));
72
            } elseif (preg_match('/^\s/', $line)) {
73
                $line = trim($line);
74
                if ('' != $line) {
75
                    if (is_null($change->summary)) {
76
                        $change->summary = $line;
77
                    }
78
                    $change->fullDescription .= $line . PHP_EOL;
79
                }
80
            }
81
        }
82
83
        $change->fullDescription = trim($change->fullDescription);
84
        $changes[] = $change;
85
86
        return $changes;
87
    }
88
}
89