Bitbucket   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
eloc 40
c 2
b 0
f 0
dl 0
loc 115
ccs 0
cts 21
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A processRefArray() 0 11 4
A getHeadInfo() 0 21 3
A getConfiguration() 0 14 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 $headUrl = 'https://api.bitbucket.org/2.0/repositories/{repository}/commits/{branch}?pagelen=1';
37
38
    /**
39
     * @var string
40
     */
41
    protected $branchesUrl = 'https://api.bitbucket.org/2.0/repositories/{repository}/refs/branches?pagelen=50';
42
43
    /**
44
     * @var string
45
     */
46
    protected $tagsUrl = 'https://api.bitbucket.org/2.0/repositories/{repository}/refs/tags?pagelen=50';
47
48
    /**
49
     * @var string
50
     */
51
    protected $downloadUrl = 'https://bitbucket.org/{repository}/get/{sha}.zip';
52
53
    /**
54
     * @var string
55
     */
56
    protected $configUrl = 'https://api.bitbucket.org/2.0/repositories/{repository}/src/{sha}/deploy.yaml';
57
58
    /**
59
     * @var string
60
     */
61
    protected $repoUrl = 'https://bitbucket.org/{repository}';
62
63
    /**
64
     * @var string
65
     */
66
    protected $branchUrl = 'https://bitbucket.org/{repository}/src/{branch}';
67
68
    /**
69
     * @var string
70
     */
71
    protected $shaUrl = 'https://bitbucket.org/{repository}/commits/{sha}';
72
73
    /**
74
     * @see \App\Provider\ProviderInterface::getHeadInfo()
75
     */
76
    public function getHeadInfo(string $repository, string $ref)
77
    {
78
        $ref = $this->cleanBranchName($ref);
79
        $params = [
80
            'repository' => $this->encodeRepository($repository),
81
            'branch'     => $ref,
82
        ];
83
        $url = Str::moustaches(
84
            $this->headUrl,
85
            $params
86
        );
87
        $data = $this->getJSON($url);
88
        if (!is_array($data) || !isset($data['values'], $data['values'][0])) {
0 ignored issues
show
introduced by
The condition is_array($data) is always true.
Loading history...
89
            throw new RuntimeException('No data found for head commit');
90
        }
91
92
        return [
93
            'sha'       => $data['values'][0]['hash'],
94
            'author'    => $data['values'][0]['author']['raw'],
95
            'committer' => $data['values'][0]['author']['raw'],
96
            'message'   => $data['values'][0]['summary']['raw'],
97
        ];
98
    }
99
100
    /**
101
     * Try to download the deploy.yaml file from the remote repository
102
     *
103
     * @param \App\Model\Project $project
104
     * @param \App\Model\Deployment $deployment
105
     * @author Ronan Chilvers <[email protected]>
106
     */
107
    protected function getConfiguration(Project $project, Deployment $deployment)
108
    {
109
        $repository = $this->encodeRepository($project->repository);
110
        $params = [
111
            'repository' => $repository,
112
            'sha'        => $deployment->sha,
113
        ];
114
        $url = Str::moustaches(
115
            $this->configUrl,
116
            $params
117
        );
118
        $response = $this->get($url);
119
120
        return $response->getBody()->getContents();
121
    }
122
123
    /**
124
     * Process a ref arrays into simplified form
125
     *
126
     * @param array $data
127
     * @return array
128
     * @author Ronan Chilvers <[email protected]>
129
     */
130
    protected function processRefArray(array $data): array
131
    {
132
        if (!isset($data['values']) || 0 == count($data['values'])) {
133
            return [];
134
        }
135
        $output = [];
136
        foreach ($data['values'] as $datum) {
137
            $output[$datum['name']] = $datum['name'];
138
        }
139
140
        return $output;
141
    }
142
}
143