Transport - Jira transport rewrite (#15561)

* Add custom Field to Jira Payload

Allmost every Jira setup is diffenerent and requires customise information to be added to the API post.

This change will allow you add a custom fields to the '"fields": {   }' using json format.
Examples: are provided in the TransportUI

* applying StyleCI fixes

* Updating Jira transport

* Updating Jira transport

* Updating Jira transport

* styleci fixes

* Renaming variables to align with existing conventions

* Fixing imput checks to make sure values are legit

* style

* Fix minor typo

---------

Co-authored-by: PipoCanaja <38363551+PipoCanaja@users.noreply.github.com>
Co-authored-by: Justin Lentz <monkeybrains7@gmail.com>
This commit is contained in:
Skylark
2024-02-22 17:07:49 +00:00
committed by GitHub
parent 36a3e39981
commit 851f682155
2 changed files with 119 additions and 45 deletions

View File

@@ -1,23 +1,16 @@
<?php
/* Copyright (C) 2015 Aldemir Akpinar <aldemir.akpinar@gmail.com>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>. */
/*Copyright (c) 2019 GitStoph <https://github.com/GitStoph>
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version. Please see LICENSE.txt at the top level of
* the source code distribution for details. */
/**
* Jira API Transport
* Jira Webhook & API Transport
*
* @author Aldemir Akpinar <aldemir.akpinar@gmail.com>
* @copyright 2017 Aldemir Akpinar, LibreNMS
* @author Skylark <https://github.com/LoveSkylark>
* @copyright 2023 Skylark
* @license GPL
*/
@@ -29,41 +22,72 @@ use LibreNMS\Util\Http;
class Jira extends Transport
{
protected string $name = 'Jira';
public function deliverAlert(array $alert_data): bool
{
// Don't create tickets for resolutions
if ($alert_data['severity'] == 'recovery') {
return true;
$webhook_on = $this->config['enable-webhook'] ?? false;
// Check if messsage is an alert or not
if ($alert_data['state'] != 0) {
$url = $this->config['jira-url'];
// If webhooks are not enabled, append the API info
if (! $webhook_on) {
$url .= '/rest/api/latest/issue';
}
// Messsage is a recovery
} else {
if (! $webhook_on) {
return false; // Webhooks not enabled, do nothing.
} else {
$url = $this->config['jira-close-url'];
}
}
$prjkey = $this->config['jira-key'];
$issuetype = $this->config['jira-type'];
$details = empty($alert_data['title']) ? 'Librenms alert for: ' . $alert_data['hostname'] : $alert_data['title'];
$project_key = $this->config['jira-key'];
$issue_type = $this->config['jira-type'];
$title = empty($alert_data['title']) ? 'Librenms alert for: ' . $alert_data['hostname'] : $alert_data['title'];
$description = $alert_data['msg'];
$url = $this->config['jira-url'] . '/rest/api/latest/issue';
// Construct the payload
$data = [
'fields' => [
'project' => [
'key' => $prjkey,
],
'summary' => $details,
'summary' => $title,
'description' => $description,
'project' => [
'key' => $project_key,
],
'issuetype' => [
'name' => $issuetype,
'name' => $issue_type,
],
],
];
// Add Custom Webhook ID to the payload
if ($webhook_on) {
if (! empty($this->config['webhook-id'])) {
$data['fields'][$this->config['webhook-id']] = $alert_data['id'];
} else {
$data['fields']['alert_id'] = $alert_data['id'];
}
}
// Add Custom fileds to the payload
$custom = json_decode($this->config['jira-custom'], true);
if (! empty($custom)) {
$data['fields'] = array_merge($data['fields'], $custom);
}
$res = Http::client()
->withBasicAuth($this->config['jira-username'], $this->config['jira-password'])
->acceptJson()
->post($url, $data);
if ($res->successful()) {
return true;
return true; // Delivery successful
}
// An error occurred, throw an exception
throw new AlertTransportDeliveryException($alert_data, $res->status(), $res->body(), $description, $data);
}
@@ -71,12 +95,6 @@ class Jira extends Transport
{
return [
'config' => [
[
'title' => 'URL',
'name' => 'jira-url',
'descr' => 'Jira URL',
'type' => 'text',
],
[
'title' => 'Project Key',
'name' => 'jira-key',
@@ -89,6 +107,18 @@ class Jira extends Transport
'descr' => 'Jira Issue Type',
'type' => 'text',
],
[
'title' => 'Open Ticket URL',
'name' => 'jira-url',
'descr' => 'Create Jira Ticket',
'type' => 'text',
],
[
'title' => 'Close Ticket URL',
'name' => 'jira-close-url',
'descr' => 'Close Jira Ticket | Webhook Only"',
'type' => 'text',
],
[
'title' => 'Jira Username',
'name' => 'jira-username',
@@ -101,10 +131,31 @@ class Jira extends Transport
'descr' => 'Jira Password',
'type' => 'password',
],
[
'title' => 'Enable Webhook',
'name' => 'enable-webhook',
'descr' => 'Use Webhook instead of API',
'type' => 'checkbox',
'default' => false,
],
[
'title' => 'Webhook Identifier',
'name' => 'webhook-id',
'descr' => 'Jira Webhook Identifier',
'type' => 'text',
],
[
'title' => 'Custom Fields',
'name' => 'jira-custom',
'type' => 'textarea',
'descr' => '{&quot;components&quot;: [{&quot;id&quot;: &quot;00001&quot;}],&#xA;&quot;customfield_10001&quot;: [{&quot;id&quot;: &quot;00002&quot;}]}',
],
],
'validation' => [
'jira-key' => 'required|string',
'jira-url' => 'required|string',
'jira-url' => 'required|url',
'jira-close-url' => 'nullable|url',
'webhook-id' => 'nullable|string',
'jira-type' => 'required|string',
'jira-username' => 'required|string',
'jira-password' => 'required|string',

View File

@@ -342,26 +342,49 @@ Configuration of the LibreNMS IRC-Bot is described [here](https://github.com/lib
## JIRA
You can have LibreNMS create issues on a Jira instance for critical
and warning alerts. The Jira transport only sets summary and
description fields. Therefore your Jira project must not have any
other mandatory field for the provided issuetype. The config fields
that need to set are Jira URL, Jira username, Jira password, Project
key, and issue type. Currently http authentication is used to access
Jira and Jira username and password will be stored as cleartext in the
You can have LibreNMS create issues on a Jira instance for critical and warning
alerts using either the Jira REST API or webhooks.
Custom fields allow you to add any required fields beyond summary and description
fields in case mandatory fields are required by your Jira project/issue type
configuration. Custom fields are defined in JSON format but ustom fields allow
you to add any required fields beyond summary and description fields in case
mandatory fields are required by your Jira project/issue type configuration.
Custom fields are defined in JSON format. Currently http authentication is used
to access Jira and Jira username and password will be stored as cleartext in the
LibreNMS database.
### REST API
The config fields that need to set for Jira REST API are: Jira Open URL, Jira username,
Jira password, Project key, and issue type.
> Note: REST API is that it is only able to open new tickets.
### Webhooks
The config fields that need to set for webhooks are: Jira Open URL, Jira Close URL,
Jira username, Jira password and webhook ID.
> Note: Webhooks allow more control over how alerts are handled in Jira. With webhooks,
> recovery messages can be sent to a different URL than alerts. Additionally, a custom
> conditional logic can be built using the webhook payload and ID to automatically close
> an open ticket if predefined conditions are met.
[Jira Issue Types](https://confluence.atlassian.com/adminjiracloud/issue-types-844500742.html)
[Jira Webhooks](https://developer.atlassian.com/cloud/jira/platform/webhooks/)
**Example:**
| Config | Example |
| ------ | ------- |
| URL | <https://myjira.mysite.com> |
| Project Key | JIRAPROJECTKEY |
| Issue Type | Myissuetype |
| Open URL | <https://myjira.mysite.com> / <https://webhook-open-url> |
| Close URL | <https://webhook-close-url> |
| Jira Username | myjirauser |
| Jira Password | myjirapass |
| Enable webhook | ON/OFF |
| Webhook ID | alert_id |
| Custom Fileds | {"components":[{"id":"00001"}], "source": "LibrenNMS"} |
## Jira Service Management