Completed
Pull Request — master (#97)
by Renato
02:38
created

Gitlab::readIssues()   B

Complexity

Conditions 6
Paths 13

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 22
rs 8.6737
cc 6
eloc 12
nc 13
nop 0
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 Carbon\Carbon;
11
use GitScrum\Contracts\ProviderInterface;
12
13
class Gitlab implements ProviderInterface
14
{
15
    private $gitlabGroups;
16
17
    public function tplUser($obj)
18
    {
19
        return [
20
            'provider_id' => $obj->id,
21
            'provider' => 'gitlab',
22
            'username' => $obj->nickname,
23
            'name' => $obj->name,
24
            'token' => $obj->token,
25
            'avatar' => @$obj->user['avatar_url'],
26
            'html_url' => @$obj->user['web_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)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

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 @return annotation as described here.

Loading history...
36
    {
37
        $organization = $this->organization($repo);
38
39
        if (!$organization) {
40
          return;
41
        }
42
43
        return (object) [
44
            'provider_id' => $repo->id,
45
            'organization_id' => $organization->id,
46
            'organization_title' => $organization->username,
47
            'slug' => $slug ? $slug : Helper::slug($repo->path),
48
            'title' => $repo->path,
49
            'fullname' => $repo->name,
50
            'is_private' => $repo->public == true,
51
            'html_url' => $repo->http_url_to_repo,
52
            'description' => $repo->description,
53
            'fork' => null,
54
            'url' => $repo->web_url,
55
            'since' => Carbon::parse($repo->created_at)->toDateTimeString(),
56
            'pushed_at' => Carbon::parse($repo->last_activity_at)->toDateTimeString(),
57
            'ssh_url' => $repo->ssh_url_to_repo,
58
            'clone_url' => $repo->ssh_url_to_repo,
59
            'homepage' => $repo->web_url,
60
            'default_branch' => $repo->default_branch,
61
        ];
62
    }
63
64
    public function tplIssue($obj, $productBacklogId)
65
    {
66
        $user = User::where('username', @$obj->assignee->username)
67
            ->where('provider', 'gitlab')->first();
68
69
        return [
70
            'provider_id' => $obj->id,
71
            'user_id' => isset($user->id) ? $user->id : Auth::user()->id,
72
            'product_backlog_id' => $productBacklogId,
73
            'effort' => 0,
74
            'config_issue_effort_id' => 1,
75
            'issue_type_id' => 1,
76
            'number' => $obj->iid,
77
            'title' => $obj->title,
78
            'description' => $obj->description,
79
            'state' => $obj->state,
80
            'html_url' => $obj->web_url,
81
            'created_at' => Carbon::parse($obj->created_at)->toDateTimeString(),
82
            'updated_at' => Carbon::parse($obj->updated_at)->toDateTimeString(),
83
        ];
84
    }
85
86
    public function readRepositories()
87
    {
88
        $repos = collect(Helper::request(env('GITLAB_INSTANCE_URI').'api/v3/projects?access_token='.Auth::user()->token));
89
90
        $response = $repos->map(function ($repo) {
91
            return $this->tplRepository($repo);
92
        });
93
94
        return $response;
95
    }
96
97
    public function createOrUpdateRepository($owner, $obj, $oldTitle = null)
98
    {
99
    }
100
101
    public function organization($obj)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

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 @return annotation as described here.

Loading history...
102
    {
103
104
        if (!isset($obj->owner) && !isset($obj->namespace)) {
105
            return false;
106
        }
107
108
        if (!isset($obj->owner) && isset($obj->namespace)) {
109
            // To avoid to make unnecessary calls to the api to get the groups info saving the fetched groups into a private variable
110
            if (!isset($this->gitlabGroups[$obj->namespace->id])) {
111
                $group = current(collect(Helper::request(env('GITLAB_INSTANCE_URI').'api/v3/groups/'.$obj->namespace->id.'?access_token='.Auth::user()->token)));
112
113
                $this->gitlabGroups[$obj->namespace->id] = $group;
114
            }
115
116
            $group = $this->gitlabGroups[$obj->namespace->id];
117
118
            $obj->owner = new \stdClass;
119
            $obj->owner->id = $group['id'];
120
            $obj->owner->username = $group['path'];
121
            $obj->owner->web_url = $group['web_url'];
122
            $obj->owner->avatar_url = $group['avatar_url'];
123
        }
124
125
        $data = [
126
            'provider_id' => $obj->owner->id,
127
            'username' => $obj->owner->username,
128
            'url' => $obj->owner->web_url,
129
            'repos_url' => null,
130
            'events_url' => null,
131
            'hooks_url' => null,
132
            'issues_url' => null,
133
            'members_url' => null,
134
            'public_members_url' => null,
135
            'avatar_url' => $obj->owner->avatar_url,
136
            'description' => null,
137
            'title' => $obj->owner->username,
138
            'blog' => null,
139
            'location' => null,
140
            'email' => null,
141
            'public_repos' => null,
142
            'html_url' => null,
143
            'total_private_repos' => null,
144
            'since' => @Carbon::parse($obj->namespace->created_at)->toDateTimeString(),
145
            'disk_usage' => null,
146
        ];
147
148
        try {
149
            $organization = Organization::create($data);
150
        } catch (\Illuminate\Database\QueryException $e) {
151
            $organization = Organization::where('username', $data['username'])
152
                ->where('provider', 'gitlab')->first();
153
        }
154
155
        $organization->users()->sync([Auth::id()]);
156
157
        return $organization;
158
    }
159
160
    public function readCollaborators($owner, $repo)
161
    {
162
    }
163
164
    public function createBranches($owner, $product_backlog_id, $repo)
165
    {
166
    }
167
168
    public function readIssues()
169
    {
170
        $repos = ProductBacklog::all();
171
172
        foreach ($repos as $repo) {
173
            $issues = Helper::request(env('GITLAB_INSTANCE_URI').'api/v3/projects/'.$repo->provider_id.
174
                '/issues?access_token='.Auth::user()->token);
175
176
            $issues = is_array($issues) ? $issues : [$issues];
177
178
            foreach ($issues as $issue) {
179
                try{
180
                    $data = $this->tplIssue($issue, $repo->id);
181
                    if (!Issue::where('provider_id', $data['provider_id'])->where('provider', 'gitlab')->first()) {
182
                        Issue::create($data)->users()->sync([$data['user_id']]);
183
                    }
184
                } catch( \Exception $e){
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
185
186
                }
187
            }
188
        }
189
    }
190
191
    public function createOrUpdateIssue($obj)
192
    {
193
    }
194
195
    public function createOrUpdateIssueComment($obj, $verb = 'POST')
196
    {
197
    }
198
199
    public function deleteIssueComment($obj)
200
    {
201
    }
202
}
203