Пример #1
0
 /**
  * Prepare the response.
  *
  * @return  mixed
  *
  * @since   1.0
  * @throws  \Exception
  */
 protected function prepareResponse()
 {
     // Verify the user has permissions to perform this action
     if (!$this->getContainer()->get('app')->getUser()->authorize('view')) {
         throw new \Exception('Anonymous votes are not allowed.');
     }
     /* @type Input $input */
     $input = $this->getContainer()->get('app')->input;
     $issue = $input->getUint('issueId');
     $experienced = $input->getInt('experienced');
     $importance = $input->getInt('importance');
     $userID = $this->getContainer()->get('app')->getUser()->id;
     if (!$issue) {
         throw new \Exception('No issue ID received.');
     }
     if (!$importance) {
         throw new \Exception('Issue importance not received');
     }
     $model = new IssueModel($this->getContainer()->get('db'));
     $data = $model->vote($issue, $experienced, $importance, $userID);
     // Add the new score
     $data->importanceScore = $data->score / $data->votes;
     $this->response->data = $data;
     $this->response->message = g11n3t('Vote successfully added');
 }
Пример #2
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->check('edit')) {
         throw new \Exception('You are not allowed to alter this item.');
     }
     $issueId = $application->input->getUint('issueId');
     if (!$issueId) {
         throw new \Exception('No issue ID received.');
     }
     $this->dispatcher = $application->getDispatcher();
     $this->addEventListener('tests');
     $this->setProjectGitHubBot($project);
     $data = new \stdClass();
     $result = new \stdClass();
     $result->user = $application->input->getUsername('user');
     $result->value = $application->input->getUint('result');
     $issueModel = new IssueModel($this->getContainer()->get('db'));
     $data->testResults = $issueModel->saveTest($issueId, $result->user, $result->value);
     $issueNumber = $issueModel->getIssueNumberById($issueId);
     $event = (new ActivityModel($this->getContainer()->get('db')))->addActivityEvent('alter_testresult', '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);
     $this->response->data = json_encode($data);
     $this->response->message = g11n3t('Test successfully added');
 }
Пример #3
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');
     if (!$issueId) {
         throw new \Exception('No issue ID received.');
     }
     $issueModel = new IssueModel($this->getContainer()->get('db'));
     $data = new \stdClass();
     $data->testResults = $issueModel->saveTest($issueId, $user->username, $result);
     $event = (new ActivityModel($this->getContainer()->get('db')))->addActivityEvent('test_item', 'now', $user->username, $project->project_id, $issueModel->getIssueNumberById($issueId), null, json_encode($result));
     $data->event = new \stdClass();
     foreach ($event as $k => $v) {
         $data->event->{$k} = $v;
     }
     $data->event->text = json_decode($data->event->text);
     $this->response->data = json_encode($data);
     $this->response->message = g11n3t('Test successfully added');
 }
Пример #4
0
 /**
  * Initialize the controller.
  *
  * This will set up default model and view classes.
  *
  * @throws \Exception
  * @throws AuthenticationException
  *
  * @return  $this  Method supports chaining
  *
  * @since   1.0
  */
 public function initialize()
 {
     parent::initialize();
     /* @type \JTracker\Application $application */
     $application = $this->getContainer()->get('app');
     $project = $application->getProject();
     $user = $application->getUser();
     $this->model->setProject($project);
     $item = $this->model->getItem($application->input->getUint('id'));
     $item->categoryids = array();
     foreach ($item->categories as $category) {
         $item->categoryids[] = $category->id;
     }
     try {
         // Check if the user has full "edit" permission
         $user->authorize('edit');
     } catch (AuthenticationException $e) {
         // Check if the user has "edit own" permission
         if (false == $user->canEditOwn($item->opened_by)) {
             throw $e;
         }
     }
     $this->view->setItem($item)->setProject($project);
     return $this;
 }
Пример #5
0
 /**
  * Method to render the view.
  *
  * @return  string  The rendered view.
  *
  * @since   1.0
  * @throws  \RuntimeException
  */
 public function render()
 {
     $this->renderer->set('item', $this->getItem());
     $this->renderer->set('project', $this->getProject());
     $this->renderer->set('statuses', $this->model->getStatuses());
     $this->renderer->set('canEditOwn', $this->canEditOwn());
     return parent::render();
 }
Пример #6
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');
 }
Пример #7
0
 /**
  * Initialize the controller.
  *
  * This will set up default model and view classes.
  *
  * @return  $this  Method supports chaining
  *
  * @since   1.0
  * @throws  \RuntimeException
  */
 public function initialize()
 {
     parent::initialize();
     /* @type \JTracker\Application $application */
     $application = $this->getContainer()->get('app');
     $project = $application->getProject();
     $user = $application->getUser();
     $user->authorize('view');
     $this->model->setProject($project);
     $item = $this->model->getItem($application->input->getUint('id'));
     $item->userTest = $this->model->getUserTest($item->id, $user->username);
     $this->view->setItem($item);
     $this->view->setEditOwn($user->canEditOwn($item->opened_by));
     $this->view->setProject($project);
     return $this;
 }
Пример #8
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();
     if (!$user->id) {
         throw new \Exception('You are not allowed to test this item.');
     }
     $issueId = $application->input->getUint('issueId');
     $result = $application->input->getUint('result');
     if (!$issueId) {
         throw new \Exception('No issue ID received.');
     }
     $model = new IssueModel($this->getContainer()->get('db'));
     $this->response->data = json_encode($model->saveTest($issueId, $user->username, $result));
     $this->response->message = g11n3t('Test successfully added');
 }
Пример #9
0
 /**
  * Prepare the response.
  *
  * @return  void
  *
  * @since   1.0
  * @throws  \RuntimeException
  */
 protected function prepareResponse()
 {
     $id = $this->getContainer()->get('app')->input->getUint('id');
     if (!$id) {
         throw new \RuntimeException('No id received.');
     }
     $model = new IssueModel($this->getContainer()->get('db'));
     $item = $model->getItem($id);
     $issue = new \stdClass();
     // @todo add more info...
     $issue->comment_count = 0;
     $issue->opened_by = $item->opened_by ?: 'n/a';
     foreach ($item->activities as $activity) {
         switch ($activity->event) {
             case 'comment':
                 $issue->comment_count++;
                 break;
             default:
                 break;
         }
     }
     $this->response->data = $issue;
 }
Пример #10
0
 /**
  * Initialize the controller.
  *
  * This will set up default model and view classes.
  *
  * @return  $this  Method supports chaining
  *
  * @since   1.0
  * @throws  \RuntimeException
  */
 public function initialize()
 {
     parent::initialize();
     /* @type \JTracker\Application $application */
     $application = $this->getContainer()->get('app');
     $project = $application->getProject();
     $user = $application->getUser();
     $user->authorize('view');
     $this->model->setProject($project);
     $item = $this->model->getItem($application->input->getUint('id'));
     if ($item->commits) {
         $commits = json_decode($item->commits);
         $lastCommit = end($commits);
         $sha = $lastCommit->sha;
     } else {
         $sha = false;
     }
     $item->userTest = $this->model->getUserTest($item->id, $user->username, $sha);
     $this->view->setItem($item);
     $this->view->setEditOwn($user->canEditOwn($item->opened_by));
     $this->view->setProject($project);
     return $this;
 }
Пример #11
0
 /**
  * Execute the controller.
  *
  * @throws \Exception
  * @throws \JTracker\Authentication\Exception\AuthenticationException
  *
  * @return  string  The rendered view.
  *
  * @since   1.0
  */
 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;
     } 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
         $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;
     } else {
         // The user has no "edit" permission.
         throw new AuthenticationException($user, 'edit');
     }
     $gitHub = GithubFactory::getInstance($application);
     if ($project->gh_user && $project->gh_project) {
         // Project is managed on GitHub
         // Check if the state has changed (e.g. open/closed)
         $oldState = $model->getOpenClosed($item->status);
         $state = $model->getOpenClosed($data['status']);
         try {
             $this->updateGitHub($item->issue_number, $data, $state, $oldState);
         } catch (GithubException $exception) {
             // DEBUG - @todo remove in stable
             $dump = ['exception' => $exception->getCode() . ' ' . $exception->getMessage(), 'user' => $project->gh_user, 'project' => $project->gh_project, 'issueNo' => $item->issue_number, 'state' => $state, 'old_state' => $oldState, 'data' => $data];
             var_dump($dump);
             die('Unrecoverable GitHub error - sry ;(');
         }
         // 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');
     }
     try {
         $data['modified_by'] = $user->username;
         $categoryModel = new CategoryModel($this->getContainer()->get('db'));
         $category['issue_id'] = $data['id'];
         $category['categories'] = $application->input->get('categories', null, 'array');
         $category['modified_by'] = $user->username;
         $category['issue_number'] = $data['issue_number'];
         $category['project_id'] = $project->project_id;
         $categoryModel->updateCategory($category);
         // Save the record.
         $model->save($data);
         $application->enqueueMessage(g11n3t('The changes have been saved.'), 'success')->redirect('/tracker/' . $application->input->get('project_alias') . '/' . $issueNumber);
     } catch (\Exception $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();
 }
Пример #12
0
 /**
  * Method to insert data for an issue from GitHub
  *
  * @return  boolean  True on success
  *
  * @since   1.0
  */
 protected function insertData()
 {
     // Figure out the state based on the action
     $action = $this->hookData->action;
     $status = $this->processStatus($action);
     $parsedText = $this->parseText($this->data->body);
     // Prepare the dates for insertion to the database
     $dateFormat = $this->db->getDateFormat();
     $opened = new Date($this->data->created_at);
     $modified = new Date($this->data->updated_at);
     $data = array();
     $data['issue_number'] = $this->data->number;
     $data['title'] = $this->data->title;
     $data['description'] = $parsedText;
     $data['description_raw'] = $this->data->body;
     $data['status'] = is_null($status) ? 1 : $status;
     $data['opened_date'] = $opened->format($dateFormat);
     $data['opened_by'] = $this->data->user->login;
     $data['modified_date'] = $modified->format($dateFormat);
     $data['modified_by'] = $this->hookData->sender->login;
     $data['project_id'] = $this->project->project_id;
     $data['has_code'] = 1;
     $data['build'] = $this->data->base->ref;
     // 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);
         $data['closed_by'] = $this->hookData->sender->login;
     }
     // If the title has a [# in it, assume it's a JoomlaCode Tracker ID
     if (preg_match('/\\[#([0-9]+)\\]/', $this->data->title, $matches)) {
         $data['foreign_number'] = $matches[1];
     } elseif (preg_match('/tracker_item_id=([0-9]+)/', $this->data->body, $matches)) {
         $data['foreign_number'] = $matches[1];
     }
     // Process labels for the item
     $data['labels'] = $this->processLabels($this->data->number);
     $model = new IssueModel($this->db);
     try {
         $model->setProject(new TrackerProject($this->db, $this->project))->add($data);
     } catch (\Exception $e) {
         $this->logger->error(sprintf('Error adding GitHub pull request %s/%s #%d to the tracker: %s', $this->project->gh_user, $this->project->gh_project, $this->data->number, $e->getMessage()));
         $this->getContainer()->get('app')->close();
     }
     // Get a table object for the new record to process in the event listeners
     $table = (new IssuesTable($this->db))->load($model->getState()->get('issue_id'));
     $this->triggerEvent('onPullAfterCreate', $table, array('action' => $action));
     // Pull the user's avatar if it does not exist
     $this->pullUserAvatar($this->data->user->login);
     // Add a reopen record to the activity table if the action is reopened
     if ($action == 'reopened') {
         $this->addActivityEvent('reopen', $data['modified_date'], $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', $data['closed_date'], $this->hookData->sender->login, $this->project->project_id, $this->data->number);
     }
     // Store was successful, update status
     $this->logger->info(sprintf('Added 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;
 }
Пример #13
0
 /**
  * Prepare the response.
  *
  * @return  void
  *
  * @since   1.0
  */
 protected function prepareResponse()
 {
     // Load the application
     $application = $this->getContainer()->get('app');
     // Load the model
     $model = new IssuesModel($this->getContainer()->get('db'), $application->input);
     // Get allowed user for view;
     $application->getUser()->authorize('view');
     // Set Current project;
     $model->setProject($application->getProject(true));
     // Set model state
     $this->setModelState($model);
     // Pagination
     $model->setPagination(new TrackerPagination(new Uri($application->get('uri.request'))));
     // Get list items
     $listItems = $model->getAjaxItems();
     // Get total pages
     $pagesTotal = $model->getPagination()->getPagesTotal();
     $currentPage = $model->getPagination()->getPageNo();
     // Render the label html for each item
     $renderer = new Renderer\TrackerExtension($this->getContainer());
     $issueModel = new IssueModel($this->getContainer()->get('db'));
     foreach ($listItems as $item) {
         $item->labelHtml = $renderer->renderLabels($item->labels);
         $item->opened_date = date('Y-m-d', strtotime($item->opened_date));
         $item->modified_date = date('Y-m-d', strtotime($item->modified_date));
         $item->closed_date = date('Y-m-d', strtotime($item->closed_date));
         $item->categories = $issueModel->getCategories($item->id);
     }
     // Prepare the response.
     $items = array('items' => $listItems, 'pagesTotal' => $pagesTotal, 'currentPage' => $currentPage);
     $this->response->data = (object) $items;
 }
Пример #14
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();
 }
Пример #15
0
 /**
  * Execute the controller.
  *
  * @return  void
  *
  * @since   1.0
  * @throws  \RuntimeException
  */
 public function execute()
 {
     /* @type \JTracker\Application $application */
     $application = $this->getContainer()->get('app');
     $user = $application->getUser();
     $user->authorize('create');
     /* @type \Joomla\Github\Github $gitHub */
     $gitHub = $this->getContainer()->get('gitHub');
     $project = $application->getProject();
     $body = $application->input->get('body', '', 'raw');
     if (!$body) {
         throw new \RuntimeException('No body received.');
     }
     // Prepare issue for the store
     $data = array();
     $data['title'] = $application->input->getString('title');
     $data['milestone_id'] = $application->input->getInt('milestone_id');
     // Process labels
     $labels = [];
     foreach ($application->input->get('labels', [], 'array') as $labelId) {
         // Filter integer
         $labels[] = (int) $labelId;
     }
     /**
      * Store the "No code attached yet" label for CMS issue
      * @todo Remove after #596 is implemented
      */
     if ($project->project_id == 1 && !in_array(39, $labels)) {
         $labels[] = 39;
     }
     $data['labels'] = implode(',', $labels);
     $issueModel = new IssueModel($this->getContainer()->get('db'));
     $issueModel->setProject($project);
     // Project is managed on GitHub
     if ($project->gh_user && $project->gh_project) {
         // @todo assignee
         $assignee = null;
         // Prepare labels
         $ghLabels = [];
         if (!empty($labels)) {
             foreach ($project->getLabels() as $id => $label) {
                 if (in_array($id, $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;
                 }
             }
         }
         $gitHubResponse = $this->updateGitHub($data['title'], $body, $assignee, $ghMilestone, $ghLabels);
         $data['opened_date'] = $gitHubResponse->created_at;
         $data['modified_date'] = $gitHubResponse->created_at;
         $data['opened_by'] = $gitHubResponse->user->login;
         $data['modified_by'] = $gitHubResponse->user->login;
         $data['number'] = $gitHubResponse->number;
         $data['description'] = $gitHub->markdown->render($body, 'gfm', $project->gh_user . '/' . $project->gh_project);
     } else {
         $data['opened_date'] = (new Date())->format($this->getContainer()->get('db')->getDateFormat());
         $data['modified_date'] = (new Date())->format($this->getContainer()->get('db')->getDateFormat());
         $data['opened_by'] = $user->username;
         $data['modified_by'] = $user->username;
         $data['number'] = $issueModel->getNextNumber();
         $data['description'] = $gitHub->markdown->render($body, 'markdown');
     }
     $data['priority'] = $application->input->getInt('priority');
     $data['build'] = $application->input->getString('build');
     $data['project_id'] = $project->project_id;
     $data['issue_number'] = $data['number'];
     $data['description_raw'] = $body;
     // Store the issue
     try {
         // Save the issues and Get the issue id from model state
         $issue_id = $issueModel->add($data)->getState()->get('issue_id');
         // Save the category for the issue
         $category['issue_id'] = $issue_id;
         $category['categories'] = $application->input->get('categories', null, 'array');
         $categoryModel = new CategoryModel($this->getContainer()->get('db'));
         $categoryModel->saveCategory($category);
     } catch (\RuntimeException $e) {
         $application->enqueueMessage($e->getMessage(), 'error');
         $application->redirect($application->get('uri.base.path') . 'tracker/' . $project->alias . '/add');
     }
     $application->enqueueMessage(g11n3t('Your report has been submitted.'), 'success');
     $application->redirect($application->get('uri.base.path') . 'tracker/' . $project->alias . '/' . $data['number']);
     return;
 }
Пример #16
0
 /**
  * Execute the controller.
  *
  * @return  void
  *
  * @since   1.0
  * @throws  \Exception
  */
 public function execute()
 {
     /* @type \JTracker\Application $application */
     $application = $this->getContainer()->get('app');
     $user = $application->getUser();
     $user->authorize('create');
     /* @type \Joomla\Github\Github $gitHub */
     $gitHub = $this->getContainer()->get('gitHub');
     $project = $application->getProject();
     $body = $application->input->get('body', '', 'raw');
     // Prepare issue for the store
     $data = array();
     $data['title'] = $application->input->getString('title');
     if (!$body) {
         throw new \Exception('No body received.');
     }
     $issueModel = new IssueModel($this->getContainer()->get('db'));
     $issueModel->setProject($project);
     if ($project->gh_user && $project->gh_project) {
         // Project is managed on GitHub
         $gitHubResponse = $gitHub->issues->create($project->gh_user, $project->gh_project, $data['title'], $body);
         if (!isset($gitHubResponse->id)) {
             throw new \Exception('Invalid response from GitHub');
         }
         $data['opened_date'] = $gitHubResponse->created_at;
         $data['modified_date'] = $gitHubResponse->created_at;
         $data['opened_by'] = $gitHubResponse->user->login;
         $data['modified_by'] = $gitHubResponse->user->login;
         $data['number'] = $gitHubResponse->number;
         $data['description'] = $gitHub->markdown->render($body, 'gfm', $project->gh_user . '/' . $project->gh_project);
     } else {
         // Project is managed by JTracker only
         $data['opened_date'] = (new Date())->format($this->getContainer()->get('db')->getDateFormat());
         $data['modified_date'] = (new Date())->format($this->getContainer()->get('db')->getDateFormat());
         $data['opened_by'] = $user->username;
         $data['modified_by'] = $user->username;
         $data['number'] = $issueModel->getNextNumber();
         $data['description'] = $gitHub->markdown->render($body, 'markdown');
     }
     $data['priority'] = $application->input->getInt('priority');
     $data['build'] = $application->input->getString('build');
     $data['project_id'] = $project->project_id;
     $data['issue_number'] = $data['number'];
     $data['description_raw'] = $body;
     // Store the issue
     try {
         // Save the issues and Get the issue id from model state
         $issue_id = $issueModel->add($data)->getState()->get('issue_id');
         // Save the category for the issue
         $category['issue_id'] = $issue_id;
         $category['categories'] = $application->input->get('categories', null, 'array');
         $categoryModel = new CategoryModel($this->getContainer()->get('db'));
         $categoryModel->saveCategory($category);
     } catch (\Exception $e) {
         $application->enqueueMessage($e->getMessage(), 'error');
         $application->redirect($application->get('uri.base.path') . 'tracker/' . $project->alias . '/add');
     }
     $application->enqueueMessage(g11n3t('Your report has been submitted.'), 'success');
     $application->redirect($application->get('uri.base.path') . 'tracker/' . $project->alias . '/' . $data['number']);
     return;
 }