1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace bbApiRequestConflicts; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Conflicts |
9
|
|
|
* @package bbApiRequestConflicts |
10
|
|
|
*/ |
11
|
|
|
class Conflicts |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* API endpoint |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
private $endpoint; |
18
|
|
|
/** |
19
|
|
|
* Guzzle client |
20
|
|
|
* @var Client |
21
|
|
|
*/ |
22
|
|
|
private $client; |
23
|
|
|
/** |
24
|
|
|
* State name |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $state; |
28
|
|
|
/** |
29
|
|
|
* Check requests only from current login |
30
|
|
|
* @var bool |
31
|
|
|
*/ |
32
|
|
|
private $checkLogin; |
33
|
|
|
/** |
34
|
|
|
* User login |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
private $login; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Conflicts constructor. |
41
|
|
|
* @param $config [] |
42
|
|
|
* @throws Exception |
43
|
|
|
*/ |
44
|
|
|
public function __construct($config) |
45
|
|
|
{ |
46
|
|
|
if (!is_array($config)) { |
47
|
|
|
throw new Exception('Config must be an array'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$config = array_merge($this->defaultConfig(), $config); |
51
|
|
|
$required = $this->requiredConfig(); |
52
|
|
|
|
53
|
|
|
foreach ($required as $option) { |
54
|
|
|
if (!isset($config[$option])) { |
55
|
|
|
throw new Exception("{$option} is required in config"); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$this->init($config); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Init properties |
64
|
|
|
* @param $config |
65
|
|
|
*/ |
66
|
|
|
protected function init($config) |
67
|
|
|
{ |
68
|
|
|
$this->client = new Client([ |
69
|
|
|
'auth' => [ |
70
|
|
|
$config['login'], $config['password'], |
71
|
|
|
] |
72
|
|
|
]); |
73
|
|
|
|
74
|
|
|
$this->endpoint = implode('/', [ |
75
|
|
|
$config['apiURL'], 'repositories', $config['owner'], $config['slug'], 'pullrequests' |
76
|
|
|
]); |
77
|
|
|
|
78
|
|
|
if (isset($config['state'])) { |
79
|
|
|
$this->state = $config['state']; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$this->login = $config['login']; |
83
|
|
|
$this->checkLogin = (bool) $config['checkLogin']; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Default config values |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
|
|
protected function defaultConfig() |
91
|
|
|
{ |
92
|
|
|
return [ |
93
|
|
|
'apiURL' => 'https://api.bitbucket.org/2.0', |
94
|
|
|
'checkLogin' => true, |
95
|
|
|
]; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Required config values |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
|
|
protected function requiredConfig() |
103
|
|
|
{ |
104
|
|
|
return [ |
105
|
|
|
'apiURL', |
106
|
|
|
'login', |
107
|
|
|
'password', |
108
|
|
|
'owner', |
109
|
|
|
'slug', |
110
|
|
|
]; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Check if there conflicts in request |
115
|
|
|
* @param $requestId |
116
|
|
|
* @return int |
117
|
|
|
*/ |
118
|
|
|
protected function isConflicts($requestId) |
119
|
|
|
{ |
120
|
|
|
$diff = $this->client->get($this->endpoint . '/' . $requestId . '/diff')->getBody(); |
121
|
|
|
return stripos($diff, '+<<<<<<< destination:'); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* List of requests links with conflicts |
126
|
|
|
* @return array |
127
|
|
|
*/ |
128
|
|
|
public function getLinks() |
129
|
|
|
{ |
130
|
|
|
$links = []; |
131
|
|
|
|
132
|
|
|
$url = $this->endpoint . ($this->state ? '?state=' . $this->state : ''); |
133
|
|
|
do { |
134
|
|
|
$response = $this->client->get($url); |
135
|
|
|
$result = json_decode($response->getBody(), true); |
136
|
|
|
$url = isset($result['next']) ? $result['next'] : null; |
137
|
|
|
|
138
|
|
|
$requests = $result['values']; |
139
|
|
|
foreach ($requests as $request) { |
140
|
|
|
if ($this->checkLogin |
141
|
|
|
&& $this->login != $request['author']['username']) { |
142
|
|
|
continue; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
if ($this->isConflicts($request['id'])) { |
146
|
|
|
$links[] = $request['links']['html']['href']; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
} while ($url); |
150
|
|
|
|
151
|
|
|
return $links; |
152
|
|
|
} |
153
|
|
|
} |