diff --git a/src/PHPCensor/BuildFactory.php b/src/PHPCensor/BuildFactory.php index e6d54c56..e1d46b3a 100644 --- a/src/PHPCensor/BuildFactory.php +++ b/src/PHPCensor/BuildFactory.php @@ -51,6 +51,9 @@ class BuildFactory case 'bitbucket': $type = 'BitbucketBuild'; break; + case 'bitbuckethg': + $type = 'BitbucketHgBuild'; + break; case 'gitlab': $type = 'GitlabBuild'; break; diff --git a/src/PHPCensor/Controller/ProjectController.php b/src/PHPCensor/Controller/ProjectController.php index ce837a75..8565660d 100644 --- a/src/PHPCensor/Controller/ProjectController.php +++ b/src/PHPCensor/Controller/ProjectController.php @@ -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)) { diff --git a/src/PHPCensor/Controller/WebhookController.php b/src/PHPCensor/Controller/WebhookController.php index d3e3c609..6ac1b5da 100644 --- a/src/PHPCensor/Controller/WebhookController.php +++ b/src/PHPCensor/Controller/WebhookController.php @@ -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')) { diff --git a/src/PHPCensor/Model/Build/BitbucketBuild.php b/src/PHPCensor/Model/Build/BitbucketBuild.php index 3a7a57d7..6c99cb76 100644 --- a/src/PHPCensor/Model/Build/BitbucketBuild.php +++ b/src/PHPCensor/Model/Build/BitbucketBuild.php @@ -3,31 +3,31 @@ namespace PHPCensor\Model\Build; /** - * Bitbucket Build Model + * BitBucket Build Model * * @author Dan Cryer */ 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; + } } diff --git a/src/PHPCensor/Model/Build/BitbucketHgBuild.php b/src/PHPCensor/Model/Build/BitbucketHgBuild.php new file mode 100644 index 00000000..9b687d29 --- /dev/null +++ b/src/PHPCensor/Model/Build/BitbucketHgBuild.php @@ -0,0 +1,67 @@ + + */ +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; + } +} diff --git a/src/PHPCensor/Model/Build/MercurialBuild.php b/src/PHPCensor/Model/Build/MercurialBuild.php index c75db5d4..30fb442c 100644 --- a/src/PHPCensor/Model/Build/MercurialBuild.php +++ b/src/PHPCensor/Model/Build/MercurialBuild.php @@ -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); diff --git a/src/PHPCensor/Model/Project.php b/src/PHPCensor/Model/Project.php index 4ce32e54..361a3f34 100644 --- a/src/PHPCensor/Model/Project.php +++ b/src/PHPCensor/Model/Project.php @@ -754,6 +754,7 @@ class Project extends Model break; case 'bitbucket': + case 'bitbuckethg': $icon = 'bitbucket'; break; diff --git a/src/PHPCensor/View/Build/errors.phtml b/src/PHPCensor/View/Build/errors.phtml index d6021d21..26822fb5 100644 --- a/src/PHPCensor/View/Build/errors.phtml +++ b/src/PHPCensor/View/Build/errors.phtml @@ -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); ?> diff --git a/src/PHPCensor/View/Project/view.phtml b/src/PHPCensor/View/Project/view.phtml index 9d6f2811..6654edf3 100644 --- a/src/PHPCensor/View/Project/view.phtml +++ b/src/PHPCensor/View/Project/view.phtml @@ -170,6 +170,7 @@ break; case 'bitbucket': + case 'bitbuckethg': $url = APP_URL . 'webhook/bitbucket/' . $project->getId(); Lang::out('webhooks_help_bitbucket', $project->getReference()); break;