2014-07-15 08:34:06 -04:00
|
|
|
module MergeRequests
|
|
|
|
class BuildService < MergeRequests::BaseService
|
|
|
|
def execute
|
2017-01-05 10:25:45 -05:00
|
|
|
self.merge_request = MergeRequest.new(params)
|
2014-07-15 08:34:06 -04:00
|
|
|
merge_request.compare_commits = []
|
2017-01-05 10:25:45 -05:00
|
|
|
merge_request.source_project = find_source_project
|
|
|
|
merge_request.target_project = find_target_project
|
|
|
|
merge_request.target_branch = find_target_branch
|
2017-03-17 09:12:16 -04:00
|
|
|
merge_request.can_be_created = branches_valid?
|
2017-01-05 10:25:45 -05:00
|
|
|
|
2017-02-15 10:01:07 -05:00
|
|
|
compare_branches if branches_present?
|
|
|
|
assign_title_and_description if merge_request.can_be_created
|
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-01-05 10:25:45 -05:00
|
|
|
delegate :target_branch, :source_branch, :source_project, :target_project, :compare_commits, :wip_title, :description, :errors, to: :merge_request
|
|
|
|
|
|
|
|
def find_source_project
|
2017-04-03 14:47:14 -04:00
|
|
|
return source_project if source_project.present? && can?(current_user, :read_project, source_project)
|
|
|
|
|
|
|
|
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
|
|
|
|
return target_project if target_project.present? && can?(current_user, :read_project, target_project)
|
2017-04-26 18:04:07 -04:00
|
|
|
project.default_merge_request_target
|
2017-01-05 10:25:45 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def find_target_branch
|
|
|
|
target_branch || target_project.default_branch
|
|
|
|
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
|
|
|
|
|
|
|
|
def branches_valid?
|
2017-02-15 10:01:07 -05:00
|
|
|
return false unless source_branch_specified? || target_branch_specified?
|
|
|
|
|
2017-01-05 10:25:45 -05:00
|
|
|
validate_branches
|
|
|
|
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-02-01 22:54:35 -05:00
|
|
|
source_branch
|
2016-12-08 04:57:52 -05:00
|
|
|
).execute(
|
2017-01-05 10:25:45 -05:00
|
|
|
target_project,
|
|
|
|
target_branch
|
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
|
|
|
|
|
2017-01-05 10:25:45 -05:00
|
|
|
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
|
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?
|
|
|
|
source_project == target_project && target_branch == source_branch
|
|
|
|
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
|
|
|
|
if match = source_branch.match(/\A(\d+)-/)
|
2016-04-28 06:36:37 -04:00
|
|
|
iid = match[1]
|
|
|
|
end
|
|
|
|
|
2017-01-05 10:25:45 -05:00
|
|
|
commits = compare_commits
|
2015-02-20 16:17:38 -05:00
|
|
|
if commits && commits.count == 1
|
|
|
|
commit = commits.first
|
2016-05-10 22:58:06 -04:00
|
|
|
merge_request.title = commit.title
|
2016-02-01 15:35:11 -05:00
|
|
|
merge_request.description ||= commit.description.try(:strip)
|
2017-01-05 10:25:45 -05:00
|
|
|
elsif iid && issue = target_project.get_issue(iid, current_user)
|
2016-04-28 06:36:37 -04:00
|
|
|
case issue
|
|
|
|
when Issue
|
|
|
|
merge_request.title = "Resolve \"#{issue.title}\""
|
|
|
|
when ExternalIssue
|
|
|
|
merge_request.title = "Resolve #{issue.title}"
|
|
|
|
end
|
2015-02-20 16:17:38 -05:00
|
|
|
else
|
2017-01-05 10:25:45 -05:00
|
|
|
merge_request.title = source_branch.titleize.humanize
|
2015-02-20 16:17:38 -05:00
|
|
|
end
|
2015-02-20 09:06:06 -05:00
|
|
|
|
2016-04-28 06:36:37 -04:00
|
|
|
if iid
|
2016-02-17 01:11:48 -05:00
|
|
|
closes_issue = "Closes ##{iid}"
|
2016-02-12 13:42:25 -05:00
|
|
|
|
2017-01-05 10:25:45 -05:00
|
|
|
if description.present?
|
2016-09-01 10:52:43 -04:00
|
|
|
merge_request.description += closes_issue.prepend("\n\n")
|
2016-02-17 01:11:48 -05:00
|
|
|
else
|
|
|
|
merge_request.description = closes_issue
|
|
|
|
end
|
2016-02-12 13:42:25 -05:00
|
|
|
end
|
|
|
|
|
2017-01-05 10:25:45 -05:00
|
|
|
merge_request.title = wip_title if commits.empty?
|
2014-07-15 08:34:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|