Skip to content

Commit

Permalink
Merge pull request dancryer#2 from Block8/master
Browse files Browse the repository at this point in the history
update fork
  • Loading branch information
yourilima committed May 5, 2014
2 parents 0bbce4c + c164295 commit 170095c
Show file tree
Hide file tree
Showing 53 changed files with 1,038 additions and 499 deletions.
13 changes: 4 additions & 9 deletions PHPCI/Builder.php
Expand Up @@ -47,11 +47,6 @@ class Builder implements LoggerAwareInterface
*/
protected $directory;

/**
* @var bool
*/
protected $success = true;

/**
* @var bool
*/
Expand Down Expand Up @@ -192,20 +187,20 @@ public function execute()
$this->build->setStarted(new \DateTime());
$this->store->save($this->build);
$this->build->sendStatusPostback();
$this->success = true;
$success = true;

try {
// Set up the build:
$this->setupBuild();

// Run the core plugin stages:
foreach (array('setup', 'test') as $stage) {
$this->success &= $this->pluginExecutor->executePlugins($this->config, $stage);
$success &= $this->pluginExecutor->executePlugins($this->config, $stage);
}

// Set the status so this can be used by complete, success and failure
// stages.
if ($this->success) {
if ($success) {
$this->build->setStatus(Build::STATUS_SUCCESS);
} else {
$this->build->setStatus(Build::STATUS_FAILED);
Expand All @@ -214,7 +209,7 @@ public function execute()
// Complete stage plugins are always run
$this->pluginExecutor->executePlugins($this->config, 'complete');

if ($this->success) {
if ($success) {
$this->pluginExecutor->executePlugins($this->config, 'success');
$this->buildLogger->logSuccess('BUILD SUCCESSFUL!');
} else {
Expand Down
2 changes: 1 addition & 1 deletion PHPCI/Command/InstallCommand.php
Expand Up @@ -195,7 +195,7 @@ protected function verifyDatabaseDetails(array $db, OutputInterface $output)
protected function writeConfigFile(array $config)
{
$dumper = new \Symfony\Component\Yaml\Dumper();
$yaml = $dumper->dump($config);
$yaml = $dumper->dump($config, 2);

file_put_contents(PHPCI_DIR . 'PHPCI/config.yml', $yaml);
}
Expand Down
90 changes: 14 additions & 76 deletions PHPCI/Controller/ProjectController.php
Expand Up @@ -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;
Expand Down Expand Up @@ -145,37 +147,16 @@ public function add()

$method = $this->request->getMethod();

if ($method == 'POST') {
$values = $this->getParams();
$pub = null;
} else {
$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 ($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 = null;
$values = $this->getParams();

$pub = file_get_contents($keyFile . '.pub');
$prv = file_get_contents($keyFile);
if ($method != 'POST') {
$sshKey = new SshKey();
$key = $sshKey->generate();

$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);
Expand Down Expand Up @@ -351,7 +332,8 @@ protected function projectForm($values, $type = 'add')

$field = new Form\Element\TextArea('build_config');
$field->setRequired(false);
$field->setLabel('PHPCI build config for this project (if you cannot add a phpci.yml file in the project repository)');
$label = 'PHPCI build config for this project (if you cannot add a phpci.yml file in the project repository)';
$field->setLabel($label);
$field->setClass('form-control');
$field->setContainerClass('form-group');
$field->setRows(6);
Expand Down Expand Up @@ -380,46 +362,8 @@ protected function projectForm($values, $type = 'add')
*/
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)
Expand Down Expand Up @@ -459,10 +403,4 @@ protected function getReferenceValidator($values)
return true;
};
}

protected function canGenerateKeys()
{
$result = @shell_exec('ssh-keygen');
return !empty($result);
}
}
2 changes: 1 addition & 1 deletion PHPCI/Controller/SettingsController.php
Expand Up @@ -62,7 +62,7 @@ public function github()
$this->settings['phpci']['github']['secret'] = $this->getParam('githubsecret', '');
$error = $this->storeSettings();

if($error) {
if ($error) {
header('Location: ' . PHPCI_URL . 'settings?saved=2');
} else {
header('Location: ' . PHPCI_URL . 'settings?saved=1');
Expand Down
2 changes: 1 addition & 1 deletion PHPCI/Helper/BuildInterpolator.php
Expand Up @@ -58,4 +58,4 @@ public function interpolate($input)
$values = array_values($this->interpolation_vars);
return str_replace($keys, $values, $input);
}
}
}
57 changes: 57 additions & 0 deletions PHPCI/Helper/Github.php
@@ -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;
}
}
13 changes: 6 additions & 7 deletions PHPCI/Helper/MailerFactory.php
Expand Up @@ -3,16 +3,16 @@
namespace PHPCI\Helper;


class MailerFactory {

class MailerFactory
{
/**
* @var array
*/
protected $emailConfig;

public function __construct($phpCiConfig = null)
public function __construct($config = null)
{
$this->emailConfig = isset($phpCiSettings['email_settings']) ?: array();
$this->emailConfig = isset($config['email_settings']) ?: array();
}

/**
Expand All @@ -33,7 +33,7 @@ public function getSwiftMailerFromConfig()
return \Swift_Mailer::newInstance($transport);
}

protected function getMailConfig($configName)
protected function getMailConfig($configName)
{
if (isset($this->emailConfig[$configName]) && $this->emailConfig[$configName] != "") {
return $this->emailConfig[$configName];
Expand All @@ -54,5 +54,4 @@ protected function getMailConfig($configName)
}
}
}

}
}
46 changes: 46 additions & 0 deletions PHPCI/Helper/SshKey.php
@@ -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;
}
}

0 comments on commit 170095c

Please sign in to comment.