Github   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
eloc 46
c 2
b 0
f 0
dl 0
loc 104
ccs 0
cts 19
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A getHeadInfo() 0 49 4
1
<?php
2
3
namespace App\Provider;
4
5
use App\Builder;
6
use App\Facades\Log;
7
use App\Facades\Settings;
8
use App\Model\Deployment;
9
use App\Model\Project;
10
use App\Provider\AbstractProvider;
11
use App\Provider\ProviderInterface;
12
use Closure;
13
use Exception;
14
use GuzzleHttp\ClientInterface;
15
use GuzzleHttp\Exception\ClientException;
16
use Psr\Http\Message\StreamInterface;
17
use Ronanchilvers\Foundation\Config;
18
use Ronanchilvers\Utility\Str;
19
use RuntimeException;
20
use Symfony\Component\Process\Exception\ProcessFailedException;
21
use Symfony\Component\Process\Process;
22
use Symfony\Component\Yaml\Yaml;
23
24
/**
25
 * Github source control provider
26
 *
27
 * @author Ronan Chilvers <[email protected]>
28
 */
29
class Github extends AbstractProvider implements ProviderInterface
30
{
31
    /**
32
     * @var array
33
     */
34
    protected $typesHandled = ['github'];
35
36
    /**
37
     * @var string
38
     */
39
    protected $headUrl = 'https://api.github.com/repos/{repository}/git/refs/{ref}';
40
41
    /**
42
     * @var string
43
     */
44
    protected $branchesUrl = 'https://api.github.com/repos/{repository}/branches';
45
46
    /**
47
     * @var string
48
     */
49
    protected $tagsUrl = 'https://api.github.com/repos/{repository}/tags';
50
51
    /**
52
     * @var string
53
     */
54
    protected $commitUrl = 'https://api.github.com/repos/{repository}/commits/{sha}';
55
56
    /**
57
     * @var string
58
     */
59
    protected $downloadUrl = 'https://api.github.com/repos/{repository}/tarball/{sha}';
60
61
    /**
62
     * @var string
63
     */
64
    protected $configUrl = 'https://api.github.com/repos/{repository}/contents/deploy.yaml?ref={sha}';
65
66
    /**
67
     * @var string
68
     */
69
    protected $repoUrl = 'https://github.com/{repository}';
70
71
    /**
72
     * @var string
73
     */
74
    protected $branchUrl = 'https://github.com/{repository}/tree/{branch}';
75
76
    /**
77
     * @var string
78
     */
79
    protected $shaUrl = 'https://github.com/{repository}/commit/{sha}';
80
81
    /**
82
     * @see \App\Provider\ProviderInterface::getHeadInfo()
83
     */
84
    public function getHeadInfo(string $repository, string $ref)
85
    {
86
        $ref = $this->cleanBranchName($ref);
87
        $data = null;
88
        foreach (['heads', 'tags'] as $type) {
89
            $params = [
90
                'repository' => $repository,
91
                'ref'     => $type . '/' . $ref,
92
            ];
93
            $url = Str::moustaches(
94
                $this->headUrl,
95
                $params
96
            );
97
            Log::debug('Querying Github for head data', [
98
                'type' => $type,
99
                'url' => $url,
100
            ]);
101
            try {
102
                $data = $this->getJSON($url);
103
                break;
104
            } catch (ClientException $ex) {
105
                Log::debug(
106
                    $ex->getMessage(),
107
                    [
108
                        'exception' => $ex,
109
                    ]
110
                );
111
            }
112
        }
113
        if (is_null($data)) {
114
            throw new RuntimeException('Unable to find head commit data from Github');
115
        }
116
        Log::debug('Head data queried successfully from Github', [
117
            'type' => $type,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $type seems to be defined by a foreach iteration on line 88. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
118
            'url' => $url,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $url seems to be defined by a foreach iteration on line 88. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
119
            'data' => $data,
120
        ]);
121
        $params['sha'] = $data['object']['sha'];
122
        $url = Str::moustaches(
123
            $this->commitUrl,
124
            $params
125
        );
126
        $data = $this->getJSON($url);
127
128
        return [
129
            'sha'       => $data['sha'],
130
            'author'    => $data['commit']['author']['email'],
131
            'committer' => $data['commit']['committer']['email'],
132
            'message'   => $data['commit']['message'],
133
        ];
134
    }
135
}
136