debian-mirror-gitlab/lib/gitlab/diff/rendered/notebook/diff_file.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

131 lines
4.4 KiB
Ruby
Raw Normal View History

2022-05-07 20:08:51 +05:30
# frozen_string_literal: true
module Gitlab
module Diff
module Rendered
module Notebook
class DiffFile < Gitlab::Diff::File
2022-07-23 23:45:48 +05:30
include Gitlab::Diff::Rendered::Notebook::DiffFileHelper
include Gitlab::Utils::StrongMemoize
2022-06-21 17:19:12 +05:30
RENDERED_TIMEOUT_BACKGROUND = 10.seconds
BACKGROUND_EXECUTION = 'background'
FOREGROUND_EXECUTION = 'foreground'
LOG_IPYNBDIFF_GENERATED = 'IPYNB_DIFF_GENERATED'
LOG_IPYNBDIFF_TIMEOUT = 'IPYNB_DIFF_TIMEOUT'
LOG_IPYNBDIFF_INVALID = 'IPYNB_DIFF_INVALID'
2022-07-16 23:28:13 +05:30
LOG_IPYNBDIFF_TRUNCATED = 'IPYNB_DIFF_TRUNCATED'
2022-06-21 17:19:12 +05:30
2022-05-07 20:08:51 +05:30
attr_reader :source_diff
delegate :repository, :diff_refs, :fallback_diff_refs, :unfolded, :unique_identifier,
to: :source_diff
def initialize(diff_file)
@source_diff = diff_file
end
def old_blob
return unless notebook_diff
strong_memoize(:old_blob) { ::Blobs::Notebook.decorate(source_diff.old_blob, notebook_diff.from.as_text) }
end
def new_blob
return unless notebook_diff
strong_memoize(:new_blob) { ::Blobs::Notebook.decorate(source_diff.new_blob, notebook_diff.to.as_text) }
end
def diff
strong_memoize(:diff) { transformed_diff }
end
def has_renderable?
!notebook_diff.nil? && diff.diff.present?
end
def rendered
self
end
def highlighted_diff_lines
2022-07-23 23:45:48 +05:30
strong_memoize(:highlighted_diff_lines) do
lines = Gitlab::Diff::Highlight.new(self, repository: self.repository).highlight
lines_in_source = lines_in_source_diff(
source_diff.highlighted_diff_lines, source_diff.deleted_file?, source_diff.new_file?
)
lines.zip(line_positions_at_source_diff(lines, transformed_blocks))
2022-08-27 11:52:29 +05:30
.map { |line, positions| mutate_line(line, positions, lines_in_source) }
2022-05-07 20:08:51 +05:30
end
end
private
def notebook_diff
strong_memoize(:notebook_diff) do
2022-07-16 23:28:13 +05:30
if source_diff.old_blob&.truncated? || source_diff.new_blob&.truncated?
log_event(LOG_IPYNBDIFF_TRUNCATED)
next
end
2022-07-23 23:45:48 +05:30
Gitlab::RenderTimeout.timeout(background: RENDERED_TIMEOUT_BACKGROUND) do
2022-06-21 17:19:12 +05:30
IpynbDiff.diff(source_diff.old_blob&.data, source_diff.new_blob&.data,
2022-07-16 23:28:13 +05:30
raise_if_invalid_nb: true,
diffy_opts: { include_diff_info: true })&.tap do
2022-06-21 17:19:12 +05:30
log_event(LOG_IPYNBDIFF_GENERATED)
end
end
rescue Timeout::Error => e
rendered_timeout.increment(source: Gitlab::Runtime.sidekiq? ? BACKGROUND_EXECUTION : FOREGROUND_EXECUTION)
log_event(LOG_IPYNBDIFF_TIMEOUT, e)
2022-08-13 15:12:31 +05:30
rescue IpynbDiff::InvalidNotebookError => e
2022-06-21 17:19:12 +05:30
log_event(LOG_IPYNBDIFF_INVALID, e)
2022-05-07 20:08:51 +05:30
end
end
def transformed_diff
return unless notebook_diff
diff = source_diff.diff.clone
diff.diff = strip_diff_frontmatter(notebook_diff.to_s(:text))
diff
end
2022-07-23 23:45:48 +05:30
def transformed_blocks
{ from: notebook_diff.from.blocks, to: notebook_diff.to.blocks }
2022-05-07 20:08:51 +05:30
end
2022-06-21 17:19:12 +05:30
def rendered_timeout
@rendered_timeout ||= Gitlab::Metrics.counter(
:ipynb_semantic_diff_timeouts_total,
'Counts the times notebook diff rendering timed out'
)
end
def log_event(message, error = nil)
Gitlab::AppLogger.info({ message: message })
2022-07-16 23:28:13 +05:30
Gitlab::ErrorTracking.log_exception(error) if error
2022-06-21 17:19:12 +05:30
nil
end
2022-07-23 23:45:48 +05:30
def mutate_line(line, mapped_positions, source_diff_lines)
line.old_pos, line.new_pos = mapped_positions
# Lines that do not appear on the original diff should not be commentable
unless source_diff_lines[:to].include?(line.new_pos) || source_diff_lines[:from].include?(line.old_pos)
line.type = "#{line.type || 'unchanged'}-nomappinginraw"
end
line.line_code = line_code(line)
line.rich_text = image_as_rich_text(line.text) || line.rich_text
line
end
2022-05-07 20:08:51 +05:30
end
end
end
end
end