From f9eb643651fab60af5fef613ea7f640a0269df1a Mon Sep 17 00:00:00 2001 From: Yonas Habteab Date: Wed, 27 Sep 2023 11:27:45 +0200 Subject: [PATCH] Transform compat `action` & `notes` url macros automatically --- library/Icingadb/Common/Macros.php | 32 +++++++++++++++++-- .../Icingadb/Widget/Detail/ObjectDetail.php | 11 +++++++ phpstan-baseline.neon | 10 ++++++ 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/library/Icingadb/Common/Macros.php b/library/Icingadb/Common/Macros.php index 733c116ef..b9f219de3 100644 --- a/library/Icingadb/Common/Macros.php +++ b/library/Icingadb/Common/Macros.php @@ -4,11 +4,16 @@ namespace Icinga\Module\Icingadb\Common; +use Exception; +use Icinga\Application\Icinga; use Icinga\Application\Logger; +use Icinga\Module\Icingadb\Compat\CompatHost; +use Icinga\Module\Icingadb\Compat\CompatService; use Icinga\Module\Icingadb\Model\Host; use ipl\Orm\Model; use ipl\Orm\Query; use ipl\Orm\ResultSet; +use OutOfBoundsException; use function ipl\Stdlib\get_php_type; @@ -99,9 +104,22 @@ public function resolveMacro(string $macro, Model $object): string } else { $value = $object->$macro; } - } catch (\Exception $e) { + } catch (Exception $e) { $value = null; - Logger::debug('Unable to resolve macro "%s". An error occurred: %s', $macro, $e); + $monitoringInstalled = Icinga::app()->getModuleManager()->hasInstalled('monitoring'); + if ($monitoringInstalled && $e instanceof OutOfBoundsException) { + $compatObj = $this->getCompatObject(); + if ($compatObj) { + try { + $value = $compatObj->$macro; + } catch (Exception $e) { + $value = null; + Logger::debug('Unable to resolve macro "%s". An error occurred: %s', $macro, $e); + } + } + } else { + Logger::debug('Unable to resolve macro "%s". An error occurred: %s', $macro, $e); + } } if ($value instanceof Query || $value instanceof ResultSet || is_array($value)) { @@ -115,4 +133,14 @@ public function resolveMacro(string $macro, Model $object): string return $value !== null ? $value : $macro; } + + /** + * Get the associated monitoring object + * + * @return CompatHost|CompatService|null + */ + protected function getCompatObject() + { + return null; + } } diff --git a/library/Icingadb/Widget/Detail/ObjectDetail.php b/library/Icingadb/Widget/Detail/ObjectDetail.php index c431a84bd..1f6ebabb4 100644 --- a/library/Icingadb/Widget/Detail/ObjectDetail.php +++ b/library/Icingadb/Widget/Detail/ObjectDetail.php @@ -20,6 +20,7 @@ use Icinga\Module\Icingadb\Common\Links; use Icinga\Module\Icingadb\Common\Macros; use Icinga\Module\Icingadb\Compat\CompatHost; +use Icinga\Module\Icingadb\Compat\CompatService; use Icinga\Module\Icingadb\Model\CustomvarFlat; use Icinga\Module\Icingadb\Web\Navigation\Action; use Icinga\Module\Icingadb\Widget\MarkdownText; @@ -87,6 +88,16 @@ protected function compatObject() return $this->compatObject; } + /** + * Get the associated monitoring object + * + * @return CompatHost|CompatService + */ + protected function getCompatObject() + { + return $this->compatObject(); + } + protected function createPrintHeader() { $info = [new HorizontalKeyValue(t('Name'), $this->object->name)]; diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index e2550b5d2..efd9f8849 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -5695,6 +5695,11 @@ parameters: count: 4 path: library/Icingadb/Web/Navigation/Action.php + - + message: "#^Dead catch \\- Exception is never thrown in the try block\\.$#" + count: 1 + path: library/Icingadb/Web/Navigation/Action.php + - message: "#^Method Icinga\\\\Module\\\\Icingadb\\\\Web\\\\Navigation\\\\Action\\:\\:parseDenylist\\(\\) should return ipl\\\\Stdlib\\\\Filter\\\\None but returns ipl\\\\Stdlib\\\\Filter\\\\Chain\\.$#" count: 1 @@ -6675,6 +6680,11 @@ parameters: count: 4 path: library/Icingadb/Widget/Detail/ObjectDetail.php + - + message: "#^Dead catch \\- Exception is never thrown in the try block\\.$#" + count: 1 + path: library/Icingadb/Widget/Detail/ObjectDetail.php + - message: "#^Method Icinga\\\\Module\\\\Icingadb\\\\Widget\\\\Detail\\\\ObjectDetail\\:\\:__construct\\(\\) has parameter \\$object with no type specified\\.$#" count: 1