/** * Method to process the list of issues and inject into the database as needed * * @return $this * * @since 1.0 * @throws \RuntimeException */ protected function processData() { $ghIssues = $this->issues; $dbIssues = $this->getDbIssues(); if (!$ghIssues) { throw new \UnexpectedValueException('No issues received...'); } $added = 0; $updated = 0; $milestones = $this->getMilestones(); $this->out(g11n3t('Adding issues to the database...'), false); $progressBar = $this->getProgressBar(count($ghIssues)); $this->usePBar ? $this->out() : null; // Start processing the pulls now foreach ($ghIssues as $count => $ghIssue) { $this->usePBar ? $progressBar->update($count + 1) : $this->out($ghIssue->number . '...', false); if (!$this->checkInRange($ghIssue->number)) { // Not in range $this->usePBar ? null : $this->out('NiR ', false); continue; } $id = 0; foreach ($dbIssues as $dbIssue) { if ($ghIssue->number == $dbIssue->issue_number) { if ($this->force) { // Force update $this->usePBar ? null : $this->out('F ', false); $id = $dbIssue->id; break; } $d1 = new Date($ghIssue->updated_at); $d2 = new Date($dbIssue->modified_date); if ($d1 == $d2) { // No update required $this->usePBar ? null : $this->out('- ', false); continue 2; } $id = $dbIssue->id; break; } } // Store the item in the database $table = new IssuesTable($this->getContainer()->get('db')); if ($id) { $table->load($id); } $table->issue_number = $ghIssue->number; $table->title = $ghIssue->title; if ($table->description_raw != $ghIssue->body) { $table->description = $this->github->markdown->render($ghIssue->body, 'gfm', $this->project->gh_user . '/' . $this->project->gh_project); $this->checkGitHubRateLimit($this->github->markdown->getRateLimitRemaining()); $table->description_raw = $ghIssue->body; } $statusTable = new StatusTable($this->getContainer()->get('db')); // Get the list of status IDs based on the GitHub issue state $state = $ghIssue->state == 'open' ? false : true; $stateIds = $statusTable->getStateStatusIds($state); // Check if the issue status is in the array; if it is, then the item didn't change open state and we don't need to change the status if (!in_array($table->status, $stateIds)) { $table->status = $state ? 10 : 1; } $table->opened_date = (new Date($ghIssue->created_at))->format('Y-m-d H:i:s'); $table->opened_by = $ghIssue->user->login; $table->modified_date = (new Date($ghIssue->updated_at))->format('Y-m-d H:i:s'); $table->modified_by = $ghIssue->user->login; $table->project_id = $this->project->project_id; $table->milestone_id = $ghIssue->milestone && isset($milestones[$ghIssue->milestone->number]) ? $milestones[$ghIssue->milestone->number] : null; // If the issue has a diff URL, it is a pull request. if (isset($ghIssue->pull_request->diff_url)) { $table->has_code = 1; // Get the pull request corresponding to an issue. $this->debugOut('Get PR for the issue'); $pullRequest = $this->github->pulls->get($this->project->gh_user, $this->project->gh_project, $ghIssue->number); $table->pr_head_ref = $pullRequest->head->ref; $status = $this->GetMergeStatus($pullRequest); if (!$status->state) { // No status found. Let's create one! $status->state = 'pending'; $status->targetUrl = 'http://issues.joomla.org/gagaga'; $status->description = 'JTracker Bug Squad working on it...'; $status->context = 'jtracker'; // @todo Project based status messages // @$this->createStatus($ghIssue, 'pending', 'http://issues.joomla.org/gagaga', 'JTracker Bug Squad working on it...', 'CI/JTracker'); } else { // Save the merge status to database $table->merge_state = $status->state; $table->gh_merge_status = json_encode($status); } // Get commits $table->commits = json_encode($this->getCommits($pullRequest)); } // Add the closed date if the status is closed if ($ghIssue->closed_at) { $table->closed_date = (new Date($ghIssue->closed_at))->format('Y-m-d H:i:s'); } // If the title has a [# in it, assume it's a JoomlaCode Tracker ID if (preg_match('/\\[#([0-9]+)\\]/', $ghIssue->title, $matches)) { $table->foreign_number = $matches[1]; } elseif (preg_match('/tracker_item_id=([0-9]+)/', $ghIssue->body, $matches)) { $table->foreign_number = $matches[1]; } $table->labels = implode(',', $this->getLabelIds($ghIssue->labels)); $table->store(true); if (!$table->id) { // Bad coder :( - @todo when does this happen ?? throw new \RuntimeException(sprintf('Invalid issue id for issue: %1$d in project id %2$s', $ghIssue->number, $this->project->project_id)); } /* @todo see issue #194 Add an open record to the activity table $activity = new ActivitiesTable($db); $activity->project_id = $this->project->project_id; $activity->issue_number = (int) $table->issue_number; $activity->user = $issue->user->login; $activity->event = 'open'; $activity->created_date = $table->opened_date; $activity->store(); / Add a close record to the activity table if the status is closed if ($issue->closed_at) { $activity = new ActivitiesTable($db); $activity->project_id = $this->project->project_id; $activity->issue_number = (int) $table->issue_number; $activity->event = 'close'; $activity->created_date = $issue->closed_at; $activity->store(); } */ // Store was successful, update status if ($id) { ++$updated; } else { ++$added; } $this->changedIssueNumbers[] = $ghIssue->number; } // Output the final result $this->out()->logOut(sprintf(g11n3t('<ok>%1$d added, %2$d updated.</ok>'), $added, $updated)); return $this; }
/** * Translate the status id to either 'open' or 'closed'. * * @param integer $statusId The status id. * * @return string * * @since 1.0 */ public function getOpenClosed($statusId) { $table = new StatusTable($this->getDb()); $table->load($statusId); return $table->closed ? 'closed' : 'open'; }