Commit 3d8fbd12 authored by micael.bergeron's avatar micael.bergeron

add support for commit (in mr) to reference filter

parent 6b3f0fee
...@@ -3,7 +3,7 @@ module RendersNotes ...@@ -3,7 +3,7 @@ module RendersNotes
preload_noteable_for_regular_notes(notes) preload_noteable_for_regular_notes(notes)
preload_max_access_for_authors(notes, @project) preload_max_access_for_authors(notes, @project)
preload_first_time_contribution_for_authors(noteable, notes) preload_first_time_contribution_for_authors(noteable, notes)
Notes::RenderService.new(current_user).execute(notes, @project) Notes::RenderService.new(current_user).execute(notes, @project, noteable_context(noteable))
notes notes
end end
...@@ -26,4 +26,13 @@ module RendersNotes ...@@ -26,4 +26,13 @@ module RendersNotes
notes.each {|n| n.specialize_for_first_contribution!(noteable)} notes.each {|n| n.specialize_for_first_contribution!(noteable)}
end end
def noteable_context(noteable)
case noteable
when MergeRequest
{ merge_request: noteable }
else
{}
end
end
end end
...@@ -1021,4 +1021,13 @@ class MergeRequest < ActiveRecord::Base ...@@ -1021,4 +1021,13 @@ class MergeRequest < ActiveRecord::Base
project.merge_requests.merged.where(author_id: author_id).empty? project.merge_requests.merged.where(author_id: author_id).empty?
end end
def banzai_render_context(field)
# this will be used to reference these commit in the context of the MR
# the URL are built differently
{
merge_request: self,
mr_commit_shas: all_commit_shas
}
end
end end
...@@ -405,6 +405,10 @@ class Note < ActiveRecord::Base ...@@ -405,6 +405,10 @@ class Note < ActiveRecord::Base
noteable_object&.touch noteable_object&.touch
end end
def banzai_render_context(field)
super.merge(noteable: noteable)
end
private private
def keep_around_commit def keep_around_commit
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
- ref = local_assigns.fetch(:ref) { merge_request&.source_branch } - ref = local_assigns.fetch(:ref) { merge_request&.source_branch }
- link = commit_path(project, commit, merge_request: merge_request) - link = commit_path(project, commit, merge_request: merge_request)
- cache_key = [project.full_path, commit.id, current_application_settings, @path.presence, current_controller?(:commits), merge_request.iid, view_details, I18n.locale] - cache_key = [project.full_path, commit.id, current_application_settings, @path.presence, current_controller?(:commits), merge_request&.iid, view_details, I18n.locale]
- cache_key.push(commit.status(ref)) if commit.status(ref) - cache_key.push(commit.status(ref)) if commit.status(ref)
= cache(cache_key, expires_in: 1.day) do = cache(cache_key, expires_in: 1.day) do
......
...@@ -24,8 +24,18 @@ module Banzai ...@@ -24,8 +24,18 @@ module Banzai
def url_for_object(commit, project) def url_for_object(commit, project)
h = Gitlab::Routing.url_helpers h = Gitlab::Routing.url_helpers
h.project_commit_url(project, commit, noteable = context[:merge_request] || context[:noteable]
only_path: context[:only_path])
if noteable.is_a?(MergeRequest) &&
noteable.all_commit_shas.include?(commit.id)
# the internal shas are in the context?
# why not preload in the object?, just make sure we have the same ref
# in all the rendering
h.diffs_project_merge_request_url(project, noteable, commit_id: commit.id)
else
h.project_commit_url(project, commit, only_path: context[:only_path])
end
end end
def object_link_text_extras(object, matches) def object_link_text_extras(object, matches)
......
...@@ -18,10 +18,10 @@ module Banzai ...@@ -18,10 +18,10 @@ module Banzai
# project - A Project to use for redacting Markdown. # project - A Project to use for redacting Markdown.
# user - The user viewing the Markdown/HTML documents, if any. # user - The user viewing the Markdown/HTML documents, if any.
# context - A Hash containing extra attributes to use during redaction # context - A Hash containing extra attributes to use during redaction
def initialize(project, user = nil, redaction_context = {}) def initialize(project, user = nil, context = {})
@project = project @project = project
@user = user @user = user
@redaction_context = redaction_context @context = base_context.merge(context)
end end
# Renders and redacts an Array of objects. # Renders and redacts an Array of objects.
...@@ -48,7 +48,8 @@ module Banzai ...@@ -48,7 +48,8 @@ module Banzai
pipeline = HTML::Pipeline.new([]) pipeline = HTML::Pipeline.new([])
objects.map do |object| objects.map do |object|
pipeline.to_document(Banzai.render_field(object, attribute)) context = context_for(object, attribute)
pipeline.to_document(Banzai.render_field(object, attribute, context))
end end
end end
...@@ -73,20 +74,19 @@ module Banzai ...@@ -73,20 +74,19 @@ module Banzai
# Returns a Banzai context for the given object and attribute. # Returns a Banzai context for the given object and attribute.
def context_for(object, attribute) def context_for(object, attribute)
base_context.merge(object.banzai_render_context(attribute)) @context.merge(object.banzai_render_context(attribute))
end end
def base_context def base_context
@base_context ||= @redaction_context.merge( {
current_user: user, current_user: user,
project: project, project: project,
skip_redaction: true skip_redaction: true
) }
end end
def save_options def save_options
return {} unless base_context[:xhtml] return {} unless @context[:xhtml]
{ save_with: Nokogiri::XML::Node::SaveOptions::AS_XHTML } { save_with: Nokogiri::XML::Node::SaveOptions::AS_XHTML }
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment