/**
  * Method to update data for an issue from GitHub
  *
  * @param   integer  $id  The comment ID
  *
  * @return  boolean  True on success
  *
  * @since   1.0
  */
 protected function updateComment($id)
 {
     // Try to render the comment with GitHub markdown
     $parsedText = $this->parseText($this->hookData->comment->body);
     // Only update fields that may have changed, there's no API endpoint to show that so make some guesses
     $data = array();
     $data['activities_id'] = $id;
     $data['text'] = $parsedText;
     $data['text_raw'] = $this->hookData->comment->body;
     try {
         $table = new ActivitiesTable($this->db);
         $table->load(array('activities_id' => $id));
         $table->save($data);
     } catch (\Exception $e) {
         $this->logger->error('Error updating the database for comment ' . $id . ':' . $e->getMessage());
         $this->getContainer()->get('app')->close();
     }
     $this->triggerEvent('onCommentAfterUpdate', $table);
     // Store was successful, update status
     $this->logger->info(sprintf('Updated comment %s/%s #%d to the tracker.', $this->project->gh_user, $this->project->gh_project, $id));
     return true;
 }
Beispiel #2
0
 /**
  * Method to process the list of issues and inject into the database as needed
  *
  * @return  $this
  *
  * @since   1.0
  */
 protected function processData()
 {
     if (!$this->items) {
         $this->logOut(g11n3t('Everything is up to date.'));
         return $this;
     }
     /* @type \Joomla\Database\DatabaseDriver $db */
     $db = $this->getContainer()->get('db');
     // Initialize our query object
     $query = $db->getQuery(true);
     $this->out(sprintf(g11n4t('Processing comments for one modified issue...', 'Processing comments for %d modified issues...', count($this->items)), count($this->items)));
     $adds = 0;
     $updates = 0;
     $count = 1;
     // Initialize our ActivitiesTable instance to insert the new record
     $table = new ActivitiesTable($db);
     // Comments ids for computing the difference
     $commentsIds = array();
     // Comments ids to delete
     $toDelete = array();
     // Start processing the comments now
     foreach ($this->items as $issueNumber => $comments) {
         if (!count($comments)) {
             $this->out()->out(sprintf(g11n3t('No comments for issue # %d'), $issueNumber));
         } else {
             $this->out()->out(sprintf(g11n4t('Processing one comment for issue # %2$d (%3$d/%4$d)', 'Processing %1$d comments for issue # %2$d (%3$d/%4$d)', count($comments)), count($comments), $issueNumber, $count, count($this->items)));
             $progressBar = $this->getProgressBar(count($comments));
             $this->usePBar ? $this->out() : null;
             foreach ($comments as $i => $comment) {
                 // Store the comment id for computing the difference
                 $commentsIds[] = $comment->id;
                 $check = $db->setQuery($query->clear()->select($table->getKeyName())->select($db->quoteName('updated_date'))->from($db->quoteName($table->getTableName()))->where($db->quoteName('gh_comment_id') . ' = ' . (int) $comment->id)->where($db->quoteName('project_id') . ' = ' . (int) $this->project->project_id))->loadObject();
                 if ($check) {
                     if (!$this->force) {
                         // If we have something already, check if it needs an update...
                         $d1 = new Date($check->updated_date);
                         $d2 = new Date($comment->updated_at);
                         if ($d1 == $d2) {
                             // No update required
                             $this->usePBar ? $progressBar->update($i + 1) : $this->out('-', false);
                             continue;
                         }
                     }
                     $table->load($check->{$table->getKeyName()});
                     $this->usePBar ? null : $this->out($this->force ? 'F ' : '~ ', false);
                 } else {
                     // New item
                     $table->reset();
                     $table->{$table->getKeyName()} = null;
                     $this->usePBar ? null : $this->out('+', false);
                 }
                 $table->gh_comment_id = $comment->id;
                 $table->issue_number = (int) $issueNumber;
                 $table->project_id = $this->project->project_id;
                 $table->user = $comment->user->login;
                 $table->event = 'comment';
                 $table->text_raw = $comment->body;
                 $table->text = $this->github->markdown->render($comment->body, 'gfm', $this->project->gh_user . '/' . $this->project->gh_project);
                 $this->checkGitHubRateLimit($this->github->markdown->getRateLimitRemaining());
                 $table->created_date = (new Date($comment->created_at))->format('Y-m-d H:i:s');
                 $table->updated_date = (new Date($comment->updated_at))->format('Y-m-d H:i:s');
                 $table->store();
                 if ($check) {
                     ++$updates;
                 } else {
                     ++$adds;
                 }
                 $this->usePBar ? $progressBar->update($i + 1) : null;
             }
             ++$count;
         }
         // Compute the difference between GitHub comments and issue comments
         $issueComments = $this->getIssueCommentsIds($issueNumber);
         $commentsToDelete = array_diff($issueComments, $commentsIds);
         $toDelete = array_merge($toDelete, $commentsToDelete);
     }
     // Delete comments which does not exist on GitHub
     if (!empty($toDelete)) {
         $this->deleteIssuesComments($toDelete);
     }
     $this->out()->outOK()->logOut(sprintf(g11n3t('%1$d added, %2$d updated, %3$d deleted.'), $adds, $updates, count($toDelete)));
     return $this;
 }