2014-07-15 12:34:06 +00:00
|
|
|
module MergeRequests
|
|
|
|
class BuildService < MergeRequests::BaseService
|
|
|
|
def execute
|
|
|
|
merge_request = MergeRequest.new(params)
|
|
|
|
|
|
|
|
# Set MR attributes
|
|
|
|
merge_request.can_be_created = false
|
|
|
|
merge_request.compare_commits = []
|
|
|
|
merge_request.source_project = project unless merge_request.source_project
|
2016-04-25 13:46:15 +00:00
|
|
|
|
|
|
|
merge_request.target_project = nil unless can?(current_user, :read_project, merge_request.target_project)
|
|
|
|
|
2014-07-15 12:34:06 +00:00
|
|
|
merge_request.target_project ||= (project.forked_from_project || project)
|
|
|
|
merge_request.target_branch ||= merge_request.target_project.default_branch
|
|
|
|
|
2015-08-11 12:33:31 +00:00
|
|
|
if merge_request.target_branch.blank? || merge_request.source_branch.blank?
|
|
|
|
message =
|
|
|
|
if params[:source_branch] || params[:target_branch]
|
|
|
|
"You must select source and target branch"
|
|
|
|
end
|
|
|
|
|
|
|
|
return build_failed(merge_request, message)
|
2014-07-15 12:34:06 +00:00
|
|
|
end
|
|
|
|
|
2016-03-03 17:38:44 +00:00
|
|
|
compare = CompareService.new.execute(
|
2014-07-29 09:11:16 +00:00
|
|
|
merge_request.source_project,
|
|
|
|
merge_request.source_branch,
|
2014-07-15 12:34:06 +00:00
|
|
|
merge_request.target_project,
|
|
|
|
merge_request.target_branch,
|
|
|
|
)
|
|
|
|
|
2016-03-03 17:38:44 +00:00
|
|
|
commits = compare.commits
|
2014-07-15 12:34:06 +00:00
|
|
|
|
|
|
|
# At this point we decide if merge request can be created
|
|
|
|
# If we have at least one commit to merge -> creation allowed
|
|
|
|
if commits.present?
|
2016-07-27 11:09:52 +00:00
|
|
|
merge_request.compare_commits = commits
|
2014-07-15 12:34:06 +00:00
|
|
|
merge_request.can_be_created = true
|
2016-03-03 17:38:44 +00:00
|
|
|
merge_request.compare = compare
|
2014-07-15 12:34:06 +00:00
|
|
|
else
|
|
|
|
merge_request.can_be_created = false
|
|
|
|
end
|
|
|
|
|
2016-04-28 10:36:37 +00:00
|
|
|
set_title_and_description(merge_request)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# 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
|
|
|
|
#
|
|
|
|
def set_title_and_description(merge_request)
|
|
|
|
if match = merge_request.source_branch.match(/\A(\d+)-/)
|
|
|
|
iid = match[1]
|
|
|
|
end
|
|
|
|
|
2015-02-20 14:06:06 +00:00
|
|
|
commits = merge_request.compare_commits
|
2015-02-20 21:17:38 +00:00
|
|
|
if commits && commits.count == 1
|
|
|
|
commit = commits.first
|
2016-05-11 02:58:06 +00:00
|
|
|
merge_request.title = commit.title
|
2016-02-01 20:35:11 +00:00
|
|
|
merge_request.description ||= commit.description.try(:strip)
|
2016-04-28 11:12:03 +00:00
|
|
|
elsif iid && (issue = merge_request.target_project.get_issue(iid)) && !issue.try(:confidential?)
|
2016-04-28 10:36:37 +00:00
|
|
|
case issue
|
|
|
|
when Issue
|
|
|
|
merge_request.title = "Resolve \"#{issue.title}\""
|
|
|
|
when ExternalIssue
|
|
|
|
merge_request.title = "Resolve #{issue.title}"
|
|
|
|
end
|
2015-02-20 21:17:38 +00:00
|
|
|
else
|
|
|
|
merge_request.title = merge_request.source_branch.titleize.humanize
|
|
|
|
end
|
2015-02-20 14:06:06 +00:00
|
|
|
|
2016-04-28 10:36:37 +00:00
|
|
|
if iid
|
2016-02-17 06:11:48 +00:00
|
|
|
closes_issue = "Closes ##{iid}"
|
2016-02-12 18:42:25 +00:00
|
|
|
|
2016-02-17 06:11:48 +00:00
|
|
|
if merge_request.description.present?
|
2016-09-01 14:52:43 +00:00
|
|
|
merge_request.description += closes_issue.prepend("\n\n")
|
2016-02-17 06:11:48 +00:00
|
|
|
else
|
|
|
|
merge_request.description = closes_issue
|
|
|
|
end
|
2016-02-12 18:42:25 +00:00
|
|
|
end
|
|
|
|
|
2014-07-15 12:34:06 +00:00
|
|
|
merge_request
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_failed(merge_request, message)
|
2014-12-26 21:24:21 +00:00
|
|
|
merge_request.errors.add(:base, message) unless message.nil?
|
2014-07-15 12:34:06 +00:00
|
|
|
merge_request.compare_commits = []
|
|
|
|
merge_request.can_be_created = false
|
|
|
|
merge_request
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|