Example #1
0
 /**
  * Prepare the response.
  *
  * @return  void
  *
  * @since   1.0
  * @throws  \Exception
  */
 protected function prepareResponse()
 {
     /* @type \JTracker\Application $application */
     $application = $this->getContainer()->get('app');
     $user = $application->getUser();
     $project = $application->getProject();
     if (!$user->id) {
         throw new \Exception('You are not allowed to test this item.');
     }
     $issueId = $application->input->getUint('issueId');
     $result = $application->input->getUint('result');
     $userComment = $application->input->get('comment', '', 'raw');
     $sha = $application->input->getCmd('sha');
     if (!$issueId) {
         throw new \Exception('No issue ID received.');
     }
     $this->dispatcher = $application->getDispatcher();
     $this->addEventListener('tests');
     $this->setProjectGitHubBot($project);
     $issueModel = new IssueModel($this->getContainer()->get('db'));
     $data = new \stdClass();
     $data->testResults = $issueModel->saveTest($issueId, $user->username, $result, $sha);
     $issueNumber = $issueModel->getIssueNumberById($issueId);
     $event = (new ActivityModel($this->getContainer()->get('db')))->addActivityEvent('test_item', 'now', $user->username, $project->project_id, $issueNumber, null, json_encode($result));
     $this->triggerEvent('onTestAfterSubmit', ['issueNumber' => $issueNumber, 'data' => $data->testResults]);
     $data->event = new \stdClass();
     foreach ($event as $k => $v) {
         $data->event->{$k} = $v;
     }
     $data->event->text = json_decode($data->event->text);
     $gitHubHelper = new GitHubHelper($this->getContainer()->get('gitHub'));
     // Create a comment to submitted on GitHub.
     switch ($result) {
         case 0:
             $comment = 'I have not tested this item.';
             break;
         case 1:
             $comment = 'I have tested this item :white_check_mark: successfully on ' . $sha;
             break;
         case 2:
             $comment = 'I have tested this item :red_circle: unsuccessfully on ' . $sha;
             break;
         default:
             throw new \UnexpectedValueException('Unexpected test result value.');
             break;
     }
     $comment .= $userComment ? '<br /><br />' . $userComment : '';
     $comment .= $gitHubHelper->getApplicationComment($application, $project, $issueNumber);
     $data->comment = $gitHubHelper->addComment($project, $issueNumber, $comment, $user->username, $this->getContainer()->get('db'));
     $this->response->data = json_encode($data);
     $this->response->message = g11n3t('Test successfully added');
 }
Example #2
0
 /**
  * Prepare the response.
  *
  * @return  mixed
  *
  * @since   1.0
  * @throws  \Exception
  */
 protected function prepareResponse()
 {
     /* @type \JTracker\Application $application */
     $application = $this->getContainer()->get('app');
     $application->getUser()->authorize('create');
     $comment = $application->input->get('text', '', 'raw');
     $issueNumber = $application->input->getInt('issue_number');
     $project = $application->getProject();
     if (!$issueNumber) {
         throw new \Exception('No issue number received.');
     }
     if (!$comment) {
         throw new \Exception('You should write a comment first...');
     }
     $gitHubHelper = new GitHubHelper($this->getContainer()->get('gitHub'));
     $comment .= $gitHubHelper->getApplicationComment($application, $project, $issueNumber);
     $this->response->data = $gitHubHelper->addComment($project, $issueNumber, $comment, $application->getUser()->username, $this->getContainer()->get('db'));
     $this->response->message = g11n3t('Your comment has been submitted');
 }
Example #3
0
 /**
  * Execute the controller.
  *
  * @return  string  The rendered view.
  *
  * @since   1.0
  * @throws  \JTracker\Authentication\Exception\AuthenticationException
  * @throws  \RuntimeException
  * @throws  \UnexpectedValueException
  */
 public function execute()
 {
     /* @type \JTracker\Application $application */
     $application = $this->getContainer()->get('app');
     $src = $application->input->get('item', array(), 'array');
     $user = $application->getUser();
     $project = $application->getProject();
     $model = new IssueModel($this->getContainer()->get('db'));
     $model->setProject($project);
     $issueNumber = isset($src['issue_number']) ? (int) $src['issue_number'] : 0;
     if (!$issueNumber) {
         throw new \UnexpectedValueException('No issue number received.');
     }
     $item = $model->getItem($issueNumber);
     $data = array();
     if ($user->check('edit')) {
         // The user has full "edit" permission.
         $data = $src;
         // Allow admins to update labels and milestones
         if (!$user->check('manage')) {
             if (!empty($item->labels)) {
                 $data['labels'] = explode(',', $item->labels);
             }
             $data['milestone_id'] = $item->milestone_id;
         }
     } elseif ($user->canEditOwn($item->opened_by)) {
         // The user has "edit own" permission.
         $data['id'] = (int) $src['id'];
         $data['issue_number'] = (int) $src['issue_number'];
         $data['title'] = $src['title'];
         $data['description_raw'] = $src['description_raw'];
         // Take the remaining values from the stored item
         if (!empty($item->labels)) {
             $data['labels'] = explode(',', $item->labels);
         }
         $data['status'] = $item->status;
         $data['priority'] = $item->priority;
         $data['build'] = $item->build;
         $data['rel_number'] = $item->rel_number;
         $data['rel_type'] = $item->rel_type;
         $data['easy'] = $item->easy;
         $data['milestone_id'] = $item->milestone_id;
     } else {
         // The user has no "edit" permission.
         throw new AuthenticationException($user, 'edit');
     }
     $gitHub = GithubFactory::getInstance($application);
     // Check if the state has changed (e.g. open/closed)
     $oldState = $model->getOpenClosed($item->status);
     $state = $model->getOpenClosed($data['status']);
     // Project is managed on GitHub
     if ($project->gh_user && $project->gh_project) {
         // @todo assignee
         $assignee = null;
         // Prepare labels
         $ghLabels = [];
         if (!empty($data['labels'])) {
             foreach ($project->getLabels() as $id => $label) {
                 if (in_array($id, $data['labels'])) {
                     $ghLabels[] = $label->name;
                 }
             }
         }
         // Prepare milestone
         $ghMilestone = null;
         if (!empty($data['milestone_id'])) {
             foreach ($project->getMilestones() as $milestone) {
                 if ($milestone->milestone_id == $data['milestone_id']) {
                     $ghMilestone = $milestone->milestone_number;
                 }
             }
         }
         try {
             $gitHubResponse = $this->updateGitHub($item->issue_number, $data, $state, $oldState, $assignee, $ghMilestone, $ghLabels);
             // Set the modified_date from GitHub (important!)
             $data['modified_date'] = $gitHubResponse->updated_at;
         } catch (GithubException $exception) {
             $this->getContainer()->get('app')->getLogger()->error(sprintf('Error code %1$s received from GitHub when editing an issue with the following data:' . ' GitHub User: %2$s; GitHub Repo: %3$s; Issue Number: %4$s; State: %5$s, Old state: %6$s' . '  The error message returned was: %7$s', $exception->getCode(), $project->gh_user, $project->gh_project, $item->issue_number, $state, $oldState, $exception->getMessage()));
             throw new \RuntimeException('Invalid response from GitHub');
         }
         // Render the description text using GitHub's markdown renderer.
         $data['description'] = $gitHub->markdown->render($data['description_raw'], 'gfm', $project->gh_user . '/' . $project->gh_project);
     } else {
         // Project is managed by JTracker only
         // Render the description text using GitHub's markdown renderer.
         $data['description'] = $gitHub->markdown->render($src['description_raw'], 'markdown');
         $data['modified_date'] = (new Date())->format($this->getContainer()->get('db')->getDateFormat());
     }
     try {
         $data['modified_by'] = $user->username;
         // If the user have edit permission, let him / her modify the categories.
         if ($user->check('edit')) {
             $categoryModel = new CategoryModel($this->getContainer()->get('db'));
             $category['issue_id'] = $data['id'];
             $category['modified_by'] = $user->username;
             $category['categories'] = $application->input->get('categories', null, 'array');
             $category['issue_number'] = $data['issue_number'];
             $category['project_id'] = $project->project_id;
             $categoryModel->updateCategory($category);
         }
         // Pass the old and new states into the save method
         $data['old_state'] = $oldState;
         $data['new_state'] = $state;
         // Values that are not supposed to change.
         $data['commits'] = $item->commits;
         $data['pr_head_sha'] = $item->pr_head_sha;
         // Save the record.
         $model->save($data);
         $comment = $application->input->get('comment', '', 'raw');
         // Save the comment.
         if ($comment) {
             /* @type \JTracker\Github\Github $github */
             $github = $this->getContainer()->get('gitHub');
             $project = $application->getProject();
             $gitHubHelper = new GitHubHelper($github);
             $comment .= $gitHubHelper->getApplicationComment($application, $project, $issueNumber);
             $data = new \stdClass();
             $db = $this->getContainer()->get('db');
             if ($project->gh_user && $project->gh_project) {
                 $gitHubResponse = $github->issues->comments->create($project->gh_user, $project->gh_project, $issueNumber, $comment);
                 if (!isset($gitHubResponse->id)) {
                     throw new \RuntimeException('Invalid response from GitHub');
                 }
                 $data->created_at = $gitHubResponse->created_at;
                 $data->opened_by = $gitHubResponse->user->login;
                 $data->comment_id = $gitHubResponse->id;
                 $data->text_raw = $gitHubResponse->body;
                 $data->text = $github->markdown->render($comment, 'gfm', $project->gh_user . '/' . $project->gh_project);
             } else {
                 $date = new Date();
                 $data->created_at = $date->format($db->getDateFormat());
                 $data->opened_by = $application->getUser()->username;
                 $data->comment_id = '???';
                 $data->text_raw = $comment;
                 $data->text = $github->markdown->render($comment, 'markdown');
             }
             $table = new ActivitiesTable($db);
             $table->event = 'comment';
             $table->created_date = $data->created_at;
             $table->project_id = $project->project_id;
             $table->issue_number = $issueNumber;
             $table->gh_comment_id = $data->comment_id;
             $table->user = $data->opened_by;
             $table->text = $data->text;
             $table->text_raw = $data->text_raw;
             $table->store();
         }
         $application->enqueueMessage('The changes have been saved.', 'success')->redirect('/tracker/' . $application->input->get('project_alias') . '/' . $issueNumber);
     } catch (\RuntimeException $exception) {
         $application->enqueueMessage($exception->getMessage(), 'error');
         // @todo preserve data when returning to edit view on failure.
         $application->redirect($application->get('uri.base.path') . 'tracker/' . $application->input->get('project_alias') . '/' . $issueNumber . '/edit');
     }
     return parent::execute();
 }
Example #4
0
 /**
  * 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;
     $gitHubBot = null;
     if ($this->project->gh_editbot_user && $this->project->gh_editbot_pass) {
         $gitHubBot = new GitHubHelper(GithubFactory::getInstance($this->getApplication(), true, $this->project->gh_editbot_user, $this->project->gh_editbot_pass));
     }
     // 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;
         // We do not have a data about the default branch
         // @todo We need to retrieve repository somehow
         $table->build = 'master';
         // 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->build = $pullRequest->base->ref;
             // If the $pullRequest->head->user object is not set, the repo/branch had been deleted by the user.
             $table->pr_head_user = isset($pullRequest->head->user) ? $pullRequest->head->user->login : '******';
             $table->pr_head_ref = $pullRequest->head->ref;
             if ($gitHubBot && $table->pr_head_sha && $table->pr_head_sha != $pullRequest->head->sha) {
                 // The PR has been updated.
                 $testers = (new IssueModel($this->getContainer()->get('db')))->getAllTests($table->id);
                 if ($testers) {
                     // Send a notification.
                     $comment = "This PR has received new commits.\n\n**CC:** @" . implode(', @', $testers);
                     // @todo send application comment - find a way to set a WEB URL in CLI scripts.
                     // @todo $comment .= $gitHubBot->getApplicationComment($this->getContainer()->get('app'), $this->project, $table->issue_number);
                     $gitHubBot->addComment($this->project, $table->issue_number, $comment, $this->project->gh_editbot_user, $this->getContainer()->get('db'));
                 }
             }
             $table->pr_head_sha = $pullRequest->head->sha;
             $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
             $commits = (new GitHubHelper(GithubFactory::getInstance($this->getApplication())))->getCommits($this->project, $table->issue_number);
             $table->commits = json_encode($commits);
         }
         // 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->check()->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;
 }
Example #5
0
 /**
  * Method to update data for an issue from GitHub
  *
  * @return  boolean  True on success
  *
  * @since   1.0
  */
 protected function updateData()
 {
     $table = new IssuesTable($this->db);
     try {
         $table->load(array('issue_number' => $this->data->number, 'project_id' => $this->project->project_id));
     } catch (\Exception $e) {
         $this->setStatusCode($e->getCode());
         $logMessage = sprintf('Error loading GitHub issue %s/%s #%d in the tracker: %s', $this->project->gh_user, $this->project->gh_project, $this->data->number, $e->getMessage());
         $this->response->error = $logMessage;
         $this->logger->error($logMessage);
         return false;
     }
     // Figure out the state based on the action
     $action = $this->hookData->action;
     $status = $this->processStatus($action, $table->status);
     // Try to render the description with GitHub markdown
     $parsedText = $this->parseText($this->data->body);
     // Prepare the dates for insertion to the database
     $dateFormat = $this->db->getDateFormat();
     $modified = new Date($this->data->updated_at);
     // Only update fields that may have changed, there's no API endpoint to show that so make some guesses
     $data = array();
     $data['id'] = $table->id;
     $data['title'] = $this->data->title;
     $data['description'] = $parsedText;
     $data['description_raw'] = $this->data->body;
     $data['status'] = is_null($status) ? $table->status : $status;
     $data['modified_date'] = $modified->format($dateFormat);
     $data['modified_by'] = $this->hookData->sender->login;
     // Add the closed date if the status is closed
     if ($this->data->closed_at) {
         $closed = new Date($this->data->closed_at);
         $data['closed_date'] = $closed->format($dateFormat);
     }
     // Process labels for the item
     $data['labels'] = $this->processLabels($this->data->number);
     // Grab some data based on the existing record
     $data['priority'] = $table->priority;
     $data['build'] = $table->build;
     $data['rel_number'] = $table->rel_number;
     $data['rel_type'] = $table->rel_type;
     $data['milestone_id'] = $table->milestone_id;
     if (empty($data['build'])) {
         $data['build'] = $this->hookData->repository->default_branch;
     }
     $model = (new IssueModel($this->db))->setProject(new TrackerProject($this->db, $this->project));
     // Check if the state has changed (e.g. open/closed)
     $oldState = $model->getOpenClosed($table->status);
     $state = is_null($status) ? $oldState : $model->getOpenClosed($data['status']);
     $data['old_state'] = $oldState;
     $data['new_state'] = $state;
     $gitHubBot = null;
     if ($this->project->gh_editbot_user && $this->project->gh_editbot_pass) {
         $gitHubBot = new GitHubHelper(GithubFactory::getInstance($this->getContainer()->get('app'), true, $this->project->gh_editbot_user, $this->project->gh_editbot_pass));
     }
     if ($gitHubBot && $table->pr_head_sha && $table->pr_head_sha != $this->data->head->sha) {
         // The PR has been updated.
         $testers = (new IssueModel($this->getContainer()->get('db')))->getAllTests($table->id);
         if ($testers) {
             // Send a notification.
             $comment = "This PR has received new commits.\n\n**CC:** @" . implode(', @', $testers);
             $comment .= $gitHubBot->getApplicationComment($this->getContainer()->get('app'), $this->project, $table->issue_number);
             $gitHubBot->addComment($this->project, $table->issue_number, $comment, $this->project->gh_editbot_user, $this->getContainer()->get('db'));
         }
     }
     $data['pr_head_sha'] = $this->data->head->sha;
     $commits = (new GitHubHelper($this->github))->getCommits($this->project, $this->data->number);
     $data['commits'] = json_encode($commits);
     try {
         $model->save($data);
     } catch (\Exception $e) {
         $this->setStatusCode($e->getCode());
         $logMessage = sprintf('Error updating GitHub pull request %s/%s #%d (Database ID #%d) to the tracker: %s', $this->project->gh_user, $this->project->gh_project, $this->data->number, $table->id, $e->getMessage());
         $this->response->error = $logMessage;
         $this->logger->error($logMessage);
         return false;
     }
     // Refresh the table object for the listeners
     $table->load($data['id']);
     $this->triggerEvent('onPullAfterUpdate', ['table' => $table]);
     // Add a reopen record to the activity table if the status is reopened
     if ($action == 'reopened') {
         $this->addActivityEvent('reopen', $this->data->updated_at, $this->hookData->sender->login, $this->project->project_id, $this->data->number);
     }
     // Add a synchronize record to the activity table if the action is synchronized
     if ($action == 'synchronize') {
         $this->addActivityEvent('synchronize', $this->data->updated_at, $this->hookData->sender->login, $this->project->project_id, $this->data->number);
     }
     // Add a close record to the activity table if the status is closed
     if ($this->data->closed_at) {
         $this->addActivityEvent('close', $this->data->closed_at, $this->hookData->sender->login, $this->project->project_id, $this->data->number);
     }
     // Store was successful, update status
     $this->logger->info(sprintf('Updated GitHub pull request %s/%s #%d (Database ID #%d) to the tracker.', $this->project->gh_user, $this->project->gh_project, $this->data->number, $table->id));
     return true;
 }