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 $_package; |
24
|
|
|
protected $_description; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string GitHub OAuth access token |
28
|
|
|
*/ |
29
|
|
|
protected $_token; |
30
|
|
|
|
31
|
|
|
public function setName($value) |
32
|
|
|
{ |
33
|
|
|
list($vendor, $package) = explode('/', $value, 2); |
34
|
|
|
$this->_name = $value; |
35
|
|
|
$this->_vendor = $vendor ?: $package; |
36
|
|
|
$this->_package = $package ?: $vendor; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function getName() |
40
|
|
|
{ |
41
|
|
|
if ($this->_name === null) { |
42
|
|
|
$this->setName($this->takePackage()->fullName); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $this->_name; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function setVendor($value) |
49
|
|
|
{ |
50
|
|
|
$this->_vendor = $value; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function getVendor() |
54
|
|
|
{ |
55
|
|
|
if ($this->_vendor === null) { |
56
|
|
|
$this->_vendor = $this->takeVendor()->name; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $this->_vendor; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function setPackage($value) |
63
|
|
|
{ |
64
|
|
|
$this->_package = $value; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function getPackage() |
68
|
|
|
{ |
69
|
|
|
if ($this->_package === null) { |
70
|
|
|
$this->_package = $this->takePackage()->name; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $this->_package; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function setDescription($value) |
77
|
|
|
{ |
78
|
|
|
$this->_description = $value; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function getDescription() |
82
|
|
|
{ |
83
|
|
|
if ($this->_description === null) { |
84
|
|
|
$this->_description = $this->takePackage()->getTitle(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $this->_description; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Create the repo on GitHub. |
92
|
|
|
* @return int exit code |
93
|
|
|
*/ |
94
|
|
|
public function actionCreate() |
95
|
|
|
{ |
96
|
|
|
return $this->request('POST', '/orgs/' . $this->getVendor() . '/repos', [ |
97
|
|
|
'name' => $this->getPackage(), |
98
|
|
|
'description' => $this->getDescription(), |
99
|
|
|
]); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Clone repo from github. |
104
|
|
|
* TODO this action must be run without `start`. |
105
|
|
|
* @param string $repo full name vendor/package |
106
|
|
|
* @return int exit code |
107
|
|
|
*/ |
108
|
|
|
public function actionClone($repo) |
109
|
|
|
{ |
110
|
|
|
return $this->passthru('git', ['clone', '[email protected]:' . $repo]); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Check if repo exists. |
115
|
|
|
* @param string $repo full name vendor/package defaults to this repo name |
116
|
|
|
* @return int exit code |
117
|
|
|
*/ |
118
|
|
|
public function actionExists($repo = null) |
119
|
|
|
{ |
120
|
|
|
return $this->exec('git', ['ls-remote', '[email protected]:' . ($repo ?: $this->getName())], true); |
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function actionRelease($version = null) |
124
|
|
|
{ |
125
|
|
|
$this->runRequest('CHANGELOG.md'); |
126
|
|
|
$changelog = $this->takeGoal('CHANGELOG.md'); |
127
|
|
|
$notes = reset($changelog->getFile()->getHandler()->releaseNotes); |
128
|
|
|
$version = $this->takeGoal('bump')->getVersion($version); |
129
|
|
|
$wait = $this->actionWaitPush(); |
130
|
|
|
if ($wait) { |
131
|
|
|
return $wait; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $this->request('POST', '/repos/' . $this->getName() . '/releases', [ |
135
|
|
|
'tag_name' => $version, |
136
|
|
|
'name' => $version, |
137
|
|
|
'body' => $notes, |
138
|
|
|
]); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Waits until push is actually finished. |
143
|
|
|
* TODO Check github if it really has latest local commit. |
144
|
|
|
* @return int 0 - success, 1 - failed |
145
|
|
|
*/ |
146
|
|
|
public function actionWaitPush() |
147
|
|
|
{ |
148
|
|
|
sleep(7); |
149
|
|
|
|
150
|
|
|
return 0; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function request($method, $path, $data) |
154
|
|
|
{ |
155
|
|
|
$url = 'https://api.github.com' . $path; |
156
|
|
|
|
157
|
|
|
return $this->passthru('curl', ['-X', $method, '-H', 'Authorization: token ' . $this->getToken(), '--data', Json::encode($data), $url]); |
|
|
|
|
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function findToken() |
|
|
|
|
161
|
|
|
{ |
162
|
|
|
return $_SERVER['GITHUB_TOKEN'] ?: $this->readpassword('GitHub token:'); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function getToken() |
166
|
|
|
{ |
167
|
|
|
if ($this->_token === null) { |
168
|
|
|
$this->_token = $this->findToken(); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return $this->_token; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
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: