Mercurial build support

This commit is contained in:
Pavel Gopanenko 2013-09-04 19:54:26 +07:00
parent 463ac8a6ee
commit e86852efa5
3 changed files with 79 additions and 8 deletions

View file

@ -43,6 +43,9 @@ class BuildFactory
case 'gitlab':
$type = 'GitlabBuild';
break;
case 'hg':
$type = 'MercurialBuild';
break;
}
$type = '\\PHPCI\\Model\\Build\\' . $type;

View file

@ -52,11 +52,14 @@ class ProjectController extends \PHPCI\Controller
*/
public function build($projectId)
{
/* @var \PHPCI\Model\Project $project */
$project = $this->_projectStore->getById($projectId);
$build = new Build();
$build->setProjectId($projectId);
$build->setCommitId('Manual');
$build->setStatus(0);
$build->setBranch('master');
$build->setBranch($project->getType() === 'hg' ? 'default' : 'master');
$build->setCreated(new \DateTime());
$build = $this->_buildStore->save($build);
@ -192,7 +195,7 @@ class ProjectController extends \PHPCI\Controller
$url = 'https://github.com/login/oauth/access_token';
$params = array('client_id' => $github['id'], 'client_secret' => $github['secret'], 'code' => $code);
$resp = $http->post($url, $params);
if ($resp['success']) {
parse_str($resp['body'], $resp);
$_SESSION['github_token'] = $resp['access_token'];
@ -207,14 +210,14 @@ class ProjectController extends \PHPCI\Controller
}
/**
* Edit a project. Handles both the form and processing.
* Edit a project. Handles both the form and processing.
*/
public function edit($projectId)
{
if (!$_SESSION['user']->getIsAdmin()) {
throw new \Exception('You do not have permission to do that.');
}
$method = $this->request->getMethod();
$project = $this->_projectStore->getById($projectId);
@ -262,7 +265,7 @@ class ProjectController extends \PHPCI\Controller
}
/**
* Create add / edit project form.
* Create add / edit project form.
*/
protected function projectForm($values, $type = 'add')
{
@ -279,12 +282,13 @@ class ProjectController extends \PHPCI\Controller
'bitbucket' => 'Bitbucket',
'gitlab' => 'Gitlab',
'remote' => 'Remote URL',
'local' => 'Local Path'
'local' => 'Local Path',
'hg' => 'Mercurial',
);
$field = new Form\Element\Select('type');
$field->setRequired(true);
$field->setPattern('^(github|bitbucket|gitlab|remote|local)');
$field->setPattern('^(github|bitbucket|gitlab|remote|local|hg)');
$field->setOptions($options);
$field->setLabel('Where is your project hosted?');
$field->setClass('form-control');
@ -304,6 +308,11 @@ class ProjectController extends \PHPCI\Controller
$type = $values['type'];
switch($type) {
case 'hg':
if (!preg_match('/^(https?):\/\//', $val)) {
throw new \Exception('Mercurial repository URL must be start with http:// or https://.');
}
break;
case 'remote':
if (!preg_match('/^(git|https?):\/\//', $val)) {
throw new \Exception('Repository URL must be start with git://, http:// or https://.');
@ -344,7 +353,7 @@ class ProjectController extends \PHPCI\Controller
$field->setClass('form-control');
$field->setContainerClass('form-group');
$form->addField($field);
$field = new Form\Element\TextArea('key');
$field->setRequired(false);
$field->setLabel('Private key to use to access repository (leave blank for local and/or anonymous remotes)');

View file

@ -0,0 +1,59 @@
<?php
/**
* 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/
*/
namespace PHPCI\Model\Build;
use PHPCI\Model\Build;
use PHPCI\Builder;
use Symfony\Component\Yaml\Parser as YamlParser;
/**
* Mercurial Build Model
* @author Pavel Gopanenko <pavelgopanenko@gmail.com>
* @package PHPCI
* @subpackage Core
*/
class MercurialBuild extends Build
{
/**
* Get the URL to be used to clone this remote repository.
*/
protected function getCloneUrl()
{
return $this->getProject()->getReference();
}
/**
* Create a working copy by cloning, copying, or similar.
*/
public function createWorkingCopy(Builder $builder, $buildPath)
{
$yamlParser = new YamlParser();
$this->cloneByHttp($builder, $buildPath);
if (!is_file($buildPath . 'phpci.yml')) {
$builder->logFailure('Project does not contain a phpci.yml file.');
return false;
}
$yamlFile = file_get_contents($buildPath . 'phpci.yml');
$builder->setConfigArray($yamlParser->parse($yamlFile));
return true;
}
/**
* Use an mercurial clone.
*/
protected function cloneByHttp(Builder $builder, $to)
{
return $builder->executeCommand('hg clone %s "%s" -r %s', $this->getCloneUrl(), $to, $this->getBranch());
}
}