Passed
Push — master ( a0c2c0...1d066e )
by Ronan
05:56 queued 02:14
created

Gitlab::getRepositoryLink()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 9
ccs 0
cts 8
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A Gitlab::encodeRepository() 0 7 1
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 Ronanchilvers\Foundation\Config;
15
use Ronanchilvers\Utility\Str;
16
use RuntimeException;
17
use Symfony\Component\Process\Exception\ProcessFailedException;
18
use Symfony\Component\Process\Process;
19
use Symfony\Component\Yaml\Yaml;
20
21
/**
22
 * Gitlab source control provider
23
 *
24
 * @author Ronan Chilvers <[email protected]>
25
 */
26
class Gitlab extends AbstractProvider implements ProviderInterface
27
{
28
    /**
29
     * @var array
30
     */
31
    protected $typesHandled = ['gitlab'];
32
33
    /**
34
     * @var string
35
     */
36
    protected $token;
37
38
    /**
39
     * @var string
40
     */
41
    protected $headUrl = 'https://gitlab.com/api/v4/projects/{repository}/repository/commits/{branch}';
42
43
    /**
44
     * @var string
45
     */
46
    protected $downloadUrl = 'https://gitlab.com/api/v4/projects/{repository}/repository/archive.tar.gz?sha={sha}';
47
48
    /**
49
     * @var string
50
     */
51
    protected $configUrl = 'https://gitlab.com/api/v4/projects/{repository}/repository/files/deploy.yaml?ref={sha}';
52
53
    /**
54
     * @var string
55
     */
56
    protected $repoUrl = 'https://gitlab.com/{repository}';
57
58
    /**
59
     * @var string
60
     */
61
    protected $branchUrl = 'https://gitlab.com/{repository}/tree/{branch}';
62
63
    /**
64
     * @var string
65
     */
66
    protected $shaUrl = 'https://gitlab.com/{repository}/commit/{sha}';
67
68
    /**
69
     * @see \App\Provider\ProviderInterface::getHeadInfo()
70
     */
71
    public function getHeadInfo(string $repository, string $branch)
72
    {
73
        $params = [
74
            'repository' => $this->encodeRepository($repository),
75
            'branch'     => $branch,
76
        ];
77
        $url = Str::moustaches(
78
            $this->headUrl,
79
            $params
80
        );
81
        $data = $this->getJSON($url);
82
83
        return [
84
            'sha'       => $data['id'],
85
            'author'    => $data['author_email'],
86
            'committer' => $data['committer_email'],
87
            'message'   => $data['title'],
88
        ];
89
    }
90
91
    /**
92
     * Encode a repository name
93
     *
94
     * @return string
95
     * @author Ronan Chilvers <[email protected]>
96
     */
97
    protected function encodeRepository($repository)
98
    {
99
        return str_replace(
100
            '.',
101
            '%2E',
102
            urlencode(
103
                $repository
104
            )
105
        );
106
    }
107
}
108