diff --git a/administrator/components/com_patchtester/PatchTester/Controller/ResetController.php b/administrator/components/com_patchtester/PatchTester/Controller/ResetController.php index 7bb551f..c345768 100644 --- a/administrator/components/com_patchtester/PatchTester/Controller/ResetController.php +++ b/administrator/components/com_patchtester/PatchTester/Controller/ResetController.php @@ -13,6 +13,7 @@ use Joomla\CMS\Filesystem\Folder; use Joomla\CMS\Language\Text; use Joomla\CMS\Router\Route; use Joomla\Filesystem\File; +use PatchTester\Helper; use PatchTester\Model\PullModel; use PatchTester\Model\PullsModel; use PatchTester\Model\TestsModel; @@ -41,9 +42,23 @@ class ResetController extends AbstractController $pullsModel = new PullsModel($this->context, null, Factory::getDbo()); $testsModel = new TestsModel(null, Factory::getDbo()); + // Get the CIServer Registry + $ciSettings = Helper::initializeCISettings(); + + // Get PatchChain as array, remove any EOL set by php + $patchChainPath = $ciSettings->get('folder.backups') . '/' . $ciSettings->get('zip.chain.name'); + $patchChain = array_reverse(explode(PHP_EOL, file_get_contents($patchChainPath))); + // Check the applied patches in the database first $appliedPatches = $testsModel->getAppliedPatches(); + if (count($patchChain) && count($appliedPatches)) + { + // Get only the pull_id and remove all occurrences with patchChain + $appliedPatches = array_map(function($patch) { return $patch->pull_id; }, $appliedPatches); + $appliedPatches = array_diff($appliedPatches, $patchChain); + } + if (count($appliedPatches)) { $revertErrored = false; @@ -53,32 +68,50 @@ class ResetController extends AbstractController { try { - $pullModel->revert($patch->id); + $pullModel->revertWithGitHub($patch); } catch (\RuntimeException $e) { $revertErrored = true; } } + } - // If we errored out reverting patches, we'll need to truncate the table - if ($revertErrored) + if (count($patchChain)) + { + $revertErrored = false; + + // Let's try to cleanly revert all applied patches with ci + foreach ($patchChain as $patch) { try { - $testsModel->truncateTable(); + $pullModel->revertWithCIServer($patch); } catch (\RuntimeException $e) { - $hasErrors = true; - - $this->getApplication()->enqueueMessage( - Text::sprintf('COM_PATCHTESTER_ERROR_TRUNCATING_PULLS_TABLE', $e->getMessage()), 'error' - ); + $revertErrored = true; } } } + // If we errored out reverting patches, we'll need to truncate the table + if ($revertErrored) + { + try + { + $testsModel->truncateTable(); + } + catch (\RuntimeException $e) + { + $hasErrors = true; + + $this->getApplication()->enqueueMessage( + Text::sprintf('COM_PATCHTESTER_ERROR_TRUNCATING_PULLS_TABLE', $e->getMessage()), 'error' + ); + } + } + // Now truncate the pulls table try { diff --git a/administrator/components/com_patchtester/PatchTester/Model/PullModel.php b/administrator/components/com_patchtester/PatchTester/Model/PullModel.php index bab5052..c30d782 100644 --- a/administrator/components/com_patchtester/PatchTester/Model/PullModel.php +++ b/administrator/components/com_patchtester/PatchTester/Model/PullModel.php @@ -615,7 +615,7 @@ class PullModel extends AbstractModel * @since 3.0 * @throws \RuntimeException */ - private function revertWithCIServer($id) + public function revertWithCIServer($id) { // Get the CIServer Registry $ciSettings = Helper::initializeCISettings(); @@ -714,7 +714,7 @@ class PullModel extends AbstractModel * @since 2.0 * @throws \RuntimeException */ - private function revertWithGitHub($id) + public function revertWithGitHub($id) { $testRecord = $this->getTestRecord($id); @@ -861,7 +861,7 @@ class PullModel extends AbstractModel $db->getQuery(true) ->select('*') ->from('#__patchtester_tests') - ->where('id = ' . (int) $id) + ->where('pull_id = ' . (int) $id) )->loadObject(); } } diff --git a/administrator/components/com_patchtester/PatchTester/View/Pulls/tmpl/default_items.php b/administrator/components/com_patchtester/PatchTester/View/Pulls/tmpl/default_items.php index f829d66..5c334c2 100644 --- a/administrator/components/com_patchtester/PatchTester/View/Pulls/tmpl/default_items.php +++ b/administrator/components/com_patchtester/PatchTester/View/Pulls/tmpl/default_items.php @@ -63,7 +63,7 @@ foreach ($this->items as $i => $item) : applied) : ?> - +