Github::createPullRequestComment()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 28

Duplication

Lines 28
Ratio 100 %

Importance

Changes 0
Metric Value
dl 28
loc 28
rs 9.472
c 0
b 0
f 0
cc 2
nc 2
nop 6
1
<?php
2
3
namespace Fabrica\Helper;
4
5
use Fabrica\Tools\Config;
6
use GuzzleHttp\Client;
7
8
/**
9
 * The Github Helper class provides some Github API call functionality.
10
 */
11
class Github
12
{
13
    /**
14
     * Create a comment on a specific file (and commit) in a Github Pull Request.
15
     *
16
     * @param  $repo
17
     * @param  $pullId
18
     * @param  $commitId
19
     * @param  $file
20
     * @param  $line
21
     * @param  $comment
22
     * @return null
23
     */
24 View Code Duplication
    public function createPullRequestComment($repo, $pullId, $commitId, $file, $line, $comment)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
25
    {
26
        $token = Config::getInstance()->get('php-censor.github.token');
27
28
        if (!$token) {
29
            return null;
30
        }
31
32
        $url = '/repos/' . strtolower($repo) . '/pulls/' . $pullId . '/comments';
33
34
        $params = [
35
            'body'      => $comment,
36
            'commit_id' => $commitId,
37
            'path'      => $file,
38
            'position'  => $line,
39
        ];
40
41
        $client = new Client();
42
        $client->post(
43
            ('https://api.github.com' . $url), [
44
            'headers' => [
45
                'Authorization' => 'Basic ' . base64_encode($token . ':x-oauth-basic'),
46
                'Content-Type'  => 'application/x-www-form-urlencoded'
47
            ],
48
            'json' => $params,
49
            ]
50
        );
51
    }
52
53
    /**
54
     * Create a comment on a Github commit.
55
     *
56
     * @param  $repo
57
     * @param  $commitId
58
     * @param  $file
59
     * @param  $line
60
     * @param  $comment
61
     * @return null
62
     */
63 View Code Duplication
    public function createCommitComment($repo, $commitId, $file, $line, $comment)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64
    {
65
        $token = Config::getInstance()->get('php-censor.github.token');
66
67
        if (!$token) {
68
            return null;
69
        }
70
71
        $url = '/repos/' . strtolower($repo) . '/commits/' . $commitId . '/comments';
72
73
        $params = [
74
            'body'     => $comment,
75
            'path'     => $file,
76
            'position' => $line,
77
        ];
78
79
        $client = new Client();
80
        $client->post(
81
            ('https://api.github.com' . $url), [
82
            'headers' => [
83
                'Authorization' => 'Basic ' . base64_encode($token . ':x-oauth-basic'),
84
                'Content-Type'  => 'application/x-www-form-urlencoded'
85
            ],
86
            'json' => $params,
87
            ]
88
        );
89
    }
90
}
91