Skip to content

Commit

Permalink
greatly refactored GitHub controller and component
Browse files Browse the repository at this point in the history
  • Loading branch information
hiqsol committed Jul 12, 2018
1 parent 7b05d35 commit 8b4b165
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 141 deletions.
124 changes: 124 additions & 0 deletions src/components/GitHub.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

namespace hidev\components;

use hidev\helpers\Helper;
use yii\helpers\Json;

/**
* GitHub component.
*/
Expand All @@ -18,6 +21,7 @@ class GitHub extends \hidev\base\Component
protected $_name;
protected $_vendor;
protected $_description;
protected $_vendorType;

/**
* @var string GitHub OAuth access token
Expand Down Expand Up @@ -58,6 +62,20 @@ public function getName()
return $this->_name;
}

public function setVendorType($value)
{
$this->_vendorType = $value;
}

public function getVendorType()
{
if (!$this->_vendorType) {
$this->_vendorType = 'org';
}

return $this->_vendorType;
}

public function setVendor($value)
{
$this->_vendor = $value;
Expand Down Expand Up @@ -85,4 +103,110 @@ public function getDescription()

return $this->_description;
}

/**
* Create the repo on GitHub.
* @return int exit code
*/
public function createRepo(string $repo = null)
{
$end = $this->getVendorType() === 'org'
?'/orgs/' . $this->getVendor() . '/repos'
: '/user/repos'
;
$res = $this->request('POST', $end, [
'name' => $this->getName(),
'description' => $this->getDescription(),
]);
if (Helper::isResponseOk($res)) {
echo "\ngit remote add origin git@github.com:{$this->getFullName()}.git\n";
echo "git push -u origin master\n";
}

return $res;
}

/**
* Clone repo from github.
* TODO this action must be run without `start`.
* @param string $repo full name vendor/package
* @return int exit code
*/
public function cloneRepo($repo)
{
return $this->passthru('git', ['clone', 'git@github.com:' . $repo]);
}

/**
* Checks if repo exists.
* @param string $repo full name vendor/package defaults to this repo name
* @return int exit code
*/
public function existsRepo($repo = null)
{
return static::exists($repo ?: $this->getFull_name());
}

/**
* Check if repo exists.
* @param string $repo
* @return bool
*/
public static function exists($repo)
{
return !static::exec('git', ['ls-remote', 'git@github.com:' . $repo], true);
}

/**
* Creates github release.
* @param string $release version number
*/
public function releaseRepo($release = null)
{
$release = $this->take('version')->getRelease($release);
$notes = $this->take('chkipper')->getReleaseNotes();
$wait = $this->waitPush();
if ($wait) {
return $wait;
}

return $this->request('POST', '/repos/' . $this->getFull_name() . '/releases', [
'tag_name' => $release,
'name' => $release,
'body' => $notes,
]);
}

/**
* Waits until push is actually finished.
* TODO Check github if it really has latest local commit.
* @return int 0 - success, 1 - failed
*/
public function waitPush()
{
sleep(7);

return 0;
}

public function request($method, $path, $data)
{
$url = 'https://api.github.com' . $path;

return $this->passthru('curl', ['-X', $method, '-H', 'Authorization: token ' . $this->getToken(), '--data', Json::encode($data), $url]);
}

public function findToken()
{
return $_SERVER['GITHUB_TOKEN'] ?: Helper::readpassword('GitHub token:');
}

public function getToken()
{
if ($this->_token === null) {
$this->_token = $this->findToken();
}

return $this->_token;
}
}
154 changes: 13 additions & 141 deletions src/console/GithubController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,100 +10,20 @@

namespace hidev\console;

use yii\helpers\Json;

/**
* Goal for GitHub.
*/
class GithubController extends CommonController
{
protected $_name;
protected $_vendor;
protected $_description;

/**
* @var string GitHub OAuth access token
*/
protected $_token;

public function setFull_name($value)
{
list($this->_vendor, $this->_name) = explode('/', $value, 2);
}

public function getFull_name()
{
return $this->getVendor() . '/' . $this->getName();
}

public function setFullName($value)
{
return $this->setFull_name($value);
}

public function getFullName()
{
return $this->getFull_name();
}

public function setName($value)
{
$this->_name = $value;
}

public function getName()
{
if (!$this->_name) {
$this->_name = $this->take('package')->name;
}

return $this->_name;
}

public function setVendor($value)
{
$this->_vendor = $value;
}

public function getVendor()
{
if (!$this->_vendor) {
$this->_vendor = $this->take('vendor')->name;
}

return $this->_vendor;
}

public function setDescription($value)
{
$this->_description = $value;
}

public function getDescription()
{
if ($this->_description === null) {
$this->_description = $this->take('package')->getTitle();
}

return $this->_description;
}

/**
* Create the repo on GitHub.
* If name not given, repo for current project created.
* @param string $repo full name
* @return int exit code
*/
public function actionCreate()
public function actionCreate(string $repo = null)
{
$res = $this->request('POST', '/orgs/' . $this->getVendor() . '/repos', [
'name' => $this->getName(),
'description' => $this->getDescription(),
]);
if (static::isResponseOk($res)) {
echo "\ngit remote add origin git@github.com:{$this->getFullName()}.git\n";
echo "git push -u origin master\n";
}

return $res;
return $this->getComponent()->createRepo($repo);
}

/**
Expand All @@ -112,9 +32,9 @@ public function actionCreate()
* @param string $repo full name vendor/package
* @return int exit code
*/
public function actionClone($repo)
public function actionClone(string $repo)
{
return $this->passthru('git', ['clone', 'git@github.com:' . $repo]);
return $this->getComponent()->cloneRepo($repo);
}

/**
Expand All @@ -124,68 +44,20 @@ public function actionClone($repo)
*/
public function actionExists($repo = null)
{
return static::exists($repo ?: $this->getFull_name());
return $this->getComponent()->existsRepo($repo);
}

/**
* Check if repo exists.
* @param string $repo
* @return bool
* Creates github release for current project repo.
* @param string $release version number
*/
public static function exists($repo)
public function releaseRepo($release = null)
{
return !static::exec('git', ['ls-remote', 'git@github.com:' . $repo], true);
return $this->getComponent()->releaseRepo($repo);
}

/**
* Creates github release.
*/
public function actionRelease($release = null)
public function getComponent()
{
$release = $this->take('version')->getRelease($release);
$notes = $this->take('chkipper')->getReleaseNotes();
$wait = $this->actionWaitPush();
if ($wait) {
return $wait;
}

return $this->request('POST', '/repos/' . $this->getFull_name() . '/releases', [
'tag_name' => $release,
'name' => $release,
'body' => $notes,
]);
}

/**
* Waits until push is actually finished.
* TODO Check github if it really has latest local commit.
* @return int 0 - success, 1 - failed
*/
public function actionWaitPush()
{
sleep(7);

return 0;
}

public function request($method, $path, $data)
{
$url = 'https://api.github.com' . $path;

return $this->passthru('curl', ['-X', $method, '-H', 'Authorization: token ' . $this->getToken(), '--data', Json::encode($data), $url]);
}

public function findToken()
{
return $_SERVER['GITHUB_TOKEN'] ?: $this->readpassword('GitHub token:');
}

public function getToken()
{
if ($this->_token === null) {
$this->_token = $this->findToken();
}

return $this->_token;
return $this->take('github');
}
}

0 comments on commit 8b4b165

Please sign in to comment.