Move unlink fork logic to a service

This commit is contained in:
Baldinof 2016-03-22 15:34:35 +01:00
parent 436caf4e8b
commit fa4126acff
5 changed files with 52 additions and 40 deletions

View file

@ -71,7 +71,7 @@ class ProjectsController < ApplicationController
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

View file

@ -917,26 +917,6 @@ class Project < ActiveRecord::Base
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

View file

@ -0,0 +1,19 @@
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

View file

@ -699,25 +699,6 @@ describe Project, models: true do
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') }

View file

@ -0,0 +1,32 @@
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