debian-mirror-gitlab/app/services/merge_requests/refresh_service.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

315 lines
11 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2015-04-26 12:48:37 +05:30
module MergeRequests
class RefreshService < MergeRequests::BaseService
2020-11-24 15:15:51 +05:30
include Gitlab::Utils::StrongMemoize
2018-12-13 13:39:08 +05:30
attr_reader :push
2015-04-26 12:48:37 +05:30
def execute(oldrev, newrev, ref)
2018-12-13 13:39:08 +05:30
@push = Gitlab::Git::Push.new(@project, oldrev, newrev, ref)
return true unless @push.branch_push?
2015-04-26 12:48:37 +05:30
2018-12-13 13:39:08 +05:30
refresh_merge_requests!
2018-11-08 19:23:39 +05:30
end
private
2018-12-13 13:39:08 +05:30
def refresh_merge_requests!
2019-12-04 20:38:33 +05:30
# n + 1: https://gitlab.com/gitlab-org/gitlab-foss/issues/60289
2023-04-23 21:23:45 +05:30
Gitlab::GitalyClient.allow_n_plus_1_calls { find_new_commits }
2019-07-31 22:56:46 +05:30
2015-11-26 14:37:03 +05:30
# Be sure to close outstanding MRs before reloading them to avoid generating an
# empty diff during a manual merge
2018-03-17 18:26:18 +05:30
close_upon_missing_source_branch_ref
post_merge_manually_merged
2020-03-13 15:44:24 +05:30
link_forks_lfs_objects
2015-04-26 12:48:37 +05:30
reload_merge_requests
2020-11-24 15:15:51 +05:30
merge_requests_for_source_branch.each do |mr|
outdate_suggestions(mr)
abort_auto_merges(mr)
mark_pending_todos_done(mr)
end
2019-12-21 20:55:43 +05:30
abort_ff_merge_requests_with_when_pipeline_succeeds
2016-09-29 09:46:39 +05:30
cache_merge_requests_closing_issues
2015-10-24 18:46:33 +05:30
2020-11-24 15:15:51 +05:30
merge_requests_for_source_branch.each do |mr|
# Leave a system note if a branch was deleted/added
if branch_added_or_removed?
comment_mr_branch_presence_changed(mr)
end
2015-10-24 18:46:33 +05:30
2020-11-24 15:15:51 +05:30
notify_about_push(mr)
2021-01-29 00:20:46 +05:30
mark_mr_as_draft_from_commits(mr)
2020-11-24 15:15:51 +05:30
execute_mr_web_hooks(mr)
2021-09-04 01:27:46 +05:30
# Run at the end of the loop to avoid any potential contention on the MR object
refresh_pipelines_on_merge_requests(mr)
2021-04-17 20:07:23 +05:30
merge_request_activity_counter.track_mr_including_ci_config(user: mr.author, merge_request: mr)
2020-11-24 15:15:51 +05:30
end
2015-04-26 12:48:37 +05:30
true
end
2020-11-24 15:15:51 +05:30
def branch_added_or_removed?
strong_memoize(:branch_added_or_removed) do
@push.branch_added? || @push.branch_removed?
end
end
2018-03-17 18:26:18 +05:30
def close_upon_missing_source_branch_ref
# MergeRequest#reload_diff ignores not opened MRs. This means it won't
# create an `empty` diff for `closed` MRs without a source branch, keeping
# the latest diff state as the last _valid_ one.
merge_requests_for_source_branch.reject(&:source_branch_exists?).each do |mr|
MergeRequests::CloseService
2021-06-08 01:23:25 +05:30
.new(project: mr.target_project, current_user: @current_user)
2018-03-17 18:26:18 +05:30
.execute(mr)
end
end
2015-04-26 12:48:37 +05:30
# Collect open merge requests that target same branch we push into
# and close if push to master include last commit from merge request
# We need this to close(as merged) merge requests that were merged into
# target branch manually
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2018-03-17 18:26:18 +05:30
def post_merge_manually_merged
2015-04-26 12:48:37 +05:30
commit_ids = @commits.map(&:id)
2018-12-05 23:21:45 +05:30
merge_requests = @project.merge_requests.opened
2021-04-17 20:07:23 +05:30
.preload_project_and_latest_diff
.preload_latest_diff_commit
2018-12-05 23:21:45 +05:30
.where(target_branch: @push.branch_name).to_a
.select(&:diff_head_commit)
2019-02-15 15:39:39 +05:30
.select do |merge_request|
commit_ids.include?(merge_request.diff_head_sha) &&
merge_request.merge_request_diff.state != 'empty'
end
merge_requests = filter_merge_requests(merge_requests)
return if merge_requests.empty?
2015-04-26 12:48:37 +05:30
2021-01-03 14:25:43 +05:30
analyzer = Gitlab::BranchPushMergeCommitAnalyzer.new(
@commits.reverse,
relevant_commit_ids: merge_requests.map(&:diff_head_sha)
)
2015-04-26 12:48:37 +05:30
2019-02-15 15:39:39 +05:30
merge_requests.each do |merge_request|
2021-01-03 14:25:43 +05:30
merge_request.merge_commit_sha = analyzer.get_merge_commit(merge_request.diff_head_sha)
2019-02-15 15:39:39 +05:30
2017-09-10 17:25:29 +05:30
MergeRequests::PostMergeService
2021-06-08 01:23:25 +05:30
.new(project: merge_request.target_project, current_user: @current_user)
2017-09-10 17:25:29 +05:30
.execute(merge_request)
2015-04-26 12:48:37 +05:30
end
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2015-04-26 12:48:37 +05:30
2020-03-13 15:44:24 +05:30
# Link LFS objects that exists in forks but does not exists in merge requests
# target project
def link_forks_lfs_objects
return unless @push.branch_updated?
merge_requests_for_forks.find_each do |mr|
LinkLfsObjectsService
2021-06-08 01:23:25 +05:30
.new(project: mr.target_project)
2020-03-13 15:44:24 +05:30
.execute(mr, oldrev: @push.oldrev, newrev: @push.newrev)
end
end
2015-04-26 12:48:37 +05:30
# Refresh merge request diff if we push to source or target branch of merge request
# Note: we should update merge requests from forks too
def reload_merge_requests
2017-09-10 17:25:29 +05:30
merge_requests = @project.merge_requests.opened
2021-04-17 20:07:23 +05:30
.by_source_or_target_branch(@push.branch_name)
.preload_project_and_latest_diff
2015-04-26 12:48:37 +05:30
2021-04-17 20:07:23 +05:30
merge_requests_from_forks = merge_requests_for_forks
.preload_project_and_latest_diff
2017-08-17 22:00:37 +05:30
2021-04-17 20:07:23 +05:30
merge_requests_array = merge_requests.to_a + merge_requests_from_forks.to_a
filter_merge_requests(merge_requests_array).each do |merge_request|
2023-05-27 22:25:52 +05:30
skip_merge_status_trigger = true
2018-12-13 13:39:08 +05:30
if branch_and_project_match?(merge_request) || @push.force_push?
merge_request.reload_diff(current_user)
2020-05-24 23:13:21 +05:30
# Clear existing merge error if the push were directed at the
# source branch. Clearing the error when the target branch
# changes will hide the error from the user.
merge_request.merge_error = nil
2023-05-27 22:25:52 +05:30
# Don't skip trigger since we to update the MR's merge status in real-time
# when the push if for the MR's source branch and project.
skip_merge_status_trigger = false
2020-01-01 13:55:28 +05:30
elsif merge_request.merge_request_diff.includes_any_commits?(push_commit_ids)
2017-09-10 17:25:29 +05:30
merge_request.reload_diff(current_user)
2015-04-26 12:48:37 +05:30
end
2016-08-24 12:49:21 +05:30
2023-05-27 22:25:52 +05:30
merge_request.skip_merge_status_trigger = skip_merge_status_trigger
2016-08-24 12:49:21 +05:30
merge_request.mark_as_unchecked
2015-04-26 12:48:37 +05:30
end
2018-03-17 18:26:18 +05:30
# Upcoming method calls need the refreshed version of
# @source_merge_requests diffs (for MergeRequest#commit_shas for instance).
merge_requests_for_source_branch(reload: true)
2015-04-26 12:48:37 +05:30
end
2018-12-13 13:39:08 +05:30
def push_commit_ids
@push_commit_ids ||= @commits.map(&:id)
end
def branch_and_project_match?(merge_request)
merge_request.source_project == @project &&
merge_request.source_branch == @push.branch_name
end
2020-11-24 15:15:51 +05:30
def outdate_suggestions(merge_request)
outdate_service.execute(merge_request)
end
2019-07-07 11:18:12 +05:30
2020-11-24 15:15:51 +05:30
def outdate_service
@outdate_service ||= Suggestions::OutdateService.new
2019-07-07 11:18:12 +05:30
end
2020-11-24 15:15:51 +05:30
def abort_auto_merges(merge_request)
abort_auto_merge(merge_request, 'source branch was updated')
2015-12-23 02:04:40 +05:30
end
2019-12-21 20:55:43 +05:30
def abort_ff_merge_requests_with_when_pipeline_succeeds
return unless @project.ff_merge_must_be_possible?
2019-12-26 22:10:19 +05:30
merge_requests_with_auto_merge_enabled_to(@push.branch_name).each do |merge_request|
next unless merge_request.auto_merge_strategy == AutoMergeService::STRATEGY_MERGE_WHEN_PIPELINE_SUCCEEDS
2019-12-21 20:55:43 +05:30
next unless merge_request.should_be_rebased?
abort_auto_merge_with_todo(merge_request, 'target branch was updated')
end
end
def abort_auto_merge_with_todo(merge_request, reason)
response = abort_auto_merge(merge_request, reason)
2021-01-03 14:25:43 +05:30
response = ServiceResponse.new(**response)
2019-12-21 20:55:43 +05:30
return unless response.success?
todo_service.merge_request_became_unmergeable(merge_request)
end
2019-12-26 22:10:19 +05:30
def merge_requests_with_auto_merge_enabled_to(target_branch)
2019-12-21 20:55:43 +05:30
@project
.merge_requests
.by_target_branch(target_branch)
2019-12-26 22:10:19 +05:30
.with_auto_merge_enabled
2019-12-21 20:55:43 +05:30
end
2020-11-24 15:15:51 +05:30
def mark_pending_todos_done(merge_request)
todo_service.merge_request_push(merge_request, @current_user)
2016-06-02 11:05:42 +05:30
end
2015-10-24 18:46:33 +05:30
def find_new_commits
2018-12-05 23:21:45 +05:30
if @push.branch_added?
2015-10-24 18:46:33 +05:30
@commits = []
merge_request = merge_requests_for_source_branch.first
return unless merge_request
begin
# Since any number of commits could have been made to the restored branch,
# find the common root to see what has been added.
2018-12-05 23:21:45 +05:30
common_ref = @project.repository.merge_base(merge_request.diff_head_sha, @push.newrev)
2015-10-24 18:46:33 +05:30
# 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
2018-12-05 23:21:45 +05:30
@commits = @project.repository.commits_between(common_ref, @push.newrev) if common_ref
2021-06-08 01:23:25 +05:30
rescue StandardError
2015-10-24 18:46:33 +05:30
end
2018-12-05 23:21:45 +05:30
elsif @push.branch_removed?
2015-10-24 18:46:33 +05:30
# No commits for a deleted branch.
@commits = []
else
2018-12-05 23:21:45 +05:30
@commits = @project.repository.commits_between(@push.oldrev, @push.newrev)
2015-10-24 18:46:33 +05:30
end
end
# Add comment about branches being deleted or added to merge requests
2020-11-24 15:15:51 +05:30
def comment_mr_branch_presence_changed(merge_request)
2018-12-05 23:21:45 +05:30
presence = @push.branch_added? ? :add : :delete
2015-10-24 18:46:33 +05:30
2020-11-24 15:15:51 +05:30
SystemNoteService.change_branch_presence(
merge_request, merge_request.project, @current_user,
:source, @push.branch_name, presence)
2015-10-24 18:46:33 +05:30
end
2023-04-23 21:23:45 +05:30
# Add comment about pushing new commits to merge requests and send notification emails
2022-10-11 01:57:18 +05:30
#
2020-11-24 15:15:51 +05:30
def notify_about_push(merge_request)
2015-10-24 18:46:33 +05:30
return unless @commits.present?
2015-04-26 12:48:37 +05:30
2020-11-24 15:15:51 +05:30
mr_commit_ids = Set.new(merge_request.commit_shas)
2015-04-26 12:48:37 +05:30
2020-11-24 15:15:51 +05:30
new_commits, existing_commits = @commits.partition do |commit|
mr_commit_ids.include?(commit.id)
end
2015-04-26 12:48:37 +05:30
2023-07-09 08:55:56 +05:30
SystemNoteService.add_commits(
merge_request, merge_request.project,
@current_user, new_commits,
existing_commits, @push.oldrev
)
2018-05-09 12:01:36 +05:30
2020-11-24 15:15:51 +05:30
notification_service.push_to_merge_request(merge_request, @current_user, new_commits: new_commits, existing_commits: existing_commits)
2015-09-11 14:41:01 +05:30
end
2021-01-29 00:20:46 +05:30
def mark_mr_as_draft_from_commits(merge_request)
2017-08-17 22:00:37 +05:30
return unless @commits.present?
2020-11-24 15:15:51 +05:30
commit_shas = merge_request.commit_shas
2017-08-17 22:00:37 +05:30
2022-07-23 23:45:48 +05:30
draft_commit = @commits.detect do |commit|
commit.draft? && commit_shas.include?(commit.sha)
2020-11-24 15:15:51 +05:30
end
2017-08-17 22:00:37 +05:30
2022-07-23 23:45:48 +05:30
if draft_commit && !merge_request.draft?
merge_request.update(title: merge_request.draft_title)
2021-01-29 00:20:46 +05:30
SystemNoteService.add_merge_request_draft_from_commit(
2020-11-24 15:15:51 +05:30
merge_request,
merge_request.project,
@current_user,
2022-07-23 23:45:48 +05:30
draft_commit
2020-11-24 15:15:51 +05:30
)
2017-08-17 22:00:37 +05:30
end
end
2015-09-11 14:41:01 +05:30
# Call merge request webhook with update branches
2020-11-24 15:15:51 +05:30
def execute_mr_web_hooks(merge_request)
execute_hooks(merge_request, 'update', old_rev: @push.oldrev)
2015-04-26 12:48:37 +05:30
end
2016-09-29 09:46:39 +05:30
# If the merge requests closes any issues, save this information in the
# `MergeRequestsClosingIssues` model (as a performance optimization).
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2016-09-29 09:46:39 +05:30
def cache_merge_requests_closing_issues
2018-12-05 23:21:45 +05:30
@project.merge_requests.where(source_branch: @push.branch_name).each do |merge_request|
2016-09-29 09:46:39 +05:30
merge_request.cache_merge_request_closes_issues!(@current_user)
end
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2016-09-29 09:46:39 +05:30
2015-04-26 12:48:37 +05:30
def filter_merge_requests(merge_requests)
merge_requests.uniq.select(&:source_project)
end
2015-10-24 18:46:33 +05:30
2018-03-17 18:26:18 +05:30
def merge_requests_for_source_branch(reload: false)
@source_merge_requests = nil if reload
2018-12-05 23:21:45 +05:30
@source_merge_requests ||= merge_requests_for(@push.branch_name)
2015-10-24 18:46:33 +05:30
end
2020-03-13 15:44:24 +05:30
def merge_requests_for_forks
@merge_requests_for_forks ||=
2021-09-04 01:27:46 +05:30
MergeRequest
.opened
.from_project(project)
.from_source_branches(@push.branch_name)
.from_fork
2020-03-13 15:44:24 +05:30
end
2015-04-26 12:48:37 +05:30
end
end
2019-12-04 20:38:33 +05:30
2021-06-08 01:23:25 +05:30
MergeRequests::RefreshService.prepend_mod_with('MergeRequests::RefreshService')