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
.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