Commit 76d9e29a authored by Grzegorz Bizon's avatar Grzegorz Bizon

Extract git push from merge request refresh service

parent 3c2d6b48
...@@ -3,17 +3,16 @@ ...@@ -3,17 +3,16 @@
module MergeRequests module MergeRequests
class RefreshService < MergeRequests::BaseService class RefreshService < MergeRequests::BaseService
def execute(oldrev, newrev, ref) def execute(oldrev, newrev, ref)
return true unless Gitlab::Git.branch_ref?(ref) @push = Gitlab::Git::Push.new(@project, oldrev, newrev, ref)
do_execute(oldrev, newrev, ref) return true unless @push.branch_push?
refresh_merge_requests!
end end
private private
def do_execute(oldrev, newrev, ref) def refresh_merge_requests!
@oldrev, @newrev = oldrev, newrev
@branch_name = Gitlab::Git.ref_name(ref)
Gitlab::GitalyClient.allow_n_plus_1_calls(&method(:find_new_commits)) Gitlab::GitalyClient.allow_n_plus_1_calls(&method(:find_new_commits))
# Be sure to close outstanding MRs before reloading them to avoid generating an # Be sure to close outstanding MRs before reloading them to avoid generating an
# empty diff during a manual merge # empty diff during a manual merge
...@@ -25,7 +24,7 @@ module MergeRequests ...@@ -25,7 +24,7 @@ module MergeRequests
cache_merge_requests_closing_issues cache_merge_requests_closing_issues
# Leave a system note if a branch was deleted/added # Leave a system note if a branch was deleted/added
if branch_added? || branch_removed? if @push.branch_added? || @push.branch_removed?
comment_mr_branch_presence_changed comment_mr_branch_presence_changed
end end
...@@ -54,8 +53,10 @@ module MergeRequests ...@@ -54,8 +53,10 @@ module MergeRequests
# rubocop: disable CodeReuse/ActiveRecord # rubocop: disable CodeReuse/ActiveRecord
def post_merge_manually_merged def post_merge_manually_merged
commit_ids = @commits.map(&:id) commit_ids = @commits.map(&:id)
merge_requests = @project.merge_requests.preload(:latest_merge_request_diff).opened.where(target_branch: @branch_name).to_a merge_requests = @project.merge_requests.opened
merge_requests = merge_requests.select(&:diff_head_commit) .preload(:latest_merge_request_diff)
.where(target_branch: @push.branch_name).to_a
.select(&:diff_head_commit)
merge_requests = merge_requests.select do |merge_request| merge_requests = merge_requests.select do |merge_request|
commit_ids.include?(merge_request.diff_head_sha) && commit_ids.include?(merge_request.diff_head_sha) &&
...@@ -70,24 +71,20 @@ module MergeRequests ...@@ -70,24 +71,20 @@ module MergeRequests
end end
# rubocop: enable CodeReuse/ActiveRecord # rubocop: enable CodeReuse/ActiveRecord
def force_push?
Gitlab::Checks::ForcePush.force_push?(@project, @oldrev, @newrev)
end
# Refresh merge request diff if we push to source or target branch of merge request # Refresh merge request diff if we push to source or target branch of merge request
# Note: we should update merge requests from forks too # Note: we should update merge requests from forks too
# rubocop: disable CodeReuse/ActiveRecord # rubocop: disable CodeReuse/ActiveRecord
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(@push.branch_name).to_a
# Fork merge requests # Fork merge requests
merge_requests += MergeRequest.opened merge_requests += MergeRequest.opened
.where(source_branch: @branch_name, source_project: @project) .where(source_branch: @push.branch_name, source_project: @project)
.where.not(target_project: @project).to_a .where.not(target_project: @project).to_a
filter_merge_requests(merge_requests).each do |merge_request| filter_merge_requests(merge_requests).each do |merge_request|
if merge_request.source_branch == @branch_name || force_push? if merge_request.source_branch == @push.branch_name || @push.force_push?
merge_request.reload_diff(current_user) merge_request.reload_diff(current_user)
else else
mr_commit_ids = merge_request.commit_shas mr_commit_ids = merge_request.commit_shas
...@@ -117,7 +114,7 @@ module MergeRequests ...@@ -117,7 +114,7 @@ module MergeRequests
end end
def find_new_commits def find_new_commits
if branch_added? if @push.branch_added?
@commits = [] @commits = []
merge_request = merge_requests_for_source_branch.first merge_request = merge_requests_for_source_branch.first
...@@ -126,28 +123,28 @@ module MergeRequests ...@@ -126,28 +123,28 @@ module MergeRequests
begin begin
# Since any number of commits could have been made to the restored branch, # Since any number of commits could have been made to the restored branch,
# find the common root to see what has been added. # find the common root to see what has been added.
common_ref = @project.repository.merge_base(merge_request.diff_head_sha, @newrev) common_ref = @project.repository.merge_base(merge_request.diff_head_sha, @push.newrev)
# If the a commit no longer exists in this repo, gitlab_git throws # If the a commit no longer exists in this repo, gitlab_git throws
# a Rugged::OdbError. This is fixed in https://gitlab.com/gitlab-org/gitlab_git/merge_requests/52 # a Rugged::OdbError. This is fixed in https://gitlab.com/gitlab-org/gitlab_git/merge_requests/52
@commits = @project.repository.commits_between(common_ref, @newrev) if common_ref @commits = @project.repository.commits_between(common_ref, @push.newrev) if common_ref
rescue rescue
end end
elsif branch_removed? elsif @push.branch_removed?
# No commits for a deleted branch. # No commits for a deleted branch.
@commits = [] @commits = []
else else
@commits = @project.repository.commits_between(@oldrev, @newrev) @commits = @project.repository.commits_between(@push.oldrev, @push.newrev)
end end
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
presence = branch_added? ? :add : :delete presence = @push.branch_added? ? :add : :delete
merge_requests_for_source_branch.each do |merge_request| 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, @branch_name, presence) :source, @push.branch_name, presence)
end end
end end
...@@ -164,7 +161,7 @@ module MergeRequests ...@@ -164,7 +161,7 @@ module MergeRequests
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, @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
...@@ -195,7 +192,7 @@ module MergeRequests ...@@ -195,7 +192,7 @@ module MergeRequests
# 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_requests_for_source_branch.each do |merge_request| merge_requests_for_source_branch.each do |merge_request|
execute_hooks(merge_request, 'update', old_rev: @oldrev) execute_hooks(merge_request, 'update', old_rev: @push.oldrev)
end end
end end
...@@ -203,7 +200,7 @@ module MergeRequests ...@@ -203,7 +200,7 @@ module MergeRequests
# `MergeRequestsClosingIssues` model (as a performance optimization). # `MergeRequestsClosingIssues` model (as a performance optimization).
# rubocop: disable CodeReuse/ActiveRecord # rubocop: disable CodeReuse/ActiveRecord
def cache_merge_requests_closing_issues def cache_merge_requests_closing_issues
@project.merge_requests.where(source_branch: @branch_name).each do |merge_request| @project.merge_requests.where(source_branch: @push.branch_name).each do |merge_request|
merge_request.cache_merge_request_closes_issues!(@current_user) merge_request.cache_merge_request_closes_issues!(@current_user)
end end
end end
...@@ -215,15 +212,7 @@ module MergeRequests ...@@ -215,15 +212,7 @@ module MergeRequests
def merge_requests_for_source_branch(reload: false) def merge_requests_for_source_branch(reload: false)
@source_merge_requests = nil if reload @source_merge_requests = nil if reload
@source_merge_requests ||= merge_requests_for(@branch_name) @source_merge_requests ||= merge_requests_for(@push.branch_name)
end
def branch_added?
Gitlab::Git.blank_ref?(@oldrev)
end
def branch_removed?
Gitlab::Git.blank_ref?(@newrev)
end end
end end
end end
...@@ -3,10 +3,17 @@ ...@@ -3,10 +3,17 @@
module Gitlab module Gitlab
module Git module Git
class Push class Push
attr_reader :oldrev, :newrev
def initialize(project, oldrev, newrev, ref) def initialize(project, oldrev, newrev, ref)
@project, @oldrev, @newrev = project, oldrev, newrev @project = project
@repository = project.repository @oldrev = oldrev
@branch_name = Gitlab::Git.ref_name(ref) @newrev = newrev
@ref = ref
end
def branch_name
@branch_name ||= Gitlab::Git.ref_name(@ref)
end end
def branch_added? def branch_added?
...@@ -20,6 +27,10 @@ module Gitlab ...@@ -20,6 +27,10 @@ module Gitlab
def force_push? def force_push?
Gitlab::Checks::ForcePush.force_push?(@project, @oldrev, @newrev) Gitlab::Checks::ForcePush.force_push?(@project, @oldrev, @newrev)
end end
def branch_push?
Gitlab::Git.branch_ref?(@ref)
end
end end
end 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