Completed
Push — master ( 07aa67...ce71ee )
by Michael
03:29
created

GitChangeInspector::getGitLogFromDeployment()   A

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
15
    public function __construct($repoDirectory)
16
    {
17
        $this->repoDirectory = $repoDirectory;
18
19
        // folder exists
20
21
        // check is a git repo
22
    }
23
24
    public function getChangesFromDeployment(Deployment $deployment)
25
    {
26
        $gitLog = $this->getGitLogFromDeployment($deployment);
27
28
        return $this->convertGitLogIntoChanges($gitLog);
29
    }
30
31
    public function getGitLogFromDeployment(Deployment $deployment)
32
    {
33
        $builder = new ProcessBuilder();
34
        $builder->setPrefix('git log');
35
36
        if (!is_null($deployment->getPreviousVersion())) {
37
            if (!is_null($deployment->getCurrentVersion())) {
38
                $revisionRange = $deployment->getPreviousVersion() . '..' . $deployment->getCurrentVersion();
39
                $builder->add($revisionRange);
40
            }
41
        }
42
43
        $process = $builder->getProcess();
44
        $process->run();
45
46
        if (!$process->isSuccessful()) {
47
            throw new ProcessFailedException($process);
48
        }
49
50
        return $process->getOutput();
51
    }
52
53
    public function convertGitLogIntoChanges($gitLog)
54
    {
55
        $changes = [];
56
57
        $gitLog = explode(\PHP_EOL, $gitLog);
58
59
        $change = new Change();
60
        foreach ($gitLog as $line) {
61
            if(strpos($line, 'commit') === 0) {
62
                if (null !== $change->reference) {
63
                    $change->fullDescription = trim($change->fullDescription);
64
                    $changes[] = $change;
65
                    $change = new Change();
66
                }
67
                $change->reference = trim(substr($line, 6));
68
            } elseif (strpos($line, 'Author:') === 0) {
69
                $change->author = trim(substr($line, 7));
70
            } elseif (preg_match('/^\s/', $line)) {
71
                $line = trim($line);
72
                if ('' != $line) {
73
                    if (null == $change->summary) {
74
                        $change->summary = $line;
75
                    }
76
                    $change->fullDescription .= $line . PHP_EOL;
77
                }
78
            }
79
        }
80
81
        $change->fullDescription = trim($change->fullDescription);
82
        $changes[] = $change;
83
84
        return $changes;
85
    }
86
}
87