/** * Get the project labels. * * @return $this * * @since 1.0 */ protected function processLabels() { $this->out(g11n3t('Fetching labels...'), false); /* @type \Joomla\Database\DatabaseDriver $db */ $db = $this->getContainer()->get('db'); $table = new LabelsTable($db); $labels = $this->github->issues->labels->getList($this->project->gh_user, $this->project->gh_project); $names = array(); $cntUpdated = 0; $cntNew = 0; foreach ($labels as $label) { try { $table->label_id = null; // Check if the label exists $table->load(array('project_id' => $this->project->project_id, 'name' => $label->name)); // Values that may have changed if ($table->color != $label->color) { $table->color = $label->color; $table->store(); ++$cntUpdated; } } catch (\RuntimeException $e) { // New label $table->project_id = $this->project->project_id; $table->name = $label->name; $table->color = $label->color; $table->store(); ++$cntNew; } $names[] = $label->name; } // Check for deleted labels $ids = $db->setQuery($db->getQuery(true)->from($db->quoteName($table->getTableName()))->select('label_id')->where($db->quoteName('project_id') . ' = ' . $this->project->project_id)->where($db->quoteName('name') . ' NOT IN (\'' . implode("', '", $names) . '\')'))->loadColumn(); if ($ids) { // Kill the orphans $db->setQuery($db->getQuery(true)->delete($db->quoteName($table->getTableName()))->where($db->quoteName('label_id') . ' IN (' . implode(', ', $ids) . ')'))->execute(); } $cntDeleted = count($ids); return $this->out('ok')->logOut(sprintf(g11n3t('Labels: %1$d new, %2$d updated, %3$d deleted.'), $cntNew, $cntUpdated, $cntDeleted)); }
/** * Get a set of ids from label names. * * @param array $labelObjects Array of label objects * * @return array * * @since 1.0 */ private function getLabelIds($labelObjects) { static $labels = array(); if (!$labels) { /* @type \Joomla\Database\DatabaseDriver $db */ $db = $this->getContainer()->get('db'); $table = new LabelsTable($db); $labelList = $db->setQuery($db->getQuery(true)->from($db->quoteName($table->getTableName()))->select(array('label_id', 'name'))->where($db->quoteName('project_id') . ' = ' . $this->project->project_id))->loadObjectList(); foreach ($labelList as $labelObject) { $labels[$labelObject->name] = $labelObject->label_id; } } $ids = array(); foreach ($labelObjects as $label) { if (!array_key_exists($label->name, $labels)) { // @todo Label does not exist :( - reload labels for the project } else { $ids[] = $labels[$label->name]; } } return $ids; }
/** * Process labels for adding into the issues table * * @param integer $issueId Issue ID to process * * @return string * * @since 1.0 */ protected function processLabels($issueId) { try { $githubLabels = $this->github->issues->get($this->project->gh_user, $this->project->gh_project, $issueId)->labels; } catch (\DomainException $exception) { $this->logger->error(sprintf('Error parsing the labels for GitHub issue %s/%s #%d - %s', $this->project->gh_user, $this->project->gh_project, $issueId, $exception->getMessage())); return ''; } $appLabelIds = array(); // Make sure the label is present in the database by pulling the ID, add it if it isn't $query = $this->db->getQuery(true); foreach ($githubLabels as $label) { $query->clear()->select($this->db->quoteName('label_id'))->from($this->db->quoteName('#__tracker_labels'))->where($this->db->quoteName('project_id') . ' = ' . (int) $this->project->project_id)->where($this->db->quoteName('name') . ' = ' . $this->db->quote($label->name)); $this->db->setQuery($query); $id = $this->db->loadResult(); // If null, add the label if ($id === null) { $table = new LabelsTable($this->db); $data = array(); $data['project_id'] = $this->project->project_id; $data['name'] = $label->name; $data['color'] = $label->color; try { $table->save($data); $id = $table->label_id; } catch (\RuntimeException $exception) { $this->logger->error(sprintf('Error adding label %s for project %s/%s to the database: %s', $label->name, $this->project->gh_user, $this->project->gh_project, $exception->getMessage())); } } // Add the ID to the array $appLabelIds[] = $id; } // Return the array as a string if (count($appLabelIds) === 0) { return ''; } else { return implode(',', $appLabelIds); } }
/** * Get a list of labels defined for the project. * * @return array * * @since 1.0 */ public function getLabels() { static $labels = array(); if (!$labels) { $db = $this->database; $table = new LabelsTable($db); $labelList = $db->setQuery($db->getQuery(true)->from($db->quoteName($table->getTableName()))->select(array('label_id', 'name', 'color'))->where($db->quoteName('project_id') . ' = ' . $this->project_id))->loadObjectList(); foreach ($labelList as $labelObject) { $l = new \stdClass(); $l->name = $labelObject->name; $l->color = $labelObject->color; $labels[$labelObject->label_id] = $l; } } return $labels; }