-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathBase.php
More file actions
161 lines (134 loc) · 3.95 KB
/
Base.php
File metadata and controls
161 lines (134 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
namespace BBCli\BBCli;
/**
* BB-CLI Base Class
*
* Extending class: Auth, Branch, Pr etc...
*
* @see https://bb-cli.github.io/docs/commands
*/
class Base
{
/**
* Default command run for actions.
*
* Example: 'list'
*/
const DEFAULT_METHOD = 'DEFAULT_METHOD_NOT_DEFINED';
/**
* Defined to custom commands for actions.
*
* Example: 'list' => 'list, l'
*/
const AVAILABLE_COMMANDS = [];
/**
* Checks the repo .git folder.
*/
const CHECK_GIT_FOLDER = true;
/**
* Construct
*/
public function __construct()
{
//
}
/**
* Make requests for Bitbucket Rest API.
*
* @param string $method
* @param string $url
* @param array $payload
* @param bool $isRepositoryUrl
* @return mixed
* @throws \Exception
* @see https://developer.atlassian.com/cloud/bitbucket/rest
*/
public function makeRequest($method = 'GET', $url = '', $payload = [], $isRepositoryUrl = true)
{
$this->checkAuth();
if ($isRepositoryUrl) {
$repoPath = getRepoPath();
$url = "/repositories/{$repoPath}{$url}";
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.bitbucket.org/2.0{$url}");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Basic '.base64_encode(userConfig('auth.username').':'.userConfig('auth.appPassword')),
]);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($method));
if ($method !== 'GET') {
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
}
$result = curl_exec($ch);
if (curl_errno($ch)) {
o('Error:' . curl_error($ch));
die;
}
$httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpStatusCode < 200 || $httpStatusCode > 299) {
if ($httpStatusCode === 401) {
throw new \Exception('Authorization error, please check your credentials.', 1);
}
$allowedStatuses = [409];
if (!in_array($httpStatusCode, $allowedStatuses)) {
o($result);
throw new \Exception('An error occurred, status code: '.$httpStatusCode, 1);
}
}
curl_close($ch);
$jsonResult = json_decode($result, true);
if (json_last_error() !== JSON_ERROR_NONE) {
return $result;
}
if (array_get($jsonResult, 'type') === 'error') {
throw new \Exception(array_get($jsonResult, 'error.message'), 1);
}
return $jsonResult;
}
/**
* Method name from alias.
*
* @param string $alias
* @return mixed
*/
public function getMethodNameFromAlias($alias)
{
foreach (static::AVAILABLE_COMMANDS as $method => $methodAliases) {
$methodAliases = array_map('trim', explode(', ', $methodAliases));
if (in_array($alias, $methodAliases)) {
return $method;
}
}
return false;
}
/**
* Lists available commands in shell autocomplete format.
*
* @return void
*/
public function listCommandsForAutocomplete()
{
$commands = array_map(function($aliases) {
return trim(explode(',', $aliases)[0]);
}, static::AVAILABLE_COMMANDS);
sort($commands);
echo implode(' ', $commands);
}
/**
* Checks the auth file.
* If an error: run bb auth command.
*
* @return void
*/
private function checkAuth()
{
if (!userConfig('auth')) {
o('You have to configure auth info to use this command.', 'red');
o('Run "bb auth" first.', 'yellow');
exit(1);
}
}
}