Skip to content

Commit

Permalink
Added new errors label in build "Errors" tab.
Browse files Browse the repository at this point in the history
  • Loading branch information
corpsee committed Dec 9, 2017
1 parent 1b65b02 commit 13f763c
Show file tree
Hide file tree
Showing 9 changed files with 186 additions and 18 deletions.
13 changes: 7 additions & 6 deletions src/PHPCensor/Languages/lang.en.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,10 @@
'group_save' => 'Save Group',

// View Build
'errors' => 'Errors',
'information' => 'Information',

'errors' => 'Errors',
'information' => 'Information',
'is_new' => 'Is new?',
'new' => 'New',
'build_x_not_found' => 'Build with ID %d does not exist.',
'build_n' => 'Build %d',
'rebuild_now' => 'Rebuild Now',
Expand Down Expand Up @@ -328,7 +329,7 @@
'build-summary' => 'Summary',
'stage' => 'Stage',
'duration' => 'Duration',
'seconds' => 'sec.',
'seconds' => 'sec.',
'plugin' => 'Plugin',
'stage_setup' => 'Setup',
'stage_test' => 'Test',
Expand All @@ -344,7 +345,7 @@
'all_severities' => 'All severities',
'filters' => 'Filters',
'errors_selected' => 'Errors selected',

'build_details' => 'Build Details',
'commit_details' => 'Commit Details',
'committer' => 'Committer',
Expand Down Expand Up @@ -419,7 +420,7 @@
'confirm_cancel' => 'Cancel',
'confirm_success' => 'Item successfully deleted.',
'confirm_failed' => 'Deletion failed! Server says: ',

'public_status_title' => 'Public status',
'public_status_image' => 'Status image',
'public_status_page' => 'Public status page',
Expand Down
4 changes: 3 additions & 1 deletion src/PHPCensor/Languages/lang.ru.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@
// View Build
'errors' => 'Ошибки',
'information' => 'Информация',
'new' => 'Новая',
'is_new' => 'Новая?',
'build_x_not_found' => 'Сборки с ID %d не существует.',
'build_n' => 'Сборка %d',
'rebuild_now' => 'Пересобрать сейчас',
Expand Down Expand Up @@ -352,7 +354,7 @@
'passing_build' => 'Успех сборки',
'failing_build' => 'Провал сборки',
'log_output' => 'Вывод лога: ',

// Error Levels:
'critical' => 'Критичный',
'high' => 'Высокий',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

use Phinx\Migration\AbstractMigration;

class AddedNewLabelForErrors extends AbstractMigration
{
public function up()
{
if ($this->hasTable('build_error')) {
$table = $this->table('build_error');

if (!$table->hasColumn('hash')) {
$table
->addColumn('hash', 'string', ['limit' => 32, 'default' => ''])
->save();
}

if (!$table->hasColumn('is_new')) {
$table
->addColumn('is_new', 'boolean', ['default' => false])
->save();
}
}
}

public function down()
{
if ($this->hasTable('build_error')) {
$table = $this->table('build_error');

if ($table->hasColumn('hash')) {
$table
->removeColumn('hash')
->save();
}

if ($table->hasColumn('is_new')) {
$table
->removeColumn('is_new')
->save();
}
}
}
}
75 changes: 75 additions & 0 deletions src/PHPCensor/Model/BuildError.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class BuildError extends Model
'severity' => null,
'message' => null,
'create_date' => null,
'hash' => null,
'is_new' => null,
];

/**
Expand All @@ -56,6 +58,8 @@ class BuildError extends Model
'severity' => 'getSeverity',
'message' => 'getMessage',
'create_date' => 'getCreateDate',
'hash' => 'getHash',
'is_new' => 'getIsNew',

// Foreign key getters:
'Build' => 'getBuild',
Expand All @@ -75,6 +79,8 @@ class BuildError extends Model
'severity' => 'setSeverity',
'message' => 'setMessage',
'create_date' => 'setCreateDate',
'hash' => 'setHash',
'is_new' => 'setIsNew',

// Foreign key setters:
'Build' => 'setBuild',
Expand Down Expand Up @@ -174,6 +180,26 @@ public function getCreateDate()
return $rtn;
}

/**
* @return string
*/
public function getHash()
{
$rtn = (string)$this->data['hash'];

return $rtn;
}

/**
* @return string
*/
public function getIsNew()
{
$rtn = $this->data['is_new'];

return $rtn;
}

/**
* @param $value int
*/
Expand Down Expand Up @@ -324,6 +350,40 @@ public function setCreateDate($value)
$this->setModified('create_date');
}

/**
* @param $value string
*/
public function setHash($value)
{
$this->validateNotNull('hash', $value);
$this->validateString('hash', $value);

if ($this->data['hash'] === $value) {
return;
}

$this->data['hash'] = $value;

$this->setModified('hash');
}

/**
* @param $value int
*/
public function setIsNew($value)
{
$this->validateNotNull('is_new', $value);
$this->validateInt('is_new', $value);

if ($this->data['is_new'] === $value) {
return;
}

$this->data['is_new'] = $value;

$this->setModified('is_new');
}

/**
* Get the Build model for this BuildError by Id.
*
Expand Down Expand Up @@ -425,6 +485,21 @@ public static function getSeverityName($severity)
}
}

/**
* @param string $plugin
* @param string $file
* @param integer $lineStart
* @param integer $lineEnd
* @param integer $severity
* @param string $message
*
* @return string
*/
public static function generateHash($plugin, $file, $lineStart, $lineEnd, $severity, $message)
{
return md5($plugin . $file . $lineStart . $lineEnd . $severity . $message);
}

/**
* Get the class to apply to HTML elements representing this error.
*
Expand Down
2 changes: 1 addition & 1 deletion src/PHPCensor/Plugin/TechnicalDebt.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static function pluginName()
{
return 'technical_debt';
}

/**
* {@inheritdoc}
*/
Expand Down
26 changes: 25 additions & 1 deletion src/PHPCensor/Store/BuildErrorStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public function getKnownPlugins($buildId)
*/
public function getKnownSeverities($buildId, $plugin = '')
{
$query = 'SELECT DISTINCT {{severity}} from {{build_error}} WHERE {{build_id}} = :build';
$query = 'SELECT DISTINCT {{severity}} FROM {{build_error}} WHERE {{build_id}} = :build';
if ($plugin) {
$query .= ' AND {{plugin}} = :plugin';
}
Expand All @@ -225,4 +225,28 @@ public function getKnownSeverities($buildId, $plugin = '')
return [];
}
}

/**
* Check if a build error is new.
*
* @param string $hash
*
* @return boolean
*/
public function getIsNewError($hash)
{
$query = 'SELECT COUNT(*) AS {{total}} FROM {{build_error}} WHERE {{hash}} = :hash';

$stmt = Database::getConnection('read')->prepareCommon($query);

$stmt->bindValue(':hash', $hash);

if ($stmt->execute()) {
$res = $stmt->fetch(\PDO::FETCH_ASSOC);

return (0 === $res['total']);
}

return true;
}
}
20 changes: 18 additions & 2 deletions src/PHPCensor/Store/BuildErrorWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use b8\Config;
use b8\Database;
use PHPCensor\Model\BuildError;
use b8\Store\Factory;

/**
* Class BuildErrorWriter
Expand Down Expand Up @@ -69,6 +71,11 @@ public function write(
if (is_null($createdDate)) {
$createdDate = new \DateTime();
}

/** @var BuildErrorStore $errorStore */
$errorStore = Factory::getStore('BuildError');
$hash = BuildError::generateHash($plugin, $file, $lineStart, $lineEnd, $severity, $message);

$this->errors[] = [
'plugin' => (string)$plugin,
'message' => (string)$message,
Expand All @@ -77,6 +84,8 @@ public function write(
'line_start' => !is_null($lineStart) ? (int)$lineStart : null,
'line_end' => !is_null($lineEnd) ? (int)$lineEnd : null,
'create_date' => $createdDate->format('Y-m-d H:i:s'),
'hash' => $hash,
'is_new' => (integer)$errorStore->getIsNewError($hash),
];

if (count($this->errors) >= $this->bufferSize) {
Expand Down Expand Up @@ -104,7 +113,9 @@ public function flush()
:line_end' . $i . ',
:severity' . $i . ',
:message' . $i . ',
:create_date' . $i . '
:create_date' . $i . ',
:hash' . $i . ',
:is_new' . $i . '
)';
$insertValuesData['build_id' . $i] = $this->buildId;
$insertValuesData['plugin' . $i] = $error['plugin'];
Expand All @@ -114,6 +125,8 @@ public function flush()
$insertValuesData['severity' . $i] = $error['severity'];
$insertValuesData['message' . $i] = $error['message'];
$insertValuesData['create_date' . $i] = $error['create_date'];
$insertValuesData['hash' . $i] = $error['hash'];
$insertValuesData['is_new' . $i] = $error['is_new'];
}
$query = '
INSERT INTO {{build_error}} (
Expand All @@ -124,12 +137,15 @@ public function flush()
{{line_end}},
{{severity}},
{{message}},
{{create_date}}
{{create_date}},
{{hash}},
{{is_new}}
)
VALUES ' . join(', ', $insertValuesPlaceholders) . '
';
$stmt = Database::getConnection('write')->prepareCommon($query);
$stmt->execute($insertValuesData);

$this->errors = [];
}
}
17 changes: 11 additions & 6 deletions src/PHPCensor/View/Build/errors.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,24 @@ foreach ($errors as $error):

<tr>
<td>
<span class="label label-<?php print $error->getSeverityClass(); ?>">
<?php print Lang::get($error->getSeverityString()); ?>
<?php if ($error->getIsNew()): ?>
<span class="label label-danger"><?= Lang::get('new'); ?></span>
<?php endif; ?>
</td>
<td>
<span class="label label-<?= $error->getSeverityClass(); ?>">
<?= Lang::get($error->getSeverityString()); ?>
</span>
</td>
<td><?php print Lang::get($error->getPlugin()); ?></td>
<td><a href="<?php print $link; ?>" target="_blank"><?php print $error->getFile(); ?></a></td>
<td><?= Lang::get($error->getPlugin()); ?></td>
<td><a href="<?php print $link; ?>" target="_blank"><?= $error->getFile(); ?></a></td>
<td>
<a href="<?php print $link; ?>" target="_blank">
<?php
if ($error->getLineStart() == $error->getLineEnd() || !$error->getLineEnd()) {
print $error->getLineStart();
echo $error->getLineStart();
} else {
print $error->getLineStart() . ' - ' . $error->getLineEnd();
echo ($error->getLineStart() . ' - ' . $error->getLineEnd());
}
?>
</a>
Expand Down
3 changes: 2 additions & 1 deletion src/PHPCensor/View/Build/view.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use PHPCensor\Model\BuildError;
<a target="_blank" href="<?= $build->getBranchLink(); ?>">
<i class="fa fa-code-fork"></i> <?= $build->getBranch(); ?>
</a>
<?php if ($tag = $build->getTag()): ?> /
<?php if ($tag = $build->getTag()): ?> /
<a target="_blank" href="<?= $build->getTagLink(); ?>">
<i class="fa fa-tag"></i> <?= $tag; ?>
</a>
Expand Down Expand Up @@ -253,6 +253,7 @@ use PHPCensor\Model\BuildError;
<table class="errors-table table table-hover">
<thead>
<tr>
<th><?php Lang::out('is_new'); ?></th>
<th><?php Lang::out('severity'); ?></th>
<th><?php Lang::out('plugin'); ?></th>
<th><?php Lang::out('file'); ?></th>
Expand Down

0 comments on commit 13f763c

Please sign in to comment.