Passed
Push — master ( 4f7527...72ebe2 )
by Ronan
05:43 queued 02:13
created

Bitbucket::processRefArray()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 11
ccs 0
cts 10
cp 0
rs 10
cc 4
nc 3
nop 1
crap 20
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 $type, string $ref)
77
    {
78
        $params = [
79
            'repository' => $this->encodeRepository($repository),
80
            'branch'     => $ref,
81
        ];
82
        $url = Str::moustaches(
83
            $this->headUrl,
84
            $params
85
        );
86
        $data = $this->getJSON($url);
87
        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...
88
            throw new RuntimeException('No data found for head commit');
89
        }
90
91
        return [
92
            'sha'       => $data['values'][0]['hash'],
93
            'author'    => $data['values'][0]['author']['raw'],
94
            'committer' => $data['values'][0]['author']['raw'],
95
            'message'   => $data['values'][0]['summary']['raw'],
96
        ];
97
    }
98
99
    /**
100
     * Try to download the deploy.yaml file from the remote repository
101
     *
102
     * @param \App\Model\Project $project
103
     * @param \App\Model\Deployment $deployment
104
     * @author Ronan Chilvers <[email protected]>
105
     */
106
    protected function getConfiguration(Project $project, Deployment $deployment)
107
    {
108
        $repository = $this->encodeRepository($project->repository);
109
        $params = [
110
            'repository' => $repository,
111
            'sha'        => $deployment->sha,
112
        ];
113
        $url = Str::moustaches(
114
            $this->configUrl,
115
            $params
116
        );
117
        $response = $this->get($url);
118
119
        return $response->getBody()->getContents();
120
    }
121
122
    /**
123
     * Process a ref arrays into simplified form
124
     *
125
     * @param array $data
126
     * @return array
127
     * @author Ronan Chilvers <[email protected]>
128
     */
129
    protected function processRefArray(array $data): array
130
    {
131
        if (!isset($data['values']) || 0 == count($data['values'])) {
132
            return [];
133
        }
134
        $output = [];
135
        foreach ($data['values'] as $datum) {
136
            $output[$datum['name']] = $datum['name'];
137
        }
138
139
        return $output;
140
    }
141
}
142