2018-07-23 09:32:32 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
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)
|
2019-08-07 15:13:13 +00:00
|
|
|
return unless project.is_a?(Project) && project.valid_repo?
|
2018-04-12 09:37:01 +00:00
|
|
|
|
2019-08-07 15:13:13 +00:00
|
|
|
_, record = records_per_parent[project].detect { |k, _v| Gitlab::Git.shas_eql?(k, id) }
|
|
|
|
|
|
|
|
record
|
2015-04-16 18:06:05 +00:00
|
|
|
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
|
|
|
|
|
2020-01-17 21:08:29 +00:00
|
|
|
# The default behaviour is `#to_i` - we just pass the hash through.
|
|
|
|
def self.parse_symbol(sha_hash, _match)
|
|
|
|
sha_hash
|
|
|
|
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
|
|
|
|
|
2019-08-07 15:13:13 +00:00
|
|
|
def parent_records(parent, ids)
|
|
|
|
parent.commits_by(oids: ids.to_a)
|
|
|
|
end
|
|
|
|
|
2017-11-22 14:48:09 +00:00
|
|
|
def noteable
|
|
|
|
context[:noteable]
|
|
|
|
end
|
|
|
|
|
|
|
|
def only_path?
|
|
|
|
context[:only_path]
|
|
|
|
end
|
2015-04-03 00:46:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|