Simplifying ProjectController a little, see #384
This commit is contained in:
parent
d98e85ffc3
commit
1e44a1531b
|
@ -10,6 +10,8 @@
|
||||||
namespace PHPCI\Controller;
|
namespace PHPCI\Controller;
|
||||||
|
|
||||||
use PHPCI\BuildFactory;
|
use PHPCI\BuildFactory;
|
||||||
|
use PHPCI\Helper\Github;
|
||||||
|
use PHPCI\Helper\SshKey;
|
||||||
use PHPCI\Model\Build;
|
use PHPCI\Model\Build;
|
||||||
use PHPCI\Model\Project;
|
use PHPCI\Model\Project;
|
||||||
use b8;
|
use b8;
|
||||||
|
@ -145,37 +147,19 @@ class ProjectController extends \PHPCI\Controller
|
||||||
|
|
||||||
$method = $this->request->getMethod();
|
$method = $this->request->getMethod();
|
||||||
|
|
||||||
|
$pub = null;
|
||||||
|
$values = array();
|
||||||
|
|
||||||
if ($method == 'POST') {
|
if ($method == 'POST') {
|
||||||
$values = $this->getParams();
|
$values = $this->getParams();
|
||||||
$pub = null;
|
$pub = null;
|
||||||
} else {
|
} else {
|
||||||
$tempPath = sys_get_temp_dir() . '/';
|
$sshKey = new SshKey();
|
||||||
|
$key = $sshKey->generate();
|
||||||
|
|
||||||
// FastCGI fix for Windows machines, where temp path is not available to
|
$values['key'] = $key['private_key'];
|
||||||
// PHP, and defaults to the unwritable system directory. If the temp
|
$values['pubkey'] = $key['public_key'];
|
||||||
// path is pointing to the system directory, shift to the 'TEMP'
|
$pub = $key['public_key'];
|
||||||
// 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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$form = $this->projectForm($values);
|
$form = $this->projectForm($values);
|
||||||
|
@ -381,46 +365,8 @@ class ProjectController extends \PHPCI\Controller
|
||||||
*/
|
*/
|
||||||
protected function githubRepositories()
|
protected function githubRepositories()
|
||||||
{
|
{
|
||||||
$token = Config::getInstance()->get('phpci.github.token');
|
$github = new Github();
|
||||||
|
die(json_encode($github->getRepositories()));
|
||||||
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'];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getReferenceValidator($values)
|
protected function getReferenceValidator($values)
|
||||||
|
@ -460,10 +406,4 @@ class ProjectController extends \PHPCI\Controller
|
||||||
return true;
|
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