/**
  * Return all issues for a given commit
  * @param TBGVCSIntegrationCommit $commit
  * @return array
  */
 public static function getIssuesByCommit(TBGVCSIntegrationCommit $commit)
 {
     $rows = TBGVCSIntegrationIssueLinksTable::getTable()->getByCommitID($commit->getID());
     $data = array();
     if (!is_object($rows)) {
         return false;
     }
     foreach ($rows->getAllRows() as $row) {
         $data[] = TBGContext::factory()->TBGVCSIntegrationIssueLink($row->get(TBGVCSIntegrationIssueLinksTable::ID), $row);
     }
     return $data;
 }
 private function _populateAffectedIssues()
 {
     if ($this->_issues == null) {
         $this->_issues = array();
         $res = TBGVCSIntegrationIssueLinksTable::getTable()->getByCommitID($this->_id);
         if ($res instanceof Resultset) {
             foreach ($res->getAllRows() as $row) {
                 $this->_issues[] = TBGContext::factory()->TBGIssue($row->get(TBGVCSIntegrationIssueLinksTable::ISSUE_NO));
             }
         }
     }
 }
 protected function _upgrade()
 {
     switch ($this->_version) {
         case "1.0":
             // Upgrade tables
             \b2db\Core::getTable('TBGVCSIntegrationCommitsTable')->create();
             \b2db\Core::getTable('TBGVCSIntegrationFilesTable')->create();
             \b2db\Core::getTable('TBGVCSIntegrationIssueLinksTable')->create();
             TBGVCSIntegrationCommitsTable::getTable()->createIndexes();
             TBGVCSIntegrationFilesTable::getTable()->createIndexes();
             TBGVCSIntegrationIssueLinksTable::getTable()->createIndexes();
             // Migrate data from old table to new tables
             $crit = new \b2db\Criteria();
             $crit->addOrderBy(TBGVCSIntegrationTable::DATE, \b2db\Criteria::SORT_DESC);
             $results = TBGVCSIntegrationTable::getTable()->doSelect($crit);
             if ($results instanceof \b2db\Resultset && $results->count() > 0) {
                 $commits = array();
                 while ($row = $results->getNextRow()) {
                     $rev = $row->get(TBGVCSIntegrationTable::NEW_REV);
                     if (array_key_exists($rev, $commits)) {
                         // Add a new file or issue to the commit data
                         $commits[$rev]['files'][$row->get(TBGVCSIntegrationTable::FILE_NAME)] = array('file_name' => $row->get(TBGVCSIntegrationTable::FILE_NAME), 'action' => $row->get(TBGVCSIntegrationTable::ACTION));
                         $commits[$rev]['issues'][$row->get(TBGVCSIntegrationTable::ISSUE_NO)] = $row->get(TBGVCSIntegrationTable::ISSUE_NO);
                     } else {
                         // All issues will be of the same project, so use one issue
                         $issue = TBGContext::factory()->TBGIssue($results->get(TBGVCSIntegrationTable::ISSUE_NO));
                         // Add details of a new commit
                         $commits[$rev] = array('commit' => array(), 'files' => array(), 'issues' => array());
                         $commits[$rev]['commit'] = array('new_rev' => $rev, 'old_rev' => $row->get(TBGVCSIntegrationTable::OLD_REV), 'author' => $row->get(TBGVCSIntegrationTable::AUTHOR), 'date' => $row->get(TBGVCSIntegrationTable::DATE), 'log' => $row->get(TBGVCSIntegrationTable::LOG), 'scope' => $row->get(TBGVCSIntegrationTable::SCOPE), 'project' => $issue->getProject());
                         $commits[$rev]['files'][$row->get(TBGVCSIntegrationTable::FILE_NAME)] = array('file_name' => $row->get(TBGVCSIntegrationTable::FILE_NAME), 'action' => $row->get(TBGVCSIntegrationTable::ACTION));
                         $commits[$rev]['issues'][$row->get(TBGVCSIntegrationTable::ISSUE_NO)] = $row->get(TBGVCSIntegrationTable::ISSUE_NO);
                     }
                 }
                 foreach ($commits as $commit) {
                     $files = array();
                     $issues = array();
                     $scope = TBGContext::factory()->TBGScope($commit['commit']['scope']);
                     try {
                         $author = TBGContext::factory()->TBGUser($commit['commit']['author']);
                     } catch (Exception $e) {
                         $author = TBGContext::factory()->TBGUser(TBGSettings::getDefaultUserID());
                     }
                     if (!$author instanceof TBGUser) {
                         $author = TBGContext::factory()->TBGUser(TBGSettings::getDefaultUserID());
                     }
                     // Add the commit
                     $inst = new TBGVCSIntegrationCommit();
                     $inst->setAuthor($author);
                     $inst->setDate($commit['commit']['date']);
                     $inst->setLog($commit['commit']['log']);
                     $inst->setPreviousRevision($commit['commit']['old_rev']);
                     $inst->setRevision($commit['commit']['new_rev']);
                     $inst->setProject($commit['commit']['project']);
                     $inst->setScope($scope);
                     $inst->save();
                     // Process issue list, remove duplicates
                     $issues = $commit['issues'];
                     $files = $commit['files'];
                     $commit = $inst;
                     foreach ($files as $file) {
                         // Add affected files
                         $inst = new TBGVCSIntegrationFile();
                         $inst->setCommit($commit);
                         $inst->setFile($file['file_name']);
                         $inst->setAction($file['action']);
                         $inst->setScope($scope);
                         $inst->save();
                     }
                     foreach ($issues as $issue) {
                         // Add affected issues
                         $issue = TBGContext::factory()->TBGIssue($issue);
                         $inst = new TBGVCSIntegrationIssueLink();
                         $inst->setIssue($issue);
                         $inst->setCommit($commit);
                         $inst->setScope($scope);
                         $inst->save();
                     }
                 }
             }
             // Migrate settings to new format
             $access_method = $this->getSetting('use_web_interface');
             $passkey = $this->getSetting('vcs_passkey');
             foreach (TBGProject::getAll() as $project) {
                 $projectId = $project->getID();
                 $web_path = $this->getSetting('web_path_' . $projectId);
                 $web_repo = $this->getSetting('web_repo_' . $projectId);
                 // Check if enabled
                 if ($web_path == '') {
                     continue;
                 }
                 switch ($this->getSetting('web_type_' . $projectId)) {
                     case 'viewvc':
                         $base_url = $web_path . '/' . '?root=' . $web_repo;
                         $link_rev = '&view=rev&revision=%revno';
                         $link_file = '&view=log';
                         $link_diff = '&r1=%revno&r2=%oldrev';
                         $link_view = '&revision=%revno&view=markup';
                         break;
                     case 'viewvc_repo':
                         $base_url = $web_path;
                         $link_rev = '/?view=rev&revision=%revno';
                         $link_file = '/%file?view=log';
                         $link_diff = '/%file?r1=%revno&r2=%oldrev';
                         $link_view = '/%file?revision=%revno&view=markup';
                         break;
                     case 'websvn':
                         $base_url = $web_path;
                         $link_rev = '/revision.php?repname=' . $web_repo . '&isdir=1&rev=%revno';
                         $link_file = '/log.php?repname=' . $web_repo . '&path=/$%file';
                         $link_diff = '/comp.php?repname=' . $web_repo . '&compare[]=/%file@%revno&compare[]=/%file@%oldrev';
                         $link_view = '/filedetails.php?repname=' . $web_repo . '&path=/%file&rev=%revno';
                         break;
                     case 'websvn_mv':
                         $base_url = $web_path;
                         $link_rev = '/' . '?repname=' . $web_repo . '&op=log&isdir=1&rev=%revno';
                         $link_file = '/%file?repname=' . $web_repo;
                         $link_diff = '/%file?repname=' . $web_repo . '&compare[]=/%file@%revno&compare[]=/%file@%oldrev';
                         $link_view = '/%file?repname=' . $web_repo . '&rev=%revno';
                         break;
                     case 'loggerhead':
                         $base_url = $web_path . '/' . $web_repo;
                         $link_rev = '/revision/%revno';
                         $link_file = '/changes';
                         $link_diff = '/revision/%revno?compare_revid=%oldrev';
                         $link_view = '/annotate/head:/%file';
                         break;
                     case 'gitweb':
                         $base_url = $web_path . '/' . '?p=' . $web_repo;
                         $link_rev = ';a=commitdiff;h=%revno';
                         $link_file = ';a=history;f=%file;hb=HEAD';
                         $link_diff = ';a=blobdiff;f=%file;hb=%revno;hpb=%oldrev';
                         $link_view = ';a=blob;f=%file;hb=%revno';
                         break;
                     case 'cgit':
                         $base_url = $web_path . '/' . $web_repo;
                         $link_rev = '/commit/?id=%revno';
                         $link_file = '/log';
                         $link_diff = '/diff/%file?id=%revno?id2=%oldrev';
                         $link_view = '/tree/%file?id=%revno';
                         break;
                     case 'hgweb':
                         $base_url = $web_path . '/' . $web_repo;
                         $link_rev = '/rev/%revno';
                         $link_file = '/log/tip/%file';
                         $link_diff = '/diff/%revno/%file';
                         $link_view = '/file/%revno/%file';
                         break;
                     case 'github':
                         $base_url = 'http://github.com/' . $web_repo;
                         $link_rev = '/commit/%revno';
                         $link_file = '/commits/master/%file';
                         $link_diff = '/commit/%revno';
                         $link_view = '/blob/%revno/%file';
                         break;
                     case 'gitlab':
                         $base_url = $web_path . '/' . $web_repo;
                         $link_rev = '/commit/%revno';
                         $link_file = '/commits/%branch/%file';
                         $link_diff = '/commit/%revno';
                         $link_view = '/blob/%revno/%file';
                         break;
                     case 'bitbucket':
                         $base_url = 'https://bitbucket.org/' . $web_repo;
                         $link_rev = '/changeset/%revno';
                         $link_file = '/history/%file';
                         $link_diff = '/changeset/%revno#chg-%file';
                         $link_view = '/src/%revno/%file';
                         break;
                     case 'gitorious':
                         $base_url = $web_path . '/' . $web_repo;
                         $link_rev = '/commit/%revno';
                         $link_file = '/blobs/history/master/%file';
                         $link_diff = '/commit/%revno';
                         $link_view = '/blobs/%revno/%file';
                         break;
                     case 'rhodecode':
                         $base_url = $web_path . '/' . $web_repo;
                         $link_rev = '/changeset/%revno';
                         $link_file = '/changelog/%revno/%file';
                         $link_diff = '/diff/%file?diff2=%revno&diff1=%oldrev&fulldiff=1&diff=diff';
                         $link_view = '/files/%revno/%file';
                         break;
                 }
                 $this->saveSetting('browser_url_' . $projectId, $base_url);
                 $this->saveSetting('log_url_' . $projectId, $link_file);
                 $this->saveSetting('blob_url_' . $projectId, $link_diff);
                 $this->saveSetting('diff_url_' . $projectId, $link_view);
                 $this->saveSetting('commit_url_' . $projectId, $link_rev);
                 // Access method
                 $this->saveSetting('access_method_' . $projectId, $access_method);
                 if ($access_method == self::ACCESS_HTTP) {
                     $this->saveSetting('access_passkey_' . $projectId, $passkey);
                 }
                 // Enable VCS Integration
                 $this->saveSetting('vcs_mode_' . $projectId, self::MODE_ISSUECOMMITS);
                 // Remove old settings
                 $this->deleteSetting('web_type_' . $projectId);
                 $this->deleteSetting('web_path_' . $projectId);
                 $this->deleteSetting('web_repo_' . $projectId);
             }
             // Remove old settings
             $this->deleteSetting('use_web_interface');
             $this->deleteSetting('vcs_passkey');
             // Upgrade module version
             $this->_version = $this->_module_version;
             $this->save();
             break;
     }
 }