|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GitScrum\Classes; |
|
4
|
|
|
|
|
5
|
|
|
use Auth; |
|
6
|
|
|
use GitScrum\Models\User; |
|
7
|
|
|
use GitScrum\Models\Issue; |
|
8
|
|
|
use GitScrum\Models\Organization; |
|
9
|
|
|
use GitScrum\Models\ProductBacklog; |
|
10
|
|
|
use GitScrum\Models\Branch; |
|
11
|
|
|
use Carbon\Carbon; |
|
12
|
|
|
use GitScrum\Contracts\ProviderInterface; |
|
13
|
|
|
|
|
14
|
|
|
class Gitlab implements ProviderInterface |
|
15
|
|
|
{ |
|
16
|
|
|
private $gitlabGroups; |
|
17
|
|
|
|
|
18
|
|
|
public function tplUser($obj) |
|
19
|
|
|
{ |
|
20
|
|
|
return [ |
|
21
|
|
|
'provider_id' => $obj->id, |
|
22
|
|
|
'provider' => 'gitlab', |
|
23
|
|
|
'username' => $obj->nickname, |
|
24
|
|
|
'name' => $obj->name, |
|
25
|
|
|
'token' => $obj->token, |
|
26
|
|
|
'avatar' => @$obj->user['avatar_url'], |
|
27
|
|
|
'html_url' => @$obj->user['web_url'], |
|
28
|
|
|
'bio' => @$obj->user['bio'], |
|
29
|
|
|
'since' => Carbon::parse($obj->user['created_at'])->toDateTimeString(), |
|
30
|
|
|
'location' => @$obj->user['location'], |
|
31
|
|
|
'blog' => @$obj->user['blog'], |
|
32
|
|
|
'email' => $obj->email, |
|
33
|
|
|
]; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function tplRepository($repo, $slug = false) |
|
|
|
|
|
|
37
|
|
|
{ |
|
38
|
|
|
$organization = $this->organization($repo); |
|
39
|
|
|
|
|
40
|
|
|
if (!$organization) { |
|
41
|
|
|
return; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
return (object) [ |
|
45
|
|
|
'provider_id' => $repo->id, |
|
46
|
|
|
'organization_id' => $organization->id, |
|
47
|
|
|
'organization_title' => $organization->username, |
|
48
|
|
|
'slug' => $slug ? $slug : Helper::slug($repo->path), |
|
49
|
|
|
'title' => $repo->path, |
|
50
|
|
|
'fullname' => $repo->name, |
|
51
|
|
|
'is_private' => $repo->public == true, |
|
52
|
|
|
'html_url' => $repo->http_url_to_repo, |
|
53
|
|
|
'description' => $repo->description, |
|
54
|
|
|
'fork' => null, |
|
55
|
|
|
'url' => $repo->web_url, |
|
56
|
|
|
'since' => Carbon::parse($repo->created_at)->toDateTimeString(), |
|
57
|
|
|
'pushed_at' => Carbon::parse($repo->last_activity_at)->toDateTimeString(), |
|
58
|
|
|
'ssh_url' => $repo->ssh_url_to_repo, |
|
59
|
|
|
'clone_url' => $repo->ssh_url_to_repo, |
|
60
|
|
|
'homepage' => $repo->web_url, |
|
61
|
|
|
'default_branch' => $repo->default_branch, |
|
62
|
|
|
]; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function tplIssue($obj, $productBacklogId) |
|
66
|
|
|
{ |
|
67
|
|
|
if (isset($obj->assignee->username)) { |
|
68
|
|
|
$user = User::where('username', @$obj->assignee->username) |
|
69
|
|
|
->where('provider', 'gitlab')->first(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return [ |
|
73
|
|
|
'provider_id' => $obj->id, |
|
74
|
|
|
'user_id' => isset($user->id) ? $user->id : Auth::user()->id, |
|
|
|
|
|
|
75
|
|
|
'product_backlog_id' => $productBacklogId, |
|
76
|
|
|
'effort' => 0, |
|
77
|
|
|
'config_issue_effort_id' => 1, |
|
78
|
|
|
'issue_type_id' => 1, |
|
79
|
|
|
'number' => $obj->iid, |
|
80
|
|
|
'title' => $obj->title, |
|
81
|
|
|
'description' => $obj->description, |
|
82
|
|
|
'state' => $obj->state, |
|
83
|
|
|
'html_url' => isset($obj->web_url) ? $obj->web_url : '', |
|
84
|
|
|
'created_at' => Carbon::parse($obj->created_at)->toDateTimeString(), |
|
85
|
|
|
'updated_at' => Carbon::parse($obj->updated_at)->toDateTimeString(), |
|
86
|
|
|
]; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function tplOrganization($obj) |
|
90
|
|
|
{ |
|
91
|
|
|
return [ |
|
92
|
|
|
'provider_id' => $obj->owner->id, |
|
93
|
|
|
'username' => $obj->owner->username, |
|
94
|
|
|
'url' => $obj->owner->web_url, |
|
95
|
|
|
'repos_url' => null, |
|
96
|
|
|
'events_url' => null, |
|
97
|
|
|
'hooks_url' => null, |
|
98
|
|
|
'issues_url' => null, |
|
99
|
|
|
'members_url' => null, |
|
100
|
|
|
'public_members_url' => null, |
|
101
|
|
|
'avatar_url' => $obj->owner->avatar_url, |
|
102
|
|
|
'description' => null, |
|
103
|
|
|
'title' => $obj->owner->username, |
|
104
|
|
|
'blog' => null, |
|
105
|
|
|
'location' => null, |
|
106
|
|
|
'email' => null, |
|
107
|
|
|
'public_repos' => null, |
|
108
|
|
|
'html_url' => null, |
|
109
|
|
|
'total_private_repos' => null, |
|
110
|
|
|
'since' => @Carbon::parse($obj->namespace->created_at)->toDateTimeString(), |
|
111
|
|
|
'disk_usage' => null, |
|
112
|
|
|
]; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function readRepositories($page = 1, &$repos = null) |
|
116
|
|
|
{ |
|
117
|
|
|
$repos = collect(Helper::request(env('GITLAB_INSTANCE_URI').'api/v3/projects?access_token='.Auth::user()->token)); |
|
118
|
|
|
|
|
119
|
|
|
$response = $repos->map(function ($repo) { |
|
120
|
|
|
return $this->tplRepository($repo); |
|
121
|
|
|
}); |
|
122
|
|
|
|
|
123
|
|
|
return $response; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public function createOrUpdateRepository($owner, $obj, $oldTitle = null) |
|
127
|
|
|
{ |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function organization($obj) |
|
|
|
|
|
|
131
|
|
|
{ |
|
132
|
|
|
if (!isset($obj->owner) && !isset($obj->namespace)) { |
|
133
|
|
|
return false; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
if (!isset($obj->owner) && isset($obj->namespace)) { |
|
137
|
|
|
// To avoid to make unnecessary calls to the api to get the groups info saving the fetched groups into a private variable |
|
138
|
|
|
if (!isset($this->gitlabGroups[$obj->namespace->id])) { |
|
139
|
|
|
$group = current(collect(Helper::request(env('GITLAB_INSTANCE_URI').'api/v3/groups/'.$obj->namespace->id.'?access_token='.Auth::user()->token))); |
|
140
|
|
|
|
|
141
|
|
|
$this->gitlabGroups[$obj->namespace->id] = $group; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
$group = $this->gitlabGroups[$obj->namespace->id]; |
|
145
|
|
|
|
|
146
|
|
|
$obj->owner = new \stdClass(); |
|
147
|
|
|
$obj->owner->id = $group['id']; |
|
148
|
|
|
$obj->owner->username = $group['path']; |
|
149
|
|
|
$obj->owner->web_url = $group['web_url']; |
|
150
|
|
|
$obj->owner->avatar_url = $group['avatar_url']; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
$data = $this->tplOrganization($obj); |
|
154
|
|
|
|
|
155
|
|
|
try { |
|
156
|
|
|
$organization = Organization::create($data); |
|
157
|
|
|
} catch (\Illuminate\Database\QueryException $e) { |
|
158
|
|
|
$organization = Organization::where('username', $data['username']) |
|
159
|
|
|
->where('provider', 'gitlab')->first(); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
$organization->users()->sync([Auth::id()]); |
|
163
|
|
|
|
|
164
|
|
|
return $organization; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Get all members from a specific group in gitlab. |
|
169
|
|
|
* |
|
170
|
|
|
* @param $group |
|
171
|
|
|
* |
|
172
|
|
|
* @return \Illuminate\Support\Collection |
|
173
|
|
|
*/ |
|
174
|
|
|
private function getGroupsMembers($group) |
|
175
|
|
|
{ |
|
176
|
|
|
$members = collect(Helper::request(env('GITLAB_INSTANCE_URI').'api/v3/groups/'.$group.'/members?access_token='.Auth::user()->token)); |
|
177
|
|
|
|
|
178
|
|
|
return $members; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Get all members from the project in gitlab. |
|
183
|
|
|
* |
|
184
|
|
|
* @param $projectId |
|
185
|
|
|
* |
|
186
|
|
|
* @return \Illuminate\Support\Collection |
|
187
|
|
|
*/ |
|
188
|
|
|
private function getProjectMembers($projectId) |
|
189
|
|
|
{ |
|
190
|
|
|
$members = collect(Helper::request(env('GITLAB_INSTANCE_URI').'api/v3/projects/'.$projectId.'/members?access_token='.Auth::user()->token)); |
|
191
|
|
|
|
|
192
|
|
|
return $members; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* A project can be shared with many groups and each group has its members |
|
197
|
|
|
* This method retrieves all members from the groups that the project is shared with. |
|
198
|
|
|
* |
|
199
|
|
|
* @param $projectId |
|
200
|
|
|
* |
|
201
|
|
|
* @return \Illuminate\Support\Collection|static |
|
202
|
|
|
*/ |
|
203
|
|
|
private function getProjectSharedGroupsMembers($projectId) |
|
204
|
|
|
{ |
|
205
|
|
|
$project = Helper::request(env('GITLAB_INSTANCE_URI').'api/v3/projects/'.$projectId.'?access_token='.Auth::user()->token); |
|
206
|
|
|
|
|
207
|
|
|
$members = new \Illuminate\Support\Collection(); |
|
208
|
|
|
|
|
209
|
|
|
if (!empty($project->shared_with_groups)) { |
|
210
|
|
|
foreach ($project->shared_with_groups as $group) { |
|
211
|
|
|
$groupsMembers = $this->getGroupsMembers($group->group_id); |
|
212
|
|
|
|
|
213
|
|
|
$members = $members->merge($groupsMembers); |
|
214
|
|
|
} |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
return $members; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Retrives all project members from three pespectives |
|
222
|
|
|
* Members from the project itself |
|
223
|
|
|
* Members of the groups that the project is owned by |
|
224
|
|
|
* Members by the groups that the project is shared with. |
|
225
|
|
|
* |
|
226
|
|
|
* @param $owner |
|
227
|
|
|
* @param $repo |
|
228
|
|
|
* @param null $providerId |
|
229
|
|
|
*/ |
|
230
|
|
|
public function readCollaborators($owner, $repo, $providerId = null) |
|
231
|
|
|
{ |
|
232
|
|
|
$collaborators = $this->getGroupsMembers($owner); |
|
233
|
|
|
|
|
234
|
|
|
if ($providerId) { |
|
235
|
|
|
$projectMembers = $this->getProjectMembers($providerId); |
|
236
|
|
|
$collaborators = $collaborators->merge($projectMembers); |
|
237
|
|
|
|
|
238
|
|
|
$projectSharedGroupsMembers = $this->getProjectSharedGroupsMembers($providerId); |
|
239
|
|
|
$collaborators = $collaborators->merge($projectSharedGroupsMembers); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
foreach ($collaborators as $collaborator) { |
|
243
|
|
|
if (isset($collaborator->id)) { |
|
244
|
|
|
$data = [ |
|
245
|
|
|
'provider_id' => $collaborator->id, |
|
246
|
|
|
'provider' => 'gitlab', |
|
247
|
|
|
'username' => $collaborator->username, |
|
248
|
|
|
'name' => $collaborator->name, |
|
249
|
|
|
'avatar' => $collaborator->avatar_url, |
|
250
|
|
|
'html_url' => $collaborator->web_url, |
|
251
|
|
|
'email' => null, |
|
252
|
|
|
'remember_token' => null, |
|
253
|
|
|
'bio' => null, |
|
254
|
|
|
'location' => null, |
|
255
|
|
|
'blog' => null, |
|
256
|
|
|
'since' => null, |
|
257
|
|
|
'token' => null, |
|
258
|
|
|
'position_held' => null, |
|
259
|
|
|
]; |
|
260
|
|
|
|
|
261
|
|
|
try { |
|
262
|
|
|
$user = User::firstOrCreate($data); |
|
|
|
|
|
|
263
|
|
|
} catch (\Exception $e) { |
|
264
|
|
|
$user = User::where('username', $collaborator->username) |
|
265
|
|
|
->where('provider', 'gitlab')->first(); |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
$userId[] = $user->id; |
|
|
|
|
|
|
269
|
|
|
} |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
$organization = Organization::where('username', $owner) |
|
273
|
|
|
->where('provider', 'gitlab')->first()->users(); |
|
274
|
|
|
|
|
275
|
|
|
if (!$organization->userActive()->count()) { |
|
276
|
|
|
$organization->attach($userId); |
|
|
|
|
|
|
277
|
|
|
} |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
public function createBranches($owner, $product_backlog_id, $repo, $providerId = null) |
|
281
|
|
|
{ |
|
282
|
|
|
$branches = collect(Helper::request(env('GITLAB_INSTANCE_URI').'api/v3/projects/'.$providerId.'/repository/branches?access_token='.Auth::user()->token)); |
|
283
|
|
|
|
|
284
|
|
|
$branchesData = []; |
|
285
|
|
|
foreach ($branches as $branch) { |
|
286
|
|
|
$branchesData[] = [ |
|
287
|
|
|
'product_backlog_id' => $product_backlog_id, |
|
288
|
|
|
'title' => $branch->name, |
|
289
|
|
|
'sha' => $branch->commit->id, |
|
290
|
|
|
'created_at' => Carbon::now(), |
|
291
|
|
|
'updated_at' => Carbon::now(), |
|
292
|
|
|
]; |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
if ($branchesData) { |
|
|
|
|
|
|
296
|
|
|
Branch::insert($branchesData); |
|
|
|
|
|
|
297
|
|
|
} |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
|
|
public function readIssues() |
|
301
|
|
|
{ |
|
302
|
|
|
$repos = ProductBacklog::all(); |
|
303
|
|
|
|
|
304
|
|
|
foreach ($repos as $repo) { |
|
305
|
|
|
$issues = Helper::request(env('GITLAB_INSTANCE_URI').'api/v3/projects/'.$repo->provider_id. |
|
306
|
|
|
'/issues?access_token='.Auth::user()->token); |
|
307
|
|
|
|
|
308
|
|
|
$issues = is_array($issues) ? $issues : [$issues]; |
|
309
|
|
|
|
|
310
|
|
|
foreach ($issues as $issue) { |
|
311
|
|
|
try { |
|
312
|
|
|
$data = $this->tplIssue($issue, $repo->id); |
|
313
|
|
|
if (!Issue::where('provider_id', $data['provider_id'])->where('provider', 'gitlab')->first()) { |
|
314
|
|
|
Issue::create($data)->users()->sync([$data['user_id']]); |
|
315
|
|
|
} |
|
316
|
|
|
} catch (\Exception $e) { |
|
|
|
|
|
|
317
|
|
|
} |
|
318
|
|
|
} |
|
319
|
|
|
} |
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
|
|
public function createOrUpdateIssue($obj) |
|
323
|
|
|
{ |
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
|
|
public function createOrUpdateIssueComment($obj, $verb = 'POST') |
|
327
|
|
|
{ |
|
328
|
|
|
} |
|
329
|
|
|
|
|
330
|
|
|
public function deleteIssueComment($obj) |
|
331
|
|
|
{ |
|
332
|
|
|
} |
|
333
|
|
|
} |
|
334
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.