Completed
Push — master ( f6cc4f...20183c )
by Andrii
03:46
created

src/console/GithubController.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\console;
12
13
use yii\helpers\Json;
14
15
/**
16
 * Goal for GitHub.
17
 */
18
class GithubController extends CommonController
19
{
20
    protected $_name;
21
    protected $_vendor;
22
    protected $_description;
23
24
    /**
25
     * @var string GitHub OAuth access token
26
     */
27
    protected $_token;
28
29
    public function setFull_name($value)
30
    {
31
        list($this->_vendor, $this->_name) = explode('/', $value, 2);
32
    }
33
34
    public function getFull_name()
35
    {
36
        return $this->getVendor() . '/' . $this->getName();
37
    }
38
39
    public function setFullName($value)
40
    {
41
        return $this->setFull_name($value);
42
    }
43
44
    public function getFullName()
45
    {
46
        return $this->getFull_name();
47
    }
48
49
    public function setName($value)
50
    {
51
        $this->_name = $value;
52
    }
53
54
    public function getName()
55
    {
56
        if (!$this->_name) {
57
            $this->_name = $this->take('package')->name;
58
        }
59
60
        return $this->_name;
61
    }
62
63
    public function setVendor($value)
64
    {
65
        $this->_vendor = $value;
66
    }
67
68
    public function getVendor()
69
    {
70
        if (!$this->_vendor) {
71
            $this->_vendor = $this->take('vendor')->name;
72
        }
73
74
        return $this->_vendor;
75
    }
76
77
    public function setDescription($value)
78
    {
79
        $this->_description = $value;
80
    }
81
82 View Code Duplication
    public function getDescription()
83
    {
84
        if ($this->_description === null) {
85
            $this->_description = $this->take('package')->getTitle();
86
        }
87
88
        return $this->_description;
89
    }
90
91
    /**
92
     * Create the repo on GitHub.
93
     * @return int exit code
94
     */
95
    public function actionCreate()
96
    {
97
        $res = $this->request('POST', '/orgs/' . $this->getVendor() . '/repos', [
98
            'name'        => $this->getName(),
99
            'description' => $this->getDescription(),
100
        ]);
101
        if (static::isResponseOk($res)) {
102
            echo "\ngit remote add origin [email protected]:{$this->getFullName()}.git\n";
103
            echo "git push -u origin master\n";
104
        }
105
106
        return $res;
107
    }
108
109
    /**
110
     * Clone repo from github.
111
     * TODO this action must be run without `start`.
112
     * @param string $repo full name vendor/package
113
     * @return int exit code
114
     */
115
    public function actionClone($repo)
116
    {
117
        return $this->passthru('git', ['clone', '[email protected]:' . $repo]);
118
    }
119
120
    /**
121
     * Checks if repo exists.
122
     * @param string $repo full name vendor/package defaults to this repo name
123
     * @return int exit code
124
     */
125
    public function actionExists($repo = null)
126
    {
127
        return static::exists($repo ?: $this->getFull_name());
128
    }
129
130
    /**
131
     * Check if repo exists.
132
     * @param string $repo
133
     * @return bool
134
     */
135
    public static function exists($repo)
136
    {
137
        return !static::exec('git', ['ls-remote', '[email protected]:' . $repo], true);
0 ignored issues
show
array('ls-remote', '[email protected]:' . $repo) is of type array<integer,string,{"0":"string","1":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
138
    }
139
140
    /**
141
     * Creates github release.
142
     */
143
    public function actionRelease($release = null)
144
    {
145
        $release = $this->take('version')->getRelease($release);
146
        $notes = $this->take('chkipper')->getReleaseNotes();
147
        $wait = $this->actionWaitPush();
148
        if ($wait) {
149
            return $wait;
150
        }
151
152
        return $this->request('POST', '/repos/' . $this->getFull_name() . '/releases', [
153
            'tag_name'  => $release,
154
            'name'      => $release,
155
            'body'      => $notes,
156
        ]);
157
    }
158
159
    /**
160
     * Waits until push is actually finished.
161
     * TODO Check github if it really has latest local commit.
162
     * @return int 0 - success, 1 - failed
163
     */
164
    public function actionWaitPush()
165
    {
166
        sleep(7);
167
168
        return 0;
169
    }
170
171
    public function request($method, $path, $data)
172
    {
173
        $url = 'https://api.github.com' . $path;
174
175
        return $this->passthru('curl', ['-X', $method, '-H', 'Authorization: token ' . $this->getToken(), '--data', Json::encode($data), $url]);
176
    }
177
178
    public function findToken()
179
    {
180
        return $_SERVER['GITHUB_TOKEN'] ?: $this->readpassword('GitHub token:');
181
    }
182
183
    public function getToken()
184
    {
185
        if ($this->_token === null) {
186
            $this->_token = $this->findToken();
187
        }
188
189
        return $this->_token;
190
    }
191
}
192