Commit 997aecf2 authored by Robert May's avatar Robert May

Reduce loops from 8 to 2

This removes 6 duplicate loops from the refresh service.
parent 9a0b8b32
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
module MergeRequests module MergeRequests
class RefreshService < MergeRequests::BaseService class RefreshService < MergeRequests::BaseService
include Gitlab::Utils::StrongMemoize
attr_reader :push attr_reader :push
def execute(oldrev, newrev, ref) def execute(oldrev, newrev, ref)
...@@ -23,25 +24,37 @@ module MergeRequests ...@@ -23,25 +24,37 @@ module MergeRequests
post_merge_manually_merged post_merge_manually_merged
link_forks_lfs_objects link_forks_lfs_objects
reload_merge_requests reload_merge_requests
outdate_suggestions
refresh_pipelines_on_merge_requests merge_requests_for_source_branch.each do |mr|
abort_auto_merges outdate_suggestions(mr)
refresh_pipelines_on_merge_requests(mr)
abort_auto_merges(mr)
mark_pending_todos_done(mr)
end
abort_ff_merge_requests_with_when_pipeline_succeeds abort_ff_merge_requests_with_when_pipeline_succeeds
mark_pending_todos_done
cache_merge_requests_closing_issues cache_merge_requests_closing_issues
# Leave a system note if a branch was deleted/added merge_requests_for_source_branch.each do |mr|
if @push.branch_added? || @push.branch_removed? # Leave a system note if a branch was deleted/added
comment_mr_branch_presence_changed if branch_added_or_removed?
end comment_mr_branch_presence_changed(mr)
end
notify_about_push notify_about_push(mr)
mark_mr_as_wip_from_commits mark_mr_as_wip_from_commits(mr)
execute_mr_web_hooks execute_mr_web_hooks(mr)
end
true true
end end
def branch_added_or_removed?
strong_memoize(:branch_added_or_removed) do
@push.branch_added? || @push.branch_removed?
end
end
def close_upon_missing_source_branch_ref def close_upon_missing_source_branch_ref
# MergeRequest#reload_diff ignores not opened MRs. This means it won't # MergeRequest#reload_diff ignores not opened MRs. This means it won't
# create an `empty` diff for `closed` MRs without a source branch, keeping # create an `empty` diff for `closed` MRs without a source branch, keeping
...@@ -140,25 +153,22 @@ module MergeRequests ...@@ -140,25 +153,22 @@ module MergeRequests
merge_request.source_branch == @push.branch_name merge_request.source_branch == @push.branch_name
end end
def outdate_suggestions def outdate_suggestions(merge_request)
outdate_service = Suggestions::OutdateService.new outdate_service.execute(merge_request)
end
merge_requests_for_source_branch.each do |merge_request| def outdate_service
outdate_service.execute(merge_request) @outdate_service ||= Suggestions::OutdateService.new
end
end end
def refresh_pipelines_on_merge_requests def refresh_pipelines_on_merge_requests(merge_request)
merge_requests_for_source_branch.each do |merge_request| create_pipeline_for(merge_request, current_user)
create_pipeline_for(merge_request, current_user)
UpdateHeadPipelineForMergeRequestWorker.perform_async(merge_request.id) UpdateHeadPipelineForMergeRequestWorker.perform_async(merge_request.id)
end
end end
def abort_auto_merges def abort_auto_merges(merge_request)
merge_requests_for_source_branch.each do |merge_request| abort_auto_merge(merge_request, 'source branch was updated')
abort_auto_merge(merge_request, 'source branch was updated')
end
end end
def abort_ff_merge_requests_with_when_pipeline_succeeds def abort_ff_merge_requests_with_when_pipeline_succeeds
...@@ -187,10 +197,8 @@ module MergeRequests ...@@ -187,10 +197,8 @@ module MergeRequests
.with_auto_merge_enabled .with_auto_merge_enabled
end end
def mark_pending_todos_done def mark_pending_todos_done(merge_request)
merge_requests_for_source_branch.each do |merge_request| todo_service.merge_request_push(merge_request, @current_user)
todo_service.merge_request_push(merge_request, @current_user)
end
end end
def find_new_commits def find_new_commits
...@@ -218,62 +226,54 @@ module MergeRequests ...@@ -218,62 +226,54 @@ module MergeRequests
end end
# Add comment about branches being deleted or added to merge requests # Add comment about branches being deleted or added to merge requests
def comment_mr_branch_presence_changed def comment_mr_branch_presence_changed(merge_request)
presence = @push.branch_added? ? :add : :delete presence = @push.branch_added? ? :add : :delete
merge_requests_for_source_branch.each do |merge_request| SystemNoteService.change_branch_presence(
SystemNoteService.change_branch_presence( merge_request, merge_request.project, @current_user,
merge_request, merge_request.project, @current_user, :source, @push.branch_name, presence)
:source, @push.branch_name, presence)
end
end end
# Add comment about pushing new commits to merge requests and send nofitication emails # Add comment about pushing new commits to merge requests and send nofitication emails
def notify_about_push def notify_about_push(merge_request)
return unless @commits.present? return unless @commits.present?
merge_requests_for_source_branch.each do |merge_request| mr_commit_ids = Set.new(merge_request.commit_shas)
mr_commit_ids = Set.new(merge_request.commit_shas)
new_commits, existing_commits = @commits.partition do |commit| new_commits, existing_commits = @commits.partition do |commit|
mr_commit_ids.include?(commit.id) mr_commit_ids.include?(commit.id)
end end
SystemNoteService.add_commits(merge_request, merge_request.project, SystemNoteService.add_commits(merge_request, merge_request.project,
@current_user, new_commits, @current_user, new_commits,
existing_commits, @push.oldrev) existing_commits, @push.oldrev)
notification_service.push_to_merge_request(merge_request, @current_user, new_commits: new_commits, existing_commits: existing_commits) notification_service.push_to_merge_request(merge_request, @current_user, new_commits: new_commits, existing_commits: existing_commits)
end
end end
def mark_mr_as_wip_from_commits def mark_mr_as_wip_from_commits(merge_request)
return unless @commits.present? return unless @commits.present?
merge_requests_for_source_branch.each do |merge_request| commit_shas = merge_request.commit_shas
commit_shas = merge_request.commit_shas
wip_commit = @commits.detect do |commit| wip_commit = @commits.detect do |commit|
commit.work_in_progress? && commit_shas.include?(commit.sha) commit.work_in_progress? && commit_shas.include?(commit.sha)
end end
if wip_commit && !merge_request.work_in_progress? if wip_commit && !merge_request.work_in_progress?
merge_request.update(title: merge_request.wip_title) merge_request.update(title: merge_request.wip_title)
SystemNoteService.add_merge_request_wip_from_commit( SystemNoteService.add_merge_request_wip_from_commit(
merge_request, merge_request,
merge_request.project, merge_request.project,
@current_user, @current_user,
wip_commit wip_commit
) )
end
end end
end end
# Call merge request webhook with update branches # Call merge request webhook with update branches
def execute_mr_web_hooks def execute_mr_web_hooks(merge_request)
merge_requests_for_source_branch.each do |merge_request| execute_hooks(merge_request, 'update', old_rev: @push.oldrev)
execute_hooks(merge_request, 'update', old_rev: @push.oldrev)
end
end end
# If the merge requests closes any issues, save this information in the # If the merge requests closes any issues, save this information in the
......
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