Merge branch 'pull-request-branch'

This commit is contained in:
Dmitry Khomutov 2018-02-23 21:00:15 +07:00
commit e3f131f37d
No known key found for this signature in database
GPG key ID: EC19426474B37AAC
17 changed files with 261 additions and 102 deletions

View file

@ -207,7 +207,6 @@ class WebhookController extends Controller
$message = $commit['message'];
$extra = [
'build_type' => 'pull_request',
'pull_request_number' => $payload['pullrequest']['id'],
'remote_branch' => $payload['pullrequest']['source']['branch']['name'],
'remote_reference' => $payload['pullrequest']['source']['repository']['full_name'],
@ -437,7 +436,7 @@ class WebhookController extends Controller
foreach ($commits as $commit) {
// Skip all but the current HEAD commit ID:
$id = $commit['sha'];
if ($id != $payload['pull_request']['head']['sha']) {
if ($id !== $payload['pull_request']['head']['sha']) {
$results[$id] = ['status' => 'ignored', 'message' => 'not branch head'];
continue;
}
@ -447,14 +446,10 @@ class WebhookController extends Controller
$committer = $commit['commit']['author']['email'];
$message = $commit['commit']['message'];
$remoteUrlKey = $payload['pull_request']['head']['repo']['private'] ? 'ssh_url' : 'clone_url';
$extra = [
'build_type' => 'pull_request',
'pull_request_id' => $payload['pull_request']['id'],
'pull_request_number' => $payload['number'],
'remote_branch' => $payload['pull_request']['head']['ref'],
'remote_url' => $payload['pull_request']['head']['repo'][$remoteUrlKey],
'remote_reference' => $payload['pull_request']['head']['repo']['full_name'],
];
$results[$id] = $this->createBuild(

View file

@ -50,6 +50,5 @@ class FixDatabaseColumns extends AbstractMigration
public function down()
{
}
}

View file

@ -6,27 +6,27 @@ class UniqueEmailAndNameUserFields extends AbstractMigration
{
public function up()
{
$user_table = $this->table('user');
$table = $this->table('user');
if (!$user_table->hasIndex('email', ['unique' => true])) {
$user_table->addIndex('email', ['unique' => true])->save();
if (!$table->hasIndex('email')) {
$table->addIndex('email', ['unique' => true])->save();
}
if (!$user_table->hasIndex('name', ['unique' => true])) {
$user_table->addIndex('name', ['unique' => true])->save();
if (!$table->hasIndex('name')) {
$table->addIndex('name', ['unique' => true])->save();
}
}
public function down()
{
$user_table = $this->table('user');
$table = $this->table('user');
if ($user_table->hasIndex('email', ['unique' => true])) {
$user_table->removeIndex(['email'], ['unique' => true])->save();
if ($table->hasIndex('email')) {
$table->removeIndex(['email'])->save();
}
if ($user_table->hasIndex('name', ['unique' => true])) {
$user_table->removeIndex(['name'], ['unique' => true])->save();
if ($table->hasIndex('name')) {
$table->removeIndex(['name'])->save();
}
}
}

View file

@ -8,11 +8,11 @@ class RemoveUniqueNameIndex extends AbstractMigration
{
$user = $this->table('user');
if ($user->hasIndex('name', ['unique' => true])) {
$user->removeIndex(['name'], ['unique' => true])->save();
if ($user->hasIndex('name')) {
$user->removeIndex(['name'])->save();
}
if (!$user->hasIndex('name', ['unique' => true])) {
if (!$user->hasIndex('name')) {
$user->addIndex(['name'], ['unique' => false])->save();
}
}

View file

@ -24,8 +24,8 @@ class ConvertErrors extends AbstractMigration
$this->metaStore = Factory::getStore('BuildMeta');
$this->errorStore = Factory::getStore('BuildError');
while ($count === 100) {
$data = $this->metaStore->getErrorsForUpgrade(100);
while ($count >= 100) {
$data = $this->metaStore->getErrorsForUpgrade(100);
$count = count($data);
/** @var \PHPCensor\Model\BuildMeta $meta */

View file

@ -0,0 +1,56 @@
<?php
use Phinx\Migration\AbstractMigration;
use b8\Store\Factory;
use PHPCensor\Store\BuildStore;
use PHPCensor\Model\Build;
class AddedRequestBranchToBuild extends AbstractMigration
{
public function up()
{
/** @var BuildStore $buildStore */
$buildStore = Factory::getStore('Build');
$count = 100;
$offset = 0;
while ($count >= 100) {
$builds = $buildStore->getBuilds(100, $offset);
$offset += 100;
$count = count($builds);
/** @var Build $build */
foreach ($builds as &$build) {
$extra = $build->getExtra();
if (isset($extra['build_type'])) {
unset($extra['build_type']);
$build->setSource(Build::SOURCE_WEBHOOK_PULL_REQUEST);
}
if (!empty($extra['remote_url'])) {
preg_match(
'/[\/:]([a-zA-Z0-9_\-]+\/[a-zA-Z0-9_\-]+)/',
$extra['remote_url'],
$matches
);
$remoteReference = $matches[1];
if ($remoteReference && empty($extra['remote_reference'])) {
$extra['remote_reference'] = $remoteReference;
}
}
unset($extra['build_type']);
unset($extra['pull_request_id']);
$build->setExtra(json_encode($extra));
$buildStore->save($build);
}
unset($build);
}
}
public function down()
{
}
}

View file

@ -711,6 +711,22 @@ class Build extends Model
return '#';
}
/**
* Get remote branch (from pull request) from another source (i.e. Github)
*/
public function getRemoteBranch()
{
return $this->getExtra('remote_branch');
}
/**
* Get link to remote branch (from pull request) from another source (i.e. Github)
*/
public function getRemoteBranchLink()
{
return '#';
}
/**
* Get link to tag from another source (i.e. Github)
*/
@ -849,13 +865,14 @@ class Build extends Model
/**
* Allows specific build types (e.g. Github) to report violations back to their respective services.
*
* @param Builder $builder
* @param $plugin
* @param $message
* @param int $severity
* @param null $file
* @param null $lineStart
* @param null $lineEnd
* @param string $plugin
* @param string $message
* @param integer $severity
* @param string $file
* @param integer $lineStart
* @param integer $lineEnd
*/
public function reportError(
Builder $builder,

View file

@ -19,6 +19,8 @@ class BitbucketBuild extends RemoteGitBuild
{
/**
* Get link to commit from another source (i.e. BitBucket)
*
* @return string
*/
public function getCommitLink()
{
@ -27,14 +29,29 @@ class BitbucketBuild extends RemoteGitBuild
/**
* Get link to branch from another source (i.e. BitBucket)
*
* @return string
*/
public function getBranchLink()
{
return 'https://bitbucket.org/' . $this->getProject()->getReference() . '/src/?at=' . $this->getBranch();
}
/**
* Get link to remote branch (from pull request) from another source (i.e. BitBucket)
*/
public function getRemoteBranchLink()
{
$remoteBranch = $this->getExtra('remote_branch');
$remoteReference = $this->getExtra('remote_reference');
return 'https://bitbucket.org/' . $remoteReference . '/src/?at=' . $remoteBranch;
}
/**
* Get link to tag from another source (i.e. BitBucket)
*
* @return string
*/
public function getTagLink()
{
@ -97,7 +114,7 @@ class BitbucketBuild extends RemoteGitBuild
$url = sprintf(
'/2.0/repositories/%s/commit/%s/statuses/build',
$this->getExtra('build_type') == 'pull_request'
Build::SOURCE_WEBHOOK_PULL_REQUEST === $this->getSource()
? $this->getExtra('remote_reference')
: $project->getReference(),
$this->getCommitId()
@ -128,6 +145,8 @@ class BitbucketBuild extends RemoteGitBuild
/**
* Get the URL to be used to clone this remote repository.
*
* @return string
*/
protected function getCloneUrl()
{
@ -149,7 +168,7 @@ class BitbucketBuild extends RemoteGitBuild
{
$reference = $this->getProject()->getReference();
if ($this->getExtra('build_type') == 'pull_request') {
if (Build::SOURCE_WEBHOOK_PULL_REQUEST === $this->getSource()) {
$reference = $this->getExtra('remote_reference');
}
@ -162,21 +181,15 @@ class BitbucketBuild extends RemoteGitBuild
}
/**
* Handle any post-clone tasks, like applying a pull request patch on top of the branch.
* @param Builder $builder
* @param $cloneTo
* @param array $extra
* @return bool
* @inheritdoc
*/
protected function postCloneSetup(Builder $builder, $cloneTo, array $extra = null)
{
$buildType = $this->getExtra('build_type');
$success = true;
$success = true;
$skipGitFinalization = false;
try {
if (!empty($buildType) && $buildType == 'pull_request') {
if (Build::SOURCE_WEBHOOK_PULL_REQUEST === $this->getSource()) {
$helper = new Bitbucket();
$diff = $helper->getPullRequestDiff(
$this->getProject()->getReference(),
@ -206,7 +219,8 @@ class BitbucketBuild extends RemoteGitBuild
/**
* Create an diff file on disk for this build.
*
* @param string $cloneTo
* @param string $cloneTo
* @param string $diff
*
* @return string
*/
@ -271,10 +285,12 @@ class BitbucketBuild extends RemoteGitBuild
/**
* Uses git diff to figure out what the diff line position is, based on the error line number.
*
* @param Builder $builder
* @param $file
* @param $line
* @return int|null
* @param string $file
* @param integer $line
*
* @return integer|null
*/
protected function getDiffLineNumber(Builder $builder, $file, $line)
{

View file

@ -2,15 +2,19 @@
namespace PHPCensor\Model\Build;
use PHPCensor\Model\Build;
/**
* BitBucket Build Model
*
* BitbucketHgBuild Build Model
*
* @author Artem Bochkov <artem.v.bochkov@gmail.com>
*/
class BitbucketHgBuild extends MercurialBuild
{
/**
* Get link to commit from another source (i.e. BitBucket)
*
* @return string
*/
public function getCommitLink()
{
@ -19,14 +23,39 @@ class BitbucketHgBuild extends MercurialBuild
/**
* Get link to branch from another source (i.e. BitBucket)
*
* @return string
*/
public function getBranchLink()
{
return 'https://bitbucket.org/' . $this->getProject()->getReference() . '/src/?at=' . $this->getBranch();
}
/**
* Get link to remote branch (from pull request) from another source (i.e. BitBucket)
*/
public function getRemoteBranchLink()
{
$remoteBranch = $this->getExtra('remote_branch');
$remoteReference = $this->getExtra('remote_reference');
return 'https://bitbucket.org/' . $remoteReference . '/src/?at=' . $remoteBranch;
}
/**
* Get link to tag from another source (i.e. BitBucket)
*
* @return string
*/
public function getTagLink()
{
return 'https://bitbucket.org/' . $this->getProject()->getReference() . '/src/?at=' . $this->getTag();
}
/**
* Get the URL to be used to clone this remote repository.
*
* @return string
*/
protected function getCloneUrl()
{
@ -48,11 +77,8 @@ class BitbucketHgBuild extends MercurialBuild
{
$reference = $this->getProject()->getReference();
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];
if (Build::SOURCE_WEBHOOK_PULL_REQUEST === $this->getSource()) {
$reference = $this->getExtra('remote_reference');
}
$link = 'https://bitbucket.org/' . $reference . '/';

View file

@ -18,23 +18,40 @@ use PHPCensor\Model\BuildError;
class GithubBuild extends RemoteGitBuild
{
/**
* Get link to commit from another source (i.e. Github)
*/
* Get link to commit from another source (i.e. Github)
*
* @return string
*/
public function getCommitLink()
{
return 'https://github.com/' . $this->getProject()->getReference() . '/commit/' . $this->getCommitId();
}
/**
* Get link to branch from another source (i.e. Github)
*/
* Get link to branch from another source (i.e. Github)
*
* @return string
*/
public function getBranchLink()
{
return 'https://github.com/' . $this->getProject()->getReference() . '/tree/' . $this->getBranch();
}
/**
* Get link to remote branch (from pull request) from another source (i.e. Github)
*/
public function getRemoteBranchLink()
{
$remoteBranch = $this->getExtra('remote_branch');
$remoteReference = $this->getExtra('remote_reference');
return 'https://github.com/' . $remoteReference . '/tree/' . $remoteBranch;
}
/**
* Get link to tag from another source (i.e. Github)
*
* @return string
*/
public function getTagLink()
{
@ -117,8 +134,10 @@ class GithubBuild extends RemoteGitBuild
}
/**
* Get the URL to be used to clone this remote repository.
*/
* Get the URL to be used to clone this remote repository.
*
* @return string
*/
protected function getCloneUrl()
{
$key = trim($this->getProject()->getSshPrivateKey());
@ -143,9 +162,9 @@ class GithubBuild extends RemoteGitBuild
if (!is_null($project)) {
$reference = $project->getReference();
$commitLink = '<a target="_blank" href="https://github.com/' . $reference . '/issues/$1">#$1</a>';
$commitLink = '<a href="https://github.com/' . $reference . '/issues/$1">#$1</a>';
$rtn = preg_replace('/\#([0-9]+)/', $commitLink, $rtn);
$rtn = preg_replace('/\@([a-zA-Z0-9_]+)/', '<a target="_blank" href="https://github.com/$1">@$1</a>', $rtn);
$rtn = preg_replace('/\@([a-zA-Z0-9_]+)/', '<a href="https://github.com/$1">@$1</a>', $rtn);
}
return $rtn;
@ -160,11 +179,8 @@ class GithubBuild extends RemoteGitBuild
{
$reference = $this->getProject()->getReference();
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];
if (Build::SOURCE_WEBHOOK_PULL_REQUEST === $this->getSource()) {
$reference = $this->getExtra('remote_reference');
}
$link = 'https://github.com/' . $reference . '/';
@ -176,20 +192,14 @@ class GithubBuild extends RemoteGitBuild
}
/**
* Handle any post-clone tasks, like applying a pull request patch on top of the branch.
* @param Builder $builder
* @param $cloneTo
* @param array $extra
* @return bool
* @inheritdoc
*/
protected function postCloneSetup(Builder $builder, $cloneTo, array $extra = null)
{
$buildType = $this->getExtra('build_type');
$success = true;
try {
if (!empty($buildType) && $buildType == 'pull_request') {
if (Build::SOURCE_WEBHOOK_PULL_REQUEST === $this->getSource()) {
$pullRequestId = $this->getExtra('pull_request_number');
$cmd = 'cd "%s" && git checkout -b php-censor/' . $this->getId()
@ -211,7 +221,7 @@ class GithubBuild extends RemoteGitBuild
}
/**
* @inheritDoc
* @inheritdoc
*/
public function reportError(
Builder $builder,
@ -259,10 +269,12 @@ class GithubBuild extends RemoteGitBuild
/**
* Uses git diff to figure out what the diff line position is, based on the error line number.
*
* @param Builder $builder
* @param $file
* @param $line
* @return int|null
* @param string $file
* @param integer $line
*
* @return integer|null
*/
protected function getDiffLineNumber(Builder $builder, $file, $line)
{

View file

@ -135,7 +135,7 @@ class RemoteGitBuild extends Build
* @param string $cloneTo
* @param array $extra
*
* @return bool
* @return boolean
*/
protected function postCloneSetup(Builder $builder, $cloneTo, array $extra = null)
{

View file

@ -24,9 +24,9 @@ foreach ($errors as $error):
</span>
</td>
<td><?= Lang::get($error->getPlugin()); ?></td>
<td><a href="<?php print $link; ?>" target="_blank"><?= $error->getFile(); ?></a></td>
<td><a href="<?= $link; ?>"><?= $error->getFile(); ?></a></td>
<td>
<a href="<?php print $link; ?>" target="_blank">
<a href="<?= $link; ?>">
<?php
if ($error->getLineStart() == $error->getLineEnd() || !$error->getLineEnd()) {
echo $error->getLineStart();

View file

@ -34,11 +34,18 @@ use PHPCensor\Model\BuildError;
<tr>
<th><?php Lang::out('branch'); ?></th>
<td style="text-align: right">
<a target="_blank" href="<?= $build->getBranchLink(); ?>">
<i class="fa fa-code-fork"></i> <?= $build->getBranch(); ?>
<?php if (Build::SOURCE_WEBHOOK_PULL_REQUEST === $build->getSource()): ?>
<a href="<?= $build->getRemoteBranchLink(); ?>">
<i class="fa fa-code-fork"></i>
<?= $build->getRemoteBranch(); ?> :
</a>
<?php endif; ?>
<a href="<?= $build->getBranchLink(); ?>">
<i class="fa fa-code-fork"></i>
<?= $build->getBranch(); ?>
</a>
<?php if ($tag = $build->getTag()): ?> /
<a target="_blank" href="<?= $build->getTagLink(); ?>">
<a href="<?= $build->getTagLink(); ?>">
<i class="fa fa-tag"></i> <?= $tag; ?>
</a>
<?php endif; ?>
@ -58,7 +65,7 @@ use PHPCensor\Model\BuildError;
<tr>
<th><?php Lang::out('merged_branches'); ?></th>
<td style="text-align: right">
<a target="_blank" href="<?= $build->getBranchLink(); ?>">
<a href="<?= $build->getBranchLink(); ?>">
<i class="fa fa-code-fork"></i> <?= $build->getBranch(); ?>
</a>
+ <?php
@ -97,7 +104,7 @@ use PHPCensor\Model\BuildError;
<tr>
<th><?php Lang::out('commit'); ?></th>
<td style="text-align: right">
<a target="_blank" href="<?= $build->getCommitLink(); ?>">
<a href="<?= $build->getCommitLink(); ?>">
<?php print substr($build->getCommitId(), 0, 7); ?>
</a>
</td>

View file

@ -147,7 +147,7 @@
<?php
if (!empty($build->getCommitId())) {
print sprintf(
'<a href="%s" target="_blank">%s %s</a>',
'<a href="%s">%s %s</a>',
$build->getCommitLink(),
substr($build->getCommitId(), 0, 7),
$build->getCommitterEmail() ? ('(' . $build->getCommitterEmail() . ')') : ''
@ -159,12 +159,22 @@
</td>
<td>
<?php if (\PHPCensor\Model\Build::SOURCE_WEBHOOK_PULL_REQUEST === $build->getSource()): ?>
<a href="<?= $build->getRemoteBranchLink(); ?>">
<i class="fa fa-code-fork"></i>
<?= $build->getRemoteBranch(); ?> :
</a>
<?php endif; ?>
<a href="<?= $build->getBranchLink();?>">
<i class="fa fa-code-fork"></i>
<?= $build->getBranch(); ?>
</a>
<?php $branches = $build->getExtra('branches'); ?>
<a href="<?= $build->getBranchLink();?>"><i class="fa fa-code-fork"></i> <?php echo $build->getBranch(); ?></a>
<?= $branches ? ' + '.implode(', ', $branches) : ''; ?>
<?php if ($tag = $build->getTag()): ?> /
<a href="<?= $build->getTagLink(); ?>" target="_blank">
<i class="fa fa-tag"></i> <?= $tag; ?>
<a href="<?= $build->getTagLink(); ?>">
<i class="fa fa-tag"></i>
<?= $tag; ?>
</a>
<?php endif; ?>
</td>

View file

@ -5,6 +5,7 @@
*/
use PHPCensor\Helper\Lang;
use PHPCensor\Model\Build;
?>
@ -56,7 +57,7 @@ $branches = $build->getExtra('branches');
<?php
if (!empty($build->getCommitId())) {
print sprintf(
'<a href="%s" target="_blank">%s %s</a>',
'<a href="%s">%s %s</a>',
$build->getCommitLink(),
substr($build->getCommitId(), 0, 7),
$build->getCommitterEmail() ? ('(' . $build->getCommitterEmail() . ')') : ''
@ -67,13 +68,21 @@ $branches = $build->getExtra('branches');
?>
</td>
<td>
<a href="<?= $build->getBranchLink(); ?>" target="_blank">
<i class="fa fa-code-fork"></i> <?= $build->getBranch(); ?>
<?php if (Build::SOURCE_WEBHOOK_PULL_REQUEST === $build->getSource()): ?>
<a href="<?= $build->getRemoteBranchLink(); ?>">
<i class="fa fa-code-fork"></i>
<?= $build->getRemoteBranch(); ?> :
</a>
<?php endif; ?>
<a href="<?= $build->getBranchLink(); ?>">
<i class="fa fa-code-fork"></i>
<?= $build->getBranch(); ?>
</a>
<?= $branches ? ' + '.implode(', ', $branches) : ''; ?>
<?php if ($tag = $build->getTag()): ?> /
<a href="<?= $build->getTagLink(); ?>" target="_blank">
<i class="fa fa-tag"></i> <?= $tag; ?>
<a href="<?= $build->getTagLink(); ?>">
<i class="fa fa-tag"></i>
<?= $tag; ?>
</a>
<?php endif; ?>
</td>

View file

@ -87,18 +87,29 @@ use PHPCensor\Model\Build;
</h3>
<div class="timeline-body">
<a href="<?= $build->getBranchLink();?>"><i class="fa fa-code-fork"></i> <?php echo $build->getBranch(); ?></a>
<?php if (Build::SOURCE_WEBHOOK_PULL_REQUEST === $build->getSource()): ?>
<a href="<?= $build->getRemoteBranchLink(); ?>">
<i class="fa fa-code-fork"></i>
<?= $build->getRemoteBranch(); ?> :
</a>
<?php endif; ?>
<a href="<?= $build->getBranchLink(); ?>">
<i class="fa fa-code-fork"></i>
<?= $build->getBranch(); ?>
</a>
<?= $branches ? ' + '.implode(', ', $branches) : ''; ?>
<?php if ($tag = $build->getTag()): ?> /
<a href="<?= $build->getTagLink(); ?>" target="_blank">
<i class="fa fa-tag"></i> <?= $tag; ?>
<a href="<?= $build->getTagLink(); ?>">
<i class="fa fa-tag"></i>
<?= $tag; ?>
</a>
<?php endif; ?>
<?php
if (!empty($build->getCommitId())) {
echo ' &mdash; ';
echo sprintf(
'<a href="%s" target="_blank">%s %s</a>',
'<a href="%s">%s %s</a>',
$build->getCommitLink(),
substr($build->getCommitId(), 0, 7),
$build->getCommitterEmail() ? ('(' . $build->getCommitterEmail() . ')') : ''

View file

@ -3,7 +3,6 @@
namespace Tests\PHPCensor\Service;
use PHPCensor\Model\Build;
use PHPCensor\Model\Project;
use PHPCensor\Service\BuildService;
/**
@ -32,12 +31,14 @@ class BuildServiceTest extends \PHPUnit\Framework\TestCase
public function setUp()
{
$this->mockBuildStore = $this->getMockBuilder('PHPCensor\Store\BuildStore')->getMock();
$this->mockBuildStore->expects($this->any())
->method('save')
->will($this->returnArgument(0));
$this->mockBuildStore
->expects($this->any())
->method('save')
->will($this->returnArgument(0));
$this->mockEnvironmentStore = $this->getMockBuilder('PHPCensor\Store\EnvironmentStore')->getMock();
$this->mockEnvironmentStore->expects($this->any())
$this->mockEnvironmentStore
->expects($this->any())
->method('getByProjectId')
->will($this->returnValue(['items' => [], 'count' => 0]));