Skip to content

Commit 8b4b165

Browse files
committed
greatly refactored GitHub controller and component
1 parent 7b05d35 commit 8b4b165

File tree

2 files changed

+137
-141
lines changed

2 files changed

+137
-141
lines changed

src/components/GitHub.php

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
namespace hidev\components;
1212

13+
use hidev\helpers\Helper;
14+
use yii\helpers\Json;
15+
1316
/**
1417
* GitHub component.
1518
*/
@@ -18,6 +21,7 @@ class GitHub extends \hidev\base\Component
1821
protected $_name;
1922
protected $_vendor;
2023
protected $_description;
24+
protected $_vendorType;
2125

2226
/**
2327
* @var string GitHub OAuth access token
@@ -58,6 +62,20 @@ public function getName()
5862
return $this->_name;
5963
}
6064

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+
6179
public function setVendor($value)
6280
{
6381
$this->_vendor = $value;
@@ -85,4 +103,110 @@ public function getDescription()
85103

86104
return $this->_description;
87105
}
106+
107+
/**
108+
* Create the repo on GitHub.
109+
* @return int exit code
110+
*/
111+
public function createRepo(string $repo = null)
112+
{
113+
$end = $this->getVendorType() === 'org'
114+
?'/orgs/' . $this->getVendor() . '/repos'
115+
: '/user/repos'
116+
;
117+
$res = $this->request('POST', $end, [
118+
'name' => $this->getName(),
119+
'description' => $this->getDescription(),
120+
]);
121+
if (Helper::isResponseOk($res)) {
122+
echo "\ngit remote add origin [email protected]:{$this->getFullName()}.git\n";
123+
echo "git push -u origin master\n";
124+
}
125+
126+
return $res;
127+
}
128+
129+
/**
130+
* Clone repo from github.
131+
* TODO this action must be run without `start`.
132+
* @param string $repo full name vendor/package
133+
* @return int exit code
134+
*/
135+
public function cloneRepo($repo)
136+
{
137+
return $this->passthru('git', ['clone', '[email protected]:' . $repo]);
138+
}
139+
140+
/**
141+
* Checks if repo exists.
142+
* @param string $repo full name vendor/package defaults to this repo name
143+
* @return int exit code
144+
*/
145+
public function existsRepo($repo = null)
146+
{
147+
return static::exists($repo ?: $this->getFull_name());
148+
}
149+
150+
/**
151+
* Check if repo exists.
152+
* @param string $repo
153+
* @return bool
154+
*/
155+
public static function exists($repo)
156+
{
157+
return !static::exec('git', ['ls-remote', '[email protected]:' . $repo], true);
158+
}
159+
160+
/**
161+
* Creates github release.
162+
* @param string $release version number
163+
*/
164+
public function releaseRepo($release = null)
165+
{
166+
$release = $this->take('version')->getRelease($release);
167+
$notes = $this->take('chkipper')->getReleaseNotes();
168+
$wait = $this->waitPush();
169+
if ($wait) {
170+
return $wait;
171+
}
172+
173+
return $this->request('POST', '/repos/' . $this->getFull_name() . '/releases', [
174+
'tag_name' => $release,
175+
'name' => $release,
176+
'body' => $notes,
177+
]);
178+
}
179+
180+
/**
181+
* Waits until push is actually finished.
182+
* TODO Check github if it really has latest local commit.
183+
* @return int 0 - success, 1 - failed
184+
*/
185+
public function waitPush()
186+
{
187+
sleep(7);
188+
189+
return 0;
190+
}
191+
192+
public function request($method, $path, $data)
193+
{
194+
$url = 'https://api.github.com' . $path;
195+
196+
return $this->passthru('curl', ['-X', $method, '-H', 'Authorization: token ' . $this->getToken(), '--data', Json::encode($data), $url]);
197+
}
198+
199+
public function findToken()
200+
{
201+
return $_SERVER['GITHUB_TOKEN'] ?: Helper::readpassword('GitHub token:');
202+
}
203+
204+
public function getToken()
205+
{
206+
if ($this->_token === null) {
207+
$this->_token = $this->findToken();
208+
}
209+
210+
return $this->_token;
211+
}
88212
}

src/console/GithubController.php

Lines changed: 13 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -10,100 +10,20 @@
1010

1111
namespace hidev\console;
1212

13-
use yii\helpers\Json;
14-
1513
/**
1614
* Goal for GitHub.
1715
*/
1816
class GithubController extends CommonController
1917
{
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-
public function getDescription()
83-
{
84-
if ($this->_description === null) {
85-
$this->_description = $this->take('package')->getTitle();
86-
}
87-
88-
return $this->_description;
89-
}
90-
9118
/**
9219
* Create the repo on GitHub.
20+
* If name not given, repo for current project created.
21+
* @param string $repo full name
9322
* @return int exit code
9423
*/
95-
public function actionCreate()
24+
public function actionCreate(string $repo = null)
9625
{
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;
26+
return $this->getComponent()->createRepo($repo);
10727
}
10828

10929
/**
@@ -112,9 +32,9 @@ public function actionCreate()
11232
* @param string $repo full name vendor/package
11333
* @return int exit code
11434
*/
115-
public function actionClone($repo)
35+
public function actionClone(string $repo)
11636
{
117-
return $this->passthru('git', ['clone', '[email protected]:' . $repo]);
37+
return $this->getComponent()->cloneRepo($repo);
11838
}
11939

12040
/**
@@ -124,68 +44,20 @@ public function actionClone($repo)
12444
*/
12545
public function actionExists($repo = null)
12646
{
127-
return static::exists($repo ?: $this->getFull_name());
47+
return $this->getComponent()->existsRepo($repo);
12848
}
12949

13050
/**
131-
* Check if repo exists.
132-
* @param string $repo
133-
* @return bool
51+
* Creates github release for current project repo.
52+
* @param string $release version number
13453
*/
135-
public static function exists($repo)
54+
public function releaseRepo($release = null)
13655
{
137-
return !static::exec('git', ['ls-remote', '[email protected]:' . $repo], true);
56+
return $this->getComponent()->releaseRepo($repo);
13857
}
13958

140-
/**
141-
* Creates github release.
142-
*/
143-
public function actionRelease($release = null)
59+
public function getComponent()
14460
{
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;
61+
return $this->take('github');
19062
}
19163
}

0 commit comments

Comments
 (0)