Exemplo n.º 1
0
 /**
  * Method to process the list of issues and inject into the database as needed
  *
  * @return  $this
  *
  * @since   1.0
  * @throws  \UnexpectedValueException
  */
 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');
     $query = $db->getQuery(true);
     $this->out(g11n3t('Adding events to the database...'), false);
     $progressBar = $this->getProgressBar(count($this->items));
     $this->usePBar ? $this->out() : null;
     $adds = 0;
     $count = 0;
     // Initialize our ActivitiesTable instance to insert the new record
     $table = new ActivitiesTable($db);
     foreach ($this->items as $issueId => $events) {
         $this->usePBar ? null : $this->out(sprintf(' #%d (%d/%d)...', $issueId, $count + 1, count($this->items)), false);
         foreach ($events as $event) {
             switch ($event->event) {
                 case 'referenced':
                 case 'closed':
                 case 'reopened':
                 case 'assigned':
                 case 'merged':
                 case 'head_ref_deleted':
                 case 'head_ref_restored':
                     $query->clear()->select($table->getKeyName())->from($db->quoteName('#__activities'))->where($db->quoteName('gh_comment_id') . ' = ' . (int) $event->id)->where($db->quoteName('project_id') . ' = ' . (int) $this->project->project_id);
                     $db->setQuery($query);
                     $id = (int) $db->loadResult();
                     $table->reset();
                     $table->{$table->getKeyName()} = null;
                     if ($id && !$this->force) {
                         if ($this->force) {
                             // Force update
                             $this->usePBar ? null : $this->out('F', false);
                             $table->{$table->getKeyName()} = $id;
                         } else {
                             // If we have something already, then move on to the next item
                             $this->usePBar ? null : $this->out('-', false);
                             continue;
                         }
                     } else {
                         $this->usePBar ? null : $this->out('+', false);
                     }
                     // Translate GitHub event names to "our" name schema
                     $evTrans = array('referenced' => 'reference', 'closed' => 'close', 'reopened' => 'reopen', 'assigned' => 'assign', 'merged' => 'merge', 'head_ref_deleted' => 'head_ref_deleted', 'head_ref_restored' => 'head_ref_restored');
                     $table->gh_comment_id = $event->id;
                     $table->issue_number = $issueId;
                     $table->project_id = $this->project->project_id;
                     $table->user = $event->actor->login;
                     $table->event = $evTrans[$event->event];
                     $table->created_date = (new Date($event->created_at))->format('Y-m-d H:i:s');
                     if ('referenced' == $event->event) {
                         // @todo obtain referenced information
                         /*
                         $reference = $this->github->issues->events->get(
                         	$this->project->gh_user, $this->project->gh_project, $event->id
                         );
                         
                         $this->checkGitHubRateLimit($this->github->issues->events->getRateLimitRemaining());
                         */
                     }
                     if ('assigned' == $event->event) {
                         $reference = $this->github->issues->events->get($this->project->gh_user, $this->project->gh_project, $event->id);
                         $table->text_raw = 'Assigned to ' . $reference->issue->assignee->login;
                         $table->text = $table->text_raw;
                         $this->checkGitHubRateLimit($this->github->issues->events->getRateLimitRemaining());
                     }
                     $table->store();
                     ++$adds;
                     break;
                 case 'mentioned':
                 case 'subscribed':
                 case 'unsubscribed':
                     continue;
                 default:
                     $this->logOut(sprintf('ERROR: Unknown Event: %s', $event->event));
                     continue;
             }
         }
         ++$count;
         $this->usePBar ? $progressBar->update($count) : null;
     }
     $this->out()->outOK()->logOut(sprintf(g11n3t('Added %d new issue events to the database'), $adds));
     return $this;
 }
Exemplo n.º 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;
 }