Commit e8c72354 authored by Baldinof's avatar Baldinof

Close merge requests when removing fork relation

parent 29ac9f64
......@@ -26,6 +26,7 @@ v 8.6.0 (unreleased)
- Show labels in dashboard and group milestone views
- Add main language of a project in the list of projects (Tiago Botelho)
- Add ability to show archived projects on dashboard, explore and group pages
- Remove fork link closes all merge requests opened on source project (Florent Baldino)
v 8.5.5
- Ensure removing a project removes associated Todo entries
......
......@@ -72,7 +72,7 @@ class ProjectsController < ApplicationController
def remove_fork
return access_denied! unless can?(current_user, :remove_fork_project, @project)
if @project.unlink_fork
if @project.unlink_fork(current_user)
flash[:notice] = 'The fork relationship has been removed.'
end
end
......
......@@ -137,6 +137,7 @@ class MergeRequest < ActiveRecord::Base
scope :by_milestone, ->(milestone) { where(milestone_id: milestone) }
scope :in_projects, ->(project_ids) { where("source_project_id in (:project_ids) OR target_project_id in (:project_ids)", project_ids: project_ids) }
scope :of_projects, ->(ids) { where(target_project_id: ids) }
scope :from_project, ->(project) { where(source_project_id: project.id) }
scope :merged, -> { with_state(:merged) }
scope :closed_and_merged, -> { with_states(:closed, :merged) }
......
......@@ -890,12 +890,22 @@ class Project < ActiveRecord::Base
self.builds_enabled = true
end
def unlink_fork
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
......
......@@ -647,4 +647,23 @@ describe Project, models: true do
project.expire_caches_before_rename('foo')
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
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