Merge branch 'feature-hg'

This commit is contained in:
Dmitry Khomutov 2017-03-25 09:33:09 +07:00
commit 03e056b845
No known key found for this signature in database
GPG key ID: 7EB36C9576F9ECB9
12 changed files with 191 additions and 121 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')) {
@ -110,8 +110,11 @@ class WebhookController extends Controller
foreach ($payload['push']['changes'] as $commit) {
try {
$email = $commit['new']['target']['author']['raw'];
$email = substr($email, 0, strpos($email, '>'));
$email = substr($email, strpos($email, '<') + 1);
if (strpos($email, '>') !== false) {
// In order not to loose email if it is RAW, w/o "<>" symbols
$email = substr($email, 0, strpos($email, '>'));
$email = substr($email, strpos($email, '<') + 1);
}
$results[$commit['new']['target']['hash']] = $this->createBuild(
$project,

View file

@ -930,4 +930,50 @@ class Build extends Model
{
return false;
}
/**
* Create an SSH key file on disk for this build.
*
* @param string $cloneTo
*
* @return string
*/
protected function writeSshKey($cloneTo)
{
$keyPath = dirname($cloneTo . '/temp');
$keyFile = $keyPath . '.key';
file_put_contents($keyFile, $this->getProject()->getSshPrivateKey());
chmod($keyFile, 0600);
return $keyFile;
}
/**
* Create an SSH wrapper script for Svn to use, to disable host key checking, etc.
*
* @param string $cloneTo
* @param string $keyFile
*
* @return string
*/
protected function writeSshWrapper($cloneTo, $keyFile)
{
$path = dirname($cloneTo . '/temp');
$wrapperFile = $path . '.sh';
$sshFlags = '-o CheckHostIP=no -o IdentitiesOnly=yes -o StrictHostKeyChecking=no -o PasswordAuthentication=no';
// Write out the wrapper script for this build:
$script = <<<OUT
#!/bin/sh
ssh {$sshFlags} -o IdentityFile={$keyFile} $*
OUT;
file_put_contents($wrapperFile, $script);
shell_exec('chmod +x "' . $wrapperFile . '"');
return $wrapperFile;
}
}

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,16 +25,16 @@ 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);
}
if (!$success) {
$builder->logFailure('Failed to clone remote git repository.');
$builder->logFailure('Failed to clone remote hg repository.');
return false;
}
@ -42,7 +42,7 @@ class MercurialBuild extends Build
}
/**
* Use a HTTP-based Mercurial clone.
* Use a HTTP-based hg clone.
*/
protected function cloneByHttp(Builder $builder, $cloneTo)
{
@ -50,15 +50,20 @@ class MercurialBuild extends Build
}
/**
* Use an SSH-based Mercurial clone.
* Use an SSH-based hg clone.
*
* @param Builder $builder
* @param string $cloneTo
*
* @return bool
*/
protected function cloneBySsh(Builder $builder, $cloneTo)
{
$keyFile = $this->writeSshKey();
$keyFile = $this->writeSshKey($cloneTo);
// Do the git clone:
$cmd = 'hg clone --ssh "ssh -i '.$keyFile.'" %s "%s"';
$success = $builder->executeCommand($cmd, $this->getCloneUrl(), $cloneTo);
// Do the hg clone:
$cmd = 'hg clone --ssh "ssh -i '.$keyFile.'" %s "%s" -r %s';
$success = $builder->executeCommand($cmd, $this->getCloneUrl(), $cloneTo, $this->getBranch());
if ($success) {
$success = $this->postCloneSetup($builder, $cloneTo);
@ -66,6 +71,7 @@ class MercurialBuild extends Build
// Remove the key file:
unlink($keyFile);
return $success;
}

View file

@ -121,48 +121,4 @@ class RemoteGitBuild extends Build
return $success;
}
/**
* Create an SSH key file on disk for this build.
* @param $cloneTo
* @return string
*/
protected function writeSshKey($cloneTo)
{
$keyPath = dirname($cloneTo . '/temp');
$keyFile = $keyPath . '.key';
// Write the contents of this project's git key to the file:
file_put_contents($keyFile, $this->getProject()->getSshPrivateKey());
chmod($keyFile, 0600);
// Return the filename:
return $keyFile;
}
/**
* Create an SSH wrapper script for Git to use, to disable host key checking, etc.
* @param $cloneTo
* @param $keyFile
* @return string
*/
protected function writeSshWrapper($cloneTo, $keyFile)
{
$path = dirname($cloneTo . '/temp');
$wrapperFile = $path . '.sh';
$sshFlags = '-o CheckHostIP=no -o IdentitiesOnly=yes -o StrictHostKeyChecking=no -o PasswordAuthentication=no';
// Write out the wrapper script for this build:
$script = <<<OUT
#!/bin/sh
ssh {$sshFlags} -o IdentityFile={$keyFile} $*
OUT;
file_put_contents($wrapperFile, $script);
shell_exec('chmod +x "'.$wrapperFile.'"');
return $wrapperFile;
}
}

View file

@ -109,8 +109,7 @@ class SubversionBuild extends Build
*/
protected function cloneBySsh(Builder $builder, $cloneTo)
{
$cmd = $this->svnCommand . ' %s "%s"';
$cmd = $this->svnCommand . ' %s "%s"';
$keyFile = $this->writeSshKey($cloneTo);
$sshWrapper = $this->writeSshWrapper($cloneTo, $keyFile);
$cmd = 'export SVN_SSH="' . $sshWrapper . '" && ' . $cmd;
@ -123,48 +122,4 @@ class SubversionBuild extends Build
return $success;
}
/**
* Create an SSH key file on disk for this build.
* @param $cloneTo
* @return string
*/
protected function writeSshKey($cloneTo)
{
$keyPath = dirname($cloneTo . '/temp');
$keyFile = $keyPath . '.key';
// Write the contents of this project's svn key to the file:
file_put_contents($keyFile, $this->getProject()->getSshPrivateKey());
chmod($keyFile, 0600);
// Return the filename:
return $keyFile;
}
/**
* Create an SSH wrapper script for Svn to use, to disable host key checking, etc.
* @param $cloneTo
* @param $keyFile
* @return string
*/
protected function writeSshWrapper($cloneTo, $keyFile)
{
$path = dirname($cloneTo . '/temp');
$wrapperFile = $path . '.sh';
$sshFlags = '-o CheckHostIP=no -o IdentitiesOnly=yes -o StrictHostKeyChecking=no -o PasswordAuthentication=no';
// Write out the wrapper script for this build:
$script = <<<OUT
#!/bin/sh
ssh {$sshFlags} -o IdentityFile={$keyFile} $*
OUT;
file_put_contents($wrapperFile, $script);
shell_exec('chmod +x "' . $wrapperFile . '"');
return $wrapperFile;
}
}

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;