|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GitScrum\Classes; |
|
4
|
|
|
|
|
5
|
|
|
use Auth; |
|
6
|
|
|
use GitScrum\Models\Branch; |
|
7
|
|
|
use GitScrum\Models\Commit; |
|
8
|
|
|
use GitScrum\Models\User; |
|
9
|
|
|
use GitScrum\Models\Issue; |
|
10
|
|
|
use GitScrum\Models\Organization; |
|
11
|
|
|
use GitScrum\Models\ProductBacklog; |
|
12
|
|
|
use Carbon\Carbon; |
|
13
|
|
|
use GitScrum\Contracts\ProviderInterface; |
|
14
|
|
|
|
|
15
|
|
|
class Github implements ProviderInterface |
|
16
|
|
|
{ |
|
17
|
|
|
public function tplUser($obj) |
|
18
|
|
|
{ |
|
19
|
|
|
return [ |
|
20
|
|
|
'provider_id' => $obj->id, |
|
21
|
|
|
'provider' => 'github', |
|
22
|
|
|
'username' => $obj->nickname, |
|
23
|
|
|
'name' => $obj->name, |
|
24
|
|
|
'token' => $obj->token, |
|
25
|
|
|
'avatar' => @$obj->user['avatar_url'], |
|
26
|
|
|
'html_url' => @$obj->user['html_url'], |
|
27
|
|
|
'bio' => @$obj->user['bio'], |
|
28
|
|
|
'since' => Carbon::parse($obj->user['created_at'])->toDateTimeString(), |
|
29
|
|
|
'location' => @$obj->user['location'], |
|
30
|
|
|
'blog' => @$obj->user['blog'], |
|
31
|
|
|
'email' => $obj->email, |
|
32
|
|
|
]; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function tplRepository($repo, $slug = false) |
|
|
|
|
|
|
36
|
|
|
{ |
|
37
|
|
|
return (object) [ |
|
38
|
|
|
'provider_id' => $repo->id, |
|
39
|
|
|
'organization_id' => $this->organization($repo->owner->login), |
|
40
|
|
|
'organization_title' => $repo->owner->login, |
|
41
|
|
|
'slug' => $slug ? $slug : Helper::slug($repo->name), |
|
42
|
|
|
'title' => $repo->name, |
|
43
|
|
|
'fullname' => $repo->full_name, |
|
44
|
|
|
'is_private' => $repo->private, |
|
45
|
|
|
'html_url' => $repo->html_url, |
|
46
|
|
|
'description' => $repo->description, |
|
47
|
|
|
'fork' => $repo->fork, |
|
48
|
|
|
'url' => $repo->url, |
|
49
|
|
|
'since' => Carbon::parse($repo->created_at)->toDateTimeString(), |
|
50
|
|
|
'pushed_at' => Carbon::parse($repo->pushed_at)->toDateTimeString(), |
|
51
|
|
|
'ssh_url' => $repo->ssh_url, |
|
52
|
|
|
'clone_url' => $repo->clone_url, |
|
53
|
|
|
'homepage' => $repo->homepage, |
|
54
|
|
|
'default_branch' => $repo->default_branch, |
|
55
|
|
|
]; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function tplIssue($obj, $productBracklogId) |
|
59
|
|
|
{ |
|
60
|
|
|
$user = User::where('username', @$obj->user->login) |
|
61
|
|
|
->where('provider', 'github')->first(); |
|
62
|
|
|
|
|
63
|
|
|
return [ |
|
64
|
|
|
'provider_id' => $obj->id, |
|
65
|
|
|
'user_id' => isset($user->id) ? $user->id : Auth::user()->id, |
|
66
|
|
|
'product_backlog_id' => $productBracklogId, |
|
67
|
|
|
'effort' => 0, |
|
68
|
|
|
'config_issue_effort_id' => 1, |
|
69
|
|
|
'issue_type_id' => 1, |
|
70
|
|
|
'number' => $obj->number, |
|
71
|
|
|
'title' => $obj->title, |
|
72
|
|
|
'description' => $obj->body, |
|
73
|
|
|
'state' => $obj->state, |
|
74
|
|
|
'html_url' => $obj->html_url, |
|
75
|
|
|
'created_at' => $obj->created_at, |
|
76
|
|
|
'updated_at' => $obj->updated_at, |
|
77
|
|
|
]; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function readRepositories() |
|
81
|
|
|
{ |
|
82
|
|
|
$repos = collect(Helper::request('https://api.github.com/user/repos')); |
|
83
|
|
|
|
|
84
|
|
|
$response = $repos->map(function ($repo) { |
|
85
|
|
|
return $this->tplRepository($repo); |
|
86
|
|
|
}); |
|
87
|
|
|
|
|
88
|
|
|
return $response; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function createOrUpdateRepository($owner, $obj, $oldTitle = null) |
|
|
|
|
|
|
92
|
|
|
{ |
|
93
|
|
|
$params = [ |
|
94
|
|
|
'name' => str_slug($obj->title, '-'), |
|
95
|
|
|
'description' => $obj->description, |
|
96
|
|
|
]; |
|
97
|
|
|
|
|
98
|
|
|
if (is_null($oldTitle)) { |
|
99
|
|
|
$endpoint = 'https://api.github.com/orgs/'.$owner.'/repos'; |
|
100
|
|
|
|
|
101
|
|
|
if (Auth::user()->username == $owner) { |
|
102
|
|
|
$endpoint = 'https://api.github.com/user/repos'; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
$response = Helper::request($endpoint, true, 'POST', $params); |
|
106
|
|
|
} else { |
|
107
|
|
|
$oldTitle = str_slug($oldTitle, '-'); |
|
108
|
|
|
$response = Helper::request('https://api.github.com/repos/'.$owner.DIRECTORY_SEPARATOR.$oldTitle, true, 'POST', $params); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return (object) $response; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function organization($login) |
|
|
|
|
|
|
115
|
|
|
{ |
|
116
|
|
|
$organization = Organization::where('username', $login) |
|
117
|
|
|
->where('provider', 'github')->first(); |
|
118
|
|
|
|
|
119
|
|
|
if (!isset($organization)) { |
|
120
|
|
|
$orgData = Helper::request('https://api.github.com/orgs/'.$login); |
|
121
|
|
|
|
|
122
|
|
|
if (!isset($orgData->id)) { |
|
123
|
|
|
$orgData = Helper::request('https://api.github.com/users/'.$login); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
$data = [ |
|
127
|
|
|
'provider_id' => @$orgData->id, |
|
128
|
|
|
'username' => @$orgData->login, |
|
129
|
|
|
'url' => @$orgData->url, |
|
130
|
|
|
'repos_url' => @$orgData->repos_url, |
|
131
|
|
|
'events_url' => @$orgData->events_url, |
|
132
|
|
|
'hooks_url' => @$orgData->hooks_url, |
|
133
|
|
|
'issues_url' => @$orgData->issues_url, |
|
134
|
|
|
'members_url' => @$orgData->members_url, |
|
135
|
|
|
'public_members_url' => @$orgData->public_members_url, |
|
136
|
|
|
'avatar_url' => @$orgData->avatar_url, |
|
137
|
|
|
'description' => @$orgData->description, |
|
138
|
|
|
'title' => @$orgData->name, |
|
139
|
|
|
'blog' => @$orgData->blog, |
|
140
|
|
|
'location' => @$orgData->location, |
|
141
|
|
|
'email' => @$orgData->email, |
|
142
|
|
|
'public_repos' => @$orgData->public_repos, |
|
143
|
|
|
'html_url' => @$orgData->html_url, |
|
144
|
|
|
'total_private_repos' => @$orgData->total_private_repos, |
|
145
|
|
|
'since' => @Carbon::parse($orgData->created_at)->toDateTimeString(), |
|
146
|
|
|
'disk_usage' => @$orgData->disk_usage, |
|
147
|
|
|
]; |
|
148
|
|
|
|
|
149
|
|
|
try { |
|
150
|
|
|
$organization = Organization::create($data); |
|
151
|
|
|
} catch (\Illuminate\Database\QueryException $e) { |
|
|
|
|
|
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
$organization->users()->sync([Auth::id()]); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
return $organization->id; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
public function readCollaborators($owner, $repo, $providerId = null) |
|
161
|
|
|
{ |
|
162
|
|
|
$collaborators = Helper::request('https://api.github.com/repos/'.$owner.'/'.$repo.'/collaborators'); |
|
163
|
|
|
foreach ($collaborators as $collaborator) { |
|
164
|
|
|
if (isset($collaborator->id)) { |
|
165
|
|
|
$data = [ |
|
166
|
|
|
'provider_id' => $collaborator->id, |
|
167
|
|
|
'username' => $collaborator->login, |
|
168
|
|
|
'name' => $collaborator->login, |
|
169
|
|
|
'avatar' => $collaborator->avatar_url, |
|
170
|
|
|
'html_url' => $collaborator->html_url, |
|
171
|
|
|
'email' => null, |
|
172
|
|
|
'remember_token' => null, |
|
173
|
|
|
'bio' => null, |
|
174
|
|
|
'location' => null, |
|
175
|
|
|
'blog' => null, |
|
176
|
|
|
'since' => null, |
|
177
|
|
|
'token' => null, |
|
178
|
|
|
'position_held' => null, |
|
179
|
|
|
]; |
|
180
|
|
|
|
|
181
|
|
|
try { |
|
182
|
|
|
$user = User::create($data); |
|
183
|
|
|
} catch (\Exception $e) { |
|
184
|
|
|
$user = User::where('username', $collaborator->login) |
|
185
|
|
|
->where('provider', 'github')->first(); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
$userId[] = $user->id; |
|
|
|
|
|
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
$organization = Organization::where('username', $owner) |
|
193
|
|
|
->where('provider', 'github')->first()->users(); |
|
194
|
|
|
|
|
195
|
|
|
if (!$organization->userActive()->count()) { |
|
196
|
|
|
$organization->attach($userId); |
|
|
|
|
|
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
public function createBranches($owner, $product_backlog_id, $repo, $providerId = null) |
|
201
|
|
|
{ |
|
202
|
|
|
$y = 0; |
|
203
|
|
|
for ($i = 1; $i > $y; ++$i) { |
|
204
|
|
|
$branches = Helper::request('https://api.github.com/repos/'.$owner.DIRECTORY_SEPARATOR.$repo.'/branches?page='.$i); |
|
205
|
|
|
foreach ($branches as $branch) { |
|
206
|
|
|
$data = [ |
|
207
|
|
|
'product_backlog_id' => $product_backlog_id, |
|
208
|
|
|
'title' => $branch->name, |
|
209
|
|
|
'sha' => $branch->commit->sha, |
|
210
|
|
|
]; |
|
211
|
|
|
Branch::create($data); |
|
212
|
|
|
} |
|
213
|
|
|
if (count($branches) < 30) { |
|
214
|
|
|
$y = $i + 2; |
|
215
|
|
|
} |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
public function readIssues() |
|
220
|
|
|
{ |
|
221
|
|
|
$repos = ProductBacklog::all(); |
|
222
|
|
|
|
|
223
|
|
|
foreach ($repos as $repo) { |
|
224
|
|
|
$issues = Helper::request('https://api.github.com/repos/'.$repo->organization->username. |
|
225
|
|
|
DIRECTORY_SEPARATOR.$repo->title.'/issues?state=all'); |
|
226
|
|
|
|
|
227
|
|
|
$issues = is_array($issues) ? $issues : [$issues]; |
|
228
|
|
|
|
|
229
|
|
|
foreach ($issues as $issue) { |
|
230
|
|
|
try { |
|
231
|
|
|
$data = $this->tplIssue($issue, $repo->id); |
|
232
|
|
|
} catch (\Exception $e) { |
|
|
|
|
|
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
if (!Issue::where('provider_id', $issue->id)->where('provider', 'github')->first()) { |
|
236
|
|
|
Issue::create($data)->users()->sync([$data['user_id']]); |
|
237
|
|
|
} |
|
238
|
|
|
} |
|
239
|
|
|
} |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
public function createOrUpdateIssue($obj) |
|
|
|
|
|
|
243
|
|
|
{ |
|
244
|
|
|
$params = [ |
|
245
|
|
|
'title' => $obj->title, |
|
246
|
|
|
'body' => $obj->description, |
|
247
|
|
|
]; |
|
248
|
|
|
|
|
249
|
|
|
$response = Helper::request('https://api.github.com/repos/'. |
|
250
|
|
|
$obj->productBacklog->organization->username.DIRECTORY_SEPARATOR. |
|
251
|
|
|
$obj->productBacklog->title.'/issues'.(isset($obj->number) ? DIRECTORY_SEPARATOR.$obj->number : ''), |
|
252
|
|
|
true, 'POST', $params); |
|
253
|
|
|
|
|
254
|
|
|
return (object) $response; |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
public function createOrUpdateIssueComment($obj, $verb = 'POST') |
|
|
|
|
|
|
258
|
|
|
{ |
|
259
|
|
|
$params = [ |
|
260
|
|
|
'body' => $obj->comment, |
|
261
|
|
|
]; |
|
262
|
|
|
|
|
263
|
|
|
$response = Helper::request('https://api.github.com/repos/'. |
|
264
|
|
|
$obj->issue->productBacklog->organization->username.DIRECTORY_SEPARATOR. |
|
265
|
|
|
$obj->issue->productBacklog->title.'/issues'.(isset($obj->provider_id) ? '' : DIRECTORY_SEPARATOR.$obj->issue->number).'/comments'. |
|
266
|
|
|
(isset($obj->provider_id) ? DIRECTORY_SEPARATOR.$obj->provider_id : ''), |
|
267
|
|
|
true, $verb, $params); |
|
268
|
|
|
|
|
269
|
|
|
return (object) $response; |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
public function deleteIssueComment($obj) |
|
|
|
|
|
|
273
|
|
|
{ |
|
274
|
|
|
return $this->createOrUpdateIssueComment($obj, 'DELETE'); |
|
275
|
|
|
} |
|
276
|
|
|
} |
|
277
|
|
|
|
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.