2021-04-15 18:09:01 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Banzai
|
|
|
|
module Filter
|
|
|
|
module References
|
|
|
|
# HTML filter that replaces commit references with links.
|
|
|
|
#
|
|
|
|
# This filter supports cross-project references.
|
|
|
|
class CommitReferenceFilter < AbstractReferenceFilter
|
|
|
|
self.reference_type = :commit
|
2021-04-22 12:09:49 +00:00
|
|
|
self.object_class = Commit
|
2021-04-15 18:09:01 +00:00
|
|
|
|
2021-04-22 12:09:49 +00:00
|
|
|
def references_in(text, pattern = object_reference_pattern)
|
2021-04-15 18:09:01 +00:00
|
|
|
text.gsub(pattern) do |match|
|
|
|
|
yield match, $~[:commit], $~[:project], $~[:namespace], $~
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_object(project, id)
|
|
|
|
return unless project.is_a?(Project) && project.valid_repo?
|
|
|
|
|
2022-05-19 09:09:08 +00:00
|
|
|
# Optimization: try exact commit hash match first
|
|
|
|
record = reference_cache.records_per_parent[project].fetch(id, nil)
|
|
|
|
|
|
|
|
unless record
|
|
|
|
_, record = reference_cache.records_per_parent[project].detect { |k, _v| Gitlab::Git.shas_eql?(k, id) }
|
|
|
|
end
|
2021-04-15 18:09:01 +00:00
|
|
|
|
|
|
|
record
|
|
|
|
end
|
|
|
|
|
|
|
|
def referenced_merge_request_commit_shas
|
|
|
|
return [] unless noteable.is_a?(MergeRequest)
|
|
|
|
|
|
|
|
@referenced_merge_request_commit_shas ||= begin
|
2021-05-11 15:10:20 +00:00
|
|
|
referenced_shas = reference_cache.references_per_parent.values.reduce(:|).to_a
|
2021-04-15 18:09:01 +00:00
|
|
|
noteable.all_commit_shas.select do |sha|
|
|
|
|
referenced_shas.any? { |ref| Gitlab::Git.shas_eql?(sha, ref) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# The default behaviour is `#to_i` - we just pass the hash through.
|
2021-04-22 12:09:49 +00:00
|
|
|
def parse_symbol(sha_hash, _match)
|
2021-04-15 18:09:01 +00:00
|
|
|
sha_hash
|
|
|
|
end
|
|
|
|
|
|
|
|
def url_for_object(commit, project)
|
|
|
|
h = Gitlab::Routing.url_helpers
|
|
|
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
|
|
|
def parent_records(parent, ids)
|
|
|
|
parent.commits_by(oids: ids.to_a)
|
|
|
|
end
|
|
|
|
|
2021-05-11 15:10:20 +00:00
|
|
|
private
|
|
|
|
|
2021-04-15 18:09:01 +00:00
|
|
|
def noteable
|
|
|
|
context[:noteable]
|
|
|
|
end
|
|
|
|
|
|
|
|
def only_path?
|
|
|
|
context[:only_path]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|