2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
module MergeRequests
|
|
|
|
class BuildService < MergeRequests::BaseService
|
2018-03-17 18:26:18 +05:30
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
def execute
|
2018-03-17 18:26:18 +05:30
|
|
|
@params_issue_iid = params.delete(:issue_iid)
|
2018-12-13 13:39:08 +05:30
|
|
|
self.merge_request = MergeRequest.new
|
|
|
|
# TODO: this should handle all quick actions that don't have side effects
|
2019-12-04 20:38:33 +05:30
|
|
|
# https://gitlab.com/gitlab-org/gitlab-foss/issues/53658
|
2018-12-13 13:39:08 +05:30
|
|
|
merge_quick_actions_into_params!(merge_request, only: [:target_branch])
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2019-07-31 22:56:46 +05:30
|
|
|
# Assign the projects first so we can use policies for `filter_params`
|
2018-03-27 19:54:05 +05:30
|
|
|
merge_request.author = current_user
|
2019-07-31 22:56:46 +05:30
|
|
|
merge_request.source_project = find_source_project
|
|
|
|
merge_request.target_project = find_target_project
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
process_params
|
2019-07-31 22:56:46 +05:30
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
merge_request.compare_commits = []
|
2019-12-04 20:38:33 +05:30
|
|
|
set_merge_request_target_branch
|
|
|
|
|
2019-07-31 22:56:46 +05:30
|
|
|
merge_request.can_be_created = projects_and_branches_valid?
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
# compare branches only if branches are valid, otherwise
|
|
|
|
# compare_branches may raise an error
|
|
|
|
if merge_request.can_be_created
|
|
|
|
compare_branches
|
|
|
|
assign_title_and_description
|
2018-11-20 20:47:30 +05:30
|
|
|
assign_labels
|
|
|
|
assign_milestone
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
merge_request
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
private
|
2015-09-25 12:07:36 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
attr_accessor :merge_request
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
delegate :target_branch,
|
|
|
|
:target_branch_ref,
|
|
|
|
:target_project,
|
|
|
|
:source_branch,
|
|
|
|
:source_branch_ref,
|
|
|
|
:source_project,
|
|
|
|
:compare_commits,
|
2022-07-23 23:45:48 +05:30
|
|
|
:draft_title,
|
2018-03-17 18:26:18 +05:30
|
|
|
:description,
|
2021-04-17 20:07:23 +05:30
|
|
|
:first_multiline_commit,
|
2018-03-17 18:26:18 +05:30
|
|
|
:errors,
|
|
|
|
to: :merge_request
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
def force_remove_source_branch
|
|
|
|
if params.key?(:force_remove_source_branch)
|
|
|
|
params.delete(:force_remove_source_branch)
|
|
|
|
else
|
|
|
|
merge_request.source_project.remove_source_branch_after_merge?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
def filter_id_params
|
2021-03-11 19:13:27 +05:30
|
|
|
# merge_request.assign_attributes(...) below is a Rails
|
|
|
|
# method that only work if all the params it is passed have
|
|
|
|
# corresponding fields in the database. As there are no fields
|
2021-04-29 21:17:54 +05:30
|
|
|
# in the database for :add_label_ids, :remove_label_ids,
|
|
|
|
# :add_assignee_ids and :remove_assignee_ids, we
|
2021-03-11 19:13:27 +05:30
|
|
|
# need to remove them from the params before the call to
|
|
|
|
# merge_request.assign_attributes(...)
|
|
|
|
#
|
2021-04-29 21:17:54 +05:30
|
|
|
# IssuableBaseService#process_label_ids and
|
|
|
|
# IssuableBaseService#process_assignee_ids take care
|
2021-03-11 19:13:27 +05:30
|
|
|
# of the removal.
|
|
|
|
params[:label_ids] = process_label_ids(params, extra_label_ids: merge_request.label_ids.to_a)
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
params[:assignee_ids] = process_assignee_ids(params, extra_assignee_ids: merge_request.assignee_ids.to_a)
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
merge_request.assign_attributes(params.to_h.compact)
|
|
|
|
end
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
def process_params
|
|
|
|
# Force remove the source branch?
|
|
|
|
merge_request.merge_params['force_remove_source_branch'] = force_remove_source_branch
|
|
|
|
|
|
|
|
# Only assign merge requests params that are allowed
|
|
|
|
self.params = assign_allowed_merge_params(merge_request, params)
|
|
|
|
|
|
|
|
# Filter out params that are either not allowed or invalid
|
|
|
|
filter_params(merge_request)
|
|
|
|
|
|
|
|
# Filter out the following from params:
|
|
|
|
# - :add_label_ids and :remove_label_ids
|
|
|
|
# - :add_assignee_ids and :remove_assignee_ids
|
|
|
|
filter_id_params
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def find_source_project
|
2019-07-31 22:56:46 +05:30
|
|
|
source_project = project_from_params(:source_project)
|
2019-01-03 12:48:30 +05:30
|
|
|
return source_project if source_project.present? && can?(current_user, :create_merge_request_from, source_project)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
project
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_target_project
|
2019-07-31 22:56:46 +05:30
|
|
|
target_project = project_from_params(:target_project)
|
2019-01-03 12:48:30 +05:30
|
|
|
return target_project if target_project.present? && can?(current_user, :create_merge_request_in, target_project)
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2019-01-03 12:48:30 +05:30
|
|
|
target_project = project.default_merge_request_target
|
|
|
|
|
|
|
|
return target_project if target_project.present? && can?(current_user, :create_merge_request_in, target_project)
|
|
|
|
|
|
|
|
project
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2019-07-31 22:56:46 +05:30
|
|
|
def project_from_params(param_name)
|
|
|
|
project_from_params = params.delete(param_name)
|
|
|
|
|
|
|
|
id_param_name = :"#{param_name}_id"
|
|
|
|
if project_from_params.nil? && params[id_param_name]
|
|
|
|
project_from_params = Project.find_by_id(params.delete(id_param_name))
|
|
|
|
end
|
|
|
|
|
|
|
|
project_from_params
|
|
|
|
end
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
def set_merge_request_target_branch
|
|
|
|
if source_branch_default? && !target_branch_specified?
|
|
|
|
merge_request.target_branch = nil
|
|
|
|
else
|
|
|
|
merge_request.target_branch ||= target_project.default_branch
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def source_branch_specified?
|
|
|
|
params[:source_branch].present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def target_branch_specified?
|
|
|
|
params[:target_branch].present?
|
|
|
|
end
|
|
|
|
|
2019-01-03 12:48:30 +05:30
|
|
|
def projects_and_branches_valid?
|
|
|
|
return false if source_project.nil? || target_project.nil?
|
2017-08-17 22:00:37 +05:30
|
|
|
return false unless source_branch_specified? || target_branch_specified?
|
|
|
|
|
2019-01-03 12:48:30 +05:30
|
|
|
validate_projects_and_branches
|
2017-08-17 22:00:37 +05:30
|
|
|
errors.blank?
|
|
|
|
end
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def compare_branches
|
|
|
|
compare = CompareService.new(
|
|
|
|
source_project,
|
2018-03-17 18:26:18 +05:30
|
|
|
source_branch_ref
|
2017-08-17 22:00:37 +05:30
|
|
|
).execute(
|
|
|
|
target_project,
|
2018-03-17 18:26:18 +05:30
|
|
|
target_branch_ref
|
2014-09-02 18:07:02 +05:30
|
|
|
)
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
if compare
|
|
|
|
merge_request.compare_commits = compare.commits
|
|
|
|
merge_request.compare = compare
|
|
|
|
end
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2019-01-03 12:48:30 +05:30
|
|
|
def validate_projects_and_branches
|
|
|
|
merge_request.validate_target_project
|
|
|
|
merge_request.validate_fork
|
|
|
|
|
|
|
|
return if errors.any?
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
add_error('You must select source and target branch') unless branches_present?
|
|
|
|
add_error('You must select different branches') if same_source_and_target?
|
|
|
|
add_error("Source branch \"#{source_branch}\" does not exist") unless source_branch_exists?
|
|
|
|
add_error("Target branch \"#{target_branch}\" does not exist") unless target_branch_exists?
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def add_error(message)
|
|
|
|
errors.add(:base, message)
|
|
|
|
end
|
|
|
|
|
|
|
|
def branches_present?
|
|
|
|
target_branch.present? && source_branch.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def same_source_and_target?
|
2019-12-04 20:38:33 +05:30
|
|
|
same_source_and_target_project? && target_branch == source_branch
|
|
|
|
end
|
|
|
|
|
|
|
|
def source_branch_default?
|
|
|
|
same_source_and_target_project? && source_branch == target_project.default_branch
|
|
|
|
end
|
|
|
|
|
|
|
|
def same_source_and_target_project?
|
|
|
|
source_project == target_project
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def source_branch_exists?
|
|
|
|
source_branch.blank? || source_project.commit(source_branch)
|
|
|
|
end
|
|
|
|
|
|
|
|
def target_branch_exists?
|
|
|
|
target_branch.blank? || target_project.commit(target_branch)
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2022-07-16 23:28:13 +05:30
|
|
|
def set_draft_title_if_needed
|
|
|
|
return unless compare_commits.empty? || Gitlab::Utils.to_boolean(params[:draft])
|
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
merge_request.title = draft_title
|
2022-07-16 23:28:13 +05:30
|
|
|
end
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
# When your branch name starts with an iid followed by a dash this pattern will be
|
|
|
|
# interpreted as the user wants to close that issue on this project.
|
|
|
|
#
|
|
|
|
# For example:
|
2021-04-17 20:07:23 +05:30
|
|
|
# - Issue 112 exists
|
|
|
|
# - title: Emoji don't show up in commit title
|
2016-06-02 11:05:42 +05:30
|
|
|
# - Source branch is: 112-fix-mep-mep
|
|
|
|
#
|
|
|
|
# Will lead to:
|
|
|
|
# - Appending `Closes #112` to the description
|
|
|
|
# - Setting the title as 'Resolves "Emoji don't show up in commit title"' if there is
|
|
|
|
# more than one commit in the MR
|
|
|
|
#
|
2017-08-17 22:00:37 +05:30
|
|
|
def assign_title_and_description
|
2022-07-23 23:45:48 +05:30
|
|
|
assign_description_from_repository_template
|
2021-04-17 20:07:23 +05:30
|
|
|
assign_title_and_description_from_commits
|
2018-11-20 20:47:30 +05:30
|
|
|
merge_request.title ||= title_from_issue if target_project.issues_enabled? || target_project.external_issue_tracker
|
2018-03-17 18:26:18 +05:30
|
|
|
merge_request.title ||= source_branch.titleize.humanize
|
2022-07-16 23:28:13 +05:30
|
|
|
set_draft_title_if_needed
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
append_closes_description
|
|
|
|
end
|
|
|
|
|
2018-11-20 20:47:30 +05:30
|
|
|
def assign_labels
|
|
|
|
return unless target_project.issues_enabled? && issue
|
|
|
|
return if merge_request.label_ids&.any?
|
|
|
|
|
|
|
|
merge_request.label_ids = issue.try(:label_ids)
|
|
|
|
end
|
|
|
|
|
|
|
|
def assign_milestone
|
|
|
|
return unless target_project.issues_enabled? && issue
|
|
|
|
return if merge_request.milestone_id.present?
|
|
|
|
|
|
|
|
merge_request.milestone_id = issue.try(:milestone_id)
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def append_closes_description
|
|
|
|
return unless issue&.to_reference.present?
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
closes_issue = "#{target_project.autoclose_referenced_issues ? 'Closes' : 'Related to'} #{issue.to_reference}"
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
if description.present?
|
2018-11-18 11:00:15 +05:30
|
|
|
descr_parts = [merge_request.description, closes_issue]
|
|
|
|
merge_request.description = descr_parts.join("\n\n")
|
2018-03-17 18:26:18 +05:30
|
|
|
else
|
|
|
|
merge_request.description = closes_issue
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
def assign_title_and_description_from_commits
|
2017-08-17 22:00:37 +05:30
|
|
|
commits = compare_commits
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
if commits&.count == 1
|
|
|
|
commit = commits.first
|
|
|
|
else
|
|
|
|
commit = first_multiline_commit
|
|
|
|
return unless commit
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
merge_request.title ||= commit.title
|
|
|
|
merge_request.description ||= commit.description.try(:strip)
|
|
|
|
end
|
|
|
|
|
2018-11-20 20:47:30 +05:30
|
|
|
def title_from_issue
|
2018-03-17 18:26:18 +05:30
|
|
|
return unless issue
|
|
|
|
|
2018-11-20 20:47:30 +05:30
|
|
|
return "Resolve \"#{issue.title}\"" if issue.is_a?(Issue)
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-11-20 20:47:30 +05:30
|
|
|
return if issue_iid.blank?
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-11-20 20:47:30 +05:30
|
|
|
title_parts = ["Resolve #{issue.to_reference}"]
|
|
|
|
branch_title = source_branch.downcase.remove(issue_iid.downcase).titleize.humanize
|
2018-11-18 11:00:15 +05:30
|
|
|
|
2018-11-20 20:47:30 +05:30
|
|
|
title_parts << "\"#{branch_title}\"" if branch_title.present?
|
|
|
|
title_parts.join(' ')
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
def assign_description_from_repository_template
|
|
|
|
return unless merge_request.description.blank?
|
|
|
|
|
|
|
|
# Use TemplateFinder to load the default template. We need this mainly for
|
|
|
|
# the project_id, in case it differs from the target project. Conveniently,
|
|
|
|
# since the underlying merge_request_template_names_hash is cached, this
|
|
|
|
# should also be relatively cheap and allows us to bail early if the project
|
|
|
|
# does not have a default template.
|
|
|
|
templates = TemplateFinder.all_template_names(target_project, :merge_requests)
|
|
|
|
template = templates.values.flatten.find { |tmpl| tmpl[:name].casecmp?('default') }
|
|
|
|
|
|
|
|
return unless template
|
|
|
|
|
|
|
|
begin
|
|
|
|
repository_template = TemplateFinder.build(
|
|
|
|
:merge_requests,
|
|
|
|
target_project,
|
|
|
|
{
|
|
|
|
name: template[:name],
|
|
|
|
source_template_project_id: template[:project_id]
|
|
|
|
}
|
|
|
|
).execute
|
|
|
|
rescue ::Gitlab::Template::Finders::RepoTemplateFinder::FileNotFoundError
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
return unless repository_template.present?
|
|
|
|
|
|
|
|
merge_request.description = repository_template.content
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def issue_iid
|
|
|
|
strong_memoize(:issue_iid) do
|
|
|
|
@params_issue_iid || begin
|
|
|
|
id = if target_project.external_issue_tracker
|
|
|
|
source_branch.match(target_project.external_issue_reference_pattern).try(:[], 0)
|
|
|
|
end
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
id || source_branch.match(/\A(\d+)-/).try(:[], 1)
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def issue
|
2018-11-20 20:47:30 +05:30
|
|
|
strong_memoize(:issue) do
|
|
|
|
target_project.get_issue(issue_iid, current_user)
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-12-04 20:38:33 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
MergeRequests::BuildService.prepend_mod_with('MergeRequests::BuildService')
|