Skip to content

Commit

Permalink
Transform compat action & notes url macros automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
yhabteab committed Sep 27, 2023
1 parent cd41a20 commit f9eb643
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
32 changes: 30 additions & 2 deletions library/Icingadb/Common/Macros.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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)) {
Expand All @@ -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;
}
}
11 changes: 11 additions & 0 deletions library/Icingadb/Widget/Detail/ObjectDetail.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)];
Expand Down
10 changes: 10 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f9eb643

Please sign in to comment.