2018-07-17 12:50:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-07-15 08:34:06 -04:00
|
|
|
module MergeRequests
|
|
|
|
class BuildService < MergeRequests::BaseService
|
2018-02-02 12:20:48 -05:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
2014-07-15 08:34:06 -04:00
|
|
|
def execute
|
2018-02-02 12:20:48 -05:00
|
|
|
@params_issue_iid = params.delete(:issue_iid)
|
2018-10-24 12:01:44 -04:00
|
|
|
self.merge_request = MergeRequest.new
|
2018-11-07 07:33:42 -05:00
|
|
|
# TODO: this should handle all quick actions that don't have side effects
|
2019-09-18 10:02:45 -04:00
|
|
|
# https://gitlab.com/gitlab-org/gitlab-foss/issues/53658
|
2018-11-07 07:33:42 -05:00
|
|
|
merge_quick_actions_into_params!(merge_request, only: [:target_branch])
|
2017-11-25 06:33:05 -05:00
|
|
|
|
2019-07-12 05:10:54 -04:00
|
|
|
# Assign the projects first so we can use policies for `filter_params`
|
2018-02-23 12:30:37 -05:00
|
|
|
merge_request.author = current_user
|
2019-07-12 05:10:54 -04:00
|
|
|
merge_request.source_project = find_source_project
|
|
|
|
merge_request.target_project = find_target_project
|
|
|
|
|
2019-10-23 17:06:17 -04:00
|
|
|
# Source project sets the default source branch removal setting
|
|
|
|
merge_request.merge_params['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
|
|
|
|
|
2019-09-25 12:25:40 -04:00
|
|
|
self.params = assign_allowed_merge_params(merge_request, params)
|
|
|
|
|
2019-07-12 05:10:54 -04:00
|
|
|
filter_params(merge_request)
|
2019-08-14 23:43:33 -04:00
|
|
|
|
|
|
|
# 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
|
|
|
|
# in the database for :add_label_ids and :remove_label_ids, we
|
|
|
|
# need to remove them from the params before the call to
|
|
|
|
# merge_request.assign_attributes(...)
|
|
|
|
#
|
|
|
|
# IssuableBaseService#process_label_ids takes care
|
|
|
|
# of the removal.
|
|
|
|
params[:label_ids] = process_label_ids(params, extra_label_ids: merge_request.label_ids.to_a)
|
|
|
|
|
2019-07-12 05:10:54 -04:00
|
|
|
merge_request.assign_attributes(params.to_h.compact)
|
|
|
|
|
2014-07-15 08:34:06 -04:00
|
|
|
merge_request.compare_commits = []
|
2019-09-05 07:40:45 -04:00
|
|
|
set_merge_request_target_branch
|
|
|
|
|
2019-07-12 05:10:54 -04:00
|
|
|
merge_request.can_be_created = projects_and_branches_valid?
|
2017-01-05 10:25:45 -05:00
|
|
|
|
2017-08-24 02:20:36 -04:00
|
|
|
# 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-09-06 13:02:51 -04:00
|
|
|
assign_labels
|
|
|
|
assign_milestone
|
2017-08-24 02:20:36 -04:00
|
|
|
end
|
2017-01-05 10:25:45 -05:00
|
|
|
|
|
|
|
merge_request
|
|
|
|
end
|
2016-04-25 09:46:15 -04:00
|
|
|
|
2017-01-05 10:25:45 -05:00
|
|
|
private
|
2016-04-25 09:46:15 -04:00
|
|
|
|
2017-01-05 10:25:45 -05:00
|
|
|
attr_accessor :merge_request
|
2014-07-15 08:34:06 -04:00
|
|
|
|
2017-11-24 06:58:05 -05:00
|
|
|
delegate :target_branch,
|
|
|
|
:target_branch_ref,
|
|
|
|
:target_project,
|
|
|
|
:source_branch,
|
|
|
|
:source_branch_ref,
|
|
|
|
:source_project,
|
|
|
|
:compare_commits,
|
|
|
|
:wip_title,
|
|
|
|
:description,
|
|
|
|
:errors,
|
|
|
|
to: :merge_request
|
2017-01-05 10:25:45 -05:00
|
|
|
|
|
|
|
def find_source_project
|
2019-07-12 05:10:54 -04:00
|
|
|
source_project = project_from_params(:source_project)
|
2018-12-06 11:04:34 -05:00
|
|
|
return source_project if source_project.present? && can?(current_user, :create_merge_request_from, source_project)
|
2017-04-03 14:47:14 -04:00
|
|
|
|
|
|
|
project
|
2017-01-05 10:25:45 -05:00
|
|
|
end
|
2016-09-13 17:05:16 -04:00
|
|
|
|
2017-01-05 10:25:45 -05:00
|
|
|
def find_target_project
|
2019-07-12 05:10:54 -04:00
|
|
|
target_project = project_from_params(:target_project)
|
2018-12-06 11:04:34 -05:00
|
|
|
return target_project if target_project.present? && can?(current_user, :create_merge_request_in, target_project)
|
2017-11-14 04:02:39 -05:00
|
|
|
|
2018-12-06 11:04:34 -05:00
|
|
|
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-01-05 10:25:45 -05:00
|
|
|
end
|
|
|
|
|
2019-07-12 05:10:54 -04:00
|
|
|
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-09-05 07:40:45 -04:00
|
|
|
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-01-05 10:25:45 -05:00
|
|
|
end
|
|
|
|
|
2017-02-15 10:01:07 -05:00
|
|
|
def source_branch_specified?
|
|
|
|
params[:source_branch].present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def target_branch_specified?
|
|
|
|
params[:target_branch].present?
|
2017-01-05 10:25:45 -05:00
|
|
|
end
|
|
|
|
|
2018-12-06 11:04:34 -05:00
|
|
|
def projects_and_branches_valid?
|
|
|
|
return false if source_project.nil? || target_project.nil?
|
2017-02-15 10:01:07 -05:00
|
|
|
return false unless source_branch_specified? || target_branch_specified?
|
|
|
|
|
2018-12-06 11:04:34 -05:00
|
|
|
validate_projects_and_branches
|
2017-01-05 10:25:45 -05:00
|
|
|
errors.blank?
|
|
|
|
end
|
|
|
|
|
|
|
|
def compare_branches
|
2016-12-08 04:57:52 -05:00
|
|
|
compare = CompareService.new(
|
2017-01-05 10:25:45 -05:00
|
|
|
source_project,
|
2017-11-24 06:58:05 -05:00
|
|
|
source_branch_ref
|
2016-12-08 04:57:52 -05:00
|
|
|
).execute(
|
2017-01-05 10:25:45 -05:00
|
|
|
target_project,
|
2017-11-24 06:58:05 -05:00
|
|
|
target_branch_ref
|
2014-07-15 08:34:06 -04:00
|
|
|
)
|
|
|
|
|
2017-02-15 10:01:07 -05:00
|
|
|
if compare
|
|
|
|
merge_request.compare_commits = compare.commits
|
|
|
|
merge_request.compare = compare
|
|
|
|
end
|
2016-04-28 06:36:37 -04:00
|
|
|
end
|
|
|
|
|
2018-12-06 11:04:34 -05:00
|
|
|
def validate_projects_and_branches
|
|
|
|
merge_request.validate_target_project
|
|
|
|
merge_request.validate_fork
|
|
|
|
|
|
|
|
return if errors.any?
|
|
|
|
|
2017-01-05 10:25:45 -05:00
|
|
|
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?
|
|
|
|
end
|
2016-10-06 21:09:15 -04:00
|
|
|
|
2017-01-05 10:25:45 -05:00
|
|
|
def add_error(message)
|
|
|
|
errors.add(:base, message)
|
|
|
|
end
|
2016-10-06 21:09:15 -04:00
|
|
|
|
2017-01-05 10:25:45 -05:00
|
|
|
def branches_present?
|
|
|
|
target_branch.present? && source_branch.present?
|
|
|
|
end
|
2016-10-06 21:09:15 -04:00
|
|
|
|
2017-01-05 10:25:45 -05:00
|
|
|
def same_source_and_target?
|
2019-09-05 07:40:45 -04:00
|
|
|
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-01-05 10:25:45 -05:00
|
|
|
end
|
2016-10-06 21:09:15 -04:00
|
|
|
|
2017-01-05 10:25:45 -05:00
|
|
|
def source_branch_exists?
|
|
|
|
source_branch.blank? || source_project.commit(source_branch)
|
|
|
|
end
|
2016-10-06 21:09:15 -04:00
|
|
|
|
2017-01-05 10:25:45 -05:00
|
|
|
def target_branch_exists?
|
|
|
|
target_branch.blank? || target_project.commit(target_branch)
|
2016-10-06 21:09:15 -04:00
|
|
|
end
|
|
|
|
|
2016-04-28 06:36:37 -04:00
|
|
|
# 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:
|
|
|
|
# - Issue 112 exists, title: Emoji don't show up in commit title
|
|
|
|
# - 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-01-05 10:25:45 -05:00
|
|
|
def assign_title_and_description
|
2017-11-25 06:33:05 -05:00
|
|
|
assign_title_and_description_from_single_commit
|
2018-08-02 13:36:43 -04:00
|
|
|
merge_request.title ||= title_from_issue if target_project.issues_enabled? || target_project.external_issue_tracker
|
2017-11-25 06:33:05 -05:00
|
|
|
merge_request.title ||= source_branch.titleize.humanize
|
|
|
|
merge_request.title = wip_title if compare_commits.empty?
|
|
|
|
|
|
|
|
append_closes_description
|
|
|
|
end
|
|
|
|
|
2018-09-06 13:02:51 -04:00
|
|
|
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
|
|
|
|
|
2017-11-25 06:33:05 -05:00
|
|
|
def append_closes_description
|
2018-02-14 06:51:49 -05:00
|
|
|
return unless issue&.to_reference.present?
|
2017-11-25 06:33:05 -05:00
|
|
|
|
2018-02-02 12:20:48 -05:00
|
|
|
closes_issue = "Closes #{issue.to_reference}"
|
2017-11-25 06:33:05 -05:00
|
|
|
|
|
|
|
if description.present?
|
2018-07-17 12:50:37 -04:00
|
|
|
descr_parts = [merge_request.description, closes_issue]
|
|
|
|
merge_request.description = descr_parts.join("\n\n")
|
2015-02-20 16:17:38 -05:00
|
|
|
else
|
2017-11-25 06:33:05 -05:00
|
|
|
merge_request.description = closes_issue
|
2015-02-20 16:17:38 -05:00
|
|
|
end
|
2017-11-25 06:33:05 -05:00
|
|
|
end
|
2015-02-20 09:06:06 -05:00
|
|
|
|
2017-11-25 06:33:05 -05:00
|
|
|
def assign_title_and_description_from_single_commit
|
|
|
|
commits = compare_commits
|
|
|
|
|
|
|
|
return unless commits&.count == 1
|
|
|
|
|
|
|
|
commit = commits.first
|
|
|
|
merge_request.title ||= commit.title
|
|
|
|
merge_request.description ||= commit.description.try(:strip)
|
|
|
|
end
|
|
|
|
|
2018-08-02 13:36:43 -04:00
|
|
|
def title_from_issue
|
2018-02-02 12:20:48 -05:00
|
|
|
return unless issue
|
|
|
|
|
2018-08-02 13:36:43 -04:00
|
|
|
return "Resolve \"#{issue.title}\"" if issue.is_a?(Issue)
|
2016-02-12 13:42:25 -05:00
|
|
|
|
2018-08-02 13:33:49 -04:00
|
|
|
return if issue_iid.blank?
|
2018-02-12 20:29:28 -05:00
|
|
|
|
2018-08-02 13:33:49 -04:00
|
|
|
title_parts = ["Resolve #{issue.to_reference}"]
|
|
|
|
branch_title = source_branch.downcase.remove(issue_iid.downcase).titleize.humanize
|
2018-07-17 12:50:37 -04:00
|
|
|
|
2018-08-02 13:33:49 -04:00
|
|
|
title_parts << "\"#{branch_title}\"" if branch_title.present?
|
2018-08-02 13:36:43 -04:00
|
|
|
title_parts.join(' ')
|
2017-11-25 06:33:05 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def issue_iid
|
2018-02-02 12:20:48 -05:00
|
|
|
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
|
|
|
|
|
|
|
|
id || source_branch.match(/\A(\d+)-/).try(:[], 1)
|
|
|
|
end
|
|
|
|
end
|
2017-11-25 06:33:05 -05:00
|
|
|
end
|
2016-02-12 13:42:25 -05:00
|
|
|
|
2017-11-25 06:33:05 -05:00
|
|
|
def issue
|
2018-09-06 13:02:51 -04:00
|
|
|
strong_memoize(:issue) do
|
|
|
|
target_project.get_issue(issue_iid, current_user)
|
|
|
|
end
|
2014-07-15 08:34:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
|
|
|
MergeRequests::BuildService.prepend_if_ee('EE::MergeRequests::BuildService')
|