Skip to content
Snippets Groups Projects
Commit 6d8e102c authored by Tiago Botelho's avatar Tiago Botelho
Browse files

Adds cacheless render to Banzai object render

parent dd7434e3
No related branches found
No related tags found
No related merge requests found
Showing
with 66 additions and 43 deletions
module RendersCommitDescription
def prepare_commit_descriptions_for_rendering(commit_descriptions)
Banzai::CommitDescriptionRenderer.render(commit_descriptions, @project, current_user)
commit_descriptions
end
end
module RendersCommits
def prepare_commits_for_rendering(commits)
Banzai::CommitRenderer.render(commits, @project, current_user)
commits
end
end
Loading
Loading
@@ -2,6 +2,7 @@ require "base64"
 
class Projects::CommitsController < Projects::ApplicationController
include ExtractsPath
include RendersCommits
 
before_action :require_non_empty_project
before_action :assign_ref_vars
Loading
Loading
@@ -56,5 +57,7 @@ class Projects::CommitsController < Projects::ApplicationController
else
@repository.commits(@ref, path: @path, limit: @limit, offset: @offset)
end
@commits = prepare_commits_for_rendering(@commits)
end
end
Loading
Loading
@@ -3,6 +3,7 @@ require 'addressable/uri'
class Projects::CompareController < Projects::ApplicationController
include DiffForPath
include DiffHelper
include RendersCommits
 
# Authorize
before_action :require_non_empty_project
Loading
Loading
@@ -50,7 +51,7 @@ class Projects::CompareController < Projects::ApplicationController
.execute(@project, @start_ref)
 
if @compare
@commits = @compare.commits
@commits = prepare_commits_for_rendering(@compare.commits)
@diffs = @compare.diffs(diff_options)
 
environment_params = @repository.branch_exists?(@head_ref) ? { ref: @head_ref } : { commit: @compare.commit }
Loading
Loading
class Projects::MergeRequests::CreationsController < Projects::MergeRequests::ApplicationController
include DiffForPath
include DiffHelper
include RendersCommits
 
skip_before_action :merge_request
skip_before_action :ensure_ref_fetched
Loading
Loading
@@ -107,7 +108,7 @@ class Projects::MergeRequests::CreationsController < Projects::MergeRequests::Ap
 
@target_project = @merge_request.target_project
@source_project = @merge_request.source_project
@commits = @merge_request.commits
@commits = prepare_commits_for_rendering(@merge_request.commits)
@commit = @merge_request.diff_head_commit
 
@note_counts = Note.where(commit_id: @commits.map(&:id))
Loading
Loading
Loading
Loading
@@ -2,6 +2,7 @@ class Projects::MergeRequestsController < Projects::MergeRequests::ApplicationCo
include ToggleSubscriptionAction
include IssuableActions
include RendersNotes
include RendersCommits
include ToggleAwardEmoji
include IssuableCollections
 
Loading
Loading
@@ -94,7 +95,7 @@ class Projects::MergeRequestsController < Projects::MergeRequests::ApplicationCo
def commits
# Get commits from repository
# or from cache if already merged
@commits = @merge_request.commits
@commits = prepare_commits_for_rendering(@merge_request.commits)
@note_counts = Note.where(commit_id: @commits.map(&:id))
.group(:commit_id).count
 
Loading
Loading
Loading
Loading
@@ -2,6 +2,7 @@ class SearchController < ApplicationController
skip_before_action :authenticate_user!
 
include SearchHelper
include RendersCommits
 
layout 'search'
 
Loading
Loading
@@ -20,6 +21,8 @@ class SearchController < ApplicationController
@search_results = search_service.search_results
@search_objects = search_service.search_objects
 
render_commits if @scope == 'commits'
check_single_commit_result
end
 
Loading
Loading
@@ -38,6 +41,10 @@ class SearchController < ApplicationController
 
private
 
def render_commits
@search_objects = prepare_commits_for_rendering(@search_objects)
end
def check_single_commit_result
if @search_results.single_commit_result?
only_commit = @search_results.objects('commits').first
Loading
Loading
Loading
Loading
@@ -21,25 +21,28 @@ module MarkupHelper
end
 
# Use this in places where you would normally use link_to(gfm(...), ...).
#
def link_to_markdown(body, url, html_options = {})
return '' if body.blank?
link_to_html(markdown(body, pipeline: :single_line), url, html_options)
end
def link_to_markdown_field(object, field, url, html_options = {})
rendered_field = markdown_field(object, field)
link_to_html(rendered_field, url, html_options)
end
# It solves a problem occurring with nested links (i.e.
# "<a>outer text <a>gfm ref</a> more outer text</a>"). This will not be
# interpreted as intended. Browsers will parse something like
# "<a>outer text </a><a>gfm ref</a> more outer text" (notice the last part is
# not linked any more). link_to_gfm corrects that. It wraps all parts to
# not linked any more). link_to_html corrects that. It wraps all parts to
# explicitly produce the correct linking behavior (i.e.
# "<a>outer text </a><a>gfm ref</a><a> more outer text</a>").
def link_to_gfm(body, url, html_options = {})
return '' if body.blank?
def link_to_html(redacted, url, html_options = {})
fragment = Nokogiri::HTML::DocumentFragment.parse(redacted)
 
context = {
project: @project,
current_user: (current_user if defined?(current_user)),
pipeline: :single_line
}
gfm_body = Banzai.render(body, context)
fragment = Nokogiri::HTML::DocumentFragment.parse(gfm_body)
if fragment.children.size == 1 && fragment.children[0].name == 'a'
# Fragment has only one node, and it's a link generated by `gfm`.
# Replace it with our requested link.
Loading
Loading
@@ -82,7 +85,10 @@ module MarkupHelper
 
def markdown_field(object, field)
object = object.for_display if object.respond_to?(:for_display)
redacted_field_html = object.try(:"redacted_#{field}_html")
return '' unless object.present?
return redacted_field_html if redacted_field_html
 
html = Banzai.render_field(object, field)
prepare_for_rendering(html, object.banzai_render_context(field))
Loading
Loading
Loading
Loading
@@ -16,6 +16,8 @@ class Commit
participant :notes_with_associations
 
attr_accessor :project, :author
attr_accessor :redacted_description_html
attr_accessor :redacted_title_html
 
DIFF_SAFE_LINES = Gitlab::Git::DiffCollection::DEFAULT_LIMITS[:max_lines]
 
Loading
Loading
@@ -26,6 +28,13 @@ class Commit
# The SHA can be between 7 and 40 hex characters.
COMMIT_SHA_PATTERN = '\h{7,40}'.freeze
 
def banzai_render_context(field)
context = { pipeline: :single_line, project: self.project }
context[:author] = self.author if self.author
context
end
class << self
def decorate(commits, project)
commits.map do |commit|
Loading
Loading
Loading
Loading
@@ -22,7 +22,7 @@
= author_avatar(commit, size: 36)
.commit-row-title
%span.item-title.str-truncated-100
= link_to_gfm commit.title, project_commit_path(@project, commit.id), class: "cdark", title: commit.title
= link_to_markdown commit.title, project_commit_path(@project, commit.id), class: "cdark", title: commit.title
.pull-right
= link_to commit.short_id, project_commit_path(@project, commit), class: "commit-sha"
&nbsp;
Loading
Loading
Loading
Loading
@@ -4,6 +4,6 @@
= link_to commit.short_id, project_commit_path(project, commit.id), class: "commit-sha"
&middot;
%span.str-truncated
= link_to_gfm commit.title, project_commit_path(project, commit.id), class: "commit-row-message"
= link_to_markdown commit.title, project_commit_path(project, commit.id), class: "commit-row-message"
&middot;
#{time_ago_with_tooltip(commit.committed_date)}
Loading
Loading
@@ -16,7 +16,7 @@
 
.commit-detail
.commit-content
= link_to_gfm commit.title, project_commit_path(project, commit.id), class: "commit-row-message item-title"
= link_to_markdown_field(commit, :title, project_commit_path(project, commit.id), class: "commit-row-message item-title")
%span.commit-row-message.visible-xs-inline
&middot;
= commit.short_id
Loading
Loading
@@ -28,7 +28,8 @@
 
- if commit.description?
%pre.commit-row-description.js-toggle-content
= preserve(markdown(commit.description, pipeline: :single_line, author: commit.author))
= preserve(markdown_field(commit, :description))
.commiter
- commit_author_link = commit_author_link(commit, avatar: false, size: 24)
- commit_timeago = time_ago_with_tooltip(commit.committed_date)
Loading
Loading
Loading
Loading
@@ -3,6 +3,6 @@
= link_to commit.short_id, project_commit_path(project, commit), class: "commit-sha"
&nbsp;
%span.str-truncated
= link_to_gfm commit.title, project_commit_path(project, commit.id), class: "commit-row-message"
= link_to_markdown_field(commit, :title, project_commit_path(project, commit.id), class: "commit-row-message")
.pull-right
#{time_ago_with_tooltip(commit.committed_date)}
Loading
Loading
@@ -12,6 +12,6 @@
%span.flex-truncate-child
- if commit_title = deployment.commit_title
= author_avatar(deployment.commit, size: 20)
= link_to_gfm commit_title, project_commit_path(@project, deployment.sha), class: "commit-row-message"
= link_to_markdown commit_title, project_commit_path(@project, deployment.sha), class: "commit-row-message"
- else
Cant find HEAD commit for this branch
%span.str-truncated
= link_to_gfm commit.full_title, project_commit_path(@project, commit.id), class: "tree-commit-link"
= link_to_markdown commit.full_title, project_commit_path(@project, commit.id), class: "tree-commit-link"
Loading
Loading
@@ -28,7 +28,7 @@
commented
- if note.system
%span.system-note-message
= note.redacted_note_html
= markdown_field(note, :note)
%a{ href: "##{dom_id(note)}" }
= time_ago_with_tooltip(note.created_at, placement: 'bottom', html_class: 'note-created-ago')
- unless note.system?
Loading
Loading
@@ -39,7 +39,7 @@
= render 'projects/notes/actions', note: note, note_editable: note_editable
.note-body{ class: note_editable ? 'js-task-list-container' : '' }
.note-text.md
= note.redacted_note_html
= markdown_field(note, :note)
= edited_time_ago_with_tooltip(note, placement: 'bottom', html_class: 'note_edited_ago')
.original-note-content.hidden{ data: { post_url: note_url(note), target_id: note.noteable.id, target_type: note.noteable.class.name.underscore } }
#{note.note}
Loading
Loading
Loading
Loading
@@ -31,8 +31,7 @@
 
- if show_last_commit_as_description
.description.prepend-top-5
= link_to_gfm project.commit.title, project_commit_path(project, project.commit),
class: "commit-row-message"
= link_to_markdown(project.commit.title, project_commit_path(project, project.commit), class: "commit-row-message")
- elsif project.description.present?
.description.prepend-top-5
= markdown_field(project, :description)
Loading
Loading
---
title: Improves markdown rendering performance for commit lists.
merge_request:
author:
type: other
Loading
Loading
@@ -114,9 +114,6 @@ def instrument_classes(instrumentation)
# This is a Rails scope so we have to instrument it manually.
instrumentation.instrument_method(Project, :visible_to_user)
 
# Needed for https://gitlab.com/gitlab-org/gitlab-ce/issues/34509
instrumentation.instrument_method(MarkupHelper, :link_to_gfm)
# Needed for https://gitlab.com/gitlab-org/gitlab-ce/issues/30224#note_32306159
instrumentation.instrument_instance_method(MergeRequestDiff, :load_commits)
 
Loading
Loading
module Banzai
module CommitDescriptionRenderer
def self.render(commit_descriptions, project, user = nil)
ObjectRenderer.new(project, user).render(commit_descriptions, :commit_description)
end
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment