Completed
Push — master ( df10dc...573db1 )
by Renato
02:31
created

Github::readIssues()   B

Complexity

Conditions 6
Paths 11

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 22
rs 8.6737
cc 6
eloc 12
nc 11
nop 0
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 templateUser($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 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...
36
    {
37
38
        return (object) [
39
            'provider_id' => $repo->id,
40
            'organization_id' => $this->organization($repo->owner->login),
41
            'organization_title' => $repo->owner->login,
42
            'slug' => $slug ? $slug : Helper::slug($repo->name),
43
            'title' => $repo->name,
44
            'fullname' => $repo->full_name,
45
            'is_private' => $repo->private,
46
            'html_url' => $repo->html_url,
47
            'description' => $repo->description,
48
            'fork' => $repo->fork,
49
            'url' => $repo->url,
50
            'since' => Carbon::parse($repo->created_at)->toDateTimeString(),
51
            'pushed_at' => Carbon::parse($repo->pushed_at)->toDateTimeString(),
52
            'ssh_url' => $repo->ssh_url,
53
            'clone_url' => $repo->clone_url,
54
            'homepage' => $repo->homepage,
55
            'default_branch' => $repo->default_branch,
56
        ];
57
58
    }
59
60
    public function templateIssue($obj, $productBracklogId)
61
    {
62
        $user = User::where('username', $obj->user->login)->first();
63
64
        return [
65
            'provider_id' => $obj->id,
66
            'user_id' => isset($user->id) ? $user->id : Auth::user()->id,
67
            'product_backlog_id' => $productBracklogId,
68
            'effort' => 0,
69
            'config_issue_effort_id' => 1,
70
            'issue_type_id' => 1,
71
            'number' => $obj->number,
72
            'title' => $obj->title,
73
            'description' => $obj->body,
74
            'state' => $obj->state,
75
            'html_url' => $obj->html_url,
76
            'created_at' => $obj->created_at,
77
            'updated_at' => $obj->updated_at,
78
        ];
79
    }
80
81
    public function readRepositories()
82
    {
83
        $repos = collect(Helper::request('https://api.github.com/user/repos'));
84
85
        $response = $repos->map(function ($repo) {
86
            return $this->templateRepository($repo);
87
        });
88
89
        return $response;
90
    }
91
92
    public function createOrUpdateRepository($owner, $obj, $oldTitle = null)
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
        $params = [
95
            'name' => str_slug($obj->title, '-'),
96
            'description' => $obj->description,
97
        ];
98
99
        if (is_null($oldTitle)) {
100
            $endpoint = 'https://api.github.com/orgs/'.$owner.'/repos';
101
102
            if (Auth::user()->username == $owner) {
103
                $endpoint = 'https://api.github.com/user/repos';
104
            }
105
106
            $response = Helper::request($endpoint, true, 'POST', $params);
107
        } else {
108
            $oldTitle = str_slug($oldTitle, '-');
109
            $response = Helper::request('https://api.github.com/repos/'.$owner.DIRECTORY_SEPARATOR.$oldTitle, true, 'POST', $params);
110
        }
111
112
        return (object) $response;
113
    }
114
115
    public function organization($login)
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...
116
    {
117
118
        $organization = Organization::where('username', $login)->first();
119
120
        if( !isset($organization) )
121
        {
122
123
            $orgData = Helper::request('https://api.github.com/orgs/'.$login);
124
125
            if (!isset($orgData->id)) {
126
                $orgData = Helper::request('https://api.github.com/users/'.$login);
127
            }
128
129
            $data = [
130
                'provider_id' => @$orgData->id,
131
                'username' => @$orgData->login,
132
                'url' => @$orgData->url,
133
                'repos_url' => @$orgData->repos_url,
134
                'events_url' => @$orgData->events_url,
135
                'hooks_url' => @$orgData->hooks_url,
136
                'issues_url' => @$orgData->issues_url,
137
                'members_url' => @$orgData->members_url,
138
                'public_members_url' => @$orgData->public_members_url,
139
                'avatar_url' => @$orgData->avatar_url,
140
                'description' => @$orgData->description,
141
                'title' => @$orgData->name,
142
                'blog' => @$orgData->blog,
143
                'location' => @$orgData->location,
144
                'email' => @$orgData->email,
145
                'public_repos' => @$orgData->public_repos,
146
                'html_url' => @$orgData->html_url,
147
                'total_private_repos' => @$orgData->total_private_repos,
148
                'since' => @Carbon::parse($orgData->created_at)->toDateTimeString(),
149
                'disk_usage' => @$orgData->disk_usage,
150
            ];
151
152
            try {
153
                $organization = Organization::create($data);
154
            } catch (\Illuminate\Database\QueryException $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
            $organization->users()->sync([Auth::id()]);
159
160
        }
161
162
        return $organization->id;
163
    }
164
165
    public function readCollaborators($owner, $repo)
166
    {
167
        $collaborators = Helper::request('https://api.github.com/repos/'.$owner.'/'.$repo.'/collaborators');
168
        foreach ($collaborators as $collaborator) {
169
            if (isset($collaborator->id)) {
170
                $data = [
171
                    'provider_id' => $collaborator->id,
172
                    'username' => $collaborator->login,
173
                    'name' => $collaborator->login,
174
                    'avatar' => $collaborator->avatar_url,
175
                    'html_url' => $collaborator->html_url,
176
                    'email' => null,
177
                    'remember_token' => null,
178
                    'bio' => null,
179
                    'location' => null,
180
                    'blog' => null,
181
                    'since' => null,
182
                    'token' => null,
183
                    'position_held' => null,
184
                ];
185
186
                try {
187
                    $user = User::create($data);
188
                } catch (\Exception $e) {
189
                    $user = User::where('username', $collaborator->login)->first();
190
                }
191
192
                $userId[] = $user->id;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$userId was never initialized. Although not strictly required by PHP, it is generally a good practice to add $userId = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
193
            }
194
        }
195
196
        $organization = Organization::where('username', $owner)->first()->users();
197
        $organization->sync($userId);
0 ignored issues
show
Bug introduced by
The variable $userId does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
198
    }
199
200
    public function createBranches($owner, $product_backlog_id, $repo)
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->templateIssue($issue, $repo->id);
232
                } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
233
                }
234
235
                if (!Issue::where('provider_id', $issue->id)->first()) {
236
                    Issue::create($data)->users()->sync([$data['user_id']]);
237
                }
238
            }
239
        }
240
    }
241
242
    public function createOrUpdateIssue($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...
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')
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...
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)
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...
273
    {
274
        return $this->createOrUpdateIssueComment($obj, 'DELETE');
275
    }
276
}
277