Completed
Push — master ( ff6e90...57ee5c )
by Andrii
03:56
created

GitHub::setName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Automation tool mixed with code generator for easier continuous development
4
 *
5
 * @link      https://github.com/hiqdev/hidev
6
 * @package   hidev
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2018, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hidev\components;
12
13
use hidev\helpers\Helper;
14
use yii\helpers\Json;
15
16
/**
17
 * GitHub component.
18
 */
19
class GitHub extends \hidev\base\Component
20
{
21
    protected $_name;
22
    protected $_vendor;
23
    protected $_description;
24
    protected $_vendorType;
25
26
    /**
27
     * @var string GitHub OAuth access token
28
     */
29
    protected $_token;
30
31
    public function setFull_name($value)
32
    {
33
        list($this->_vendor, $this->_name) = explode('/', $value, 2);
34
    }
35
36
    public function getFull_name()
37
    {
38
        return $this->getVendor() . '/' . $this->getName();
39
    }
40
41
    public function setFullName($value)
42
    {
43
        return $this->setFull_name($value);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->setFull_name($value) targeting hidev\components\GitHub::setFull_name() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
44
    }
45
46
    public function getFullName()
47
    {
48
        return $this->getFull_name();
49
    }
50
51
    public function setName($value)
52
    {
53
        $this->_name = $value;
54
    }
55
56
    public function getName()
57
    {
58
        if (!$this->_name) {
59
            $this->_name = $this->take('package')->name;
60
        }
61
62
        return $this->_name;
63
    }
64
65
    public function setVendorType($value)
66
    {
67
        $this->_vendorType = $value;
68
    }
69
70
    public function getVendorType()
71
    {
72
        if (!$this->_vendorType) {
73
            $this->_vendorType = 'org';
74
        }
75
76
        return $this->_vendorType;
77
    }
78
79
    public function setVendor($value)
80
    {
81
        $this->_vendor = $value;
82
    }
83
84
    public function getVendor()
85
    {
86
        if (!$this->_vendor) {
87
            $this->_vendor = $this->take('vendor')->name;
88
        }
89
90
        return $this->_vendor;
91
    }
92
93
    public function setDescription($value)
94
    {
95
        $this->_description = $value;
96
    }
97
98
    public function getDescription()
99
    {
100
        if ($this->_description === null) {
101
            $this->_description = $this->take('package')->getTitle();
102
        }
103
104
        return $this->_description;
105
    }
106
107
    /**
108
     * Create the repo on GitHub.
109
     * @return int exit code
110
     */
111
    public function createRepo(string $repo = null)
0 ignored issues
show
Unused Code introduced by
The parameter $repo is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

111
    public function createRepo(/** @scrutinizer ignore-unused */ string $repo = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
112
    {
113
        $end = $this->getVendorType() === 'org'
114
            ?'/orgs/' . $this->getVendor() . '/repos'
115
            : '/user/repos'
116
        ;
117
        var_dump($end);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($end) looks like debug code. Are you sure you do not want to remove it?
Loading history...
118
        $res = $this->request('POST', $end, [
119
            'name'        => $this->getName(),
120
            'description' => $this->getDescription(),
121
        ]);
122
        if (Helper::isResponseOk($res)) {
123
            echo "\ngit remote add origin [email protected]:{$this->getFullName()}.git\n";
124
            echo "git push -u origin master\n";
125
        }
126
127
        return $res;
128
    }
129
130
    /**
131
     * Clone repo from github.
132
     * TODO this action must be run without `start`.
133
     * @param string $repo full name vendor/package
134
     * @return int exit code
135
     */
136
    public function cloneRepo($repo)
137
    {
138
        return $this->passthru('git', ['clone', '[email protected]:' . $repo]);
139
    }
140
141
    /**
142
     * Checks if repo exists.
143
     * @param string $repo full name vendor/package defaults to this repo name
144
     * @return int exit code
145
     */
146
    public function existsRepo($repo = null)
147
    {
148
        return static::exists($repo ?: $this->getFull_name());
0 ignored issues
show
Bug Best Practice introduced by
The expression return static::exists($r... $this->getFull_name()) returns the type boolean which is incompatible with the documented return type integer.
Loading history...
149
    }
150
151
    /**
152
     * Check if repo exists.
153
     * @param string $repo
154
     * @return bool
155
     */
156
    public static function exists($repo)
157
    {
158
        return !static::exec('git', ['ls-remote', '[email protected]:' . $repo], true);
0 ignored issues
show
Bug introduced by
array('ls-remote', '[email protected]:' . $repo) of type array<integer,string> is incompatible with the type string expected by parameter $args of hidev\base\Component::exec(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

158
        return !static::exec('git', /** @scrutinizer ignore-type */ ['ls-remote', '[email protected]:' . $repo], true);
Loading history...
Bug Best Practice introduced by
The method hidev\base\Component::exec() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

158
        return !static::/** @scrutinizer ignore-call */ exec('git', ['ls-remote', '[email protected]:' . $repo], true);
Loading history...
159
    }
160
161
    /**
162
     * Creates github release.
163
     * @param string $release version number
164
     */
165
    public function releaseRepo($release = null)
166
    {
167
        $release = $this->take('version')->getRelease($release);
168
        $notes = $this->take('chkipper')->getReleaseNotes();
169
        $wait = $this->waitPush();
170
        if ($wait) {
171
            return $wait;
172
        }
173
174
        return $this->request('POST', '/repos/' . $this->getFull_name() . '/releases', [
175
            'tag_name'  => $release,
176
            'name'      => $release,
177
            'body'      => $notes,
178
        ]);
179
    }
180
181
    /**
182
     * Waits until push is actually finished.
183
     * TODO Check github if it really has latest local commit.
184
     * @return int 0 - success, 1 - failed
185
     */
186
    public function waitPush()
187
    {
188
        sleep(7);
189
190
        return 0;
191
    }
192
193
    public function request($method, $path, $data)
194
    {
195
        $url = 'https://api.github.com' . $path;
196
197
        return $this->passthru('curl', ['-X', $method, '-H', 'Authorization: token ' . $this->getToken(), '--data', Json::encode($data), $url]);
198
    }
199
200
    public function findToken()
201
    {
202
        return $_SERVER['GITHUB_TOKEN'] ?: Helper::readpassword('GitHub token:');
203
    }
204
205
    public function getToken()
206
    {
207
        if ($this->_token === null) {
208
            $this->_token = $this->findToken();
209
        }
210
211
        return $this->_token;
212
    }
213
}
214