debian-mirror-gitlab/app/services/merge_requests/get_urls_service.rb

65 lines
2 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2016-09-13 17:45:13 +05:30
module MergeRequests
class GetUrlsService < BaseService
attr_reader :project
def initialize(project)
@project = project
end
def execute(changes)
2017-08-17 22:00:37 +05:30
return [] unless project.printing_merge_request_link_enabled
2016-09-13 17:45:13 +05:30
branches = get_branches(changes)
merge_requests_map = opened_merge_requests_from_source_branches(branches)
branches.map do |branch|
existing_merge_request = merge_requests_map[branch]
if existing_merge_request
url_for_existing_merge_request(existing_merge_request)
else
url_for_new_merge_request(branch)
end
end
end
private
def opened_merge_requests_from_source_branches(branches)
merge_requests = MergeRequest.from_project(project).opened.from_source_branches(branches)
2017-08-17 22:00:37 +05:30
merge_requests.index_by(&:source_branch)
2016-09-13 17:45:13 +05:30
end
def get_branches(changes)
return [] if project.empty_repo?
2016-09-29 09:46:39 +05:30
return [] unless project.merge_requests_enabled?
2016-09-13 17:45:13 +05:30
changes_list = Gitlab::ChangesList.new(changes)
changes_list.map do |change|
next unless Gitlab::Git.branch_ref?(change[:ref])
# Deleted branch
next if Gitlab::Git.blank_ref?(change[:newrev])
# Default branch
branch_name = Gitlab::Git.branch_name(change[:ref])
next if branch_name == project.default_branch
branch_name
end.compact
end
def url_for_new_merge_request(branch_name)
merge_request_params = { source_branch: branch_name }
2017-09-10 17:25:29 +05:30
url = Gitlab::Routing.url_helpers.project_new_merge_request_url(project, merge_request: merge_request_params)
2016-09-13 17:45:13 +05:30
{ branch_name: branch_name, url: url, new_merge_request: true }
end
def url_for_existing_merge_request(merge_request)
target_project = merge_request.target_project
2017-09-10 17:25:29 +05:30
url = Gitlab::Routing.url_helpers.project_merge_request_url(target_project, merge_request)
2016-09-13 17:45:13 +05:30
{ branch_name: merge_request.source_branch, url: url, new_merge_request: false }
end
end
end