Passed
Pull Request — master (#26)
by Ronan
04:04
created

Bitbucket   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 29
c 1
b 0
f 0
dl 0
loc 93
ccs 0
cts 28
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A processRefArray() 0 11 4
A getHeadInfo() 0 17 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
 * Bitbucket source control provider
23
 *
24
 * @author Ronan Chilvers <[email protected]>
25
 */
26
class Bitbucket extends AbstractProvider implements ProviderInterface
27
{
28
    /**
29
     * @var array
30
     */
31
    protected $typesHandled = ['bitbucket'];
32
33
    /**
34
     * @var string
35
     */
36
    protected $token;
37
38
    /**
39
     * @var string
40
     */
41
    protected $headUrl = 'https://api.bitbucket.org/2.0/repositories/{repository}/commits/{branch}?pagelen=1';
42
43
    /**
44
     * @var string
45
     */
46
    protected $branchesUrl = 'https://api.bitbucket.org/2.0/repositories/{repository}/refs/branches?pagelen=50';
47
48
    /**
49
     * @var string
50
     */
51
    protected $tagsUrl = 'https://api.bitbucket.org/2.0/repositories/{repository}/refs/tags?pagelen=50';
52
53
    /**
54
     * @var string
55
     */
56
    protected $downloadUrl = 'https://api.bitbucket.org/2.0/{repository}/repository/archive.tar.gz?sha={sha}';
57
58
    /**
59
     * @var string
60
     */
61
    protected $configUrl = 'https://api.bitbucket.org/2.0/{repository}/repository/files/deploy.yaml?ref={sha}';
62
63
    /**
64
     * @var string
65
     */
66
    protected $repoUrl = 'https://gitlab.com/{repository}';
67
68
    /**
69
     * @var string
70
     */
71
    protected $branchUrl = 'https://gitlab.com/{repository}/tree/{branch}';
72
73
    /**
74
     * @var string
75
     */
76
    protected $shaUrl = 'https://gitlab.com/{repository}/commit/{sha}';
77
78
    /**
79
     * @see \App\Provider\ProviderInterface::getHeadInfo()
80
     */
81
    public function getHeadInfo(string $repository, string $type, string $ref)
82
    {
83
        $params = [
84
            'repository' => $this->encodeRepository($repository),
85
            'branch'     => $ref,
86
        ];
87
        $url = Str::moustaches(
88
            $this->headUrl,
89
            $params
90
        );
91
        $data = $this->getJSON($url);
92
93
        return [
94
            'sha'       => $data['id'],
95
            'author'    => $data['author_email'],
96
            'committer' => $data['committer_email'],
97
            'message'   => $data['title'],
98
        ];
99
    }
100
101
    /**
102
     * Process a ref arrays into simplified form
103
     *
104
     * @param array $data
105
     * @return array
106
     * @author Ronan Chilvers <[email protected]>
107
     */
108
    protected function processRefArray(array $data): array
109
    {
110
        if (!isset($data['values']) || 0 == count($data['values'])) {
111
            return [];
112
        }
113
        $output = [];
114
        foreach ($data['values'] as $datum) {
115
            $output[$datum['name']] = $datum['name'];
116
        }
117
118
        return $output;
119
    }
120
}
121