2013-05-10 13:28:43 +02:00
|
|
|
<?php
|
2013-05-16 03:16:56 +02:00
|
|
|
/**
|
|
|
|
* PHPCI - Continuous Integration for PHP
|
|
|
|
*
|
|
|
|
* @copyright Copyright 2013, Block 8 Limited.
|
|
|
|
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
|
|
|
|
* @link http://www.phptesting.org/
|
|
|
|
*/
|
2013-05-10 13:28:43 +02:00
|
|
|
|
|
|
|
namespace PHPCI\Controller;
|
|
|
|
use b8,
|
|
|
|
PHPCI\Model\Build,
|
|
|
|
PHPCI\Model\Project,
|
|
|
|
b8\Form,
|
|
|
|
b8\Registry;
|
|
|
|
|
2013-05-16 03:30:48 +02:00
|
|
|
/**
|
|
|
|
* Project Controller - Allows users to create, edit and view projects.
|
|
|
|
* @author Dan Cryer <dan@block8.co.uk>
|
|
|
|
* @package PHPCI
|
|
|
|
* @subpackage Web
|
|
|
|
*/
|
2013-05-10 13:28:43 +02:00
|
|
|
class ProjectController extends b8\Controller
|
|
|
|
{
|
|
|
|
public function init()
|
|
|
|
{
|
|
|
|
$this->_buildStore = b8\Store\Factory::getStore('Build');
|
|
|
|
$this->_projectStore = b8\Store\Factory::getStore('Project');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function view($projectId)
|
|
|
|
{
|
|
|
|
$project = $this->_projectStore->getById($projectId);
|
|
|
|
$page = $this->getParam('p', 1);
|
|
|
|
$builds = $this->getLatestBuildsHtml($projectId, (($page - 1) * 10));
|
|
|
|
|
|
|
|
$view = new b8\View('Project');
|
|
|
|
$view->builds = $builds[0];
|
|
|
|
$view->total = $builds[1];
|
|
|
|
$view->project = $project;
|
|
|
|
$view->page = $page;
|
|
|
|
|
|
|
|
return $view->render();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function build($projectId)
|
|
|
|
{
|
|
|
|
$build = new Build();
|
|
|
|
$build->setProjectId($projectId);
|
|
|
|
$build->setCommitId('Manual');
|
|
|
|
$build->setStatus(0);
|
|
|
|
$build->setBranch('master');
|
|
|
|
$build->setCreated(new \DateTime());
|
|
|
|
|
|
|
|
$build = $this->_buildStore->save($build);
|
|
|
|
|
|
|
|
header('Location: /build/view/' . $build->getId());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function delete($id)
|
|
|
|
{
|
2013-05-10 17:25:51 +02:00
|
|
|
if(!Registry::getInstance()->get('user')->getIsAdmin())
|
|
|
|
{
|
|
|
|
throw new \Exception('You do not have permission to do that.');
|
|
|
|
}
|
|
|
|
|
2013-05-10 13:28:43 +02:00
|
|
|
$project = $this->_projectStore->getById($id);
|
|
|
|
$this->_projectStore->delete($project);
|
|
|
|
|
|
|
|
header('Location: /');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function builds($projectId)
|
|
|
|
{
|
|
|
|
$builds = $this->getLatestBuildsHtml($projectId);
|
|
|
|
die($builds[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getLatestBuildsHtml($projectId, $start = 0)
|
|
|
|
{
|
|
|
|
$builds = $this->_buildStore->getWhere(array('project_id' => $projectId), 10, $start, array(), array('id' => 'DESC'));
|
|
|
|
$view = new b8\View('BuildsTable');
|
|
|
|
$view->builds = $builds['items'];
|
|
|
|
|
|
|
|
return array($view->render(), $builds['count']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function add()
|
|
|
|
{
|
2013-05-10 17:25:51 +02:00
|
|
|
if(!Registry::getInstance()->get('user')->getIsAdmin())
|
|
|
|
{
|
|
|
|
throw new \Exception('You do not have permission to do that.');
|
|
|
|
}
|
|
|
|
|
2013-05-10 13:28:43 +02:00
|
|
|
$method = Registry::getInstance()->get('requestMethod');
|
|
|
|
|
|
|
|
if($method == 'POST')
|
|
|
|
{
|
|
|
|
$values = $this->getParams();
|
|
|
|
$pub = null;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-05-14 19:34:26 +02:00
|
|
|
$tempPath = sys_get_temp_dir() . '/';
|
2013-05-15 17:04:55 +02:00
|
|
|
|
|
|
|
// 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/';
|
|
|
|
}
|
|
|
|
|
2013-05-14 19:01:14 +02:00
|
|
|
$id = $tempPath . md5(microtime(true));
|
|
|
|
if (!is_dir($tempPath)) {
|
|
|
|
mkdir($tempPath);
|
|
|
|
}
|
2013-05-10 13:28:43 +02:00
|
|
|
shell_exec('ssh-keygen -q -t rsa -b 2048 -f '.$id.' -N "" -C "deploy@phpci"');
|
|
|
|
|
|
|
|
$pub = file_get_contents($id . '.pub');
|
|
|
|
$prv = file_get_contents($id);
|
|
|
|
|
2013-05-14 18:49:39 +02:00
|
|
|
$values = array('key' => $prv, 'pubkey' => $pub, 'token' => $_SESSION['github_token']);
|
2013-05-10 13:28:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$form = $this->projectForm($values);
|
|
|
|
|
|
|
|
if($method != 'POST' || ($method == 'POST' && !$form->validate()))
|
|
|
|
{
|
2013-05-14 17:37:54 +02:00
|
|
|
$gh = \b8\Registry::getInstance()->get('github_app');
|
|
|
|
$code = $this->getParam('code', null);
|
|
|
|
|
|
|
|
if(!is_null($code))
|
|
|
|
{
|
|
|
|
$http = new \b8\HttpClient();
|
|
|
|
$resp = $http->post('https://github.com/login/oauth/access_token', array('client_id' => $gh['id'], 'client_secret' => $gh['secret'], 'code' => $code));
|
|
|
|
|
|
|
|
if($resp['success'])
|
|
|
|
{
|
|
|
|
parse_str($resp['body'], $resp);
|
|
|
|
$_SESSION['github_token'] = $resp['access_token'];
|
2013-05-16 02:41:20 +02:00
|
|
|
header('Location: /project/add');
|
|
|
|
die;
|
2013-05-14 17:37:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-10 13:28:43 +02:00
|
|
|
$view = new b8\View('ProjectForm');
|
|
|
|
$view->type = 'add';
|
|
|
|
$view->project = null;
|
|
|
|
$view->form = $form;
|
|
|
|
$view->key = $pub;
|
2013-05-14 17:37:54 +02:00
|
|
|
$view->token = $_SESSION['github_token'] ? $_SESSION['github_token'] : null;
|
2013-05-10 13:28:43 +02:00
|
|
|
|
|
|
|
return $view->render();
|
|
|
|
}
|
|
|
|
|
|
|
|
$values = $form->getValues();
|
|
|
|
$values['git_key'] = $values['key'];
|
|
|
|
|
|
|
|
$project = new Project();
|
|
|
|
$project->setValues($values);
|
|
|
|
|
|
|
|
$project = $this->_projectStore->save($project);
|
|
|
|
|
|
|
|
header('Location: /project/view/' . $project->getId());
|
|
|
|
die;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function edit($id)
|
|
|
|
{
|
2013-05-10 17:25:51 +02:00
|
|
|
if(!Registry::getInstance()->get('user')->getIsAdmin())
|
|
|
|
{
|
|
|
|
throw new \Exception('You do not have permission to do that.');
|
|
|
|
}
|
|
|
|
|
2013-05-10 13:28:43 +02:00
|
|
|
$method = Registry::getInstance()->get('requestMethod');
|
|
|
|
$project = $this->_projectStore->getById($id);
|
|
|
|
|
|
|
|
if($method == 'POST')
|
|
|
|
{
|
|
|
|
$values = $this->getParams();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$values = $project->getDataArray();
|
|
|
|
$values['key'] = $values['git_key'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$form = $this->projectForm($values, 'edit/' . $id);
|
|
|
|
|
|
|
|
if($method != 'POST' || ($method == 'POST' && !$form->validate()))
|
|
|
|
{
|
|
|
|
$view = new b8\View('ProjectForm');
|
|
|
|
$view->type = 'edit';
|
|
|
|
$view->project = $project;
|
|
|
|
$view->form = $form;
|
|
|
|
$view->key = null;
|
|
|
|
|
|
|
|
return $view->render();
|
|
|
|
}
|
|
|
|
|
|
|
|
$values = $form->getValues();
|
|
|
|
$values['git_key'] = $values['key'];
|
|
|
|
|
|
|
|
$project->setValues($values);
|
|
|
|
$project = $this->_projectStore->save($project);
|
|
|
|
|
|
|
|
header('Location: /project/view/' . $project->getId());
|
|
|
|
die;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function projectForm($values, $type = 'add')
|
|
|
|
{
|
|
|
|
$form = new Form();
|
|
|
|
$form->setMethod('POST');
|
|
|
|
$form->setAction('/project/' . $type);
|
|
|
|
$form->addField(new Form\Element\Csrf('csrf'));
|
2013-05-14 17:37:54 +02:00
|
|
|
$form->addField(new Form\Element\Hidden('token'));
|
|
|
|
$form->addField(new Form\Element\Hidden('pubkey'));
|
2013-05-10 13:28:43 +02:00
|
|
|
|
|
|
|
$field = new Form\Element\Select('type');
|
|
|
|
$field->setRequired(true);
|
2013-05-14 20:57:37 +02:00
|
|
|
$field->setPattern('^(github|bitbucket|local)');
|
|
|
|
$field->setOptions(array('choose' => 'Select repository type...', 'github' => 'Github', 'bitbucket' => 'Bitbucket', 'local' => 'Local Path'));
|
2013-05-10 13:28:43 +02:00
|
|
|
$field->setLabel('Where is your project hosted?');
|
|
|
|
$field->setClass('span4');
|
|
|
|
$form->addField($field);
|
|
|
|
|
2013-05-14 17:37:54 +02:00
|
|
|
if(isset($_SESSION['github_token']))
|
|
|
|
{
|
|
|
|
$field = new Form\Element\Select('github');
|
|
|
|
$field->setLabel('Choose a Github repository:');
|
|
|
|
$field->setClass('span4');
|
|
|
|
$field->setOptions($this->getGithubRepositories());
|
|
|
|
$form->addField($field);
|
|
|
|
}
|
|
|
|
|
2013-05-10 13:28:43 +02:00
|
|
|
$field = new Form\Element\Text('reference');
|
|
|
|
$field->setRequired(true);
|
2013-05-16 01:33:12 +02:00
|
|
|
$field->setValidator(function($val) use ($values)
|
|
|
|
{
|
|
|
|
$type = $values['type'];
|
|
|
|
|
|
|
|
switch($type) {
|
|
|
|
case 'local':
|
|
|
|
if(!is_dir($val)) {
|
|
|
|
throw new \Exception('The path you specified does not exist.');
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'github':
|
|
|
|
case 'bitbucket':
|
|
|
|
if(!preg_match('/^[a-zA-Z0-9_\-]+\/[a-zA-Z0-9_\-]+$/', $val)) {
|
|
|
|
throw new \Exception('Repository name must be in the format "owner/repo".');
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
2013-05-14 20:44:14 +02:00
|
|
|
$field->setLabel('Repository Name / URL (Remote) or Path (Local)');
|
2013-05-10 13:28:43 +02:00
|
|
|
$field->setClass('span4');
|
|
|
|
$form->addField($field);
|
|
|
|
|
2013-05-14 17:37:54 +02:00
|
|
|
$field = new Form\Element\Text('title');
|
|
|
|
$field->setRequired(true);
|
|
|
|
$field->setLabel('Project Title');
|
|
|
|
$field->setClass('span4');
|
|
|
|
$form->addField($field);
|
|
|
|
|
2013-05-10 13:28:43 +02:00
|
|
|
$field = new Form\Element\TextArea('key');
|
|
|
|
$field->setRequired(false);
|
2013-05-14 20:44:14 +02:00
|
|
|
$field->setLabel('Private key to use to access repository (leave blank for local and/or anonymous remotes)');
|
2013-05-10 13:28:43 +02:00
|
|
|
$field->setClass('span7');
|
|
|
|
$field->setRows(6);
|
|
|
|
$form->addField($field);
|
|
|
|
|
|
|
|
$field = new Form\Element\Submit();
|
|
|
|
$field->setValue('Save Project');
|
|
|
|
$field->setClass('btn-success');
|
|
|
|
$form->addField($field);
|
|
|
|
|
|
|
|
$form->setValues($values);
|
|
|
|
return $form;
|
|
|
|
}
|
2013-05-14 17:37:54 +02:00
|
|
|
|
|
|
|
protected function getGithubRepositories()
|
|
|
|
{
|
|
|
|
$http = new \b8\HttpClient();
|
|
|
|
$res = $http->get('https://api.github.com/user/repos', array('type' => 'all', 'access_token' => $_SESSION['github_token']));
|
|
|
|
|
|
|
|
$rtn = array();
|
|
|
|
$rtn['choose'] = 'Select a repository...';
|
|
|
|
if($res['success'])
|
|
|
|
{
|
|
|
|
foreach($res['body'] as $repo)
|
|
|
|
{
|
|
|
|
$rtn[$repo['full_name']] = $repo['full_name'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rtn;
|
|
|
|
}
|
2013-05-10 13:28:43 +02:00
|
|
|
}
|