Added tags for Github builds.

This commit is contained in:
Dmitry Khomutov 2017-04-22 20:02:24 +07:00
parent f26f000bb4
commit 647a5cedcd
No known key found for this signature in database
GPG key ID: 7EB36C9576F9ECB9
9 changed files with 151 additions and 22 deletions

View file

@ -57,9 +57,9 @@ class CreateBuildCommand extends Command
*/ */
public function execute(InputInterface $input, OutputInterface $output) public function execute(InputInterface $input, OutputInterface $output)
{ {
$projectId = $input->getArgument('projectId'); $projectId = $input->getArgument('projectId');
$commitId = $input->getOption('commit'); $commitId = $input->getOption('commit');
$branch = $input->getOption('branch'); $branch = $input->getOption('branch');
$environment = $input->hasOption('environment') ? $input->getOption('environment') : null; $environment = $input->hasOption('environment') ? $input->getOption('environment') : null;
$project = $this->projectStore->getById($projectId); $project = $this->projectStore->getById($projectId);

View file

@ -148,7 +148,16 @@ class ProjectController extends PHPCensor\Controller
} }
$email = $_SESSION['php-censor-user']->getEmail(); $email = $_SESSION['php-censor-user']->getEmail();
$build = $this->buildService->createBuild($project, $environment, null, urldecode($branch), $email, null, $extra); $build = $this->buildService->createBuild(
$project,
$environment,
null,
urldecode($branch),
null,
$email,
null,
$extra
);
if ($this->buildService->queueError) { if ($this->buildService->queueError) {
$_SESSION['global_error'] = Lang::get('add_to_queue_failed'); $_SESSION['global_error'] = Lang::get('add_to_queue_failed');

View file

@ -121,6 +121,7 @@ class WebhookController extends Controller
$project, $project,
$commit['new']['target']['hash'], $commit['new']['target']['hash'],
$commit['new']['name'], $commit['new']['name'],
null,
$email, $email,
$commit['new']['target']['message'] $commit['new']['target']['message']
); );
@ -152,6 +153,7 @@ class WebhookController extends Controller
$project, $project,
$commit['raw_node'], $commit['raw_node'],
$commit['branch'], $commit['branch'],
null,
$email, $email,
$commit['message'] $commit['message']
); );
@ -179,7 +181,7 @@ class WebhookController extends Controller
$commitMessage = $this->getParam('message'); $commitMessage = $this->getParam('message');
$committer = $this->getParam('committer'); $committer = $this->getParam('committer');
return $this->createBuild($project, $commit, $branch, $committer, $commitMessage); return $this->createBuild($project, $commit, $branch, null, $committer, $commitMessage);
} }
/** /**
@ -245,7 +247,9 @@ class WebhookController extends Controller
$results[$commit['id']] = ['status' => 'ignored']; $results[$commit['id']] = ['status' => 'ignored'];
} else { } else {
try { try {
$tag = null;
if ($isTag) { if ($isTag) {
$tag = str_replace('refs/tags/', '', $payload['ref']);
$branch = str_replace('refs/heads/', '', $payload['base_ref']); $branch = str_replace('refs/heads/', '', $payload['base_ref']);
$committer = $payload['pusher']['email']; $committer = $payload['pusher']['email'];
} else { } else {
@ -257,6 +261,7 @@ class WebhookController extends Controller
$project, $project,
$commit['id'], $commit['id'],
$branch, $branch,
$tag,
$committer, $committer,
$commit['message'] $commit['message']
); );
@ -339,7 +344,7 @@ class WebhookController extends Controller
'remote_url' => $payload['pull_request']['head']['repo'][$remoteUrlKey], 'remote_url' => $payload['pull_request']['head']['repo'][$remoteUrlKey],
]; ];
$results[$id] = $this->createBuild($project, $id, $branch, $committer, $message, $extra); $results[$id] = $this->createBuild($project, $id, $branch, null, $committer, $message, $extra);
$status = 'ok'; $status = 'ok';
} catch (Exception $ex) { } catch (Exception $ex) {
$results[$id] = ['status' => 'failed', 'error' => $ex->getMessage()]; $results[$id] = ['status' => 'failed', 'error' => $ex->getMessage()];
@ -367,7 +372,7 @@ class WebhookController extends Controller
$commit = $attributes['last_commit']; $commit = $attributes['last_commit'];
$committer = $commit['author']['email']; $committer = $commit['author']['email'];
return $this->createBuild($project, $commit['id'], $branch, $committer, $commit['message']); return $this->createBuild($project, $commit['id'], $branch, null, $committer, $commit['message']);
} }
} }
@ -385,6 +390,7 @@ class WebhookController extends Controller
$project, $project,
$commit['id'], $commit['id'],
$branch, $branch,
null,
$committer, $committer,
$commit['message'] $commit['message']
); );
@ -417,7 +423,7 @@ class WebhookController extends Controller
$commitMessage = $this->getParam('message'); $commitMessage = $this->getParam('message');
$committer = $this->getParam('committer'); $committer = $this->getParam('committer');
return $this->createBuild($project, $commit, $branch, $committer, $commitMessage); return $this->createBuild($project, $commit, $branch, null, $committer, $commitMessage);
} }
/** /**
@ -471,6 +477,7 @@ class WebhookController extends Controller
$project, $project,
$commit['id'], $commit['id'],
$branch, $branch,
null,
$committer, $committer,
$commit['message'] $commit['message']
); );
@ -490,11 +497,12 @@ class WebhookController extends Controller
* Wrapper for creating a new build. * Wrapper for creating a new build.
* *
* @param Project $project * @param Project $project
* @param string $commitId * @param string $commitId
* @param string $branch * @param string $branch
* @param string $committer * @param string $tag
* @param string $commitMessage * @param string $committer
* @param array $extra * @param string $commitMessage
* @param array $extra
* *
* @return array * @return array
* *
@ -504,6 +512,7 @@ class WebhookController extends Controller
Project $project, Project $project,
$commitId, $commitId,
$branch, $branch,
$tag,
$committer, $committer,
$commitMessage, $commitMessage,
array $extra = null array $extra = null
@ -533,11 +542,21 @@ class WebhookController extends Controller
foreach ($environment_names as $environment_name) { foreach ($environment_names as $environment_name) {
if (!in_array($environment_name, $ignore_environments)) { if (!in_array($environment_name, $ignore_environments)) {
// If not, create a new build job for it: // If not, create a new build job for it:
$build = $this->buildService->createBuild($project, $environment_name, $commitId, $project->getBranch(), $committer, $commitMessage, $extra); $build = $this->buildService->createBuild(
$created_builds[] = array( $project,
'id' => $build->getID(), $environment_name,
'environment' => $environment_name, $commitId,
$project->getBranch(),
$tag,
$committer,
$commitMessage,
$extra
); );
$created_builds[] = [
'id' => $build->getID(),
'environment' => $environment_name,
];
} else { } else {
$duplicates[] = array_search($environment_name, $ignore_environments); $duplicates[] = array_search($environment_name, $ignore_environments);
} }
@ -556,8 +575,18 @@ class WebhookController extends Controller
} }
} else { } else {
$environment_name = null; $environment_name = null;
if (!in_array($environment_name, $ignore_environments)) { if (!in_array($environment_name, $ignore_environments) ||$tag) {
$build = $this->buildService->createBuild($project, null, $commitId, $branch, $committer, $commitMessage, $extra); $build = $this->buildService->createBuild(
$project,
null,
$commitId,
$branch,
$tag,
$committer,
$commitMessage,
$extra
);
return ['status' => 'ok', 'buildID' => $build->getID()]; return ['status' => 'ok', 'buildID' => $build->getID()];
} else { } else {
return [ return [

View file

@ -0,0 +1,24 @@
<?php
use Phinx\Migration\AbstractMigration;
class AddedTagColumnToBuildTable extends AbstractMigration
{
public function up()
{
$table = $this->table('build');
if (!$table->hasColumn('tag')) {
$table->addColumn('tag', 'string', ['limit' => 250, 'null' => true])->save();
}
}
public function down()
{
$table = $this->table('build');
if ($table->hasColumn('tag')) {
$table->removeColumn('tag')->save();
}
}
}

View file

@ -51,6 +51,7 @@ class Build extends Model
'status' => null, 'status' => null,
'log' => null, 'log' => null,
'branch' => null, 'branch' => null,
'tag' => null,
'created' => null, 'created' => null,
'started' => null, 'started' => null,
'finished' => null, 'finished' => null,
@ -71,6 +72,7 @@ class Build extends Model
'status' => 'getStatus', 'status' => 'getStatus',
'log' => 'getLog', 'log' => 'getLog',
'branch' => 'getBranch', 'branch' => 'getBranch',
'tag' => 'getTag',
'created' => 'getCreated', 'created' => 'getCreated',
'started' => 'getStarted', 'started' => 'getStarted',
'finished' => 'getFinished', 'finished' => 'getFinished',
@ -94,6 +96,7 @@ class Build extends Model
'status' => 'setStatus', 'status' => 'setStatus',
'log' => 'setLog', 'log' => 'setLog',
'branch' => 'setBranch', 'branch' => 'setBranch',
'setTag' => 'setTag',
'created' => 'setCreated', 'created' => 'setCreated',
'started' => 'setStarted', 'started' => 'setStarted',
'finished' => 'setFinished', 'finished' => 'setFinished',
@ -142,6 +145,11 @@ class Build extends Model
'length' => 250, 'length' => 250,
'default' => 'master', 'default' => 'master',
], ],
'tag' => [
'type' => 'varchar',
'length' => 250,
'default' => null,
],
'created' => [ 'created' => [
'type' => 'datetime', 'type' => 'datetime',
'nullable' => true, 'nullable' => true,
@ -986,6 +994,36 @@ class Build extends Model
return false; return false;
} }
/**
* Get the value of Tag / tag.
*
* @return string
*/
public function getTag()
{
$rtn = $this->data['tag'];
return $rtn;
}
/**
* Set the value of Tag / tag.
*
* @param $value string
*/
public function setTag($value)
{
$this->validateString('Tag', $value);
if ($this->data['tag'] === $value) {
return;
}
$this->data['tag'] = $value;
$this->setModified('tag');
}
/** /**
* Get the value of Environment / environment. * Get the value of Environment / environment.
* *

View file

@ -38,6 +38,7 @@ class BuildService
* @param string $environment * @param string $environment
* @param string|null $commitId * @param string|null $commitId
* @param string|null $branch * @param string|null $branch
* @param string|null $tag
* @param string|null $committerEmail * @param string|null $committerEmail
* @param string|null $commitMessage * @param string|null $commitMessage
* @param string|null $extra * @param string|null $extra
@ -49,6 +50,7 @@ class BuildService
$environment, $environment,
$commitId = null, $commitId = null,
$branch = null, $branch = null,
$tag = null,
$committerEmail = null, $committerEmail = null,
$commitMessage = null, $commitMessage = null,
$extra = null $extra = null
@ -75,6 +77,10 @@ class BuildService
$build->setBranch($project->getBranch()); $build->setBranch($project->getBranch());
} }
if (!is_null($tag)) {
$build->setTag($tag);
}
if (!is_null($committerEmail)) { if (!is_null($committerEmail)) {
$build->setCommitterEmail($committerEmail); $build->setCommitterEmail($committerEmail);
} }

View file

@ -27,6 +27,9 @@
<a target="_blank" href="<?php print $build->getBranchLink(); ?>"> <a target="_blank" href="<?php print $build->getBranchLink(); ?>">
<span class="label label-default"><?php print $build->getBranch(); ?></span> <span class="label label-default"><?php print $build->getBranch(); ?></span>
</a> </a>
<?php if ($tag = $build->getTag()): ?> /
<span class='label label-info'><?= $tag; ?></span>
<?php endif; ?>
</td> </td>
</tr> </tr>

View file

@ -59,7 +59,10 @@ $branches = $build->getExtra('branches');
</td> </td>
<td> <td>
<a href="<?php print $build->getBranchLink(); ?>" target="_blank"><?php print $build->getBranch(); ?></a> <a href="<?php print $build->getBranchLink(); ?>" target="_blank"><?php print $build->getBranch(); ?></a>
<?php print $branches ? ' + '.implode(', ', $branches) : ''; ?> <?= $branches ? ' + '.implode(', ', $branches) : ''; ?>
<?php if ($tag = $build->getTag()): ?> /
<span class='label label-info'><?= $tag; ?></span>
<?php endif; ?>
</td> </td>
<td> <td>
<?php <?php

View file

@ -87,7 +87,15 @@ class BuildServiceTest extends \PHPUnit_Framework_TestCase
$project->setType('hg'); $project->setType('hg');
$project->setId(101); $project->setId(101);
$returnValue = $this->testedService->createBuild($project, null, '123', 'testbranch', 'test@example.com', 'test'); $returnValue = $this->testedService->createBuild(
$project,
null,
'123',
'testbranch',
null,
'test@example.com',
'test'
);
$this->assertEquals('testbranch', $returnValue->getBranch()); $this->assertEquals('testbranch', $returnValue->getBranch());
$this->assertEquals('123', $returnValue->getCommitId()); $this->assertEquals('123', $returnValue->getCommitId());
@ -109,7 +117,16 @@ class BuildServiceTest extends \PHPUnit_Framework_TestCase
$project->setType('bitbucket'); $project->setType('bitbucket');
$project->setId(101); $project->setId(101);
$returnValue = $this->testedService->createBuild($project, null, null, null, null, null, ['item1' => 1001]); $returnValue = $this->testedService->createBuild(
$project,
null,
null,
null,
null,
null,
null,
['item1' => 1001]
);
$this->assertEquals(1001, $returnValue->getExtra('item1')); $this->assertEquals(1001, $returnValue->getExtra('item1'));
} }