2019-04-11 08:17:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-03-22 10:34:35 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 11:08:50 -04:00
|
|
|
RSpec.describe Projects::UnlinkForkService, :use_clean_rails_memory_store_caching do
|
2017-09-30 14:21:08 -04:00
|
|
|
include ProjectForksHelper
|
2016-03-22 10:34:35 -04:00
|
|
|
|
2017-09-30 14:21:08 -04:00
|
|
|
subject { described_class.new(forked_project, user) }
|
|
|
|
|
|
|
|
let(:project) { create(:project, :public) }
|
2019-11-28 10:06:57 -05:00
|
|
|
let!(:forked_project) { fork_project(project, user) }
|
2016-03-22 10:34:35 -04:00
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
context 'with opened merge request on the source project' do
|
2018-08-31 13:16:34 -04:00
|
|
|
let(:merge_request) { create(:merge_request, source_project: forked_project, target_project: forked_project.forked_from_project) }
|
2017-11-03 06:31:29 -04:00
|
|
|
let(:merge_request2) { create(:merge_request, source_project: forked_project, target_project: fork_project(project)) }
|
|
|
|
let(:merge_request_in_fork) { create(:merge_request, source_project: forked_project, target_project: forked_project) }
|
|
|
|
|
2021-05-11 23:10:21 -04:00
|
|
|
let(:mr_close_service) { MergeRequests::CloseService.new(project: forked_project, current_user: user) }
|
2016-03-22 10:34:35 -04:00
|
|
|
|
|
|
|
before do
|
2017-06-21 09:48:12 -04:00
|
|
|
allow(MergeRequests::CloseService).to receive(:new)
|
2021-05-11 23:10:21 -04:00
|
|
|
.with(project: forked_project, current_user: user)
|
2017-06-21 09:48:12 -04:00
|
|
|
.and_return(mr_close_service)
|
2016-03-22 10:34:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'close all pending merge requests' do
|
|
|
|
expect(mr_close_service).to receive(:execute).with(merge_request)
|
2017-11-03 06:31:29 -04:00
|
|
|
expect(mr_close_service).to receive(:execute).with(merge_request2)
|
2016-03-22 10:34:35 -04:00
|
|
|
|
|
|
|
subject.execute
|
|
|
|
end
|
2017-11-03 06:31:29 -04:00
|
|
|
|
|
|
|
it 'does not close merge requests for the project being unlinked' do
|
|
|
|
expect(mr_close_service).not_to receive(:execute).with(merge_request_in_fork)
|
|
|
|
end
|
2016-03-22 10:34:35 -04:00
|
|
|
end
|
|
|
|
|
2017-09-30 14:21:08 -04:00
|
|
|
it 'removes the link to the fork network' do
|
|
|
|
expect(forked_project.fork_network_member).to be_present
|
|
|
|
expect(forked_project.fork_network).to be_present
|
2016-03-22 10:34:35 -04:00
|
|
|
|
2016-04-03 11:17:14 -04:00
|
|
|
subject.execute
|
2017-09-30 14:21:08 -04:00
|
|
|
forked_project.reload
|
|
|
|
|
|
|
|
expect(forked_project.fork_network_member).to be_nil
|
|
|
|
expect(forked_project.reload.fork_network).to be_nil
|
2016-03-22 10:34:35 -04:00
|
|
|
end
|
2017-08-14 09:22:09 -04:00
|
|
|
|
|
|
|
it 'refreshes the forks count cache of the source project' do
|
2017-09-30 14:21:08 -04:00
|
|
|
source = forked_project.forked_from_project
|
2017-08-14 09:22:09 -04:00
|
|
|
|
|
|
|
expect(source.forks_count).to eq(1)
|
|
|
|
|
|
|
|
subject.execute
|
2020-07-09 11:08:59 -04:00
|
|
|
BatchLoader::Executor.clear_current
|
2017-08-14 09:22:09 -04:00
|
|
|
|
|
|
|
expect(source.forks_count).to be_zero
|
|
|
|
end
|
2017-11-03 06:31:29 -04:00
|
|
|
|
|
|
|
context 'when the original project was deleted' do
|
|
|
|
it 'does not fail when the original project is deleted' do
|
|
|
|
source = forked_project.forked_from_project
|
2020-10-14 05:08:46 -04:00
|
|
|
source.destroy!
|
2017-11-03 06:31:29 -04:00
|
|
|
forked_project.reload
|
|
|
|
|
|
|
|
expect { subject.execute }.not_to raise_error
|
|
|
|
end
|
|
|
|
end
|
2019-11-28 10:06:57 -05:00
|
|
|
|
|
|
|
context 'when given project is a source of forks' do
|
|
|
|
let!(:forked_project_2) { fork_project(project, user) }
|
|
|
|
let!(:fork_of_fork) { fork_project(forked_project, user) }
|
|
|
|
|
|
|
|
subject { described_class.new(project, user) }
|
|
|
|
|
|
|
|
context 'with opened merge requests from fork back to root project' do
|
|
|
|
let!(:merge_request) { create(:merge_request, source_project: project, target_project: forked_project) }
|
|
|
|
let!(:merge_request2) { create(:merge_request, source_project: project, target_project: fork_project(project)) }
|
|
|
|
let!(:merge_request_in_fork) { create(:merge_request, source_project: forked_project, target_project: forked_project) }
|
|
|
|
|
2021-05-11 23:10:21 -04:00
|
|
|
let(:mr_close_service) { MergeRequests::CloseService.new(project: project, current_user: user) }
|
2019-11-28 10:06:57 -05:00
|
|
|
|
|
|
|
before do
|
|
|
|
allow(MergeRequests::CloseService).to receive(:new)
|
2021-05-11 23:10:21 -04:00
|
|
|
.with(project: project, current_user: user)
|
2019-11-28 10:06:57 -05:00
|
|
|
.and_return(mr_close_service)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'closes all pending merge requests' do
|
|
|
|
expect(mr_close_service).to receive(:execute).with(merge_request)
|
|
|
|
expect(mr_close_service).to receive(:execute).with(merge_request2)
|
|
|
|
|
|
|
|
subject.execute
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not close merge requests that do not come from the project being unlinked' do
|
|
|
|
expect(mr_close_service).not_to receive(:execute).with(merge_request_in_fork)
|
|
|
|
|
|
|
|
subject.execute
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'removes its link to the fork network and updates direct network members' do
|
|
|
|
expect(project.fork_network_member).to be_present
|
|
|
|
expect(project.fork_network).to be_present
|
|
|
|
expect(project.forked_to_members.count).to eq(2)
|
|
|
|
expect(forked_project.forked_to_members.count).to eq(1)
|
|
|
|
expect(fork_of_fork.forked_to_members.count).to eq(0)
|
|
|
|
|
|
|
|
subject.execute
|
|
|
|
|
|
|
|
project.reload
|
|
|
|
forked_project.reload
|
|
|
|
fork_of_fork.reload
|
|
|
|
|
|
|
|
expect(project.fork_network_member).to be_nil
|
|
|
|
expect(project.fork_network).to be_nil
|
|
|
|
expect(forked_project.fork_network).to have_attributes(root_project_id: nil,
|
|
|
|
deleted_root_project_name: project.full_name)
|
|
|
|
expect(project.forked_to_members.count).to eq(0)
|
|
|
|
expect(forked_project.forked_to_members.count).to eq(1)
|
|
|
|
expect(fork_of_fork.forked_to_members.count).to eq(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'refreshes the forks count cache of the given project' do
|
|
|
|
expect(project.forks_count).to eq(2)
|
|
|
|
|
|
|
|
subject.execute
|
2020-07-09 11:08:59 -04:00
|
|
|
BatchLoader::Executor.clear_current
|
2019-11-28 10:06:57 -05:00
|
|
|
|
|
|
|
expect(project.forks_count).to be_zero
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'and is node with a parent' do
|
|
|
|
subject { described_class.new(forked_project, user) }
|
|
|
|
|
|
|
|
context 'with opened merge requests from and to given project' do
|
|
|
|
let!(:mr_from_parent) { create(:merge_request, source_project: project, target_project: forked_project) }
|
|
|
|
let!(:mr_to_parent) { create(:merge_request, source_project: forked_project, target_project: project) }
|
|
|
|
let!(:mr_to_child) { create(:merge_request, source_project: forked_project, target_project: fork_of_fork) }
|
|
|
|
let!(:mr_from_child) { create(:merge_request, source_project: fork_of_fork, target_project: forked_project) }
|
|
|
|
let!(:merge_request_in_fork) { create(:merge_request, source_project: forked_project, target_project: forked_project) }
|
|
|
|
|
2021-05-11 23:10:21 -04:00
|
|
|
let(:mr_close_service) { MergeRequests::CloseService.new(project: forked_project, current_user: user) }
|
2019-11-28 10:06:57 -05:00
|
|
|
|
|
|
|
before do
|
|
|
|
allow(MergeRequests::CloseService).to receive(:new)
|
2021-05-11 23:10:21 -04:00
|
|
|
.with(project: forked_project, current_user: user)
|
2019-11-28 10:06:57 -05:00
|
|
|
.and_return(mr_close_service)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'close all pending merge requests' do
|
|
|
|
merge_requests = [mr_from_parent, mr_to_parent, mr_from_child, mr_to_child]
|
|
|
|
|
|
|
|
merge_requests.each do |mr|
|
|
|
|
expect(mr_close_service).to receive(:execute).with(mr).and_call_original
|
|
|
|
end
|
|
|
|
|
|
|
|
subject.execute
|
|
|
|
|
|
|
|
merge_requests = MergeRequest.where(id: merge_requests)
|
|
|
|
|
|
|
|
expect(merge_requests).to all(have_attributes(state: 'closed'))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not close merge requests which do not come from the project being unlinked' do
|
|
|
|
expect(mr_close_service).not_to receive(:execute).with(merge_request_in_fork)
|
|
|
|
|
|
|
|
subject.execute
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'refreshes the forks count cache of the parent and the given project' do
|
|
|
|
expect(project.forks_count).to eq(2)
|
|
|
|
expect(forked_project.forks_count).to eq(1)
|
|
|
|
|
|
|
|
subject.execute
|
2020-07-09 11:08:59 -04:00
|
|
|
BatchLoader::Executor.clear_current
|
2019-11-28 10:06:57 -05:00
|
|
|
|
|
|
|
expect(project.forks_count).to eq(1)
|
|
|
|
expect(forked_project.forks_count).to eq(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'removes its link to the fork network and updates direct network members' do
|
|
|
|
expect(project.fork_network).to be_present
|
|
|
|
expect(forked_project.fork_network).to be_present
|
|
|
|
expect(fork_of_fork.fork_network).to be_present
|
|
|
|
|
|
|
|
expect(project.forked_to_members.count).to eq(2)
|
|
|
|
expect(forked_project.forked_to_members.count).to eq(1)
|
|
|
|
expect(fork_of_fork.forked_to_members.count).to eq(0)
|
|
|
|
|
|
|
|
subject.execute
|
|
|
|
project.reload
|
|
|
|
forked_project.reload
|
|
|
|
fork_of_fork.reload
|
|
|
|
|
|
|
|
expect(project.fork_network).to be_present
|
|
|
|
expect(forked_project.fork_network).to be_nil
|
|
|
|
expect(fork_of_fork.fork_network).to be_present
|
|
|
|
|
|
|
|
expect(project.forked_to_members.count).to eq(1) # 1 child is gone
|
|
|
|
expect(forked_project.forked_to_members.count).to eq(0)
|
|
|
|
expect(fork_of_fork.forked_to_members.count).to eq(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-31 14:09:19 -04:00
|
|
|
context 'a project with pool repository' do
|
|
|
|
let(:project) { create(:project, :public, :repository) }
|
|
|
|
let!(:pool_repository) { create(:pool_repository, :ready, source_project: project) }
|
|
|
|
|
|
|
|
subject { described_class.new(project, user) }
|
|
|
|
|
|
|
|
it 'when unlinked leaves pool repository' do
|
|
|
|
expect { subject.execute }.to change { project.reload.has_pool_repository? }.from(true).to(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-11-28 10:06:57 -05:00
|
|
|
context 'when given project is not part of a fork network' do
|
|
|
|
let!(:project_without_forks) { create(:project, :public) }
|
|
|
|
|
|
|
|
subject { described_class.new(project_without_forks, user) }
|
|
|
|
|
|
|
|
it 'does not raise errors' do
|
|
|
|
expect { subject.execute }.not_to raise_error
|
|
|
|
end
|
|
|
|
end
|
2016-03-22 10:34:35 -04:00
|
|
|
end
|