Commit 223f1f3d authored by Sean McGivern's avatar Sean McGivern

Merge branch '24795_refactor_merge_request_build_service' into 'master'

Refactor MergeRequest::BuildService

Closes #24795

See merge request !8462
parents 4427a263 14326c88
module MergeRequests module MergeRequests
class BuildService < MergeRequests::BaseService class BuildService < MergeRequests::BaseService
def execute def execute
merge_request = MergeRequest.new(params) self.merge_request = MergeRequest.new(params)
# Set MR attributes
merge_request.can_be_created = true merge_request.can_be_created = true
merge_request.compare_commits = [] merge_request.compare_commits = []
merge_request.source_project = project unless merge_request.source_project merge_request.source_project = find_source_project
merge_request.target_project = find_target_project
merge_request.target_branch = find_target_branch
merge_request.target_project = nil unless can?(current_user, :read_project, merge_request.target_project) if branches_specified? && branches_valid?
compare_branches
assign_title_and_description
else
merge_request.can_be_created = false
end
merge_request.target_project ||= (project.forked_from_project || project) merge_request
merge_request.target_branch ||= merge_request.target_project.default_branch end
messages = validate_branches(merge_request) private
return build_failed(merge_request, messages) unless messages.empty?
compare = CompareService.new.execute( attr_accessor :merge_request
merge_request.source_project,
merge_request.source_branch,
merge_request.target_project,
merge_request.target_branch,
)
merge_request.compare_commits = compare.commits delegate :target_branch, :source_branch, :source_project, :target_project, :compare_commits, :wip_title, :description, :errors, to: :merge_request
merge_request.compare = compare
set_title_and_description(merge_request) def find_source_project
source_project || project
end end
private def find_target_project
return target_project if target_project.present? && can?(current_user, :read_project, target_project)
project.forked_from_project || project
end
def validate_branches(merge_request) def find_target_branch
messages = [] target_branch || target_project.default_branch
end
if merge_request.target_branch.blank? || merge_request.source_branch.blank? def branches_specified?
messages << params[:source_branch] && params[:target_branch]
if params[:source_branch] || params[:target_branch]
"You must select source and target branch"
end end
def branches_valid?
validate_branches
errors.blank?
end end
if merge_request.source_project == merge_request.target_project && def compare_branches
merge_request.target_branch == merge_request.source_branch compare = CompareService.new.execute(
source_project,
source_branch,
target_project,
target_branch
)
messages << 'You must select different branches' merge_request.compare_commits = compare.commits
merge_request.compare = compare
end
def validate_branches
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
def add_error(message)
errors.add(:base, message)
end
def branches_present?
target_branch.present? && source_branch.present?
end end
# See if source and target branches exist def same_source_and_target?
if merge_request.source_branch.present? && !merge_request.source_project.commit(merge_request.source_branch) source_project == target_project && target_branch == source_branch
messages << "Source branch \"#{merge_request.source_branch}\" does not exist"
end end
if merge_request.target_branch.present? && !merge_request.target_project.commit(merge_request.target_branch) def source_branch_exists?
messages << "Target branch \"#{merge_request.target_branch}\" does not exist" source_branch.blank? || source_project.commit(source_branch)
end end
messages def target_branch_exists?
target_branch.blank? || target_project.commit(target_branch)
end end
# When your branch name starts with an iid followed by a dash this pattern will be # When your branch name starts with an iid followed by a dash this pattern will be
...@@ -71,17 +97,17 @@ module MergeRequests ...@@ -71,17 +97,17 @@ module MergeRequests
# - Setting the title as 'Resolves "Emoji don't show up in commit title"' if there is # - Setting the title as 'Resolves "Emoji don't show up in commit title"' if there is
# more than one commit in the MR # more than one commit in the MR
# #
def set_title_and_description(merge_request) def assign_title_and_description
if match = merge_request.source_branch.match(/\A(\d+)-/) if match = source_branch.match(/\A(\d+)-/)
iid = match[1] iid = match[1]
end end
commits = merge_request.compare_commits commits = compare_commits
if commits && commits.count == 1 if commits && commits.count == 1
commit = commits.first commit = commits.first
merge_request.title = commit.title merge_request.title = commit.title
merge_request.description ||= commit.description.try(:strip) merge_request.description ||= commit.description.try(:strip)
elsif iid && issue = merge_request.target_project.get_issue(iid, current_user) elsif iid && issue = target_project.get_issue(iid, current_user)
case issue case issue
when Issue when Issue
merge_request.title = "Resolve \"#{issue.title}\"" merge_request.title = "Resolve \"#{issue.title}\""
...@@ -89,31 +115,20 @@ module MergeRequests ...@@ -89,31 +115,20 @@ module MergeRequests
merge_request.title = "Resolve #{issue.title}" merge_request.title = "Resolve #{issue.title}"
end end
else else
merge_request.title = merge_request.source_branch.titleize.humanize merge_request.title = source_branch.titleize.humanize
end end
if iid if iid
closes_issue = "Closes ##{iid}" closes_issue = "Closes ##{iid}"
if merge_request.description.present? if description.present?
merge_request.description += closes_issue.prepend("\n\n") merge_request.description += closes_issue.prepend("\n\n")
else else
merge_request.description = closes_issue merge_request.description = closes_issue
end end
end end
merge_request.title = merge_request.wip_title if commits.empty? merge_request.title = wip_title if commits.empty?
merge_request
end
def build_failed(merge_request, messages)
messages.compact.each do |message|
merge_request.errors.add(:base, message)
end
merge_request.compare_commits = []
merge_request.can_be_created = false
merge_request
end end
end end
end end
---
title: Refactor MergeRequests::BuildService
merge_request: 8462
author: Rydkin Maxim
...@@ -11,7 +11,7 @@ feature 'Merge Requests > User uses slash commands', feature: true, js: true do ...@@ -11,7 +11,7 @@ feature 'Merge Requests > User uses slash commands', feature: true, js: true do
it_behaves_like 'issuable record that supports slash commands in its description and notes', :merge_request do it_behaves_like 'issuable record that supports slash commands in its description and notes', :merge_request do
let(:issuable) { create(:merge_request, source_project: project) } let(:issuable) { create(:merge_request, source_project: project) }
let(:new_url_opts) { { merge_request: { source_branch: 'feature' } } } let(:new_url_opts) { { merge_request: { source_branch: 'feature', target_branch: 'master' } } }
end end
describe 'merge-request-only commands' do describe 'merge-request-only commands' do
......
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