Commit d11319c4 authored by Lee Tickett's avatar Lee Tickett Committed by Mayra Cabrera

Delete issue contacts if project root changed

Issue contacts can only come from an issues root group.
This MR ensures that moving a project to a new root group
will delete all issue contacts.

At a later stage we will look to see if we can copy the
contacts to the new root group, but we need to tackle things
like duplication.

Changelog: fixed
parent b711569d
...@@ -16,6 +16,12 @@ class CustomerRelations::IssueContact < ApplicationRecord ...@@ -16,6 +16,12 @@ class CustomerRelations::IssueContact < ApplicationRecord
.pluck(:contact_id) .pluck(:contact_id)
end end
def self.delete_for_project(project_id)
joins(:issue)
.where(issues: { project_id: project_id })
.delete_all
end
private private
def contact_belongs_to_issue_group_or_ancestor def contact_belongs_to_issue_group_or_ancestor
......
...@@ -2,11 +2,11 @@ ...@@ -2,11 +2,11 @@
# Projects::TransferService class # Projects::TransferService class
# #
# Used for transfer project to another namespace # Used to transfer a project to another namespace
# #
# Ex. # Ex.
# # Move projects to namespace with ID 17 by user # # Move project to namespace by user
# Projects::TransferService.new(project, user, namespace_id: 17).execute # Projects::TransferService.new(project, user).execute(namespace)
# #
module Projects module Projects
class TransferService < BaseService class TransferService < BaseService
...@@ -103,6 +103,8 @@ module Projects ...@@ -103,6 +103,8 @@ module Projects
update_repository_configuration(@new_path) update_repository_configuration(@new_path)
remove_issue_contacts
execute_system_hooks execute_system_hooks
end end
...@@ -254,6 +256,12 @@ module Projects ...@@ -254,6 +256,12 @@ module Projects
namespace_traversal_ids: new_namespace.traversal_ids namespace_traversal_ids: new_namespace.traversal_ids
} }
end end
def remove_issue_contacts
return unless @old_group&.root_ancestor != @new_namespace&.root_ancestor
CustomerRelations::IssueContact.delete_for_project(project.id)
end
end end
end end
......
...@@ -80,4 +80,12 @@ RSpec.describe CustomerRelations::IssueContact do ...@@ -80,4 +80,12 @@ RSpec.describe CustomerRelations::IssueContact do
expect { described_class.find_contact_ids_by_emails(issue.id, Array(0..too_many_emails)) }.to raise_error(ArgumentError) expect { described_class.find_contact_ids_by_emails(issue.id, Array(0..too_many_emails)) }.to raise_error(ArgumentError)
end end
end end
describe '.delete_for_project' do
let_it_be(:issue_contacts) { create_list(:issue_customer_relations_contact, 3, :for_issue, issue: create(:issue, project: project)) }
it 'destroys all issue_contacts for project' do
expect { described_class.delete_for_project(project.id) }.to change { described_class.count }.by(-3)
end
end
end end
...@@ -5,13 +5,14 @@ require 'spec_helper' ...@@ -5,13 +5,14 @@ require 'spec_helper'
RSpec.describe Projects::TransferService do RSpec.describe Projects::TransferService do
include GitHelpers include GitHelpers
let_it_be(:user) { create(:user) }
let_it_be(:group) { create(:group) } let_it_be(:group) { create(:group) }
let_it_be(:user) { create(:user) }
let_it_be(:group_integration) { create(:integrations_slack, :group, group: group, webhook: 'http://group.slack.com') } let_it_be(:group_integration) { create(:integrations_slack, :group, group: group, webhook: 'http://group.slack.com') }
let(:project) { create(:project, :repository, :legacy_storage, namespace: user.namespace) } let(:project) { create(:project, :repository, :legacy_storage, namespace: user.namespace) }
let(:target) { group }
subject(:execute_transfer) { described_class.new(project, user).execute(group).tap { project.reload } } subject(:execute_transfer) { described_class.new(project, user).execute(target).tap { project.reload } }
context 'with npm packages' do context 'with npm packages' do
before do before do
...@@ -690,6 +691,32 @@ RSpec.describe Projects::TransferService do ...@@ -690,6 +691,32 @@ RSpec.describe Projects::TransferService do
end end
end end
context 'handling issue contacts' do
let_it_be(:root_group) { create(:group) }
let(:project) { create(:project, group: root_group) }
before do
root_group.add_owner(user)
target.add_owner(user)
create_list(:issue_customer_relations_contact, 2, :for_issue, issue: create(:issue, project: project))
end
context 'with the same root_ancestor' do
let(:target) { create(:group, parent: root_group) }
it 'retains issue contacts' do
expect { execute_transfer }.not_to change { CustomerRelations::IssueContact.count }
end
end
context 'with a different root_ancestor' do
it 'deletes issue contacts' do
expect { execute_transfer }.to change { CustomerRelations::IssueContact.count }.by(-2)
end
end
end
def rugged_config def rugged_config
rugged_repo(project.repository).config rugged_repo(project.repository).config
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