Completed
Pull Request — master (#94)
by Renato
02:46
created

Gitlab::templateRepository()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 22
rs 9.2
cc 2
eloc 19
nc 2
nop 2
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
    public function tplUser($obj)
16
    {
17
        return [
18
            'provider_id' => $obj->id,
19
            'provider' => 'gitlab',
20
            'username' => $obj->nickname,
21
            'name' => $obj->name,
22
            'token' => $obj->token,
23
            'avatar' => @$obj->user['avatar_url'],
24
            'html_url' => @$obj->user['web_url'],
25
            'bio' => @$obj->user['bio'],
26
            'since' => Carbon::parse($obj->user['created_at'])->toDateTimeString(),
27
            'location' => @$obj->user['location'],
28
            'blog' => @$obj->user['blog'],
29
            'email' => $obj->email,
30
        ];
31
    }
32
33
    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...
34
    {
35
        return (object) [
36
            'provider_id' => $repo->id,
37
            'organization_id' => $this->organization($repo),
38
            'organization_title' => $repo->owner->username,
39
            'slug' => $slug ? $slug : Helper::slug($repo->path),
40
            'title' => $repo->path,
41
            'fullname' => $repo->name,
42
            'is_private' => $repo->public == true,
43
            'html_url' => $repo->http_url_to_repo,
44
            'description' => $repo->description,
45
            'fork' => null,
46
            'url' => $repo->web_url,
47
            'since' => Carbon::parse($repo->created_at)->toDateTimeString(),
48
            'pushed_at' => Carbon::parse($repo->last_activity_at)->toDateTimeString(),
49
            'ssh_url' => $repo->ssh_url_to_repo,
50
            'clone_url' => $repo->ssh_url_to_repo,
51
            'homepage' => $repo->web_url,
52
            'default_branch' => $repo->default_branch,
53
        ];
54
    }
55
56
    public function tplIssue($obj, $productBacklogId)
57
    {
58
        $user = User::where('username', @$obj->assignee->username)
59
            ->where('provider', 'gitlab')->first();
60
61
        return [
62
            'provider_id' => $obj->id,
63
            'user_id' => isset($user->id) ? $user->id : Auth::user()->id,
64
            'product_backlog_id' => $productBacklogId,
65
            'effort' => 0,
66
            'config_issue_effort_id' => 1,
67
            'issue_type_id' => 1,
68
            'number' => $obj->iid,
69
            'title' => $obj->title,
70
            'description' => $obj->description,
71
            'state' => $obj->state,
72
            'html_url' => $obj->web_url,
73
            'created_at' => Carbon::parse($obj->created_at)->toDateTimeString(),
74
            'updated_at' => Carbon::parse($obj->updated_at)->toDateTimeString(),
75
        ];
76
    }
77
78
    public function readRepositories()
79
    {
80
        $repos = collect(Helper::request(env('GITLAB_INSTANCE_URI').'api/v3/projects?access_token='.Auth::user()->token));
81
82
        $response = $repos->map(function ($repo) {
83
            return $this->tplRepository($repo);
84
        });
85
86
        return $response;
87
    }
88
89
    public function createOrUpdateRepository($owner, $obj, $oldTitle = null)
90
    {
91
    }
92
93
    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...
94
    {
95
        $data = [
96
            'provider_id' => $obj->owner->id,
97
            'username' => $obj->owner->username,
98
            'url' => $obj->owner->web_url,
99
            'repos_url' => null,
100
            'events_url' => null,
101
            'hooks_url' => null,
102
            'issues_url' => null,
103
            'members_url' => null,
104
            'public_members_url' => null,
105
            'avatar_url' => $obj->owner->avatar_url,
106
            'description' => null,
107
            'title' => $obj->owner->username,
108
            'blog' => null,
109
            'location' => null,
110
            'email' => null,
111
            'public_repos' => null,
112
            'html_url' => null,
113
            'total_private_repos' => null,
114
            'since' => @Carbon::parse($obj->namespace->created_at)->toDateTimeString(),
115
            'disk_usage' => null,
116
        ];
117
118
        try {
119
            $organization = Organization::create($data);
120
        } catch (\Illuminate\Database\QueryException $e) {
121
            $organization = Organization::where('username', $data['username'])
122
                ->where('provider', 'gitlab')->first();
123
        }
124
125
        $organization->users()->sync([Auth::id()]);
126
127
        return $organization->id;
128
    }
129
130
    public function readCollaborators($owner, $repo)
131
    {
132
    }
133
134
    public function createBranches($owner, $product_backlog_id, $repo)
135
    {
136
    }
137
138
    public function readIssues()
139
    {
140
        $repos = ProductBacklog::all();
141
142
        foreach ($repos as $repo) {
143
            $issues = Helper::request(env('GITLAB_INSTANCE_URI').'api/v3/projects/'.$repo->provider_id.
144
                '/issues?access_token='.Auth::user()->token);
145
146
            $issues = is_array($issues) ? $issues : [$issues];
147
148
            foreach ($issues as $issue) {
149
                try{
150
                    $data = $this->tplIssue($issue, $repo->id);
151
                    if (!Issue::where('provider_id', $data['provider_id'])->where('provider', 'gitlab')->first()) {
152
                        Issue::create($data)->users()->sync([$data['user_id']]);
153
                    }
154
                } catch( \Exception $e){
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
155
156
                }
157
            }
158
        }
159
    }
160
161
    public function createOrUpdateIssue($obj)
162
    {
163
    }
164
165
    public function createOrUpdateIssueComment($obj, $verb = 'POST')
166
    {
167
    }
168
169
    public function deleteIssueComment($obj)
170
    {
171
    }
172
}
173