Simplifying ProjectController a little, see #384
This commit is contained in:
parent
c253634f4b
commit
f0d35605b6
|
@ -10,6 +10,8 @@
|
|||
namespace PHPCI\Controller;
|
||||
|
||||
use PHPCI\BuildFactory;
|
||||
use PHPCI\Helper\Github;
|
||||
use PHPCI\Helper\SshKey;
|
||||
use PHPCI\Model\Build;
|
||||
use PHPCI\Model\Project;
|
||||
use b8;
|
||||
|
@ -145,37 +147,19 @@ class ProjectController extends \PHPCI\Controller
|
|||
|
||||
$method = $this->request->getMethod();
|
||||
|
||||
$pub = null;
|
||||
$values = array();
|
||||
|
||||
if ($method == 'POST') {
|
||||
$values = $this->getParams();
|
||||
$pub = null;
|
||||
} else {
|
||||
$tempPath = sys_get_temp_dir() . '/';
|
||||
$sshKey = new SshKey();
|
||||
$key = $sshKey->generate();
|
||||
|
||||
// FastCGI fix for Windows machines, where temp path is not available to
|
||||
// PHP, and defaults to the unwritable system directory. If the temp
|
||||
// path is pointing to the system directory, shift to the 'TEMP'
|
||||
// sub-folder, which should also exist, but actually be writable.
|
||||
if ($tempPath == getenv("SystemRoot") . '/') {
|
||||
$tempPath = getenv("SystemRoot") . '/TEMP/';
|
||||
}
|
||||
|
||||
$keyFile = $tempPath . md5(microtime(true));
|
||||
|
||||
if (!is_dir($tempPath)) {
|
||||
mkdir($tempPath);
|
||||
}
|
||||
|
||||
if ($this->canGenerateKeys()) {
|
||||
shell_exec('ssh-keygen -q -t rsa -b 2048 -f '.$keyFile.' -N "" -C "deploy@phpci"');
|
||||
|
||||
$pub = file_get_contents($keyFile . '.pub');
|
||||
$prv = file_get_contents($keyFile);
|
||||
|
||||
$values = array('key' => $prv, 'pubkey' => $pub);
|
||||
} else {
|
||||
$pub = null;
|
||||
$values = array();
|
||||
}
|
||||
$values['key'] = $key['private_key'];
|
||||
$values['pubkey'] = $key['public_key'];
|
||||
$pub = $key['public_key'];
|
||||
}
|
||||
|
||||
$form = $this->projectForm($values);
|
||||
|
@ -381,46 +365,8 @@ class ProjectController extends \PHPCI\Controller
|
|||
*/
|
||||
protected function githubRepositories()
|
||||
{
|
||||
$token = Config::getInstance()->get('phpci.github.token');
|
||||
|
||||
if (!$token) {
|
||||
die(json_encode(null));
|
||||
}
|
||||
|
||||
$cache = \b8\Cache::getCache(\b8\Cache::TYPE_APC);
|
||||
$rtn = $cache->get('phpci_github_repos');
|
||||
|
||||
if (!$rtn) {
|
||||
$orgs = $this->doGithubApiRequest('/user/orgs', array('access_token' => $token));
|
||||
|
||||
$params = array('type' => 'all', 'access_token' => $token);
|
||||
$repos = array();
|
||||
$repos['user'] = $this->doGithubApiRequest('/user/repos', $params);
|
||||
|
||||
|
||||
foreach ($orgs as $org) {
|
||||
$repos[$org['login']] = $this->doGithubApiRequest('/orgs/'.$org['login'].'/repos', $params);
|
||||
}
|
||||
|
||||
$rtn = array();
|
||||
foreach ($repos as $repoGroup) {
|
||||
foreach ($repoGroup as $repo) {
|
||||
$rtn['repos'][] = $repo['full_name'];
|
||||
}
|
||||
}
|
||||
|
||||
$cache->set('phpci_github_repos', $rtn);
|
||||
}
|
||||
|
||||
die(json_encode($rtn));
|
||||
}
|
||||
|
||||
protected function doGithubApiRequest($url, $params)
|
||||
{
|
||||
$http = new \b8\HttpClient('https://api.github.com');
|
||||
$res = $http->get($url, $params);
|
||||
|
||||
return $res['body'];
|
||||
$github = new Github();
|
||||
die(json_encode($github->getRepositories()));
|
||||
}
|
||||
|
||||
protected function getReferenceValidator($values)
|
||||
|
@ -460,10 +406,4 @@ class ProjectController extends \PHPCI\Controller
|
|||
return true;
|
||||
};
|
||||
}
|
||||
|
||||
protected function canGenerateKeys()
|
||||
{
|
||||
$result = @shell_exec('ssh-keygen');
|
||||
return !empty($result);
|
||||
}
|
||||
}
|
||||
|
|
57
PHPCI/Helper/Github.php
Normal file
57
PHPCI/Helper/Github.php
Normal file
|
@ -0,0 +1,57 @@
|
|||
<?php
|
||||
|
||||
namespace PHPCI\Helper;
|
||||
|
||||
use b8\Cache;
|
||||
use b8\Config;
|
||||
use b8\HttpClient;
|
||||
|
||||
class Github
|
||||
{
|
||||
public function makeRequest($url, $params)
|
||||
{
|
||||
$http = new HttpClient('https://api.github.com');
|
||||
$res = $http->get($url, $params);
|
||||
|
||||
return $res['body'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an array of repositories from Github's API.
|
||||
*/
|
||||
public function getRepositories()
|
||||
{
|
||||
$token = Config::getInstance()->get('phpci.github.token');
|
||||
|
||||
if (!$token) {
|
||||
die(json_encode(null));
|
||||
}
|
||||
|
||||
$cache = Cache::getCache(Cache::TYPE_APC);
|
||||
$rtn = $cache->get('phpci_github_repos');
|
||||
|
||||
if (!$rtn) {
|
||||
$orgs = $this->makeRequest('/user/orgs', array('access_token' => $token));
|
||||
|
||||
$params = array('type' => 'all', 'access_token' => $token);
|
||||
$repos = array();
|
||||
$repos['user'] = $this->makeRequest('/user/repos', $params);
|
||||
|
||||
|
||||
foreach ($orgs as $org) {
|
||||
$repos[$org['login']] = $this->makeRequest('/orgs/'.$org['login'].'/repos', $params);
|
||||
}
|
||||
|
||||
$rtn = array();
|
||||
foreach ($repos as $repoGroup) {
|
||||
foreach ($repoGroup as $repo) {
|
||||
$rtn['repos'][] = $repo['full_name'];
|
||||
}
|
||||
}
|
||||
|
||||
$cache->set('phpci_github_repos', $rtn);
|
||||
}
|
||||
|
||||
return $rtn;
|
||||
}
|
||||
}
|
46
PHPCI/Helper/SshKey.php
Normal file
46
PHPCI/Helper/SshKey.php
Normal file
|
@ -0,0 +1,46 @@
|
|||
<?php
|
||||
|
||||
namespace PHPCI\Helper;
|
||||
|
||||
class SshKey
|
||||
{
|
||||
public function generate()
|
||||
{
|
||||
$tempPath = sys_get_temp_dir() . '/';
|
||||
|
||||
// FastCGI fix for Windows machines, where temp path is not available to
|
||||
// PHP, and defaults to the unwritable system directory. If the temp
|
||||
// path is pointing to the system directory, shift to the 'TEMP'
|
||||
// sub-folder, which should also exist, but actually be writable.
|
||||
if (IS_WIN && $tempPath == getenv("SystemRoot") . '/') {
|
||||
$tempPath = getenv("SystemRoot") . '/TEMP/';
|
||||
}
|
||||
|
||||
$keyFile = $tempPath . md5(microtime(true));
|
||||
|
||||
if (!is_dir($tempPath)) {
|
||||
mkdir($tempPath);
|
||||
}
|
||||
|
||||
$return = array();
|
||||
|
||||
if ($this->canGenerateKeys()) {
|
||||
shell_exec('ssh-keygen -q -t rsa -b 2048 -f '.$keyFile.' -N "" -C "deploy@phpci"');
|
||||
|
||||
$pub = file_get_contents($keyFile . '.pub');
|
||||
$prv = file_get_contents($keyFile);
|
||||
|
||||
$return = array('private_key' => $prv, 'public_key' => $pub);
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
public function canGenerateKeys()
|
||||
{
|
||||
$keygen = @shell_exec('ssh-keygen');
|
||||
$canGenerateKeys = !empty($keygen);
|
||||
|
||||
return $canGenerateKeys;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue