Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regulate object links based on backend used #128

Merged
merged 4 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion application/clicommands/SendCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public function problemAction()
if ($ackPipe) {
$cmd = new LegacyCommandPipe($ackPipe);
} else {
$cmd = (new IcingaCommandPipe())->setMonitoringInfo($info);
$cmd = new IcingaCommandPipe($info);
}
if ($cmd->acknowledge($ackAuthor, $ackMessage, $host, $service)) {
Logger::info("Problem has been acknowledged for $key");
Expand Down
25 changes: 6 additions & 19 deletions library/Jira/IcingaCommandPipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Icinga\Module\Jira;

use Icinga\Application\Modules\Module;
use Icinga\Module\Jira\ProvidedHook\Icingadb\IcingadbSupport;
use Icinga\Module\Monitoring\Command\Object\AcknowledgeProblemCommand;
use Icinga\Module\Monitoring\Command\Transport\CommandTransport;
use Icinga\Module\Icingadb\Command\Transport\CommandTransport as IcingadbCommandTransport;
Expand All @@ -13,20 +11,16 @@

class IcingaCommandPipe
{
/** @var MonitoringInfo */
private $monitoringInfo;

public function acknowledge($author, $message, $host, $service = null)
public function __construct(MonitoringInfo $monitoringInfo)
{
if ($this->monitoringInfo === null) {
if (Module::exists('icingadb') && IcingadbSupport::useIcingaDbAsBackend()) {
$backend = new IcingadbBackend();
} else {
$backend = new IdoBackend();
}

$this->setMonitoringInfo($backend->getMonitoringInfo($host, $service));
}
$this->monitoringInfo = $monitoringInfo;
}

public function acknowledge($author, $message, $host, $service = null)
{
if (! $this->monitoringInfo->hasObject()) {
if ($service !== null) {
throw new IcingaException(
Expand Down Expand Up @@ -57,13 +51,6 @@ public function acknowledge($author, $message, $host, $service = null)
return true;
}

public function setMonitoringInfo(MonitoringInfo $info)
{
$this->monitoringInfo = $info;

return $this;
}

protected function getAcknowledgeProblemCommand()
{
if ($this->monitoringInfo->getObject() instanceof MonitoredObject) {
Expand Down
2 changes: 1 addition & 1 deletion library/Jira/Web/Form/NewIssueForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ protected function eventuallyAcknowledge($key, $host, $service)
$ackMessage = "Jira issue $key has been created";

try {
$cmd = new IcingaCommandPipe();
$cmd = new IcingaCommandPipe($this->monitoringInfo);
if ($cmd->acknowledge('Jira', $ackMessage, $host, $service)) {
Logger::info("Problem has been acknowledged for $key");
}
Expand Down
213 changes: 179 additions & 34 deletions library/Jira/Web/RenderingHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
use Icinga\Date\DateFormatter;
use Icinga\Module\Jira\ProvidedHook\Icingadb\IcingadbSupport;
use Icinga\Module\Jira\RestApi;
use ipl\Html\Attributes;
use ipl\Html\Html;
use ipl\Html\HtmlElement;
use ipl\Html\HtmlString;
use ipl\Web\Url;
use ipl\Web\Widget\Icon;
use ipl\Web\Widget\Link;
Expand All @@ -17,49 +20,191 @@
{
protected $api;

public function linkToMonitoring($host, $service)
/** @var ?string Host name from the icingaKey JIRA ticket field */
protected $hostName;

/** @var ?string Service name from the icingaKey JIRA ticket field */
protected $serviceName;

/** @var ?Link Host link */
protected $hostLink;

/** @var ?Link Service link */
protected $serviceLink;

/**
* Set the name of monitored host
*
* @param string $hostName
*
* @return $this
*/
public function setHostName(string $hostName): self
{
if ($service === null) {
return $this->linkToMonitoringHost($host);
} else {
return $this->linkToMonitoringService($host, $service);
}
$this->hostName = $hostName;

return $this;
}

public function linkToMonitoringHost($host)

/**
* Set the name of monitored service
*
* @param string $serviceName
*
* @return $this
*/
public function setServiceName(string $serviceName): self
{
if (Module::exists('icingadb') && IcingadbSupport::useIcingaDbAsBackend()) {
return new Link([new Icon('server'), $host], Url::fromPath('icingadb/host', [
'name' => $host
]), [
'title' => t('Show Icinga Host State'),
]);
}
$this->serviceName = $serviceName;

return new Link([new Icon('laptop'), $host], Url::fromPath('monitoring/host/show', [
'host' => $host
]), [
'title' => t('Show Icinga Host State'),
]);
return $this;
}

public function linkToMonitoringService($host, $service)
/**
* Set the link of monitored host
*
* @param Link $hostLink
*
* @return $this
*/
public function setHostLink(Link $hostLink): self
{
if (Module::exists('icingadb') && IcingadbSupport::useIcingaDbAsBackend()) {
return new Link([new Icon('cog'), $service], Url::fromPath('icingadb/service', [
'name' => $service,
'host.name' => $host,
]), [
'title' => t('Show Icinga Service State'),
]);
}
$this->hostLink = $hostLink;

return new Link([new Icon('cog'), $service], Url::fromPath('monitoring/service/show', [
'host' => $host,
'service' => $service,
]), [
'title' => t('Show Icinga Service State'),
]);
return $this;
}

/**
* Set the link of monitored service
*
* @param Link $serviceLink
*
* @return $this
*/
public function setServiceLink(Link $serviceLink): self
{
$this->serviceLink = $serviceLink;

return $this;
}

/**
* Get the link of monitored host
*
* @return ?Link
*/
public function getHostLink(): ?Link
{
return $this->hostLink;
}

/**
* Get the link of monitored service
*
* @return ?Link
*/
public function getServiceLink(): ?Link
{
return $this->serviceLink;
}


/**
* Get the formatted issue comment using author, time and description or comment body
*
* @param string|HtmlElement[] $author
* @param string $time
* @param string $body
*
* @return HtmlElement[]
*/
public function getIssueComment($author, string $time, string $body): array
{
return [
new HtmlElement('h3', null, Html::sprintf('%s: %s', $this->shortTimeSince($time), $author)),
new HtmlElement('pre', new Attributes(['class' => 'comment']), $this->formatBody($body)),
];
}

/**
* Format the given issue description or comment body
*
* @param string $body
*
* @return HtmlString
*/
public function formatBody(string $body): HtmlString
{
// Replace object urls in the given string with link elements
$body = preg_replace_callback('/\[([^|]+)\|([^]]+)]/', function ($match) {
$url = Url::fromPath($match[2]);
$link = new Link($match[1], $url);
$urlPath = $url->getPath();

$monitoringObjectLink = in_array($urlPath, ['monitoring/host/show', 'monitoring/service/show']);
$icingadbObjectLink = in_array($urlPath, ['icingadb/host', 'icingadb/service']);

if ($monitoringObjectLink || $icingadbObjectLink) {
$transformToIcingadbLink = $monitoringObjectLink
&& Module::exists('icingadb')
&& IcingadbSupport::useIcingaDbAsBackend();
if (strpos($urlPath, '/service') !== false) {
if ($monitoringObjectLink) {
$urlServiceParam = $url->getParam('service');
$urlHostParam = $url->getParam('host');
if ($transformToIcingadbLink) {
$url->setPath('icingadb/service')
->remove(['service', 'host'])
->overwriteParams(['name' => $urlServiceParam, 'host.name' => $urlHostParam]);
$link->setUrl($url);
}
} else {
$urlServiceParam = $url->getParam('name');
$urlHostParam = $url->getParam('host.name');
}

if (
! $this->serviceLink
&& $urlServiceParam === $this->serviceName
&& $urlHostParam === $this->hostName
) {
$serviceLink = clone $link;
$serviceLink->setContent([new Icon('cog'), $match[1]])
->addAttributes(['title' => t('Show Icinga Service State')]);
$this->setServiceLink($serviceLink);
}
} else {
$icon = new Icon('server');
if ($monitoringObjectLink) {
$urlHostParam = $url->getParam('host');
if ($transformToIcingadbLink) {
$url->setPath('icingadb/host')
->remove('host')
->setParam('name', $urlHostParam);

Check failure on line 184 in library/Jira/Web/RenderingHelper.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Parameter #2 $value of method Icinga\Web\Url::setParam() expects array|bool|string, mixed given.

Check failure on line 184 in library/Jira/Web/RenderingHelper.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Parameter #2 $value of method Icinga\Web\Url::setParam() expects array|bool|string, mixed given.

Check failure on line 184 in library/Jira/Web/RenderingHelper.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.4 on ubuntu-latest

Parameter #2 $value of method Icinga\Web\Url::setParam() expects array|bool|string, mixed given.

Check failure on line 184 in library/Jira/Web/RenderingHelper.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.0 on ubuntu-latest

Parameter #2 $value of method Icinga\Web\Url::setParam() expects array|bool|string, mixed given.

Check failure on line 184 in library/Jira/Web/RenderingHelper.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.1 on ubuntu-latest

Parameter #2 $value of method Icinga\Web\Url::setParam() expects array|bool|string, mixed given.

Check failure on line 184 in library/Jira/Web/RenderingHelper.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.2 on ubuntu-latest

Parameter #2 $value of method Icinga\Web\Url::setParam() expects array|bool|string, mixed given.

Check failure on line 184 in library/Jira/Web/RenderingHelper.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.3 on ubuntu-latest

Parameter #2 $value of method Icinga\Web\Url::setParam() expects array|bool|string, mixed given.
$link->setUrl($url);
} else {
$icon = new Icon('laptop');
}
} else {
$urlHostParam = $url->getParam('name');
}

if (! $this-> hostLink && $urlHostParam === $this->hostName) {
$hostLink = clone $link;
$hostLink->setContent([$icon, $match[1]])
->addAttributes(['title' => t('Show Icinga Host State')]);
$this->setHostLink($hostLink);
}
}
} elseif ($url->isExternal()) {
$link->addAttributes(['target' => '_blank']);
}

return $link->render();
}, $body);

return new HtmlString($body);

Check failure on line 207 in library/Jira/Web/RenderingHelper.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Parameter #1 $content of class ipl\Html\HtmlString constructor expects string, string|null given.

Check failure on line 207 in library/Jira/Web/RenderingHelper.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Parameter #1 $content of class ipl\Html\HtmlString constructor expects string, string|null given.

Check failure on line 207 in library/Jira/Web/RenderingHelper.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.4 on ubuntu-latest

Parameter #1 $content of class ipl\Html\HtmlString constructor expects string, string|null given.

Check failure on line 207 in library/Jira/Web/RenderingHelper.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.0 on ubuntu-latest

Parameter #1 $content of class ipl\Html\HtmlString constructor expects string, string|null given.

Check failure on line 207 in library/Jira/Web/RenderingHelper.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.1 on ubuntu-latest

Parameter #1 $content of class ipl\Html\HtmlString constructor expects string, string|null given.

Check failure on line 207 in library/Jira/Web/RenderingHelper.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.2 on ubuntu-latest

Parameter #1 $content of class ipl\Html\HtmlString constructor expects string, string|null given.

Check failure on line 207 in library/Jira/Web/RenderingHelper.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.3 on ubuntu-latest

Parameter #1 $content of class ipl\Html\HtmlString constructor expects string, string|null given.
}

public function linkToJira($caption, $url, $attributes = [])
Expand Down
Loading
Loading