2015-12-15 14:51:16 +00:00
|
|
|
module Banzai
|
|
|
|
module Filter
|
2015-04-03 00:46:43 +00:00
|
|
|
# HTML filter that replaces merge request references with links. References
|
2015-04-14 22:34:01 +00:00
|
|
|
# to merge requests that do not exist are ignored.
|
2015-04-03 00:46:43 +00:00
|
|
|
#
|
|
|
|
# This filter supports cross-project references.
|
2017-12-01 09:21:05 +00:00
|
|
|
class MergeRequestReferenceFilter < IssuableReferenceFilter
|
2016-05-26 11:16:43 +00:00
|
|
|
self.reference_type = :merge_request
|
|
|
|
|
2015-11-16 20:56:28 +00:00
|
|
|
def self.object_class
|
|
|
|
MergeRequest
|
2015-04-03 00:46:43 +00:00
|
|
|
end
|
|
|
|
|
2015-11-16 20:56:28 +00:00
|
|
|
def url_for_object(mr, project)
|
2016-03-24 16:00:26 +00:00
|
|
|
h = Gitlab::Routing.url_helpers
|
2017-06-29 17:06:35 +00:00
|
|
|
h.project_merge_request_url(project, mr,
|
2015-04-03 00:46:43 +00:00
|
|
|
only_path: context[:only_path])
|
|
|
|
end
|
2015-11-30 20:14:46 +00:00
|
|
|
|
2018-04-02 23:25:36 +00:00
|
|
|
def object_link_title(object, matches)
|
|
|
|
object_link_commit_title(object, matches) || super
|
|
|
|
end
|
|
|
|
|
2015-11-30 20:14:46 +00:00
|
|
|
def object_link_text_extras(object, matches)
|
|
|
|
extras = super
|
|
|
|
|
2018-04-02 23:25:36 +00:00
|
|
|
if commit_ref = object_link_commit_ref(object, matches)
|
|
|
|
return extras.unshift(commit_ref)
|
|
|
|
end
|
|
|
|
|
2015-12-08 12:04:59 +00:00
|
|
|
path = matches[:path] if matches.names.include?("path")
|
2018-04-02 23:25:36 +00:00
|
|
|
|
2015-12-08 12:04:59 +00:00
|
|
|
case path
|
|
|
|
when '/diffs'
|
2015-11-30 20:14:46 +00:00
|
|
|
extras.unshift "diffs"
|
2015-12-08 12:04:59 +00:00
|
|
|
when '/commits'
|
|
|
|
extras.unshift "commits"
|
2015-12-08 21:28:28 +00:00
|
|
|
when '/builds'
|
|
|
|
extras.unshift "builds"
|
2015-11-30 20:14:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
extras
|
|
|
|
end
|
2017-12-01 09:21:05 +00:00
|
|
|
|
|
|
|
def parent_records(parent, ids)
|
|
|
|
parent.merge_requests
|
|
|
|
.where(iid: ids.to_a)
|
|
|
|
.includes(target_project: :namespace)
|
|
|
|
end
|
2018-04-02 23:25:36 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def object_link_commit_title(object, matches)
|
|
|
|
object_link_commit(object, matches)&.title
|
|
|
|
end
|
|
|
|
|
|
|
|
def object_link_commit_ref(object, matches)
|
|
|
|
object_link_commit(object, matches)&.short_id
|
|
|
|
end
|
|
|
|
|
|
|
|
def object_link_commit(object, matches)
|
|
|
|
return unless matches.names.include?('query') && query = matches[:query]
|
|
|
|
|
|
|
|
# Removes leading "?". CGI.parse expects "arg1&arg2&arg3"
|
|
|
|
params = CGI.parse(query.sub(/^\?/, ''))
|
|
|
|
|
|
|
|
return unless commit_sha = params['commit_id']&.first
|
|
|
|
|
|
|
|
if commit = find_commit_by_sha(object, commit_sha)
|
|
|
|
Commit.from_hash(commit.to_hash, object.project)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_commit_by_sha(object, commit_sha)
|
|
|
|
@all_commits ||= {}
|
|
|
|
@all_commits[object.id] ||= object.all_commits
|
|
|
|
|
|
|
|
@all_commits[object.id].find { |commit| commit.sha == commit_sha }
|
|
|
|
end
|
2015-04-03 00:46:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|