Commit 59cb9301 authored by Vitali Tatarintev's avatar Vitali Tatarintev

Don't process payload_attribute_mapping when it's blank

Fixes https://gitlab.com/gitlab-org/gitlab/-/issues/321101
parent 4fc571e0
......@@ -10,12 +10,6 @@ module EE
private
def payload_attribute_mapping(mappings)
Array(mappings).each_with_object({}) do |m, h|
h[m.field_name] = { path: m.path, type: m.type, label: m.label }
end
end
def validate_payload_example!(payload_example)
return if ::Gitlab::Utils::DeepSize.new(payload_example).valid?
......@@ -30,9 +24,23 @@ module EE
validate_payload_example!(args[:payload_example])
args.slice(*base_args.keys, :payload_example).merge(
payload_attribute_mapping: payload_attribute_mapping(args[:payload_attribute_mappings])
)
args
.slice(*base_args.keys, :payload_example)
.merge(payload_attribute_mapping_params(args))
end
def payload_attribute_mapping_params(args)
# Don't process payload_attribute_mapping when it's not part of a mutation params.
# Otherwise, it's going to reset already persisted value.
return {} unless args.key?(:payload_attribute_mappings)
{ payload_attribute_mapping: payload_attribute_mapping(args[:payload_attribute_mappings]) }
end
def payload_attribute_mapping(mappings)
Array(mappings).each_with_object({}) do |m, h|
h[m.field_name] = { path: m.path, type: m.type, label: m.label }
end
end
end
end
......
......@@ -33,14 +33,14 @@ RSpec.describe 'Updating an existing HTTP Integration' do
}
graphql_mutation(:http_integration_update, variables) do
<<~QL
clientMutationId
errors
integration {
id
name
active
url
}
clientMutationId
errors
integration {
id
name
active
url
}
QL
end
end
......@@ -67,6 +67,54 @@ RSpec.describe 'Updating an existing HTTP Integration' do
it_behaves_like 'validating the payload_example'
it_behaves_like 'validating the payload_attribute_mappings'
context 'when the custom mappings attributes are not part of the mutation variables' do
let_it_be(:payload_example) do
{ 'alert' => { 'name' => 'Test alert', 'desc' => 'Description' } }
end
let_it_be(:mapping) do
{
'title' => { 'path' => %w(alert name), 'type' => 'string', 'label' => 'Title' },
'description' => { 'path' => %w(alert desc), 'type' => 'string' }
}
end
let_it_be(:integration) do
create(:alert_management_http_integration, project: project, payload_example: payload_example, payload_attribute_mapping: mapping)
end
let(:mutation) do
variables = {
id: GitlabSchema.id_from_object(integration).to_s,
name: 'Modified Name'
}
graphql_mutation(:http_integration_update, variables) do
<<~QL
clientMutationId
errors
integration {
id
name
}
QL
end
end
it 'does not resets the custom mapping attributes', :aggregate_failures do
post_graphql_mutation(mutation, current_user: current_user)
integration_response = mutation_response['integration']
expect(response).to have_gitlab_http_status(:success)
expect(integration_response['id']).to eq(GitlabSchema.id_from_object(integration).to_s)
expect(integration_response['name']).to eq('Modified Name')
integration.reload
expect(integration.payload_example).to eq(payload_example)
expect(integration.payload_attribute_mapping).to eq(mapping)
end
end
context 'with the custom mappings feature unavailable' do
before do
stub_licensed_features(multiple_alert_http_integrations: false)
......
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