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; } }
private function _fixTimestamps() { // Unlimited execution time set_time_limit(0); foreach (TBGScope::getAll() as $scope) { TBGContext::setScope($scope); // The first job is to work out the offsets that need applying $offsets = array('system', 'users'); $offsets['users'] = array(); $offsets['system'] = (int) TBGSettings::getGMToffset() * 3600; $settingstable = TBGSettingsTable::getTable(); $crit = $settingstable->getCriteria(); $crit->addWhere(TBGSettingsTable::NAME, 'timezone'); $crit->addWhere(TBGSettingsTable::MODULE, 'core'); $crit->addWhere(TBGSettingsTable::UID, 0, \b2db\Criteria::DB_NOT_EQUALS); $crit->addWhere(TBGSettingsTable::VALUE, 0, \b2db\Criteria::DB_NOT_EQUALS); $crit->addWhere(TBGSettingsTable::VALUE, 'sys', \b2db\Criteria::DB_NOT_EQUALS); $crit->addWhere(TBGSettingsTable::SCOPE, $scope->getID()); $res = $settingstable->doSelect($crit); if ($res instanceof \b2db\Resultset) { while ($user = $res->getNextRow()) { $offsets['users']['uid_' . $user->get(TBGSettingsTable::UID)] = (int) $user->get(TBGSettingsTable::VALUE) * 3600; } } // Now go through every thing which requires updating TBGContext::addAutoloaderClassPath(THEBUGGENIE_MODULES_PATH . 'publish' . DS . 'classes' . DS . 'B2DB'); TBGContext::addAutoloaderClassPath(THEBUGGENIE_MODULES_PATH . 'publish' . DS . 'classes'); // ARTICLE HISTORY $this->_fixUserDependentTimezone($offsets, TBGArticleHistoryTable::getTable(), TBGArticleHistoryTable::AUTHOR, TBGArticleHistoryTable::DATE, $scope); // ARTICLES $this->_fixUserDependentTimezone($offsets, TBGArticlesTable::getTable(), TBGArticlesTable::AUTHOR, TBGArticlesTable::DATE, $scope); // BUILDS $this->_fixNonUserDependentTimezone($offsets, TBGBuildsTable::getTable(), TBGBuildsTable::RELEASE_DATE, $scope, TBGBuildsTable::RELEASED); // COMMENTS $this->_fixUserDependentTimezone($offsets, TBGCommentsTable::getTable(), array('a' => TBGCommentsTable::POSTED_BY, 'b' => TBGCommentsTable::UPDATED_BY), array('a' => TBGCommentsTable::POSTED, 'b' => TBGCommentsTable::UPDATED), $scope); // EDITIONS $this->_fixNonUserDependentTimezone($offsets, TBGEditionsTable::getTable(), TBGEditionsTable::RELEASE_DATE, $scope, TBGEditionsTable::RELEASED); // ISSUES // This is a bit more complex so do this manually - we have to poke around with the issue log $table = TBGIssuesTable::getTable(); $crit = $table->getCriteria(); $crit->addWhere(TBGIssuesTable::SCOPE, $scope->getID()); $crit->addWhere(TBGIssuesTable::DELETED, false); $res = $table->doSelect($crit); if ($res) { while ($row = $res->getNextRow()) { $crit = TBGLogTable::getTable()->getCriteria(); $crit->addSelectionColumn(TBGLogTable::UID); $crit->addWhere(TBGLogTable::CHANGE_TYPE, TBGLogTable::LOG_ISSUE_ASSIGNED); $crit->addWhere(TBGLogTable::TARGET, $row->get(TBGIssuesTable::ID)); $crit->addWhere(TBGLogTable::TARGET_TYPE, TBGLogTable::TYPE_ISSUE); $crit->addOrderBy(TBGLogTable::TIME, b2db\Criteria::SORT_DESC); $crit->addOrderBy(TBGLogTable::ID, b2db\Criteria::SORT_DESC); if ($row2 = TBGLogTable::getTable()->doSelectOne($crit)) { $assigned_by = $row2->get(TBGLogTable::UID); } $crit = TBGLogTable::getTable()->getCriteria(); $crit->addSelectionColumn(TBGLogTable::UID); $crit->addWhere(TBGLogTable::TARGET, $row->get(TBGIssuesTable::ID)); $crit->addWhere(TBGLogTable::TARGET_TYPE, TBGLogTable::TYPE_ISSUE); $crit->addOrderBy(TBGLogTable::TIME, b2db\Criteria::SORT_DESC); $crit->addOrderBy(TBGLogTable::ID, b2db\Criteria::SORT_DESC); if ($row2 = TBGLogTable::getTable()->doSelectOne($crit)) { $updated_by = $row2->get(TBGLogTable::UID); } unset($crit); unset($row2); if (array_key_exists('uid_' . $row->get(TBGIssuesTable::POSTED_BY), $offsets['users'])) { $offset = $offsets['users']['uid_' . $row->get(TBGIssuesTable::POSTED_BY)]; } else { $offset = $offsets['system']; } if (isset($updated_by) && array_key_exists('uid_' . $updated_by, $offsets['users'])) { $offset2 = $offsets['users']['uid_' . $updated_by]; } elseif (isset($updated_by)) { $offset2 = $offsets['system']; } if (isset($assigned_by) && array_key_exists('uid_' . $assigned_by, $offsets['users'])) { $offset3 = $offsets['users']['uid_' . $assigned_by]; } elseif (isset($assigned_by)) { $offset3 = $offsets['system']; } $crit2 = $table->getCriteria(); $crit2->addUpdate(TBGIssuesTable::POSTED, (int) $row->get(TBGIssuesTable::POSTED) + $offset); if (isset($offset2)) { $crit2->addUpdate(TBGIssuesTable::LAST_UPDATED, (int) $row->get(TBGIssuesTable::LAST_UPDATED) + $offset2); unset($offset2); } if (isset($offset3)) { $crit2->addUpdate(TBGIssuesTable::BEING_WORKED_ON_BY_USER_SINCE, (int) $row->get(TBGIssuesTable::BEING_WORKED_ON_BY_USER_SINCE) + $offset3); unset($offset3); } $crit2->addWhere(TBGIssuesTable::ID, $row->get(TBGIssuesTable::ID)); $table->doUpdate($crit2); } } // LOG $this->_fixUserDependentTimezone($offsets, TBGLogTable::getTable(), TBGLogTable::UID, TBGLogTable::TIME, $scope); // MILESTONES // The conditions are a bit different here so do it manually $table = TBGMilestonesTable::getTable(); $crit = $table->getCriteria(); $crit->addWhere(TBGMilestonesTable::SCOPE, $scope->getID()); $res = $table->doSelect($crit); if ($res) { while ($row = $res->getNextRow()) { $offset = $offsets['system']; $crit2 = $table->getCriteria(); $added = 0; if ($row->get(TBGMilestonesTable::REACHED) > 0) { $crit2->addUpdate(TBGMilestonesTable::REACHED, (int) $row->get(TBGMilestonesTable::REACHED) + $offset); $added = 1; } if ($row->get(TBGMilestonesTable::SCHEDULED) > 0) { $crit2->addUpdate(TBGMilestonesTable::SCHEDULED, (int) $row->get(TBGMilestonesTable::SCHEDULED) + $offset); $added = 1; } if ($row->get(TBGMilestonesTable::STARTING) > 0) { $crit2->addUpdate(TBGMilestonesTable::STARTING, (int) $row->get(TBGMilestonesTable::STARTING) + $offset); $added = 1; } // Only do something if at least one call to addUpdate is done if ($added == 1) { $crit2->addWhere(TBGMilestonesTable::ID, $row->get(TBGMilestonesTable::ID)); $table->doUpdate($crit2); } } } // PROJECTS $this->_fixNonUserDependentTimezone($offsets, TBGProjectsTable::getTable(), TBGProjectsTable::RELEASE_DATE, $scope, TBGProjectsTable::RELEASED); // VCS INTEGRATION // check if module is loaded $modules = TBGModulesTable::getTable()->getModulesForScope($scope->getID()); if ($modules['vcs_integration'] == true) { TBGContext::addAutoloaderClassPath(THEBUGGENIE_MODULES_PATH . 'vcs_integration' . DS . 'classes' . DS . 'B2DB'); TBGContext::addAutoloaderClassPath(THEBUGGENIE_MODULES_PATH . 'vcs_integration' . DS . 'classes'); $this->_fixUserDependentTimezone($offsets, TBGVCSIntegrationTable::getTable(), TBGVCSIntegrationTable::AUTHOR, TBGVCSIntegrationTable::DATE, $scope); } } }
public function listen_viewissue_panel(TBGEvent $event) { $web_path = $this->getSetting('web_path_' . $event->getSubject()->getProject()->getID()); $web_repo = $this->getSetting('web_repo_' . $event->getSubject()->getProject()->getID()); if (empty($web_repo) || empty($web_path)) { return; } $data = TBGVCSIntegrationTable::getTable()->getCommitsByIssue($event->getSubject()->getId()); if (!is_array($data)) { TBGActionComponent::includeTemplate('vcs_integration/viewissue_commits_top', array('items' => false)); } else { TBGActionComponent::includeTemplate('vcs_integration/viewissue_commits_top', array('items' => true)); /* Now produce each box */ foreach ($data as $revno => $entry) { $revision = $revno; /* Build correct URLs */ switch ($this->getSetting('web_type_' . $event->getSubject()->getProject()->getID())) { case 'viewvc': $link_rev = $web_path . '/' . '?root=' . $web_repo . '&view=rev&revision=' . $revision; break; case 'viewvc_repo': $link_rev = $web_path . '/' . '?view=rev&revision=' . $revision; break; case 'websvn': $link_rev = $web_path . '/revision.php?repname=' . $web_repo . '&isdir=1&rev=' . $revision; break; case 'websvn_mv': $link_rev = $web_path . '/' . '?repname=' . $web_repo . '&op=log&isdir=1&rev=' . $revision; break; case 'loggerhead': $link_rev = $web_path . '/' . $web_repo . '/revision/' . $revision; break; case 'gitweb': $link_rev = $web_path . '/' . '?p=' . $web_repo . ';a=commitdiff;h=' . $revision; break; case 'cgit': $link_rev = $web_path . '/' . $web_repo . '/commit/?id=' . $revision; break; case 'hgweb': $link_rev = $web_path . '/' . $web_repo . '/rev/' . $revision; break; case 'github': $link_rev = 'http://github.com/' . $web_repo . '/commit/' . $revision; break; case 'gitorious': $link_rev = $web_path . '/' . $web_repo . '/commit/' . $revision; break; } /* Now we have everything, render the template */ include_template('vcs_integration/commitbox', array("projectId" => $event->getSubject()->getProject()->getID(), "id" => $entry[0][0], "revision" => $revision, "author" => $entry[0][1], "date" => $entry[0][2], "log" => $entry[0][3], "files" => $entry[1])); } TBGActionComponent::includeTemplate('vcs_integration/viewissue_commits_bottom'); } }