Read about our upcoming Code of Conduct on this issue

Commit fa4126ac authored by Baldinof's avatar Baldinof
Browse files

Move unlink fork logic to a service

parent d74594d5a80c
......@@ -71,7 +71,7 @@ def transfer
def remove_fork
return access_denied! unless can?(current_user, :remove_fork_project, @project)
if @project.unlink_fork(current_user)
if ::Projects::UnlinkForkService.new(@project, current_user).execute
flash[:notice] = 'The fork relationship has been removed.'
end
end
......
......@@ -917,26 +917,6 @@ def enable_ci
self.builds_enabled = true
end
def unlink_fork(user)
if forked?
forked_from_project.lfs_objects.find_each do |lfs_object|
lfs_object.projects << self
end
merge_requests = forked_from_project.merge_requests.opened.from_project(self)
unless merge_requests.empty?
close_service = MergeRequests::CloseService.new(self, user)
merge_requests.each do |mr|
close_service.execute(mr)
end
end
forked_project_link.destroy
end
end
def any_runners?(&block)
if runners.active.any?(&block)
return true
......
module Projects
class UnlinkForkService < BaseService
def execute
return unless @project.forked?
@project.forked_from_project.lfs_objects.find_each do |lfs_object|
lfs_object.projects << self
end
merge_requests = @project.forked_from_project.merge_requests.opened.from_project(@project)
merge_requests.each do |mr|
MergeRequests::CloseService.new(@project, @current_user).execute(mr)
end
@project.forked_project_link.destroy
end
end
end
......@@ -699,25 +699,6 @@
end
end
describe '#unlink_fork' do
let(:fork_link) { create(:forked_project_link) }
let(:fork_project) { fork_link.forked_to_project }
let(:user) { create(:user) }
let(:merge_request) { create(:merge_request, source_project: fork_project, target_project: fork_link.forked_from_project) }
let!(:close_service) { MergeRequests::CloseService.new(fork_project, user) }
it 'remove fork relation and close all pending merge requests' do
allow(MergeRequests::CloseService).to receive(:new).
with(fork_project, user).
and_return(close_service)
expect(close_service).to receive(:execute).with(merge_request)
expect(fork_project.forked_project_link).to receive(:destroy)
fork_project.unlink_fork(user)
end
end
describe '.search_by_title' do
let(:project) { create(:project, name: 'kittens') }
......
require 'spec_helper'
describe Projects::UnlinkForkService, services: true do
subject { Projects::UnlinkForkService.new(fork_project, user) }
let(:fork_link) { create(:forked_project_link) }
let(:fork_project) { fork_link.forked_to_project }
let(:user) { create(:user) }
context 'with opened merge request on the source project' do
let(:merge_request) { create(:merge_request, source_project: fork_project, target_project: fork_link.forked_from_project) }
let(:mr_close_service) { MergeRequests::CloseService.new(fork_project, user) }
before do
allow(MergeRequests::CloseService).to receive(:new).
with(fork_project, user).
and_return(mr_close_service)
end
it 'close all pending merge requests' do
expect(mr_close_service).to receive(:execute).with(merge_request)
subject.execute
end
end
it 'remove fork relation' do
expect(fork_project.forked_project_link).to receive(:destroy)
subject.execute
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