Skip to content

Commit

Permalink
Fix incorrect object link rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
raviks789 committed Apr 17, 2024
1 parent 16c390a commit aa14ea6
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 40 deletions.
127 changes: 87 additions & 40 deletions library/Jira/Web/RenderingHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,47 @@ class RenderingHelper
{
protected $api;

/** @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): RenderingHelper
{
$this->hostName = $hostName;

return $this;
}


/**
* Set the name of monitored service
*
* @param string $serviceName
*
* @return $this
*/
public function setServiceName(string $serviceName): RenderingHelper
{
$this->serviceName = $serviceName;

return $this;
}

/**
* Set the link of monitored host
*
Expand Down Expand Up @@ -115,53 +150,65 @@ public function formatBody(string $body): HtmlString
$url = Url::fromPath($match[2]);
$link = new Link(
$match[1],
$url,
['target' => '_blank']
$url
);

if ($url->hasParam('service') || $url->hasParam('host.name')) {
if (
strpos($match[2], 'icingaweb2/monitoring') !== false
&& (Module::exists('icingadb') && IcingadbSupport::useIcingaDbAsBackend())
) {
$link = new Link(
$match[1],
Url::fromPath(
'icingadb/service',
[
'name' => $url->getParam('service'),
'host.name' => $url->getParam('host'),
]
),
['target' => '_blank']
);
}
$monitoringObjectLink = strpos($match[2], 'icingaweb2/monitoring/host/show') !== false
|| strpos($match[2], 'icingaweb2/monitoring/service/show') !== false;

$icingadbObjectLink = strpos($match[2], 'icingaweb2/icingadb/host') !== false
|| strpos($match[2], 'icingaweb2/icingadb/service') !== false;

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

$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 (strpos($match[2], 'icingaweb2/monitoring') !== false) {
if (Module::exists('icingadb') && IcingadbSupport::useIcingaDbAsBackend()) {
$link = new Link(
$match[1],
Url::fromPath(
if ($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 (Module::exists('icingadb') && IcingadbSupport::useIcingaDbAsBackend()) {
$link->setUrl(Url::fromPath(
'icingadb/host',
['name' => $url->getParam('host')]
),
['target' => '_blank']
);
['name' => $urlHostParam]
));
} else {
$icon = new Icon('laptop');
}
} else {
$icon = new Icon('laptop');
$urlHostParam = $url->getParam('name');
}
}

$hostLink = clone $link;
$hostLink->setContent([$icon, $match[1]])
->addAttributes(['title' => t('Show Icinga Host State')]);
$this->setHostLink($hostLink);
if ($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']);
}

$urls[] = $link->render();
Expand Down
2 changes: 2 additions & 0 deletions library/Jira/Web/Table/IssueDetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@ protected function assemble()
$icingaKey = preg_replace('/^BEGIN(.+)END$/', '$1', $fields->$keyField);
$parts = explode('!', $icingaKey);
$host = array_shift($parts);
$this->helper->setHostName($host);
if (empty($parts)) {
$service = null;
} else {
$service = array_shift($parts);
$this->helper->setServiceName($service);
}
if (isset($fields->icingaUser)) {
$user = $fields->icingaUser;
Expand Down

0 comments on commit aa14ea6

Please sign in to comment.