Build sources instead of 'Manual' word.

This commit is contained in:
Dmitry Khomutov 2017-04-13 22:00:29 +07:00
commit 145670acde
25 changed files with 266 additions and 109 deletions

View file

@ -25,6 +25,12 @@ class Build extends Model
const STATUS_RUNNING = 1;
const STATUS_SUCCESS = 2;
const STATUS_FAILED = 3;
const SOURCE_UNKNOWN = 0;
const SOURCE_MANUAL_WEB = 1;
const SOURCE_MANUAL_CONSOLE = 2;
const SOURCE_PERIODICAL = 3;
const SOURCE_WEBHOOK = 4;
/**
* @var array
@ -59,6 +65,7 @@ class Build extends Model
'commit_message' => null,
'extra' => null,
'environment' => null,
'source' => Build::SOURCE_UNKNOWN,
];
/**
@ -80,6 +87,7 @@ class Build extends Model
'commit_message' => 'getCommitMessage',
'extra' => 'getExtra',
'environment' => 'getEnvironment',
'source' => 'getSource',
// Foreign key getters:
'Project' => 'getProject',
@ -104,6 +112,7 @@ class Build extends Model
'commit_message' => 'setCommitMessage',
'extra' => 'setExtra',
'environment' => 'setEnvironment',
'source' => 'setSource',
// Foreign key setters:
'Project' => 'setProject',
@ -182,10 +191,15 @@ class Build extends Model
'default' => null,
],
'environment' => [
'type' => 'varchar',
'length' => 250,
'type' => 'varchar',
'length' => 250,
'default' => null,
],
'source' => [
'type' => 'int',
'length' => 11,
'default' => Build::SOURCE_UNKNOWN,
],
];
/**
@ -213,25 +227,25 @@ class Build extends Model
/**
* Get the value of Id / id.
*
* @return int
* @return integer
*/
public function getId()
{
$rtn = $this->data['id'];
return $rtn;
return (integer)$rtn;
}
/**
* Get the value of ProjectId / project_id.
*
* @return int
* @return integer
*/
public function getProjectId()
{
$rtn = $this->data['project_id'];
return $rtn;
return (integer)$rtn;
}
/**
@ -249,13 +263,13 @@ class Build extends Model
/**
* Get the value of Status / status.
*
* @return int
* @return integer
*/
public function getStatus()
{
$rtn = $this->data['status'];
return $rtn;
return (integer)$rtn;
}
/**
@ -1032,6 +1046,36 @@ class Build extends Model
$this->setModified('tag');
}
/**
* Get the value of source.
*
* @return string
*/
public function getSource()
{
$rtn = $this->data['source'];
return (integer)$rtn;
}
/**
* Set the value of source.
*
* @param $value integer
*/
public function setSource($value)
{
$this->validateInt('Source', $value);
if ($this->data['source'] === $value) {
return;
}
$this->data['source'] = $value;
$this->setModified('source');
}
/**
* Get the value of Environment / environment.
*
@ -1107,4 +1151,24 @@ OUT;
return $wrapperFile;
}
/**
* @return string
*/
public function getSourceHumanize()
{
switch ($this->getSource()) {
case Build::SOURCE_WEBHOOK:
return 'source_webhook';
case Build::SOURCE_MANUAL_WEB:
return 'source_manual_web';
case Build::SOURCE_MANUAL_CONSOLE:
return 'source_manual_console';
case Build::SOURCE_PERIODICAL:
return 'source_periodical';
case Build::SOURCE_UNKNOWN:
default:
return 'source_unknown';
}
}
}

View file

@ -7,6 +7,7 @@ use PHPCensor\Builder;
use PHPCensor\Helper\Bitbucket;
use PHPCensor\Helper\Diff;
use b8\Config;
use PHPCensor\Model\Build;
use PHPCensor\Model\BuildError;
/**
@ -47,17 +48,16 @@ class BitbucketBuild extends RemoteGitBuild
*/
public function sendStatusPostback()
{
if ('Manual' === $this->getCommitId()) {
if (Build::SOURCE_WEBHOOK !== $this->getSource()) {
return false;
}
$project = $this->getProject();
$project = $this->getProject();
if (empty($project)) {
return false;
}
$username = Config::getInstance()->get('php-censor.bitbucket.username');
$username = Config::getInstance()->get('php-censor.bitbucket.username');
$appPassword = Config::getInstance()->get('php-censor.bitbucket.app_password');
if (empty($username) || empty($appPassword) || empty($this->data['id'])) {
@ -261,18 +261,18 @@ class BitbucketBuild extends RemoteGitBuild
*/
protected function getDiffLineNumber(Builder $builder, $file, $line)
{
$line = (integer)$line;
$builder->logExecOutput(false);
$line = (integer)$line;
$prNumber = $this->getExtra('pull_request_number');
$path = $builder->buildPath;
$path = $builder->buildPath;
if (!empty($prNumber)) {
$builder->executeCommand('cd %s && git diff origin/%s "%s"', $path, $this->getBranch(), $file);
} else {
$commitId = $this->getCommitId();
$compare = $commitId == 'Manual' ? 'HEAD' : $commitId;
$compare = empty($commitId) ? 'HEAD' : $commitId;
$builder->executeCommand('cd %s && git diff %s^^ "%s"', $path, $compare, $file);
}
@ -281,7 +281,7 @@ class BitbucketBuild extends RemoteGitBuild
$diff = $builder->getLastOutput();
$helper = new Diff();
$lines = $helper->getLinePositions($diff);
$lines = $helper->getLinePositions($diff);
return isset($lines[$line]) ? $lines[$line] : null;
}

View file

@ -7,6 +7,7 @@ use PHPCensor\Builder;
use PHPCensor\Helper\Diff;
use PHPCensor\Helper\Github;
use b8\Config;
use PHPCensor\Model\Build;
use PHPCensor\Model\BuildError;
/**
@ -45,7 +46,7 @@ class GithubBuild extends RemoteGitBuild
*/
public function sendStatusPostback()
{
if ('Manual' === $this->getCommitId()) {
if (Build::SOURCE_WEBHOOK !== $this->getSource()) {
return false;
}
@ -246,18 +247,18 @@ class GithubBuild extends RemoteGitBuild
*/
protected function getDiffLineNumber(Builder $builder, $file, $line)
{
$line = (integer)$line;
$builder->logExecOutput(false);
$line = (integer)$line;
$prNumber = $this->getExtra('pull_request_number');
$path = $builder->buildPath;
$path = $builder->buildPath;
if (!empty($prNumber)) {
$builder->executeCommand('cd %s && git diff origin/%s "%s"', $path, $this->getBranch(), $file);
} else {
$commitId = $this->getCommitId();
$compare = $commitId == 'Manual' ? 'HEAD' : $commitId;
$compare = empty($commitId) ? 'HEAD' : $commitId;
$builder->executeCommand('cd %s && git diff %s^^ "%s"', $path, $compare, $file);
}
@ -266,7 +267,7 @@ class GithubBuild extends RemoteGitBuild
$diff = $builder->getLastOutput();
$helper = new Diff();
$lines = $helper->getLinePositions($diff);
$lines = $helper->getLinePositions($diff);
return isset($lines[$line]) ? $lines[$line] : null;
}

View file

@ -12,11 +12,7 @@ class GogsBuild extends RemoteGitBuild
*/
public function getCommitLink()
{
if ($this->getCommitId() !== 'manual'){
return $this->getProject()->getReference() . '/commit/' . $this->getCommitId();
}
return parent::getCommitLink();
return $this->getProject()->getReference() . '/commit/' . $this->getCommitId();
}
/**

View file

@ -35,6 +35,7 @@ class MercurialBuild extends Build
if (!$success) {
$builder->logFailure('Failed to clone remote hg repository.');
return false;
}
@ -84,12 +85,12 @@ class MercurialBuild extends Build
*/
protected function postCloneSetup(Builder $builder, $cloneTo, array $extra = null)
{
$success = true;
$commit = $this->getCommitId();
$success = true;
$commitId = $this->getCommitId();
// Allow switching to a specific branch:
if (!empty($commit) && $commit != 'Manual') {
$cmd = 'cd "%s" && hg checkout %s';
if (!empty($commitId)) {
$cmd = 'cd "%s" && hg checkout %s';
$success = $builder->executeCommand($cmd, $cloneTo, $this->getBranch());
}

View file

@ -137,18 +137,24 @@ class RemoteGitBuild extends Build
*/
protected function postCloneSetup(Builder $builder, $cloneTo, array $extra = null)
{
$success = true;
$commit = $this->getCommitId();
$chdir = 'cd "%s"';
$success = true;
$commitId = $this->getCommitId();
$chdir = 'cd "%s"';
if (empty($this->getEnvironment()) && !empty($commit) && $commit != 'Manual') {
$cmd = $chdir . ' && git checkout %s --quiet';
$success = $builder->executeCommand($cmd, $cloneTo, $commit);
if (empty($this->getEnvironment()) && !empty($commitId)) {
$cmd = $chdir . ' && git checkout %s --quiet';
$success = $builder->executeCommand($cmd, $cloneTo, $commitId);
}
// Always update the commit hash with the actual HEAD hash
if ($builder->executeCommand($chdir . ' && git rev-parse HEAD', $cloneTo)) {
$this->setCommitId(trim($builder->getLastOutput()));
$commitId = trim($builder->getLastOutput());
$this->setCommitId($commitId);
if ($builder->executeCommand($chdir . ' && git log -1 --pretty=format:%%s %s', $cloneTo, $commitId)) {
$this->setCommitMessage(trim($builder->getLastOutput()));
}
}
return $success;

View file

@ -95,7 +95,7 @@ class SubversionBuild extends Build
{
$cmd = $this->svnCommand;
if ($this->getCommitId() != 'Manual') {
if (!empty($this->getCommitId())) {
$cmd .= ' -r %s %s "%s"';
$success = $builder->executeCommand($cmd, $this->getCommitId(), $this->getCloneUrl(), $cloneTo);
} else {