Add support for Bitbucket's Mercurial based repos

This commit is contained in:
bochkovprivate 2017-03-19 11:17:10 +07:00 committed by Dmitry Khomutov
parent ea8c5393bc
commit 7084b49da7
No known key found for this signature in database
GPG key ID: 7EB36C9576F9ECB9
9 changed files with 126 additions and 22 deletions

View file

@ -51,6 +51,9 @@ class BuildFactory
case 'bitbucket':
$type = 'BitbucketBuild';
break;
case 'bitbuckethg':
$type = 'BitbucketHgBuild';
break;
case 'gitlab':
$type = 'GitlabBuild';
break;

View file

@ -332,19 +332,20 @@ class ProjectController extends PHPCensor\Controller
$form->addField(new Form\Element\Hidden('pubkey'));
$options = [
'choose' => Lang::get('select_repository_type'),
'github' => 'GitHub',
'bitbucket' => 'Bitbucket',
'gitlab' => 'GitLab',
'gogs' => 'Gogs',
'remote' => 'Git',
'local' => Lang::get('local'),
'hg' => 'Mercurial (Hg)',
'svn' => 'SVN',
'choose' => Lang::get('select_repository_type'),
'github' => 'GitHub',
'bitbucket' => 'Bitbucket (Git)',
'bitbuckethg' => 'Bitbucket (Hg)',
'gitlab' => 'GitLab',
'gogs' => 'Gogs',
'remote' => 'Git',
'local' => Lang::get('local'),
'hg' => 'Mercurial (Hg)',
'svn' => 'SVN',
];
$field = Form\Element\Select::create('type', Lang::get('where_hosted'), true);
$field->setPattern('^(github|bitbucket|gitlab|gogs|remote|local|hg|svn)');
$field->setPattern('^(github|bitbucket|bitbuckethg|gitlab|gogs|remote|local|hg|svn)');
$field->setOptions($options);
$field->setClass('form-control')->setContainerClass('form-group');
$form->addField($field);
@ -428,7 +429,7 @@ class ProjectController extends PHPCensor\Controller
$validators = [
'hg' => [
'regex' => '/^(https?):\/\//',
'regex' => '/^(ssh|https?):\/\//',
'message' => Lang::get('error_mercurial')
],
'remote' => [
@ -447,6 +448,10 @@ class ProjectController extends PHPCensor\Controller
'regex' => '/^[a-zA-Z0-9_\-]+\/[a-zA-Z0-9_\-\.]+$/',
'message' => Lang::get('error_bitbucket')
],
'bitbuckethg' => [
'regex' => '/^[a-zA-Z0-9_\-]+\/[a-zA-Z0-9_\-\.]+$/',
'message' => Lang::get('error_bitbucket')
],
];
if (in_array($type, $validators) && !preg_match($validators[$type]['regex'], $val)) {

View file

@ -80,7 +80,7 @@ class WebhookController extends Controller
*/
public function bitbucket($projectId)
{
$project = $this->fetchProject($projectId, ['bitbucket', 'remote']);
$project = $this->fetchProject($projectId, ['bitbucket', 'bitbuckethg', 'remote']);
// Support both old services and new webhooks
if ($payload = $this->getParam('payload')) {

View file

@ -3,31 +3,31 @@
namespace PHPCensor\Model\Build;
/**
* Bitbucket Build Model
* BitBucket Build Model
*
* @author Dan Cryer <dan@block8.co.uk>
*/
class BitbucketBuild extends RemoteGitBuild
{
/**
* Get link to commit from another source (i.e. Github)
*/
* Get link to commit from another source (i.e. BitBucket)
*/
public function getCommitLink()
{
return 'https://bitbucket.org/' . $this->getProject()->getReference() . '/commits/' . $this->getCommitId();
}
/**
* Get link to branch from another source (i.e. Github)
*/
* Get link to branch from another source (i.e. BitBucket)
*/
public function getBranchLink()
{
return 'https://bitbucket.org/' . $this->getProject()->getReference() . '/src/?at=' . $this->getBranch();
}
/**
* Get the URL to be used to clone this remote repository.
*/
* Get the URL to be used to clone this remote repository.
*/
protected function getCloneUrl()
{
$key = trim($this->getProject()->getSshPrivateKey());
@ -38,4 +38,30 @@ class BitbucketBuild extends RemoteGitBuild
return 'https://bitbucket.org/' . $this->getProject()->getReference() . '.git';
}
}
/**
* Get a template to use for generating links to files.
*
* @return string
*/
public function getFileLinkTemplate()
{
$reference = $this->getProject()->getReference();
$branch = $this->getBranch();
if ($this->getExtra('build_type') == 'pull_request') {
$matches = [];
preg_match('/[\/:]([a-zA-Z0-9_\-]+\/[a-zA-Z0-9_\-]+)/', $this->getExtra('remote_url'), $matches);
$reference = $matches[1];
$branch = $this->getExtra('remote_branch');
}
$link = 'https://bitbucket.org/' . $reference . '/';
$link .= 'src/' . $branch . '/';
$link .= '{FILE}';
$link .= '#{BASEFILE}-{LINE}';
return $link;
}
}

View file

@ -0,0 +1,67 @@
<?php
namespace PHPCensor\Model\Build;
/**
* BitBucket Build Model
*
* @author Artem Bochkov <artem.v.bochkov@gmail.com>
*/
class BitbucketHgBuild extends MercurialBuild
{
/**
* Get link to commit from another source (i.e. BitBucket)
*/
public function getCommitLink()
{
return 'https://bitbucket.org/' . $this->getProject()->getReference() . '/commits/' . $this->getCommitId();
}
/**
* Get link to branch from another source (i.e. BitBucket)
*/
public function getBranchLink()
{
return 'https://bitbucket.org/' . $this->getProject()->getReference() . '/src/?at=' . $this->getBranch();
}
/**
* Get the URL to be used to clone this remote repository.
*/
protected function getCloneUrl()
{
$key = trim($this->getProject()->getSshPrivateKey());
if (!empty($key)) {
return 'ssh://hg@bitbucket.org/' . $this->getProject()->getReference();
} else {
return 'https://bitbucket.org/' . $this->getProject()->getReference();
}
}
/**
* Get a template to use for generating links to files.
*
* @return string
*/
public function getFileLinkTemplate()
{
$reference = $this->getProject()->getReference();
$branch = $this->getBranch();
if ($this->getExtra('build_type') == 'pull_request') {
$matches = [];
preg_match('/[\/:]([a-zA-Z0-9_\-]+\/[a-zA-Z0-9_\-]+)/', $this->getExtra('remote_url'), $matches);
$reference = $matches[1];
$branch = $this->getExtra('remote_branch');
}
$link = 'https://bitbucket.org/' . $reference . '/';
$link .= 'src/' . $branch . '/';
$link .= '{FILE}';
$link .= '#{BASEFILE}-{LINE}';
return $link;
}
}

View file

@ -25,9 +25,9 @@ class MercurialBuild extends Build
*/
public function createWorkingCopy(Builder $builder, $buildPath)
{
$key = trim($this->getProject()->getSshPublicKey());
$key = trim($this->getProject()->getSshPrivateKey());
if (!empty($key) && strpos($this->getProject()->getReference(), 'ssh') > -1) {
if (!empty($key)) {
$success = $this->cloneBySsh($builder, $buildPath);
} else {
$success = $this->cloneByHttp($builder, $buildPath);

View file

@ -754,6 +754,7 @@ class Project extends Model
break;
case 'bitbucket':
case 'bitbuckethg':
$icon = 'bitbucket';
break;

View file

@ -6,7 +6,8 @@ $linkTemplate = $build->getFileLinkTemplate();
/** @var \PHPCensor\Model\BuildError[] $errors */
foreach ($errors as $error):
$link = str_replace('{FILE}', $error->getFile(), $linkTemplate);
$link = str_replace('{BASEFILE}', basename($error->getFile()), $linkTemplate);
$link = str_replace('{FILE}', $error->getFile(), $link);
$link = str_replace('{LINE}', $error->getLineStart(), $link);
$link = str_replace('{LINE_END}', $error->getLineEnd(), $link);
?>

View file

@ -170,6 +170,7 @@
break;
case 'bitbucket':
case 'bitbuckethg':
$url = APP_URL . 'webhook/bitbucket/' . $project->getId();
Lang::out('webhooks_help_bitbucket', $project->getReference());
break;