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

Github::deleteIssueComment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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
        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 templateIssue($obj, $productBracklogId)
59
    {
60
        $user = User::where('username', $obj->user->login)->first();
61
62
        return [
63
            'provider_id' => $obj->id,
64
            'user_id' => isset($user->id) ? $user->id : Auth::user()->id,
65
            'product_backlog_id' => $productBracklogId,
66
            'effort' => 0,
67
            'config_issue_effort_id' => 1,
68
            'issue_type_id' => 1,
69
            'number' => $obj->number,
70
            'title' => $obj->title,
71
            'description' => $obj->body,
72
            'state' => $obj->state,
73
            'html_url' => $obj->html_url,
74
            'created_at' => $obj->created_at,
75
            'updated_at' => $obj->updated_at,
76
        ];
77
    }
78
79
    public function readRepositories()
80
    {
81
        $repos = collect(Helper::request('https://api.github.com/user/repos'));
82
83
        $response = $repos->map(function ($repo) {
84
            return $this->templateRepository($repo);
85
        });
86
87
        return $response;
88
    }
89
90
    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...
91
    {
92
        $params = [
93
            'name' => str_slug($obj->title, '-'),
94
            'description' => $obj->description,
95
        ];
96
97
        if (is_null($oldTitle)) {
98
            $endpoint = 'https://api.github.com/orgs/'.$owner.'/repos';
99
100
            if (Auth::user()->username == $owner) {
101
                $endpoint = 'https://api.github.com/user/repos';
102
            }
103
104
            $response = Helper::request($endpoint, true, 'POST', $params);
105
        } else {
106
            $oldTitle = str_slug($oldTitle, '-');
107
            $response = Helper::request('https://api.github.com/repos/'.$owner.DIRECTORY_SEPARATOR.$oldTitle, true, 'POST', $params);
108
        }
109
110
        return (object) $response;
111
    }
112
113
    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...
114
    {
115
        $orgData = Helper::request('https://api.github.com/orgs/'.$login);
116
117
        if (!isset($orgData->id)) {
118
            $orgData = Helper::request('https://api.github.com/users/'.$login);
119
        }
120
121
        $data = [
122
            'provider_id' => @$orgData->id,
123
            'username' => @$orgData->login,
124
            'url' => @$orgData->url,
125
            'repos_url' => @$orgData->repos_url,
126
            'events_url' => @$orgData->events_url,
127
            'hooks_url' => @$orgData->hooks_url,
128
            'issues_url' => @$orgData->issues_url,
129
            'members_url' => @$orgData->members_url,
130
            'public_members_url' => @$orgData->public_members_url,
131
            'avatar_url' => @$orgData->avatar_url,
132
            'description' => @$orgData->description,
133
            'title' => @$orgData->name,
134
            'blog' => @$orgData->blog,
135
            'location' => @$orgData->location,
136
            'email' => @$orgData->email,
137
            'public_repos' => @$orgData->public_repos,
138
            'html_url' => @$orgData->html_url,
139
            'total_private_repos' => @$orgData->total_private_repos,
140
            'since' => @Carbon::parse($orgData->created_at)->toDateTimeString(),
141
            'disk_usage' => @$orgData->disk_usage,
142
        ];
143
144
        try {
145
            $organization = Organization::create($data);
146
        } catch (\Illuminate\Database\QueryException $e) {
147
            $organization = Organization::where('username', $orgData->login)->first();
148
        }
149
150
        $organization->users()->sync([Auth::id()]);
151
152
        return $organization->id;
153
    }
154
155
    public function readCollaborators($owner, $repo)
156
    {
157
        $collaborators = Helper::request('https://api.github.com/repos/'.$owner.'/'.$repo.'/collaborators');
158
        foreach ($collaborators as $collaborator) {
159
            if (isset($collaborator->id)) {
160
                $data = [
161
                    'provider_id' => $collaborator->id,
162
                    'username' => $collaborator->login,
163
                    'name' => $collaborator->login,
164
                    'avatar' => $collaborator->avatar_url,
165
                    'html_url' => $collaborator->html_url,
166
                    'email' => null,
167
                    'remember_token' => null,
168
                    'bio' => null,
169
                    'location' => null,
170
                    'blog' => null,
171
                    'since' => null,
172
                    'token' => null,
173
                    'position_held' => null,
174
                ];
175
176
                try {
177
                    $user = User::create($data);
178
                } catch (\Exception $e) {
179
                    $user = User::where('username', $collaborator->login)->first();
180
                }
181
182
                $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...
183
            }
184
        }
185
186
        $organization = Organization::where('username', $owner)->first()->users();
187
        $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...
188
    }
189
190
    public function createBranches($owner, $product_backlog_id, $repo)
191
    {
192
        $y = 0;
193
        for ($i = 1; $i > $y; ++$i) {
194
            $branches = Helper::request('https://api.github.com/repos/'.$owner.DIRECTORY_SEPARATOR.$repo.'/branches?page='.$i);
195
            foreach ($branches as $branch) {
196
                $data = [
197
                    'product_backlog_id' => $product_backlog_id,
198
                    'title' => $branch->name,
199
                    'sha' => $branch->commit->sha,
200
                ];
201
                Branch::create($data);
202
            }
203
            if (count($branches) < 30) {
204
                $y = $i + 2;
205
            }
206
        }
207
    }
208
209
    public function readIssues()
210
    {
211
        $repos = ProductBacklog::all();
212
213
        foreach ($repos as $repo) {
214
            $issues = Helper::request('https://api.github.com/repos/'.$repo->organization->username.
215
                DIRECTORY_SEPARATOR.$repo->title.'/issues?state=all');
216
217
            $issues = is_array($issues) ? $issues : [$issues];
218
219
            foreach ($issues as $issue) {
220
                try {
221
                    $data = $this->templateIssue($issue, $repo->id);
222
                } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
223
                }
224
225
                if (!Issue::where('provider_id', $issue->id)->first()) {
226
                    Issue::create($data)->users()->sync([$data['user_id']]);
227
                }
228
            }
229
        }
230
    }
231
232
    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...
233
    {
234
        $params = [
235
            'title' => $obj->title,
236
            'body' => $obj->description,
237
        ];
238
239
        $response = Helper::request('https://api.github.com/repos/'.
240
            $obj->productBacklog->organization->username.DIRECTORY_SEPARATOR.
241
            $obj->productBacklog->title.'/issues'.(isset($obj->number) ? DIRECTORY_SEPARATOR.$obj->number : ''),
242
            true, 'POST', $params);
243
244
        return (object) $response;
245
    }
246
247
    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...
248
    {
249
        $params = [
250
            'body' => $obj->comment,
251
        ];
252
253
        $response = Helper::request('https://api.github.com/repos/'.
254
            $obj->issue->productBacklog->organization->username.DIRECTORY_SEPARATOR.
255
            $obj->issue->productBacklog->title.'/issues'.(isset($obj->provider_id) ? '' : DIRECTORY_SEPARATOR.$obj->issue->number).'/comments'.
256
            (isset($obj->provider_id) ? DIRECTORY_SEPARATOR.$obj->provider_id : ''),
257
            true, $verb, $params);
258
259
        return (object) $response;
260
    }
261
262
    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...
263
    {
264
        return $this->createOrUpdateIssueComment($obj, 'DELETE');
265
    }
266
}
267