Skip to content

Commit

Permalink
Add support for marking errors as new.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Cryer committed Oct 27, 2017
1 parent 358fc7c commit 2a99f10
Show file tree
Hide file tree
Showing 8 changed files with 642 additions and 233 deletions.
36 changes: 36 additions & 0 deletions PHPCI/Migrations/20171027132159_error_hash.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php


use Phinx\Migration\AbstractMigration;

class ErrorHash extends AbstractMigration
{
/**
* Change Method.
*
* Write your reversible migrations using this method.
*
* More information on writing migrations is available here:
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
*
* The following commands can be used in this method and Phinx will
* automatically reverse them when rolling back:
*
* createTable
* renameTable
* addColumn
* renameColumn
* addIndex
* addForeignKey
*
* Remember to call "create()" or "update()" and NOT "save()" when working
* with the Table class.
*/
public function change()
{
$errors = $this->table('build_error');
$errors->addColumn('hash', 'string', ['limit' => 32, 'null' => true, 'default' => null]);
$errors->addColumn('is_new', 'boolean');
$errors->save();
}
}
79 changes: 79 additions & 0 deletions PHPCI/Model/Base/BuildErrorBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class BuildErrorBase extends Model
'severity' => null,
'message' => null,
'created_date' => null,
'hash' => null,
'is_new' => null,
);

/**
Expand All @@ -58,6 +60,8 @@ class BuildErrorBase extends Model
'severity' => 'getSeverity',
'message' => 'getMessage',
'created_date' => 'getCreatedDate',
'hash' => 'getHash',
'is_new' => 'getIsNew',

// Foreign key getters:
'Build' => 'getBuild',
Expand All @@ -77,6 +81,8 @@ class BuildErrorBase extends Model
'severity' => 'setSeverity',
'message' => 'setMessage',
'created_date' => 'setCreatedDate',
'hash' => 'setHash',
'is_new' => 'setIsNew',

// Foreign key setters:
'Build' => 'setBuild',
Expand Down Expand Up @@ -135,6 +141,16 @@ class BuildErrorBase extends Model
'type' => 'datetime',
'default' => null,
),
'hash' => array(
'type' => 'varchar',
'length' => 32,
'default' => null,
),
'is_new' => array(
'type' => 'tiynint',
'length' => 1,
'default' => 0,
),
);

/**
Expand Down Expand Up @@ -270,6 +286,30 @@ public function getCreatedDate()
return $rtn;
}

/**
* Get the value of Hash / hash.
*
* @return string
*/
public function getHash()
{
$rtn = $this->data['hash'];

return $rtn;
}

/**
* Get the value of IsNew / is_new.
*
* @return string
*/
public function getIsNew()
{
$rtn = $this->data['is_new'] ? true : false;

return $rtn;
}

/**
* Set the value of Id / id.
*
Expand Down Expand Up @@ -444,6 +484,45 @@ public function setCreatedDate($value)
$this->_setModified('created_date');
}

/**
* Set the value of Hash / hash.
*
* Must not be null.
* @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');
}

/**
* Set the value of IsNew / is_new.
*
* Must not be null.
* @param $value bool
*/
public function setIsNew($value)
{
$value = ($value == true) ? 1 : 0;

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
1 change: 1 addition & 0 deletions PHPCI/Model/Build.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ public function reportError(
$error->setFile($file);
$error->setLineStart($lineStart);
$error->setLineEnd($lineEnd);
$error->hash();

return Factory::getStore('BuildError')->save($error);
}
Expand Down
15 changes: 15 additions & 0 deletions PHPCI/Model/BuildError.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

namespace PHPCI\Model;

use b8\Store\Factory;
use PHPCI\Model\Base\BuildErrorBase;
use PHPCI\Store;

/**
* BuildError Model
Expand Down Expand Up @@ -60,4 +62,17 @@ public function getSeverityClass()
return 'default';
}
}

public function hash()
{
$hash = $this->getPlugin() . '|' . $this->getFile() . '|' . $this->getLineStart();
$hash .= '|' . $this->getLineEnd() . '|' . $this->getSeverity() . '|' . $this->getMessage();

$this->setHash(md5($hash));

/** @var Store\BuildErrorStore $errorStore */
$errorStore = Factory::getStore('BuildError');

$this->setIsNew($errorStore->getIsNewError($this->getHash()));
}
}
20 changes: 20 additions & 0 deletions PHPCI/Store/BuildErrorStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,24 @@ public function getErrorTotalForBuild($buildId)
return array();
}
}

/**
* Check if a build error is new.
*/
public function getIsNewError($hash)
{
$query = 'SELECT COUNT(*) AS total FROM build_error
WHERE `hash` = :errorhash';

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

$stmt->bindValue(':errorhash', $hash, \PDO::PARAM_INT);

if ($stmt->execute()) {
$res = $stmt->fetch(\PDO::FETCH_ASSOC);
return $res['total'] == 0;
} else {
return true;
}
}
}
5 changes: 5 additions & 0 deletions PHPCI/View/Build/errors.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ foreach ($errors as $error):
?>

<tr class="bg-<?php print $error->getSeverityClass(); ?>">
<td>
<?php if ($error->getIsNew()): ?>
<span class="label label-danger">New</span>
<?php endif; ?>
</td>
<td>
<span class="label label-<?php print $error->getSeverityClass(); ?>">
<?php print Lang::get($error->getSeverityString()); ?>
Expand Down
1 change: 1 addition & 0 deletions PHPCI/View/Build/view.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@
<table class="errors-table table table-hover dataTable">
<thead>
<tr>
<th></th>
<th>Severity</th>
<th>Plugin</th>
<th>File</th>
Expand Down
Loading

0 comments on commit 2a99f10

Please sign in to comment.