getUserStateFromRequest($this->context . '.filter.search', 'filter_search', ''); $this->setState('filter.search', $search); // Load the parameters. $params = JComponentHelper::getParams('com_patchtester'); $this->setState('params', $params); $this->setState('github_user', $params->get('org', 'joomla')); $this->setState('github_repo', $params->get('repo', 'joomla-cms')); // List state information. parent::populateState('a.pull_id', 'desc'); } /** * Retrieves a list of applied patches * * @return mixed * * @since 1.0 */ public function getAppliedPatches() { $db = $this->getDbo(); $db->setQuery( $db->getQuery(true) ->select('*') ->from($db->quoteName('#__patchtester_tests')) ->where($db->quoteName('applied') . ' = 1') ); try { $tests = $db->loadObjectList('pull_id'); return $tests; } catch (RuntimeException $e) { $this->setError($e->getMessage()); return false; } } /** * 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->getDbo(); $query = $db->getQuery(true); // Select the required fields from the table. $query->select($this->getState('list.select', 'a.*')); $query->from($db->quoteName('#__patchtester_pulls', 'a')); // Join the tests table to get applied patches $query->select($db->quoteName('t.id', 'applied')); $query->join('LEFT', $db->quoteName('#__patchtester_tests', 't') . ' ON t.pull_id = a.pull_id'); // Filter by search $search = $this->getState('filter.search'); if (!empty($search)) { $search = $db->quote('%' . $db->escape($search, true) . '%'); $query->where( '(' . $db->quoteName('a.title') . ' LIKE ' . $search . ') OR ' . '(' . $db->quoteName('a.pull_id') . ' LIKE ' . $search . ') OR ' ); } // Handle the list ordering. $ordering = $this->getState('list.ordering'); $direction = $this->getState('list.direction'); if (!empty($ordering)) { $query->order($db->escape($ordering) . ' ' . $db->escape($direction)); } return $query; } /** * Method to request new data from GitHub * * @return void * * @since 2.0 * @throws RuntimeException */ public function requestFromGithub() { // Get the Github object $github = PatchtesterHelper::initializeGithub(); // If over the API limit, we can't build this list if ($github->authorization->getRateLimit()->rate->remaining > 0) { // Sanity check, ensure there aren't any applied patches if (count($this->getAppliedPatches()) >= 1) { throw new RuntimeException(JText::_('COM_PATCHTESTER_ERROR_APPLIED_PATCHES')); } $pulls = array(); $page = 0; do { $page++; try { $items = $github->pulls->getList($this->getState('github_user'), $this->getState('github_repo'), 'open', $page, 100); } catch (DomainException $e) { throw new RuntimeException(JText::sprintf('COM_PATCHTESTER_ERROR_GITHUB_FETCH', $e->getMessage())); } $count = is_array($items) ? count($items) : 0; if ($count) { $pulls = array_merge($pulls, $items); } } while ($count); // Dump the old data now $this->getDbo()->truncateTable('#__patchtester_pulls'); foreach ($pulls as &$pull) { // Build the data object to store in the database $data = new stdClass; $data->pull_id = $pull->number; $data->title = $pull->title; $data->description = $pull->body; $data->pull_url = $pull->html_url; try { $this->getDbo()->insertObject('#__patchtester_pulls', $data, 'id'); } catch (RuntimeException $e) { throw new RuntimeException(JText::sprintf('COM_PATCHTESTER_ERROR_INSERT_DATABASE', $e->getMessage())); } } } else { throw new RuntimeException(JText::sprintf('COM_PATCHTESTER_API_LIMIT_LIST', JFactory::getDate($this->rate->reset))); } } }