31
0
mirror of https://github.com/joomla-extensions/patchtester.git synced 2024-06-03 14:10:47 +00:00

Add a label filter

Signed-off-by: Roland Dalmulder <contact@rolandd.com>
This commit is contained in:
Roland Dalmulder 2020-10-10 17:05:13 +02:00
parent 76f43fa151
commit 4592caba1e
No known key found for this signature in database
GPG Key ID: E11DEC58660B9E9A
5 changed files with 343 additions and 218 deletions

View File

@ -108,6 +108,7 @@ class DisplayController extends AbstractController
$state->set('filter.branch', $app->getUserStateFromRequest($this->context . '.filter.branch', 'filter_branch', ''));
$state->set('filter.rtc', $app->getUserStateFromRequest($this->context . '.filter.rtc', 'filter_rtc', ''));
$state->set('filter.npm', $app->getUserStateFromRequest($this->context . '.filter.npm', 'filter_npm', ''));
$state->set('filter.label', $app->getUserStateFromRequest($this->context . '.filter.label', 'filter_label', ''));
// Pre-fill the limits.
$limit = $app->getUserStateFromRequest('global.list.limit', 'limit', $app->input->get('list_limit', 20), 'uint');

View File

@ -36,7 +36,7 @@ class PullsModel extends AbstractModel
* @var array
* @since 2.0
*/
protected $sortFields = array('a.pull_id', 'a.title');
protected $sortFields = array('pulls.pull_id', 'pulls.title');
/**
* Instantiate the model.
@ -47,8 +47,9 @@ class PullsModel extends AbstractModel
*
* @since 2.0
*/
public function __construct($context, Registry $state = null, \JDatabaseDriver $db = null)
{
public function __construct($context, Registry $state = null,
\JDatabaseDriver $db = null
) {
parent::__construct($state, $db);
$this->context = $context;
@ -61,7 +62,7 @@ class PullsModel extends AbstractModel
*
* @since 3.0.0
*/
public function getBranches()
public function getBranches(): array
{
$db = $this->getDb();
$query = $db->getQuery(true);
@ -75,6 +76,30 @@ class PullsModel extends AbstractModel
return $db->setQuery($query)->loadAssocList();
}
/**
* Method to get an array of labels.
*
* @return array The list of labels
*
* @since 4.0.0
*/
public function getLabels(): array
{
$db = $this->getDb();
$query = $db->getQuery(true);
// Select distinct branches excluding empty values
$query->select(
'DISTINCT(' . $db->quoteName('name') . ') AS ' . $db->quoteName(
'text'
)
)
->from($db->quoteName('#__patchtester_pulls_labels'))
->order($db->quoteName('name') . ' ASC');
return $db->setQuery($query)->loadAssocList();
}
/**
* Method to get an array of data items.
*
@ -91,7 +116,10 @@ class PullsModel extends AbstractModel
return $this->cache[$store];
}
$items = $this->getList($this->getListQueryCache(), $this->getStart(), $this->getState()->get('list.limit'));
$items = $this->getList(
$this->getListQueryCache(), $this->getStart(),
$this->getState()->get('list.limit')
);
$db = $this->getDb();
$query = $db->getQuery(true)
@ -102,7 +130,9 @@ class PullsModel extends AbstractModel
$items,
static function ($item) use ($db, $query) {
$query->clear('where');
$query->where($db->quoteName('pull_id') . ' = ' . $item->pull_id);
$query->where(
$db->quoteName('pull_id') . ' = ' . $item->pull_id
);
$db->setQuery($query);
$item->labels = $db->loadObjectList();
@ -114,135 +144,6 @@ class PullsModel extends AbstractModel
return $this->cache[$store];
}
/**
* Method to get a JDatabaseQuery object for retrieving the data set from a database.
*
* @return \JDatabaseQuery A JDatabaseQuery object to retrieve the data set.
*
* @since 2.0
*/
protected function getListQuery()
{
// Create a new query object.
$db = $this->getDb();
$query = $db->getQuery(true);
// Select the required fields from the table.
$query->select('a.*');
$query->from('#__patchtester_pulls AS a');
// Join the tests table to get applied patches
$query->select('t.id AS applied');
$query->join('LEFT', '#__patchtester_tests AS t ON t.pull_id = a.pull_id');
// Filter by search
$search = $this->getState()->get('filter.search');
if (!empty($search))
{
if (stripos($search, 'id:') === 0)
{
$query->where('a.pull_id = ' . (int) substr($search, 3));
}
elseif (is_numeric($search))
{
$query->where('a.pull_id = ' . (int) $search);
}
else
{
$query->where('(a.title LIKE ' . $db->quote('%' . $db->escape($search, true) . '%') . ')');
}
}
// Filter for applied patches
$applied = $this->getState()->get('filter.applied');
if (!empty($applied))
{
// Not applied patches have a NULL value, so build our value part of the query based on this
$value = $applied === 'no' ? ' IS NULL' : ' = 1';
$query->where($db->quoteName('applied') . $value);
}
// Filter for branch
$branch = $this->getState()->get('filter.branch');
if (!empty($branch))
{
$query->where($db->quoteName('branch') . ' = ' . $db->quote($branch));
}
// Filter for RTC patches
$applied = $this->getState()->get('filter.rtc');
if (!empty($applied))
{
// Not applied patches have a NULL value, so build our value part of the query based on this
$value = $applied === 'no' ? '0' : '1';
$query->where($db->quoteName('is_rtc') . ' = ' . $value);
}
// Filter for NPM patches
$npm = $this->getState()->get('filter.npm');
if (!empty($npm))
{
// Not applied patches have a NULL value, so build our value part of the query based on this
$value = $npm === 'no' ? '0' : '1';
$query->where($db->quoteName('is_npm') . ' = ' . $value);
}
// Handle the list ordering.
$ordering = $this->getState()->get('list.ordering', 'a.pull_id');
$direction = $this->getState()->get('list.direction', 'DESC');
if (!empty($ordering))
{
$query->order($db->escape($ordering) . ' ' . $db->escape($direction));
}
return $query;
}
/**
* Method to get a Pagination object for the data set.
*
* @return Pagination A Pagination object for the data set.
*
* @since 2.0
*/
public function getPagination()
{
// Get a storage key.
$store = $this->getStoreId('getPagination');
// Try to load the data from internal storage.
if (isset($this->cache[$store]))
{
return $this->cache[$store];
}
// Create the pagination object and add the object to the internal cache.
$this->cache[$store] = new Pagination($this->getTotal(), $this->getStart(), (int) $this->getState()->get('list.limit', 20));
return $this->cache[$store];
}
/**
* Retrieves the array of authorized sort fields
*
* @return array
*
* @since 2.0
*/
public function getSortFields()
{
return $this->sortFields;
}
/**
* Method to get a store id based on the model configuration state.
*
@ -267,6 +168,196 @@ class PullsModel extends AbstractModel
return md5($this->context . ':' . $id);
}
/**
* Gets an array of objects from the results of database query.
*
* @param \JDatabaseQuery|string $query The query.
* @param integer $limitstart Offset.
* @param integer $limit The number of records.
*
* @return array An array of results.
*
* @since 2.0
* @throws RuntimeException
*/
protected function getList($query, $limitstart = 0, $limit = 0)
{
return $this->getDb()->setQuery($query, $limitstart, $limit)
->loadObjectList();
}
/**
* Method to cache the last query constructed.
*
* This method ensures that the query is constructed only once for a given state of the model.
*
* @return \JDatabaseQuery A JDatabaseQuery object
*
* @since 2.0
*/
protected function getListQueryCache()
{
// Capture the last store id used.
static $lastStoreId;
// Compute the current store id.
$currentStoreId = $this->getStoreId();
// If the last store id is different from the current, refresh the query.
if ($lastStoreId != $currentStoreId || empty($this->query))
{
$lastStoreId = $currentStoreId;
$this->query = $this->getListQuery();
}
return $this->query;
}
/**
* Method to get a JDatabaseQuery object for retrieving the data set from a database.
*
* @return \JDatabaseQuery A JDatabaseQuery object to retrieve the data set.
*
* @since 2.0
*/
protected function getListQuery()
{
// Create a new query object.
$db = $this->getDb();
$query = $db->getQuery(true);
$labelQuery = $db->getQuery(true);
$query->select('pulls.*')
->select($db->quoteName('tests.id', 'applied'))
->from($db->quoteName('#__patchtester_pulls', 'pulls'))
->leftJoin(
$db->quoteName('#__patchtester_tests', 'tests')
. ' ON ' . $db->quoteName('tests.pull_id') . ' = '
. $db->quoteName('pulls.pull_id')
);
// Filter by search
$search = $this->getState()->get('filter.search');
if (!empty($search))
{
if (stripos($search, 'id:') === 0)
{
$query->where(
$db->quoteName('pulls.pull_id') . ' = ' . (int) substr(
$search, 3
)
);
}
elseif (is_numeric($search))
{
$query->where(
$db->quoteName('pulls.pull_id') . ' = ' . (int) $search
);
}
else
{
$query->where(
'(' . $db->quoteName('pulls.title') . ' LIKE ' . $db->quote(
'%' . $db->escape($search, true) . '%'
) . ')'
);
}
}
// Filter for applied patches
$applied = $this->getState()->get('filter.applied');
if (!empty($applied))
{
// Not applied patches have a NULL value, so build our value part of the query based on this
$value = $applied === 'no' ? ' IS NULL' : ' = 1';
$query->where($db->quoteName('pulls.applied') . $value);
}
// Filter for branch
$branch = $this->getState()->get('filter.branch');
if (!empty($branch))
{
$query->where(
$db->quoteName('pulls.branch') . ' = ' . $db->quote($branch)
);
}
// Filter for RTC patches
$applied = $this->getState()->get('filter.rtc');
if (!empty($applied))
{
// Not applied patches have a NULL value, so build our value part of the query based on this
$value = $applied === 'no' ? '0' : '1';
$query->where($db->quoteName('pulls.is_rtc') . ' = ' . $value);
}
// Filter for NPM patches
$npm = $this->getState()->get('filter.npm');
if (!empty($npm))
{
// Not applied patches have a NULL value, so build our value part of the query based on this
$value = $npm === 'no' ? '0' : '1';
$query->where($db->quoteName('pulls.is_npm') . ' = ' . $value);
}
$labels = $this->getState()->get('filter.label');
if (!empty($labels) && $labels[0] !== '')
{
$labelQuery
->select($db->quoteName('pulls_labels.pull_id'))
->select(
'COUNT(' . $db->quoteName('pulls_labels.name') . ') AS '
. $db->quoteName('labelCount')
)
->from(
$db->quoteName(
'#__patchtester_pulls_labels', 'pulls_labels'
)
)
->where(
$db->quoteName('pulls_labels.name') . ' IN (' . implode(
',', $db->quote($labels)
) . ')'
)
->group($db->quoteName('pulls_labels.pull_id'));
$query->leftJoin(
'(' . $labelQuery->__toString() . ')' . ' AS ' . $db->quoteName(
'pulls_labels'
)
. ' ON ' . $db->quoteName('pulls_labels.pull_id') . ' = '
. $db->quoteName('pulls.pull_id')
)
->where(
$db->quoteName('pulls_labels.labelCount') . ' = ' . count(
$labels
)
);
}
$ordering = $this->getState()->get('list.ordering', 'pulls.pull_id');
$direction = $this->getState()->get('list.direction', 'DESC');
if (!empty($ordering))
{
$query->order(
$db->escape($ordering) . ' ' . $db->escape($direction)
);
}
return $query;
}
/**
* Method to get the starting number of items for the data set.
*
@ -318,11 +409,82 @@ class PullsModel extends AbstractModel
}
// Load the total and add the total to the internal cache.
$this->cache[$store] = (int) $this->getListCount($this->getListQueryCache());
$this->cache[$store] = (int) $this->getListCount(
$this->getListQueryCache()
);
return $this->cache[$store];
}
/**
* Returns a record count for the query.
*
* @param \JDatabaseQuery|string $query The query.
*
* @return integer Number of rows for query.
*
* @since 2.0
*/
protected function getListCount($query)
{
// Use fast COUNT(*) on JDatabaseQuery objects if there no GROUP BY or HAVING clause:
if ($query instanceof \JDatabaseQuery && $query->type == 'select'
&& $query->group === null
&& $query->having === null)
{
$query = clone $query;
$query->clear('select')->clear('order')->select('COUNT(*)');
$this->getDb()->setQuery($query);
return (int) $this->getDb()->loadResult();
}
// Otherwise fall back to inefficient way of counting all results.
$this->getDb()->setQuery($query)->execute();
return (int) $this->getDb()->getNumRows();
}
/**
* Method to get a Pagination object for the data set.
*
* @return Pagination A Pagination object for the data set.
*
* @since 2.0
*/
public function getPagination()
{
// Get a storage key.
$store = $this->getStoreId('getPagination');
// Try to load the data from internal storage.
if (isset($this->cache[$store]))
{
return $this->cache[$store];
}
// Create the pagination object and add the object to the internal cache.
$this->cache[$store] = new Pagination(
$this->getTotal(), $this->getStart(),
(int) $this->getState()->get('list.limit', 20)
);
return $this->cache[$store];
}
/**
* Retrieves the array of authorized sort fields
*
* @return array
*
* @since 2.0
*/
public function getSortFields()
{
return $this->sortFields;
}
/**
* Method to request new data from GitHub
*
@ -359,7 +521,8 @@ class PullsModel extends AbstractModel
catch (UnexpectedResponse $exception)
{
throw new \RuntimeException(
Text::sprintf('COM_PATCHTESTER_ERROR_GITHUB_FETCH',
Text::sprintf(
'COM_PATCHTESTER_ERROR_GITHUB_FETCH',
$exception->getMessage()
),
$exception->getCode(),
@ -383,11 +546,16 @@ class PullsModel extends AbstractModel
$linkHeader = $linkHeader[0];
}
preg_match('/(\?page=[0-9]{1,3}&per_page=' . $batchSize . '+>; rel=\"last\")/', $linkHeader, $matches);
preg_match(
'/(\?page=[0-9]{1,3}&per_page=' . $batchSize
. '+>; rel=\"last\")/', $linkHeader, $matches
);
if ($matches && isset($matches[0]))
{
$pageSegment = str_replace('&per_page=' . $batchSize, '', $matches[0]);
$pageSegment = str_replace(
'&per_page=' . $batchSize, '', $matches[0]
);
preg_match('/\d+/', $pageSegment, $pages);
$lastPage = (int) $pages[0];
@ -423,7 +591,11 @@ class PullsModel extends AbstractModel
{
$branch = substr($label->name, 3);
}
elseif (in_array(strtolower($label->name), ['npm resource changed', 'composer dependency changed'], true))
elseif (in_array(
strtolower($label->name),
['npm resource changed', 'composer dependency changed'],
true
))
{
$isNPM = true;
}
@ -433,7 +605,7 @@ class PullsModel extends AbstractModel
[
(int) $pull->number,
$this->getDb()->quote($label->name),
$this->getDb()->quote($label->color)
$this->getDb()->quote($label->color),
]
);
}
@ -441,8 +613,12 @@ class PullsModel extends AbstractModel
// Build the data object to store in the database
$pullData = [
(int) $pull->number,
$this->getDb()->quote(HTMLHelper::_('string.truncate', $pull->title, 150)),
$this->getDb()->quote(HTMLHelper::_('string.truncate', $pull->body, 100)),
$this->getDb()->quote(
HTMLHelper::_('string.truncate', $pull->title, 150)
),
$this->getDb()->quote(
HTMLHelper::_('string.truncate', $pull->body, 100)
),
$this->getDb()->quote($pull->pull_request->html_url),
(int) $isRTC,
(int) $isNPM,
@ -464,7 +640,10 @@ class PullsModel extends AbstractModel
$this->getDb()->setQuery(
$this->getDb()->getQuery(true)
->insert('#__patchtester_pulls')
->columns(['pull_id', 'title', 'description', 'pull_url', 'is_rtc', 'is_npm', 'branch'])
->columns(
['pull_id', 'title', 'description', 'pull_url',
'is_rtc', 'is_npm', 'branch']
)
->values($data)
);
@ -510,7 +689,7 @@ class PullsModel extends AbstractModel
return [
'complete' => false,
'page' => ($page + 1),
'lastPage' => $lastPage ?? false
'lastPage' => $lastPage ?? false,
];
}
@ -525,76 +704,4 @@ class PullsModel extends AbstractModel
{
$this->getDb()->truncateTable('#__patchtester_pulls');
}
/**
* Gets an array of objects from the results of database query.
*
* @param \JDatabaseQuery|string $query The query.
* @param integer $limitstart Offset.
* @param integer $limit The number of records.
*
* @return array An array of results.
*
* @since 2.0
* @throws RuntimeException
*/
protected function getList($query, $limitstart = 0, $limit = 0)
{
return $this->getDb()->setQuery($query, $limitstart, $limit)->loadObjectList();
}
/**
* Returns a record count for the query.
*
* @param \JDatabaseQuery|string $query The query.
*
* @return integer Number of rows for query.
*
* @since 2.0
*/
protected function getListCount($query)
{
// Use fast COUNT(*) on JDatabaseQuery objects if there no GROUP BY or HAVING clause:
if ($query instanceof \JDatabaseQuery && $query->type == 'select' && $query->group === null && $query->having === null)
{
$query = clone $query;
$query->clear('select')->clear('order')->select('COUNT(*)');
$this->getDb()->setQuery($query);
return (int) $this->getDb()->loadResult();
}
// Otherwise fall back to inefficient way of counting all results.
$this->getDb()->setQuery($query)->execute();
return (int) $this->getDb()->getNumRows();
}
/**
* Method to cache the last query constructed.
*
* This method ensures that the query is constructed only once for a given state of the model.
*
* @return \JDatabaseQuery A JDatabaseQuery object
*
* @since 2.0
*/
protected function getListQueryCache()
{
// Capture the last store id used.
static $lastStoreId;
// Compute the current store id.
$currentStoreId = $this->getStoreId();
// If the last store id is different from the current, refresh the query.
if ($lastStoreId != $currentStoreId || empty($this->query))
{
$lastStoreId = $currentStoreId;
$this->query = $this->getListQuery();
}
return $this->query;
}
}

View File

@ -34,6 +34,14 @@ class PullsHtmlView extends DefaultHtmlView
*/
protected $branches = [];
/**
* Array containing the list of labels
*
* @var array
* @since 4.0.0
*/
protected $labels = [];
/**
* Array containing environment errors
*
@ -101,6 +109,7 @@ class PullsHtmlView extends DefaultHtmlView
$this->pagination = $this->model->getPagination();
$this->trackerAlias = TrackerHelper::getTrackerAlias($this->state->get('github_user'), $this->state->get('github_repo'));
$this->branches = $this->model->getBranches();
$this->labels = $this->model->getLabels();
}
// Change the layout if there are environment errors

View File

@ -35,11 +35,12 @@ $listOrder = $this->escape($this->state->get('list.fullordering', 'a.pull_id
$listLimit = (int) ($this->state->get('list.limit'));
$filterApplied = $this->escape($this->state->get('filter.applied'));
$filterBranch = $this->escape($this->state->get('filter.branch'));
$filterLabel = $this->state->get('filter.label');
$filterRtc = $this->escape($this->state->get('filter.rtc'));
$filterNpm = $this->escape($this->state->get('filter.npm'));
$visible = '';
if ($filterApplied || $filterBranch || $filterRtc || $filterNpm)
if ($filterApplied || $filterBranch || $filterLabel || $filterRtc || $filterNpm)
{
$visible = 'js-stools-container-filters-visible';
}
@ -117,6 +118,12 @@ if ($filterApplied || $filterBranch || $filterRtc || $filterNpm)
<option value="no"<?php echo $filterNpm === 'no' ? ' selected="selected"' : ''; ?>><?php echo Text::_('COM_PATCHTESTER_NOT_NPM'); ?></option>
</select>
</div>
<div class="js-stools-field-filter">
<select name="filter_label[]" class="custom-select" multiple="" onchange="this.form.submit();" size="5">
<option value=""><?php echo Text::_('COM_PATCHTESTER_FILTER_LABEL'); ?></option>
<?php echo HTMLHelper::_('select.options', $this->labels, 'text', 'text', $filterLabel, false); ?>
</select>
</div>
<div class="js-stools-field-filter">
<select name="filter_branch" class="custom-select" onchange="this.form.submit();">
<option value=""><?php echo Text::_('COM_PATCHTESTER_FILTER_BRANCH'); ?></option>

View File

@ -75,6 +75,7 @@ COM_PATCHTESTER_FILE_DELETED_DOES_NOT_EXIST_S="The file marked for deletion does
COM_PATCHTESTER_FILE_MODIFIED_DOES_NOT_EXIST_S="The file marked for modification does not exist: %s"
COM_PATCHTESTER_FILTER_APPLIED_PATCHES="Filter Applied Patches"
COM_PATCHTESTER_FILTER_BRANCH="Filter Target Branch"
COM_PATCHTESTER_FILTER_LABEL="Filter Label"
COM_PATCHTESTER_FILTER_NPM_PATCHES="Filter NPM Patches"
COM_PATCHTESTER_FILTER_RTC_PATCHES="Filter RTC Patches"
COM_PATCHTESTER_FILTER_SEARCH_DESCRIPTION="Search the list by title or prefix with 'id:' to search by Pull ID."