2014-09-02 18:07:02 +05:30
|
|
|
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-09-25 12:07:36 +05:30
|
|
|
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-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
compare_result = CompareService.new.execute(
|
|
|
|
merge_request.source_project,
|
|
|
|
merge_request.source_branch,
|
|
|
|
merge_request.target_project,
|
|
|
|
merge_request.target_branch,
|
|
|
|
)
|
|
|
|
|
|
|
|
commits = compare_result.commits
|
|
|
|
|
|
|
|
# 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-09-11 14:41:01 +05:30
|
|
|
merge_request.compare_commits = Commit.decorate(commits, merge_request.source_project)
|
2014-09-02 18:07:02 +05:30
|
|
|
merge_request.can_be_created = true
|
|
|
|
merge_request.compare_failed = false
|
|
|
|
|
|
|
|
# Try to collect diff for merge request.
|
|
|
|
diffs = compare_result.diffs
|
|
|
|
|
|
|
|
if diffs.present?
|
|
|
|
merge_request.compare_diffs = diffs
|
|
|
|
|
|
|
|
elsif diffs == false
|
|
|
|
merge_request.can_be_created = false
|
|
|
|
merge_request.compare_failed = true
|
|
|
|
end
|
|
|
|
else
|
|
|
|
merge_request.can_be_created = false
|
|
|
|
merge_request.compare_failed = false
|
|
|
|
end
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
commits = merge_request.compare_commits
|
|
|
|
if commits && commits.count == 1
|
|
|
|
commit = commits.first
|
|
|
|
merge_request.title = commit.title
|
2016-04-02 18:10:28 +05:30
|
|
|
merge_request.description ||= commit.description.try(:strip)
|
2015-04-26 12:48:37 +05:30
|
|
|
else
|
|
|
|
merge_request.title = merge_request.source_branch.titleize.humanize
|
|
|
|
end
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
merge_request
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_failed(merge_request, message)
|
2015-04-26 12:48:37 +05:30
|
|
|
merge_request.errors.add(:base, message) unless message.nil?
|
2014-09-02 18:07:02 +05:30
|
|
|
merge_request.compare_commits = []
|
|
|
|
merge_request.can_be_created = false
|
|
|
|
merge_request
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|