From e86852efa5ab0737940835716ee82f86df9a65a7 Mon Sep 17 00:00:00 2001 From: Pavel Gopanenko Date: Wed, 4 Sep 2013 19:54:26 +0700 Subject: [PATCH] Mercurial build support --- PHPCI/BuildFactory.php | 3 ++ PHPCI/Controller/ProjectController.php | 25 +++++++---- PHPCI/Model/Build/MercurialBuild.php | 59 ++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 8 deletions(-) create mode 100644 PHPCI/Model/Build/MercurialBuild.php diff --git a/PHPCI/BuildFactory.php b/PHPCI/BuildFactory.php index 689e5604..e4628434 100644 --- a/PHPCI/BuildFactory.php +++ b/PHPCI/BuildFactory.php @@ -43,6 +43,9 @@ class BuildFactory case 'gitlab': $type = 'GitlabBuild'; break; + case 'hg': + $type = 'MercurialBuild'; + break; } $type = '\\PHPCI\\Model\\Build\\' . $type; diff --git a/PHPCI/Controller/ProjectController.php b/PHPCI/Controller/ProjectController.php index e343ac88..e1b3e613 100644 --- a/PHPCI/Controller/ProjectController.php +++ b/PHPCI/Controller/ProjectController.php @@ -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)'); diff --git a/PHPCI/Model/Build/MercurialBuild.php b/PHPCI/Model/Build/MercurialBuild.php new file mode 100644 index 00000000..de95c06b --- /dev/null +++ b/PHPCI/Model/Build/MercurialBuild.php @@ -0,0 +1,59 @@ + +* @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()); + } +}