Completed
Push — master ( aa18f9...004da0 )
by Andrii
09:23 queued 07:23
created

GithubController::actionClone()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
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-2016, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hidev\controllers;
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 1
    public function getFull_name()
35
    {
36 1
        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 1
    public function getName()
55
    {
56 1
        if (!$this->_name) {
57 1
            $this->_name = $this->takePackage()->name;
58
        }
59
60 1
        return $this->_name;
61
    }
62
63
    public function setVendor($value)
64
    {
65
        $this->_vendor = $value;
66
    }
67
68 1
    public function getVendor()
69
    {
70 1
        if (!$this->_vendor) {
71 1
            $this->_vendor = $this->takeVendor()->name;
72
        }
73
74 1
        return $this->_vendor;
75
    }
76
77
    public function setDescription($value)
78
    {
79
        $this->_description = $value;
80
    }
81
82
    public function getDescription()
83
    {
84
        if ($this->_description === null) {
85
            $this->_description = $this->takePackage()->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::isOk($res)) {
102
            echo "\ngit remote add origin [email protected]:{$this->getFullName()}.git\ngit push -u origin master\n";
103
        }
104
105
        return $res;
106
    }
107
108
    /**
109
     * Clone repo from github.
110
     * TODO this action must be run without `start`.
111
     * @param string $repo full name vendor/package
112
     * @return int exit code
113
     */
114
    public function actionClone($repo)
115
    {
116
        return $this->passthru('git', ['clone', '[email protected]:' . $repo]);
117
    }
118
119
    /**
120
     * Action to check if repo exists.
121
     * @param string $repo full name vendor/package defaults to this repo name
122
     * @return int exit code
123
     */
124
    public function actionExists($repo = null)
125
    {
126
        return static::exists($repo ?: $this->getFull_name());
127
    }
128
129
    /**
130
     * Check if repo exists.
131
     * @param string $repo
132
     * @return bool
133
     */
134
    public static function exists($repo)
135
    {
136
        return !static::exec('git', ['ls-remote', '[email protected]:' . $repo], true);
0 ignored issues
show
Documentation introduced by
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...
137
    }
138
139
    /**
140
     * Creates github release.
141
     */
142
    public function actionRelease($version = null)
143
    {
144
        $version = $this->takeGoal('version')->getVersion($version);
145
        $notes = $this->takeGoal('chkipper')->getReleaseNotes();
146
        $wait = $this->actionWaitPush();
147
        if ($wait) {
148
            return $wait;
149
        }
150
151
        return $this->request('POST', '/repos/' . $this->getFull_name() . '/releases', [
152
            'tag_name'  => $version,
153
            'name'      => $version,
154
            'body'      => $notes,
155
        ]);
156
    }
157
158
    /**
159
     * Waits until push is actually finished.
160
     * TODO Check github if it really has latest local commit.
161
     * @return int 0 - success, 1 - failed
162
     */
163
    public function actionWaitPush()
164
    {
165
        sleep(7);
166
167
        return 0;
168
    }
169
170
    public function request($method, $path, $data)
171
    {
172
        $url = 'https://api.github.com' . $path;
173
174
        return $this->passthru('curl', ['-X', $method, '-H', 'Authorization: token ' . $this->getToken(), '--data', Json::encode($data), $url]);
175
    }
176
177
    public function findToken()
0 ignored issues
show
Coding Style introduced by
findToken uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
178
    {
179
        return $_SERVER['GITHUB_TOKEN'] ?: $this->readpassword('GitHub token:');
180
    }
181
182
    public function getToken()
183
    {
184
        if ($this->_token === null) {
185
            $this->_token = $this->findToken();
186
        }
187
188
        return $this->_token;
189
    }
190
}
191