Commit dd778448 authored by Sean McGivern's avatar Sean McGivern

Merge branch 'refresh-service-optimisations' into 'master'

Reduce MergeRequest::RefreshService loops

See merge request gitlab-org/gitlab!40135
parents aa65d826 d46ab19e
...@@ -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
merge_requests_for_source_branch.each do |mr|
# Leave a system note if a branch was deleted/added # Leave a system note if a branch was deleted/added
if @push.branch_added? || @push.branch_removed? if branch_added_or_removed?
comment_mr_branch_presence_changed comment_mr_branch_presence_changed(mr)
end 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,26 +153,23 @@ module MergeRequests ...@@ -140,26 +153,23 @@ 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
merge_requests_for_source_branch.each do |merge_request|
outdate_service.execute(merge_request) outdate_service.execute(merge_request)
end end
def outdate_service
@outdate_service ||= Suggestions::OutdateService.new
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
return unless @project.ff_merge_must_be_possible? return unless @project.ff_merge_must_be_possible?
...@@ -187,11 +197,9 @@ module MergeRequests ...@@ -187,11 +197,9 @@ 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
if @push.branch_added? if @push.branch_added?
...@@ -218,21 +226,18 @@ module MergeRequests ...@@ -218,21 +226,18 @@ 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|
...@@ -245,12 +250,10 @@ module MergeRequests ...@@ -245,12 +250,10 @@ module MergeRequests
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|
...@@ -267,14 +270,11 @@ module MergeRequests ...@@ -267,14 +270,11 @@ module MergeRequests
) )
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
# `MergeRequestsClosingIssues` model (as a performance optimization). # `MergeRequestsClosingIssues` model (as a performance optimization).
......
---
title: Reduce MergeRequest::RefreshService loops
merge_request: 40135
author:
type: performance
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