2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-12-23 02:04:40 +05:30
|
|
|
module Banzai
|
|
|
|
module Filter
|
|
|
|
# HTML filter that replaces commit references with links.
|
|
|
|
#
|
|
|
|
# This filter supports cross-project references.
|
|
|
|
class CommitReferenceFilter < AbstractReferenceFilter
|
2016-06-16 23:09:34 +05:30
|
|
|
self.reference_type = :commit
|
|
|
|
|
2015-12-23 02:04:40 +05:30
|
|
|
def self.object_class
|
|
|
|
Commit
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.references_in(text, pattern = Commit.reference_pattern)
|
|
|
|
text.gsub(pattern) do |match|
|
2017-08-17 22:00:37 +05:30
|
|
|
yield match, $~[:commit], $~[:project], $~[:namespace], $~
|
2015-12-23 02:04:40 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
def find_object(project, id)
|
2018-05-09 12:01:36 +05:30
|
|
|
return unless project.is_a?(Project)
|
|
|
|
|
2015-12-23 02:04:40 +05:30
|
|
|
if project && project.valid_repo?
|
2018-03-27 19:54:05 +05:30
|
|
|
# n+1: https://gitlab.com/gitlab-org/gitlab-ce/issues/43894
|
|
|
|
Gitlab::GitalyClient.allow_n_plus_1_calls { project.commit(id) }
|
2015-12-23 02:04:40 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def referenced_merge_request_commit_shas
|
|
|
|
return [] unless noteable.is_a?(MergeRequest)
|
|
|
|
|
|
|
|
@referenced_merge_request_commit_shas ||= begin
|
|
|
|
referenced_shas = references_per_parent.values.reduce(:|).to_a
|
|
|
|
noteable.all_commit_shas.select do |sha|
|
|
|
|
referenced_shas.any? { |ref| Gitlab::Git.shas_eql?(sha, ref) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-12-23 02:04:40 +05:30
|
|
|
def url_for_object(commit, project)
|
2016-06-02 11:05:42 +05:30
|
|
|
h = Gitlab::Routing.url_helpers
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
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?)
|
|
|
|
else
|
|
|
|
h.project_commit_url(project,
|
|
|
|
commit,
|
|
|
|
only_path: only_path?)
|
|
|
|
end
|
2015-12-23 02:04:40 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
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
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def noteable
|
|
|
|
context[:noteable]
|
|
|
|
end
|
|
|
|
|
|
|
|
def only_path?
|
|
|
|
context[:only_path]
|
|
|
|
end
|
2015-12-23 02:04:40 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|