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 25, 2024
1 parent 0f05267 commit 0671c7c
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 58 deletions.
154 changes: 96 additions & 58 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): self
{
$this->hostName = $hostName;

return $this;
}


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

return $this;
}

/**
* Set the link of monitored host
*
Expand Down Expand Up @@ -108,75 +143,78 @@ public function getIssueComment($author, string $time, string $body): array
*/
public function formatBody(string $body): HtmlString
{
$urls = [];

// Replace object urls in the given string with link elements
$body = preg_replace_callback('/\[([^|]+)\|([^]]+)]/', function ($match) use (&$urls) {
$body = preg_replace_callback('/\[([^|]+)\|([^]]+)]/', function ($match) {
$url = Url::fromPath($match[2]);
$link = new Link(
$match[1],
$url,
['target' => '_blank']
);

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']
);
}

$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(
'icingadb/host',
['name' => $url->getParam('host')]
),
['target' => '_blank']
);
$link = new Link($match[1], $url);
$urlPath = $url->getPath();

$monitoringObjectLink = strpos($urlPath, 'monitoring/host/show') !== false
|| strpos($urlPath, 'monitoring/service/show') !== false;

$icingadbObjectLink = strpos($urlPath, 'icingadb/host') !== false
|| strpos($urlPath, 'icingadb/service') !== false;

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');
} else {
$urlServiceParam = $url->getParam('name');
$urlHostParam = $url->getParam('host.name');
}

if ($urlServiceParam === $this->serviceName && $urlHostParam === $this->hostName) {
if ($transformToIcingadbLink) {
$url->setPath('icingadb/service')
->remove(['service', 'host'])
->setParams(['name' => $urlServiceParam, 'host.name' => $urlHostParam]);
$link->setUrl($url);
}

$serviceLink = clone $link;
$serviceLink->setContent([new Icon('cog'), $match[1]])
->addAttributes(['title' => t('Show Icinga Service State')]);
$this->setServiceLink($serviceLink);
}
} else {
if ($monitoringObjectLink) {
$urlHostParam = $url->getParam('host');
$icon = new Icon('laptop');
} else {
$urlHostParam = $url->getParam('name');
$icon = new Icon('server');
}
}

$hostLink = clone $link;
$hostLink->setContent([$icon, $match[1]])
->addAttributes(['title' => t('Show Icinga Host State')]);
$this->setHostLink($hostLink);
}
if ($urlHostParam === $this->hostName) {
if ($transformToIcingadbLink) {
$url->setPath('icingadb/host')
->remove('host')
->setParams(['name' => $urlHostParam]);

$urls[] = $link->render();

return '$objectLink' . (count($urls) - 1) . '$';
}, $body);
$link->setUrl($url);
$icon = new Icon('server');
}

$html = Html::wantHtml($body)->render();
$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']);
}

foreach ($urls as $i => $url) {
$html = str_replace('$objectLink' . $i . '$', $url, $html);
}
return $link->render();
}, $body);

// This is safe.
return new HtmlString($html);
return new HtmlString($body);

Check failure on line 217 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 217 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 217 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 217 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 217 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 217 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 217 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
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);
$helper->setHostName($host);
if (empty($parts)) {
$service = null;
} else {
$service = array_shift($parts);
$helper->setServiceName($service);
}
if (isset($fields->icingaUser)) {
$user = $fields->icingaUser;
Expand Down

0 comments on commit 0671c7c

Please sign in to comment.