Completed
Push — master ( 0f3d33...d90500 )
by Andrii
03:05
created

GithubController::setFullName()   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 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
/*
4
 * Task runner, code generator and build tool for easier continuos integration
5
 *
6
 * @link      https://github.com/hiqdev/hidev
7
 * @package   hidev
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hidev\controllers;
13
14
use yii\helpers\Json;
15
16
/**
17
 * Goal for GitHub.
18
 */
19
class GithubController extends CommonController
20
{
21
    protected $_name;
22
    protected $_vendor;
23
    protected $_description;
24
25
    /**
26
     * @var string GitHub OAuth access token
27
     */
28
    protected $_token;
29
30
    public function setFull_name($value)
31
    {
32
        list($this->_vendor, $this->_name) = explode('/', $value, 2);
33
    }
34
35
    public function getFull_name()
36
    {
37
        return $this->getVendor() . '/' . $this->getName();
38
    }
39
40
    public function setFullName($value)
41
    {
42
        return $this->setFull_name($value);
43
    }
44
45
    public function getFullName()
46
    {
47
        return $this->getFull_name();
48
    }
49
50
    public function setName($value)
51
    {
52
        $this->_name = $value;
53
    }
54
55
    public function getName()
56
    {
57
        if (!$this->_name) {
58
            $this->_name = $this->takePackage()->name;
59
        }
60
61
        return $this->_name;
62
    }
63
64
    public function setVendor($value)
65
    {
66
        $this->_vendor = $value;
67
    }
68
69
    public function getVendor()
70
    {
71
        if (!$this->_vendor) {
72
            $this->_vendor = $this->takeVendor()->name;
73
        }
74
75
        return $this->_vendor;
76
    }
77
78
    public function setDescription($value)
79
    {
80
        $this->_description = $value;
81
    }
82
83
    public function getDescription()
84
    {
85
        if ($this->_description === null) {
86
            $this->_description = $this->takePackage()->getTitle();
87
        }
88
89
        return $this->_description;
90
    }
91
92
    /**
93
     * Create the repo on GitHub.
94
     * @return int exit code
95
     */
96
    public function actionCreate()
97
    {
98
        return $this->request('POST', '/orgs/' . $this->getVendor() . '/repos', [
99
            'name'        => $this->getName(),
100
            'description' => $this->getDescription(),
101
        ]);
102
    }
103
104
    /**
105
     * Clone repo from github.
106
     * TODO this action must be run without `start`.
107
     * @param string $repo full name vendor/package
108
     * @return int exit code
109
     */
110
    public function actionClone($repo)
111
    {
112
        return $this->passthru('git', ['clone', '[email protected]:' . $repo]);
0 ignored issues
show
Documentation introduced by
array('clone', '[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...
113
    }
114
115
    /**
116
     * Action to check if repo exists.
117
     * @param string $repo full name vendor/package defaults to this repo name
118
     * @return int exit code
119
     */
120
    public function actionExists($repo = null)
121
    {
122
        return static::exists($repo ?: $this->getFull_name());
123
    }
124
125
    /**
126
     * Check if repo exists.
127
     * @param string $repo
128
     * @return bool
129
     */
130
    public static function exists($repo)
131
    {
132
        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...
133
    }
134
135
    public function actionRelease($version = null)
136
    {
137
        $this->runRequest('CHANGELOG.md');
138
        $changelog = $this->takeGoal('CHANGELOG.md');
139
        $notes = reset($changelog->getFile()->getHandler()->releaseNotes);
140
        $version = $this->takeGoal('bump')->getVersion($version);
141
        $wait = $this->actionWaitPush();
142
        if ($wait) {
143
            return $wait;
144
        }
145
146
        return $this->request('POST', '/repos/' . $this->getFull_name() . '/releases', [
147
            'tag_name'  => $version,
148
            'name'      => $version,
149
            'body'      => $notes,
150
        ]);
151
    }
152
153
    /**
154
     * Waits until push is actually finished.
155
     * TODO Check github if it really has latest local commit.
156
     * @return int 0 - success, 1 - failed
157
     */
158
    public function actionWaitPush()
159
    {
160
        sleep(7);
161
162
        return 0;
163
    }
164
165
    public function request($method, $path, $data)
166
    {
167
        $url = 'https://api.github.com' . $path;
168
169
        return $this->passthru('curl', ['-X', $method, '-H', 'Authorization: token ' . $this->getToken(), '--data', Json::encode($data), $url]);
0 ignored issues
show
Documentation introduced by
array('-X', $method, '-H...n::encode($data), $url) is of type array<integer,?,{"0":"st..."string","6":"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...
170
    }
171
172
    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...
173
    {
174
        return $_SERVER['GITHUB_TOKEN'] ?: $this->readpassword('GitHub token:');
175
    }
176
177
    public function getToken()
178
    {
179
        if ($this->_token === null) {
180
            $this->_token = $this->findToken();
181
        }
182
183
        return $this->_token;
184
    }
185
}
186