AddGitInformation.php
1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
namespace Facade\Ignition\Middleware;
use Facade\FlareClient\Report;
use ReflectionClass;
use Symfony\Component\Process\Exception\RuntimeException;
use Symfony\Component\Process\Process;
class AddGitInformation
{
    public function handle(Report $report, $next)
    {
        try {
            $report->group('git', [
                'hash' => $this->hash(),
                'message' => $this->message(),
                'tag' => $this->tag(),
                'remote' => $this->remote(),
            ]);
        } catch (RuntimeException $exception) {
        }
        return $next($report);
    }
    public function hash(): ?string
    {
        return $this->command("git log --pretty=format:'%H' -n 1");
    }
    public function message(): ?string
    {
        return $this->command("git log --pretty=format:'%s' -n 1");
    }
    public function tag(): ?string
    {
        return $this->command('git describe --tags --abbrev=0');
    }
    public function remote(): ?string
    {
        return $this->command('git config --get remote.origin.url');
    }
    protected function command($command)
    {
        $process = (new ReflectionClass(Process::class))->hasMethod('fromShellCommandline')
            ? Process::fromShellCommandline($command, base_path())
            : new Process($command, base_path());
        $process->run();
        return trim($process->getOutput());
    }
}