Commit 411e124b authored by Sean McGivern's avatar Sean McGivern

Merge branch '1457-reset-approvals-on-closed-mrs' into 'master'

"Reset approvals on push" also reset approvals of a closed merged request

Closes #1457

See merge request !1051
parents 50370d88 73cc477b
...@@ -121,8 +121,6 @@ class Project < ActiveRecord::Base ...@@ -121,8 +121,6 @@ class Project < ActiveRecord::Base
# Merge Requests for target project should be removed with it # Merge Requests for target project should be removed with it
has_many :merge_requests, dependent: :destroy, foreign_key: 'target_project_id' has_many :merge_requests, dependent: :destroy, foreign_key: 'target_project_id'
# Merge requests from source project should be kept when source project was removed
has_many :fork_merge_requests, foreign_key: 'source_project_id', class_name: 'MergeRequest'
has_many :issues, dependent: :destroy has_many :issues, dependent: :destroy
has_many :labels, dependent: :destroy, class_name: 'ProjectLabel' has_many :labels, dependent: :destroy, class_name: 'ProjectLabel'
has_many :services, dependent: :destroy has_many :services, dependent: :destroy
......
...@@ -38,15 +38,13 @@ module MergeRequests ...@@ -38,15 +38,13 @@ module MergeRequests
private private
def merge_requests_for(branch) # Returns all origin and fork merge requests from `@project` satisfying passed arguments.
origin_merge_requests = @project.origin_merge_requests def merge_requests_for(source_branch, mr_states: [:opened])
.opened.where(source_branch: branch).to_a MergeRequest
.with_state(mr_states)
fork_merge_requests = @project.fork_merge_requests .where(source_branch: source_branch, source_project_id: @project.id)
.opened.where(source_branch: branch).to_a .preload(:source_project) # we don't need a #includes since we're just preloading for the #select
.select(&:source_project)
(origin_merge_requests + fork_merge_requests)
.uniq.select(&:source_project)
end end
def pipeline_merge_requests(pipeline) def pipeline_merge_requests(pipeline)
......
...@@ -43,7 +43,7 @@ module MergeRequests ...@@ -43,7 +43,7 @@ module MergeRequests
commit_ids.include?(merge_request.diff_head_sha) commit_ids.include?(merge_request.diff_head_sha)
end end
merge_requests.uniq.select(&:source_project).each do |merge_request| filter_merge_requests(merge_requests).each do |merge_request|
MergeRequests::PostMergeService. MergeRequests::PostMergeService.
new(merge_request.target_project, @current_user). new(merge_request.target_project, @current_user).
execute(merge_request) execute(merge_request)
...@@ -59,10 +59,13 @@ module MergeRequests ...@@ -59,10 +59,13 @@ module MergeRequests
def reload_merge_requests def reload_merge_requests
merge_requests = @project.merge_requests.opened. merge_requests = @project.merge_requests.opened.
by_source_or_target_branch(@branch_name).to_a by_source_or_target_branch(@branch_name).to_a
merge_requests += fork_merge_requests
merge_requests = filter_merge_requests(merge_requests)
merge_requests.each do |merge_request| # Fork merge requests
merge_requests += MergeRequest.opened
.where(source_branch: @branch_name, source_project: @project)
.where.not(target_project: @project).to_a
filter_merge_requests(merge_requests).each do |merge_request|
if merge_request.source_branch == @branch_name || force_push? if merge_request.source_branch == @branch_name || force_push?
merge_request.reload_diff merge_request.reload_diff
else else
...@@ -76,9 +79,11 @@ module MergeRequests ...@@ -76,9 +79,11 @@ module MergeRequests
end end
end end
# Reset approvals for merge request # Note: Closed merge requests also need approvals reset.
def reset_approvals_for_merge_requests def reset_approvals_for_merge_requests
merge_requests_for_source_branch.each do |merge_request| merge_requests = merge_requests_for(@branch_name, mr_states: [:opened, :reopened, :closed])
merge_requests.each do |merge_request|
target_project = merge_request.target_project target_project = merge_request.target_project
if target_project.approvals_before_merge.nonzero? && if target_project.approvals_before_merge.nonzero? &&
...@@ -190,16 +195,7 @@ module MergeRequests ...@@ -190,16 +195,7 @@ module MergeRequests
end end
def merge_requests_for_source_branch def merge_requests_for_source_branch
@source_merge_requests ||= begin @source_merge_requests ||= merge_requests_for(@branch_name)
merge_requests = @project.origin_merge_requests.opened.where(source_branch: @branch_name).to_a
merge_requests += fork_merge_requests
filter_merge_requests(merge_requests)
end
end
def fork_merge_requests
@fork_merge_requests ||= @project.fork_merge_requests.opened.
where(source_branch: @branch_name).to_a
end end
def branch_added? def branch_added?
......
---
title: Also reset approvals on push when merge request is closed
merge_request: 1051
author:
...@@ -118,6 +118,8 @@ describe MergeRequests::RefreshService, services: true do ...@@ -118,6 +118,8 @@ describe MergeRequests::RefreshService, services: true do
context 'push to fork repo source branch' do context 'push to fork repo source branch' do
let(:refresh_service) { service.new(@fork_project, @user) } let(:refresh_service) { service.new(@fork_project, @user) }
context 'open fork merge request' do
before do before do
allow(refresh_service).to receive(:execute_hooks) allow(refresh_service).to receive(:execute_hooks)
refresh_service.execute(@oldrev, @newrev, 'refs/heads/master') refresh_service.execute(@oldrev, @newrev, 'refs/heads/master')
...@@ -139,6 +141,29 @@ describe MergeRequests::RefreshService, services: true do ...@@ -139,6 +141,29 @@ describe MergeRequests::RefreshService, services: true do
it { expect(@fork_merge_request.approvals).to be_empty } it { expect(@fork_merge_request.approvals).to be_empty }
end end
context 'closed fork merge request' do
before do
@fork_merge_request.close!
allow(refresh_service).to receive(:execute_hooks)
refresh_service.execute(@oldrev, @newrev, 'refs/heads/master')
reload_mrs
end
it 'do not execute hooks with update action' do
expect(refresh_service).not_to have_received(:execute_hooks)
end
it { expect(@merge_request.notes).to be_empty }
it { expect(@merge_request).to be_open }
it { expect(@merge_request.approvals).not_to be_empty }
it { expect(@fork_merge_request.notes).to be_empty }
it { expect(@fork_merge_request).to be_closed }
it { expect(@build_failed_todo).to be_pending }
it { expect(@fork_build_failed_todo).to be_pending }
it { expect(@fork_merge_request.approvals).to be_empty }
end
end
context 'push to fork repo target branch' do context 'push to fork repo target branch' do
describe 'changes to merge requests' do describe 'changes to merge requests' do
before do before do
...@@ -197,7 +222,7 @@ describe MergeRequests::RefreshService, services: true do ...@@ -197,7 +222,7 @@ describe MergeRequests::RefreshService, services: true do
end end
end end
context 'when approvals_before_merge is disabled' do context 'when reset_approvals_on_push is disabled' do
before do before do
@project.update(reset_approvals_on_push: false) @project.update(reset_approvals_on_push: false)
refresh_service = service.new(@project, @user) refresh_service = service.new(@project, @user)
...@@ -225,7 +250,22 @@ describe MergeRequests::RefreshService, services: true do ...@@ -225,7 +250,22 @@ describe MergeRequests::RefreshService, services: true do
end end
end end
context 'when there are approvals to be reset' do context 'when there are approvals' do
context 'closed merge request' do
before do
@merge_request.close!
refresh_service = service.new(@project, @user)
allow(refresh_service).to receive(:execute_hooks)
refresh_service.execute(@oldrev, @newrev, 'refs/heads/master')
reload_mrs
end
it 'resets the approvals' do
expect(@merge_request.approvals).to be_empty
end
end
context 'opened merge request' do
before do before do
refresh_service = service.new(@project, @user) refresh_service = service.new(@project, @user)
allow(refresh_service).to receive(:execute_hooks) allow(refresh_service).to receive(:execute_hooks)
...@@ -238,6 +278,7 @@ describe MergeRequests::RefreshService, services: true do ...@@ -238,6 +278,7 @@ describe MergeRequests::RefreshService, services: true do
end end
end end
end end
end
context 'push new branch that exists in a merge request' do context 'push new branch that exists in a merge request' do
let(:refresh_service) { service.new(@fork_project, @user) } let(:refresh_service) { service.new(@fork_project, @user) }
......
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