Commit 6c105a39 authored by Vitali Tatarintev's avatar Vitali Tatarintev

Return custom mapping fields for HttpIntegration

Returns custom mapping related fields for
Alert Management Integration GraphQL query
parent 00d64e00
......@@ -56,3 +56,5 @@ module Types
end
end
end
Types::AlertManagement::IntegrationType.include_if_ee('::EE::Types::AlertManagement::IntegrationType')
......@@ -33,6 +33,14 @@ module Types
def active
prometheus_service.manual_configuration?
end
def payload_example
{}
end
def payload_attribute_mappings
[]
end
end
end
end
......@@ -634,6 +634,16 @@ type AlertManagementHttpIntegration implements AlertManagementIntegration {
"""
name: String
"""
The custom mapping of GitLab alert attributes to fields from the payload_example.
"""
payloadAttributeMappings: [AlertManagementPayloadAlertMappingField!]
"""
The example of an alert payload.
"""
payloadExample: JsonString
"""
Token used to authenticate alert notification requests.
"""
......@@ -676,6 +686,16 @@ interface AlertManagementIntegration {
"""
name: String
"""
The custom mapping of GitLab alert attributes to fields from the payload_example.
"""
payloadAttributeMappings: [AlertManagementPayloadAlertMappingField!]
"""
The example of an alert payload.
"""
payloadExample: JsonString
"""
Token used to authenticate alert notification requests.
"""
......@@ -862,6 +882,31 @@ enum AlertManagementPayloadAlertFieldType {
STRING
}
"""
Parsed field (with its name) from an alert used for custom mappings
"""
type AlertManagementPayloadAlertMappingField {
"""
A GitLab alert field name.
"""
fieldName: AlertManagementPayloadAlertFieldName
"""
Human-readable label of the payload path.
"""
label: String
"""
Path to value inside payload JSON.
"""
path: [String!]
"""
Type of the parsed value.
"""
type: AlertManagementPayloadAlertFieldType
}
"""
An endpoint and credentials used to accept Prometheus alerts for a project
"""
......@@ -886,6 +931,16 @@ type AlertManagementPrometheusIntegration implements AlertManagementIntegration
"""
name: String
"""
The custom mapping of GitLab alert attributes to fields from the payload_example.
"""
payloadAttributeMappings: [AlertManagementPayloadAlertMappingField!]
"""
The example of an alert payload.
"""
payloadExample: JsonString
"""
Token used to authenticate alert notification requests.
"""
......
......@@ -1578,6 +1578,42 @@
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "payloadAttributeMappings",
"description": "The custom mapping of GitLab alert attributes to fields from the payload_example.",
"args": [
],
"type": {
"kind": "LIST",
"name": null,
"ofType": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "OBJECT",
"name": "AlertManagementPayloadAlertMappingField",
"ofType": null
}
}
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "payloadExample",
"description": "The example of an alert payload.",
"args": [
],
"type": {
"kind": "SCALAR",
"name": "JsonString",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "token",
"description": "Token used to authenticate alert notification requests.",
......@@ -1711,6 +1747,42 @@
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "payloadAttributeMappings",
"description": "The custom mapping of GitLab alert attributes to fields from the payload_example.",
"args": [
],
"type": {
"kind": "LIST",
"name": null,
"ofType": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "OBJECT",
"name": "AlertManagementPayloadAlertMappingField",
"ofType": null
}
}
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "payloadExample",
"description": "The example of an alert payload.",
"args": [
],
"type": {
"kind": "SCALAR",
"name": "JsonString",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "token",
"description": "Token used to authenticate alert notification requests.",
......@@ -2143,6 +2215,83 @@
],
"possibleTypes": null
},
{
"kind": "OBJECT",
"name": "AlertManagementPayloadAlertMappingField",
"description": "Parsed field (with its name) from an alert used for custom mappings",
"fields": [
{
"name": "fieldName",
"description": "A GitLab alert field name.",
"args": [
],
"type": {
"kind": "ENUM",
"name": "AlertManagementPayloadAlertFieldName",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "label",
"description": "Human-readable label of the payload path.",
"args": [
],
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "path",
"description": "Path to value inside payload JSON.",
"args": [
],
"type": {
"kind": "LIST",
"name": null,
"ofType": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "String",
"ofType": null
}
}
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "type",
"description": "Type of the parsed value.",
"args": [
],
"type": {
"kind": "ENUM",
"name": "AlertManagementPayloadAlertFieldType",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
}
],
"inputFields": null,
"interfaces": [
],
"enumValues": null,
"possibleTypes": null
},
{
"kind": "OBJECT",
"name": "AlertManagementPrometheusIntegration",
......@@ -2208,6 +2357,42 @@
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "payloadAttributeMappings",
"description": "The custom mapping of GitLab alert attributes to fields from the payload_example.",
"args": [
],
"type": {
"kind": "LIST",
"name": null,
"ofType": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "OBJECT",
"name": "AlertManagementPayloadAlertMappingField",
"ofType": null
}
}
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "payloadExample",
"description": "The example of an alert payload.",
"args": [
],
"type": {
"kind": "SCALAR",
"name": "JsonString",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "token",
"description": "Token used to authenticate alert notification requests.",
......@@ -274,6 +274,8 @@ An endpoint and credentials used to accept alerts for a project.
| `apiUrl` | String | URL at which Prometheus metrics can be queried to populate the metrics dashboard. |
| `id` | ID! | ID of the integration. |
| `name` | String | Name of the integration. |
| `payloadAttributeMappings` | AlertManagementPayloadAlertMappingField! => Array | The custom mapping of GitLab alert attributes to fields from the payload_example. |
| `payloadExample` | JsonString | The example of an alert payload. |
| `token` | String | Token used to authenticate alert notification requests. |
| `type` | AlertManagementIntegrationType! | Type of integration. |
| `url` | String | Endpoint which accepts alert notifications. |
......@@ -288,6 +290,17 @@ Parsed field from an alert used for custom mappings.
| `path` | String! => Array | Path to value inside payload JSON. |
| `type` | AlertManagementPayloadAlertFieldType | Type of the parsed value. |
### AlertManagementPayloadAlertMappingField
Parsed field (with its name) from an alert used for custom mappings.
| Field | Type | Description |
| ----- | ---- | ----------- |
| `fieldName` | AlertManagementPayloadAlertFieldName | A GitLab alert field name. |
| `label` | String | Human-readable label of the payload path. |
| `path` | String! => Array | Path to value inside payload JSON. |
| `type` | AlertManagementPayloadAlertFieldType | Type of the parsed value. |
### AlertManagementPrometheusIntegration
An endpoint and credentials used to accept Prometheus alerts for a project.
......@@ -298,6 +311,8 @@ An endpoint and credentials used to accept Prometheus alerts for a project.
| `apiUrl` | String | URL at which Prometheus metrics can be queried to populate the metrics dashboard. |
| `id` | ID! | ID of the integration. |
| `name` | String | Name of the integration. |
| `payloadAttributeMappings` | AlertManagementPayloadAlertMappingField! => Array | The custom mapping of GitLab alert attributes to fields from the payload_example. |
| `payloadExample` | JsonString | The example of an alert payload. |
| `token` | String | Token used to authenticate alert notification requests. |
| `type` | AlertManagementIntegrationType! | Type of integration. |
| `url` | String | Endpoint which accepts alert notifications. |
......
# frozen_string_literal: true
module EE
module Types
module AlertManagement
module IntegrationType
extend ActiveSupport::Concern
included do
field :payload_example, ::Types::JsonStringType,
null: true,
description: 'The example of an alert payload.'
field :payload_attribute_mappings, [::Types::AlertManagement::PayloadAlertMappingFieldType],
null: true,
description: 'The custom mapping of GitLab alert attributes to fields from the payload_example.',
resolver: ::Resolvers::AlertManagement::PayloadAlertMappingFieldResolver
end
end
end
end
end
# frozen_string_literal: true
module Resolvers
module AlertManagement
class PayloadAlertMappingFieldResolver < BaseResolver
type ::Types::AlertManagement::PayloadAlertMappingFieldType, null: true
alias_method :integration, :object
def resolve
return [] unless integration.is_a?(::AlertManagement::HttpIntegration)
integration.payload_attribute_mapping.map do |field_name, mapping|
::AlertManagement::AlertPayloadField.new(
project: integration.project,
field_name: field_name,
path: mapping['path'],
label: mapping['label'],
type: mapping['type']
)
end
end
end
end
end
# frozen_string_literal: true
module Types
module AlertManagement
class PayloadAlertMappingFieldType < BaseObject
graphql_name 'AlertManagementPayloadAlertMappingField'
description 'Parsed field (with its name) from an alert used for custom mappings'
authorize :read_alert_management_alert
field :field_name,
::Types::AlertManagement::PayloadAlertFieldNameEnum,
null: true,
description: 'A GitLab alert field name.'
field :path,
[GraphQL::STRING_TYPE],
null: true,
description: 'Path to value inside payload JSON.'
field :label,
GraphQL::STRING_TYPE,
null: true,
description: 'Human-readable label of the payload path.'
field :type,
::Types::AlertManagement::PayloadAlertFieldTypeEnum,
null: true,
description: 'Type of the parsed value.'
end
end
end
......@@ -5,7 +5,7 @@ module AlertManagement
include ActiveModel::Model
include ActiveModel::Validations
attr_accessor :project, :path, :label, :type
attr_accessor :project, :path, :label, :type, :field_name
ARRAY_TYPE = 'array'
DATETIME_TYPE = 'datetime'
......
......@@ -5,13 +5,36 @@ RSpec.describe 'getting Alert Management Integrations' do
include ::Gitlab::Routing.url_helpers
include GraphqlHelpers
let_it_be(:payload_example) do
{
alert: {
desc: 'Alert description',
name: 'Alert name'
}
}
end
let_it_be(:payload_attribute_mapping) do
{
title: { path: %w(alert name), type: 'string' },
description: { path: %w(alert desc), type: 'string', label: 'Description' }
}
end
let_it_be(:project) { create(:project, :repository) }
let_it_be(:current_user) { create(:user) }
let_it_be(:prometheus_service) { create(:prometheus_service, project: project) }
let_it_be(:project_alerting_setting) { create(:project_alerting_setting, project: project) }
let_it_be(:active_http_integration) { create(:alert_management_http_integration, project: project) }
let_it_be(:inactive_http_integration) { create(:alert_management_http_integration, :inactive, project: project) }
let_it_be(:other_project_http_integration) { create(:alert_management_http_integration) }
let_it_be(:active_http_integration) do
create(
:alert_management_http_integration,
project: project,
payload_example: payload_example,
payload_attribute_mapping: payload_attribute_mapping
)
end
let(:fields) do
<<~QUERY
......@@ -31,6 +54,7 @@ RSpec.describe 'getting Alert Management Integrations' do
before do
stub_licensed_features(multiple_alert_http_integrations: true)
stub_feature_flags(multiple_http_integrations_custom_mapping: project)
end
context 'with integrations' do
......@@ -67,7 +91,22 @@ RSpec.describe 'getting Alert Management Integrations' do
'active' => active_http_integration.active,
'token' => active_http_integration.token,
'url' => active_http_integration.url,
'apiUrl' => nil
'apiUrl' => nil,
'payloadExample' => payload_example.to_json,
'payloadAttributeMappings' => [
{
'fieldName' => 'TITLE',
'label' => nil,
'path' => %w(alert name),
'type' => 'STRING'
},
{
'fieldName' => 'DESCRIPTION',
'label' => 'Description',
'path' => %w(alert desc),
'type' => 'STRING'
}
]
},
{
'id' => GitlabSchema.id_from_object(inactive_http_integration).to_s,
......@@ -76,7 +115,9 @@ RSpec.describe 'getting Alert Management Integrations' do
'active' => inactive_http_integration.active,
'token' => inactive_http_integration.token,
'url' => inactive_http_integration.url,
'apiUrl' => nil
'apiUrl' => nil,
'payloadExample' => "{}",
'payloadAttributeMappings' => []
},
{
'id' => GitlabSchema.id_from_object(prometheus_service).to_s,
......@@ -85,7 +126,9 @@ RSpec.describe 'getting Alert Management Integrations' do
'active' => prometheus_service.manual_configuration?,
'token' => project_alerting_setting.token,
'url' => "http://localhost/#{project.full_path}/prometheus/alerts/notify.json",
'apiUrl' => prometheus_service.api_url
'apiUrl' => prometheus_service.api_url,
'payloadExample' => "{}",
'payloadAttributeMappings' => []
}
)
end
......
......@@ -16,6 +16,10 @@ RSpec.describe GitlabSchema.types['AlertManagementIntegration'] do
api_url
]
if Gitlab.ee?
expected_fields.push('payload_example', 'payload_attribute_mappings')
end
expect(described_class).to have_graphql_fields(*expected_fields)
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment