From 3d8fbd12b8f234aa62f4b5ceed21076a7afbcd23 Mon Sep 17 00:00:00 2001 From: "micael.bergeron" Date: Mon, 20 Nov 2017 09:02:01 -0500 Subject: [PATCH] add support for commit (in mr) to reference filter --- app/controllers/concerns/renders_notes.rb | 11 ++++++++++- app/models/merge_request.rb | 9 +++++++++ app/models/note.rb | 4 ++++ app/views/projects/commits/_commit.html.haml | 2 +- lib/banzai/filter/commit_reference_filter.rb | 14 ++++++++++++-- lib/banzai/object_renderer.rb | 16 ++++++++-------- 6 files changed, 44 insertions(+), 12 deletions(-) diff --git a/app/controllers/concerns/renders_notes.rb b/app/controllers/concerns/renders_notes.rb index 824ad06465c..a35313c917c 100644 --- a/app/controllers/concerns/renders_notes.rb +++ b/app/controllers/concerns/renders_notes.rb @@ -3,7 +3,7 @@ module RendersNotes preload_noteable_for_regular_notes(notes) preload_max_access_for_authors(notes, @project) 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 end @@ -26,4 +26,13 @@ module RendersNotes notes.each {|n| n.specialize_for_first_contribution!(noteable)} end + + def noteable_context(noteable) + case noteable + when MergeRequest + { merge_request: noteable } + else + {} + end + end end diff --git a/app/models/merge_request.rb b/app/models/merge_request.rb index 949d42f865c..d60b9fb6b2d 100644 --- a/app/models/merge_request.rb +++ b/app/models/merge_request.rb @@ -1021,4 +1021,13 @@ class MergeRequest < ActiveRecord::Base project.merge_requests.merged.where(author_id: author_id).empty? 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 diff --git a/app/models/note.rb b/app/models/note.rb index 1357e75d907..fb4a52f8c6e 100644 --- a/app/models/note.rb +++ b/app/models/note.rb @@ -405,6 +405,10 @@ class Note < ActiveRecord::Base noteable_object&.touch end + def banzai_render_context(field) + super.merge(noteable: noteable) + end + private def keep_around_commit diff --git a/app/views/projects/commits/_commit.html.haml b/app/views/projects/commits/_commit.html.haml index 45b4ef12ec9..8c28becf471 100644 --- a/app/views/projects/commits/_commit.html.haml +++ b/app/views/projects/commits/_commit.html.haml @@ -4,7 +4,7 @@ - ref = local_assigns.fetch(:ref) { merge_request&.source_branch } - 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(cache_key, expires_in: 1.day) do diff --git a/lib/banzai/filter/commit_reference_filter.rb b/lib/banzai/filter/commit_reference_filter.rb index 714e0319025..f4e0c3111f5 100644 --- a/lib/banzai/filter/commit_reference_filter.rb +++ b/lib/banzai/filter/commit_reference_filter.rb @@ -24,8 +24,18 @@ module Banzai def url_for_object(commit, project) h = Gitlab::Routing.url_helpers - h.project_commit_url(project, commit, - only_path: context[:only_path]) + noteable = context[:merge_request] || context[:noteable] + + 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 def object_link_text_extras(object, matches) diff --git a/lib/banzai/object_renderer.rb b/lib/banzai/object_renderer.rb index ecb3affbba5..29c4e60f70c 100644 --- a/lib/banzai/object_renderer.rb +++ b/lib/banzai/object_renderer.rb @@ -18,10 +18,10 @@ module Banzai # project - A Project to use for redacting Markdown. # user - The user viewing the Markdown/HTML documents, if any. # 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 @user = user - @redaction_context = redaction_context + @context = base_context.merge(context) end # Renders and redacts an Array of objects. @@ -48,7 +48,8 @@ module Banzai pipeline = HTML::Pipeline.new([]) 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 @@ -73,20 +74,19 @@ module Banzai # Returns a Banzai context for the given object and attribute. def context_for(object, attribute) - base_context.merge(object.banzai_render_context(attribute)) + @context.merge(object.banzai_render_context(attribute)) end def base_context - @base_context ||= @redaction_context.merge( + { current_user: user, project: project, skip_redaction: true - ) + } end def save_options - return {} unless base_context[:xhtml] - + return {} unless @context[:xhtml] { save_with: Nokogiri::XML::Node::SaveOptions::AS_XHTML } end end