Completed
Pull Request — master (#86)
by Renato
02:36
created

Gitlab::deleteIssueComment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 1
nc 1
nop 1
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 templateUser($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 templateRepository($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 templateIssue($obj, $productBacklogId)
57
    {
58
        $user = User::where('username', $obj->assignee->username)->first();
59
60
        return [
61
            'provider_id' => $obj->id,
62
            'user_id' => isset($user->id) ? $user->id : Auth::user()->id,
63
            'product_backlog_id' => $productBacklogId,
64
            'effort' => 0,
65
            'config_issue_effort_id' => 1,
66
            'issue_type_id' => 1,
67
            'number' => $obj->iid,
68
            'title' => $obj->title,
69
            'description' => $obj->description,
70
            'state' => $obj->state,
71
            'html_url' => $obj->web_url,
72
            'created_at' => Carbon::parse($obj->created_at)->toDateTimeString(),
73
            'updated_at' => Carbon::parse($obj->updated_at)->toDateTimeString(),
74
        ];
75
    }
76
77
    public function readRepositories()
78
    {
79
        $repos = collect(Helper::request(env('GITLAB_INSTANCE_URI').'api/v3/projects?access_token='.Auth::user()->token));
80
81
        $response = $repos->map(function ($repo) {
82
            return $this->templateRepository($repo);
83
        });
84
85
        return $response;
86
    }
87
88
    public function createOrUpdateRepository($owner, $obj, $oldTitle = null)
89
    {
90
    }
91
92
    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...
93
    {
94
        $data = [
95
            'provider_id' => $obj->owner->id,
96
            'username' => $obj->owner->username,
97
            'url' => $obj->owner->web_url,
98
            'repos_url' => null,
99
            'events_url' => null,
100
            'hooks_url' => null,
101
            'issues_url' => null,
102
            'members_url' => null,
103
            'public_members_url' => null,
104
            'avatar_url' => $obj->owner->avatar_url,
105
            'description' => null,
106
            'title' => $obj->owner->username,
107
            'blog' => null,
108
            'location' => null,
109
            'email' => null,
110
            'public_repos' => null,
111
            'html_url' => null,
112
            'total_private_repos' => null,
113
            'since' => @Carbon::parse($obj->namespace->created_at)->toDateTimeString(),
114
            'disk_usage' => null,
115
        ];
116
117
        try {
118
            $organization = Organization::create($data);
119
        } catch (\Illuminate\Database\QueryException $e) {
120
            $organization = Organization::where('username', $data['username'])->first();
121
        }
122
123
        $organization->users()->sync([Auth::id()]);
124
125
        return $organization->id;
126
    }
127
128
    public function readCollaborators($owner, $repo)
129
    {
130
    }
131
132
    public function createBranches($owner, $product_backlog_id, $repo)
133
    {
134
    }
135
136
    public function readIssues()
137
    {
138
        $repos = ProductBacklog::all();
139
140
        foreach ($repos as $repo) {
141
            $issues = Helper::request(env('GITLAB_INSTANCE_URI').'api/v3/projects/'.$repo->provider_id.
142
                '/issues?access_token='.Auth::user()->token);
143
144
            $issues = is_array($issues) ? $issues : [$issues];
145
146
            foreach ($issues as $issue) {
147
                try {
148
                    $data = $this->templateIssue($issue, $repo->id);
149
                } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
150
                }
151
152
                if (!Issue::where('provider_id', $issue->id)->first()) {
153
                    Issue::create($data)->users()->sync([$data['user_id']]);
154
                }
155
            }
156
        }
157
    }
158
159
    public function createOrUpdateIssue($obj)
160
    {
161
    }
162
163
    public function createOrUpdateIssueComment($obj, $verb = 'POST')
164
    {
165
    }
166
167
    public function deleteIssueComment($obj)
168
    {
169
    }
170
}
171