2014-07-15 08:34:06 -04: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_failed = false
|
|
|
|
merge_request.compare_commits = []
|
|
|
|
merge_request.compare_diffs = []
|
|
|
|
merge_request.source_project = project unless merge_request.source_project
|
|
|
|
merge_request.target_project ||= (project.forked_from_project || project)
|
|
|
|
merge_request.target_branch ||= merge_request.target_project.default_branch
|
|
|
|
|
2015-08-11 08:33:31 -04: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 08:34:06 -04:00
|
|
|
end
|
|
|
|
|
2014-07-29 05:11:16 -04:00
|
|
|
compare_result = CompareService.new.execute(
|
|
|
|
merge_request.source_project,
|
|
|
|
merge_request.source_branch,
|
2014-07-15 08:34:06 -04:00
|
|
|
merge_request.target_project,
|
|
|
|
merge_request.target_branch,
|
|
|
|
)
|
|
|
|
|
2014-07-29 05:11:16 -04:00
|
|
|
commits = compare_result.commits
|
2014-07-15 08:34:06 -04: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?
|
2015-04-21 09:09:15 -04:00
|
|
|
merge_request.compare_commits = Commit.decorate(commits, merge_request.source_project)
|
2014-07-15 08:34:06 -04:00
|
|
|
merge_request.can_be_created = true
|
|
|
|
merge_request.compare_failed = false
|
|
|
|
|
|
|
|
# Try to collect diff for merge request.
|
2014-07-29 05:11:16 -04:00
|
|
|
diffs = compare_result.diffs
|
2014-07-15 08:34:06 -04:00
|
|
|
|
|
|
|
if diffs.present?
|
|
|
|
merge_request.compare_diffs = diffs
|
2014-07-15 11:28:21 -04:00
|
|
|
|
|
|
|
elsif diffs == false
|
|
|
|
merge_request.can_be_created = false
|
|
|
|
merge_request.compare_failed = true
|
2014-07-15 08:34:06 -04:00
|
|
|
end
|
|
|
|
else
|
|
|
|
merge_request.can_be_created = false
|
2014-07-15 16:04:14 -04:00
|
|
|
merge_request.compare_failed = false
|
2014-07-15 08:34:06 -04:00
|
|
|
end
|
|
|
|
|
2015-02-20 09:06:06 -05:00
|
|
|
commits = merge_request.compare_commits
|
2015-02-20 16:17:38 -05:00
|
|
|
if commits && commits.count == 1
|
|
|
|
commit = commits.first
|
|
|
|
merge_request.title = commit.title
|
|
|
|
merge_request.description = commit.description.try(:strip)
|
|
|
|
else
|
|
|
|
merge_request.title = merge_request.source_branch.titleize.humanize
|
|
|
|
end
|
2015-02-20 09:06:06 -05:00
|
|
|
|
2014-07-15 08:34:06 -04:00
|
|
|
merge_request
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_failed(merge_request, message)
|
2014-12-26 16:24:21 -05:00
|
|
|
merge_request.errors.add(:base, message) unless message.nil?
|
2014-07-15 08:34:06 -04:00
|
|
|
merge_request.compare_commits = []
|
|
|
|
merge_request.can_be_created = false
|
|
|
|
merge_request
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|