Commit 9092b865 authored by Mayra Cabrera's avatar Mayra Cabrera

Merge branch '350327-project-move-handle-crm' into 'master'

Delete issue contacts if project root changed

See merge request gitlab-org/gitlab!80394
parents 3d6d5913 d11319c4
......@@ -16,6 +16,12 @@ class CustomerRelations::IssueContact < ApplicationRecord
.pluck(:contact_id)
end
def self.delete_for_project(project_id)
joins(:issue)
.where(issues: { project_id: project_id })
.delete_all
end
private
def contact_belongs_to_issue_group_or_ancestor
......
......@@ -2,11 +2,11 @@
# Projects::TransferService class
#
# Used for transfer project to another namespace
# Used to transfer a project to another namespace
#
# Ex.
# # Move projects to namespace with ID 17 by user
# Projects::TransferService.new(project, user, namespace_id: 17).execute
# # Move project to namespace by user
# Projects::TransferService.new(project, user).execute(namespace)
#
module Projects
class TransferService < BaseService
......@@ -103,6 +103,8 @@ module Projects
update_repository_configuration(@new_path)
remove_issue_contacts
execute_system_hooks
end
......@@ -254,6 +256,12 @@ module Projects
namespace_traversal_ids: new_namespace.traversal_ids
}
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
......
......@@ -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)
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
......@@ -5,13 +5,14 @@ require 'spec_helper'
RSpec.describe Projects::TransferService do
include GitHelpers
let_it_be(:user) { create(:user) }
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(: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
before do
......@@ -690,6 +691,32 @@ RSpec.describe Projects::TransferService do
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
rugged_repo(project.repository).config
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