2015-12-15 14:51:16 +00:00
|
|
|
module Banzai
|
|
|
|
module Filter
|
2015-04-14 22:34:01 +00:00
|
|
|
# HTML filter that replaces commit references with links.
|
2015-04-03 00:46:43 +00:00
|
|
|
#
|
|
|
|
# This filter supports cross-project references.
|
2015-11-30 20:14:46 +00:00
|
|
|
class CommitReferenceFilter < AbstractReferenceFilter
|
2016-05-26 11:16:43 +00:00
|
|
|
self.reference_type = :commit
|
|
|
|
|
2015-11-30 20:14:46 +00:00
|
|
|
def self.object_class
|
|
|
|
Commit
|
|
|
|
end
|
2015-04-03 00:46:43 +00:00
|
|
|
|
2015-12-01 14:51:27 +00:00
|
|
|
def self.references_in(text, pattern = Commit.reference_pattern)
|
|
|
|
text.gsub(pattern) do |match|
|
2016-11-02 23:49:13 +00:00
|
|
|
yield match, $~[:commit], $~[:project], $~[:namespace], $~
|
2015-04-03 00:46:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-05-26 11:16:43 +00:00
|
|
|
def find_object(project, id)
|
2018-04-12 09:37:01 +00:00
|
|
|
return unless project.is_a?(Project)
|
|
|
|
|
2015-04-16 18:06:05 +00:00
|
|
|
if project && project.valid_repo?
|
2018-03-05 18:48:56 +00:00
|
|
|
# n+1: https://gitlab.com/gitlab-org/gitlab-ce/issues/43894
|
|
|
|
Gitlab::GitalyClient.allow_n_plus_1_calls { project.commit(id) }
|
2015-04-16 18:06:05 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-22 14:48:09 +00:00
|
|
|
def referenced_merge_request_commit_shas
|
|
|
|
return [] unless noteable.is_a?(MergeRequest)
|
|
|
|
|
|
|
|
@referenced_merge_request_commit_shas ||= begin
|
2017-12-07 15:35:56 +00:00
|
|
|
referenced_shas = references_per_parent.values.reduce(:|).to_a
|
2017-11-22 14:48:09 +00:00
|
|
|
noteable.all_commit_shas.select do |sha|
|
|
|
|
referenced_shas.any? { |ref| Gitlab::Git.shas_eql?(sha, ref) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-11-30 20:14:46 +00:00
|
|
|
def url_for_object(commit, project)
|
2016-03-24 16:00:26 +00:00
|
|
|
h = Gitlab::Routing.url_helpers
|
2017-11-20 14:02:01 +00:00
|
|
|
|
2017-11-22 14:48:09 +00:00
|
|
|
if referenced_merge_request_commit_shas.include?(commit.id)
|
|
|
|
h.diffs_project_merge_request_url(project,
|
|
|
|
noteable,
|
|
|
|
commit_id: commit.id,
|
|
|
|
only_path: only_path?)
|
2017-11-20 14:02:01 +00:00
|
|
|
else
|
2017-11-22 14:48:09 +00:00
|
|
|
h.project_commit_url(project,
|
|
|
|
commit,
|
|
|
|
only_path: only_path?)
|
2017-11-20 14:02:01 +00:00
|
|
|
end
|
2015-04-03 00:46:43 +00:00
|
|
|
end
|
2015-11-30 20:14:46 +00:00
|
|
|
|
2015-12-08 12:04:59 +00:00
|
|
|
def object_link_text_extras(object, matches)
|
|
|
|
extras = super
|
|
|
|
|
|
|
|
path = matches[:path] if matches.names.include?("path")
|
|
|
|
if path == '/builds'
|
|
|
|
extras.unshift "builds"
|
|
|
|
end
|
|
|
|
|
|
|
|
extras
|
|
|
|
end
|
2017-11-22 14:48:09 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def noteable
|
|
|
|
context[:noteable]
|
|
|
|
end
|
|
|
|
|
|
|
|
def only_path?
|
|
|
|
context[:only_path]
|
|
|
|
end
|
2015-04-03 00:46:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|