Commit 570b1885 authored by Lin Jen-Shin's avatar Lin Jen-Shin

Fix updating/deleting insights

See https://gitlab.com/gitlab-org/gitlab-ee/issues/11099
parent 485d3c9d
......@@ -25,7 +25,7 @@ module EE
:membership_lock,
:repository_size_limit
].tap do |params_ee|
params_ee << { insight_attributes: :project_id } if current_group&.insights_available?
params_ee << { insight_attributes: [:id, :project_id, :_destroy] } if current_group&.insights_available?
params_ee << :file_template_project_id if current_group&.feature_available?(:custom_file_templates_for_namespace)
params_ee << :custom_project_templates_group_id if current_group&.group_project_template_available?
end
......
......@@ -19,7 +19,7 @@ module EE
has_one :saml_provider
has_one :insight, foreign_key: :namespace_id
accepts_nested_attributes_for :insight
accepts_nested_attributes_for :insight, allow_destroy: true
has_one :scim_oauth_access_token
has_many :ldap_group_links, foreign_key: 'group_id', dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
......
......@@ -12,6 +12,8 @@ module EE
return false if group.errors.present?
end
remove_insight_if_insight_project_absent
super.tap { |success| log_audit_event if success }
end
......@@ -60,6 +62,13 @@ module EE
).execute.exists?
end
def remove_insight_if_insight_project_absent
if params.dig(:insight_attributes, :project_id) == ''
params[:insight_attributes][:_destroy] = true
params[:insight_attributes].delete(:project_id)
end
end
def log_audit_event
EE::Audit::GroupChangesAuditor.new(current_user, group).execute
end
......
......@@ -16,6 +16,7 @@
.form-group
- insight = @group.insight || @group.build_insight
= form.fields_for :insight_attributes, insight do |insight_form|
= insight_form.hidden_field :id
= insight_form.label :project_id, class: 'label-light' do
.form-text.text-muted
= _('Select a repository')
......
---
title: Fix removing and updating insights config, and foreign key constraints.
merge_request: 11030
author:
type: fixed
......@@ -45,14 +45,26 @@ describe GroupsController do
end.to change { group.reload.file_template_project_id }.to(project.id)
end
it 'updates insight project_id successfully' do
project = create(:project, group: group)
context 'with insights feature' do
let(:project) { create(:project, group: group) }
before do
stub_licensed_features(insights: true)
end
stub_licensed_features(insights: true)
it 'updates insight project_id successfully' do
post :update, params: { id: group.to_param, group: { insight_attributes: { project_id: project.id } } }
post :update, params: { id: group.to_param, group: { insight_attributes: { project_id: project.id } } }
expect(group.reload.insight.project).to eq(project)
end
it 'removes insight successfully' do
insight = group.create_insight(project: project)
expect(group.reload.insight.project).to eq(project)
post :update, params: { id: group.to_param, group: { insight_attributes: { id: insight.id, project_id: '' } } }
expect(group.reload.insight).to be_nil
end
end
end
end
......
......@@ -906,8 +906,8 @@ module Gitlab
end
def remove_foreign_key_if_exists(*args)
if foreign_key_exists?(args)
remove_foreign_key(args)
if foreign_key_exists?(*args)
remove_foreign_key(*args)
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