Commit fba059da authored by Eduardo Bonet's avatar Eduardo Bonet

Moving CustomDiff to /lib folder

parent 7bcf737a
# frozen_string_literal: true
module CustomDiffHelper
class << self
def preprocess_before_diff(path, old_blob, new_blob)
return unless path.ends_with? '.ipynb'
transformed_diff = IpynbDiff.diff(old_blob&.data, new_blob&.data,
diff_opts: { context: 5, include_diff_info: true },
transform_options: { cell_decorator: :percent },
raise_if_invalid_notebook: true)
new_diff = strip_diff_frontmatter(transformed_diff)
transformed_for_diff(new_blob, old_blob) if new_diff
Gitlab::AppLogger.info({ message: new_diff ? 'IPYNB_DIFF_GENERATED' : 'IPYNB_DIFF_NIL' })
new_diff
rescue IpynbDiff::InvalidNotebookError => e
Gitlab::ErrorTracking.log_exception(e)
nil
end
def transformed_blob_language(blob)
'md' if transformed_for_diff?(blob)
end
def transformed_blob_data(blob)
if transformed_for_diff?(blob)
IpynbDiff.transform(blob.data,
raise_errors: true,
options: { include_metadata: false, cell_decorator: :percent })
end
end
def strip_diff_frontmatter(diff_content)
diff_content.scan(/.*\n/)[2..-1]&.join('') if diff_content.present?
end
def blobs_with_transformed_diffs
@blobs_with_transformed_diffs ||= {}
end
def transformed_for_diff?(blob)
blobs_with_transformed_diffs[blob]
end
def transformed_for_diff(*blobs)
blobs.each do |b|
blobs_with_transformed_diffs[b] = true if b
end
end
end
end
...@@ -15,8 +15,8 @@ class BlobPresenter < Gitlab::View::Presenter::Delegated ...@@ -15,8 +15,8 @@ class BlobPresenter < Gitlab::View::Presenter::Delegated
Gitlab::Highlight.highlight( Gitlab::Highlight.highlight(
blob.path, blob.path,
CustomDiffHelper.transformed_blob_data(blob) || limited_blob_data(to: to), Gitlab::Diff::CustomDiff.transformed_blob_data(blob) || limited_blob_data(to: to),
language: CustomDiffHelper.transformed_blob_language(blob) || language, language: Gitlab::Diff::CustomDiff.transformed_blob_language(blob) || language,
plain: plain plain: plain
) )
end end
......
# frozen_string_literal: true
module Gitlab
module Diff
module CustomDiff
class << self
def preprocess_before_diff(path, old_blob, new_blob)
return unless path.ends_with? '.ipynb'
transformed_diff = IpynbDiff.diff(old_blob&.data, new_blob&.data,
diff_opts: { context: 5, include_diff_info: true },
transform_options: { cell_decorator: :percent },
raise_if_invalid_notebook: true)
new_diff = strip_diff_frontmatter(transformed_diff)
transformed_for_diff(new_blob, old_blob) if new_diff
Gitlab::AppLogger.info({ message: new_diff ? 'IPYNB_DIFF_GENERATED' : 'IPYNB_DIFF_NIL' })
new_diff
rescue IpynbDiff::InvalidNotebookError => e
Gitlab::ErrorTracking.log_exception(e)
nil
end
def transformed_blob_language(blob)
'md' if transformed_for_diff?(blob)
end
def transformed_blob_data(blob)
if transformed_for_diff?(blob)
IpynbDiff.transform(blob.data,
raise_errors: true,
options: { include_metadata: false, cell_decorator: :percent })
end
end
def strip_diff_frontmatter(diff_content)
diff_content.scan(/.*\n/)[2..-1]&.join('') if diff_content.present?
end
def blobs_with_transformed_diffs
@blobs_with_transformed_diffs ||= {}
end
def transformed_for_diff?(blob)
blobs_with_transformed_diffs[blob]
end
def transformed_for_diff(*blobs)
blobs.each do |b|
blobs_with_transformed_diffs[b] = true if b
end
end
end
end
end
end
...@@ -44,7 +44,7 @@ module Gitlab ...@@ -44,7 +44,7 @@ module Gitlab
new_blob_lazy new_blob_lazy
old_blob_lazy old_blob_lazy
diff.diff = CustomDiffHelper.preprocess_before_diff(diff.new_path, old_blob_lazy, new_blob_lazy) || diff.diff if use_custom_diff? diff.diff = Gitlab::Diff::CustomDiff.preprocess_before_diff(diff.new_path, old_blob_lazy, new_blob_lazy) || diff.diff if use_custom_diff?
end end
def use_custom_diff? def use_custom_diff?
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe CustomDiffHelper do RSpec.describe Gitlab::Diff::CustomDiff do
include RepoHelpers include RepoHelpers
let(:project) { create(:project, :repository) } let(:project) { create(:project, :repository) }
...@@ -10,53 +10,53 @@ RSpec.describe CustomDiffHelper do ...@@ -10,53 +10,53 @@ RSpec.describe CustomDiffHelper do
let(:ipynb_blob) { repository.blob_at('f6b7a707', 'files/ipython/markdown-table.ipynb') } let(:ipynb_blob) { repository.blob_at('f6b7a707', 'files/ipython/markdown-table.ipynb') }
let(:blob) { repository.blob_at('HEAD', 'files/ruby/regex.rb') } let(:blob) { repository.blob_at('HEAD', 'files/ruby/regex.rb') }
describe "#preprocess_before_diff" do describe '#preprocess_before_diff' do
context 'for ipynb files' do context 'for ipynb files' do
it 'transforms the diff' do it 'transforms the diff' do
expect(CustomDiffHelper.preprocess_before_diff(ipynb_blob.path, nil, ipynb_blob)).not_to include('cells') expect(described_class.preprocess_before_diff(ipynb_blob.path, nil, ipynb_blob)).not_to include('cells')
end end
it 'adds the blob to the list of transformed blobs' do it 'adds the blob to the list of transformed blobs' do
CustomDiffHelper.preprocess_before_diff(ipynb_blob.path, nil, ipynb_blob) described_class.preprocess_before_diff(ipynb_blob.path, nil, ipynb_blob)
expect(CustomDiffHelper.transformed_for_diff?(ipynb_blob)).to be_truthy expect(described_class.transformed_for_diff?(ipynb_blob)).to be_truthy
end end
end end
context 'for other files' do context 'for other files' do
it 'returns nil' do it 'returns nil' do
expect(CustomDiffHelper.preprocess_before_diff(blob.path, nil, blob)).to be_nil expect(described_class.preprocess_before_diff(blob.path, nil, blob)).to be_nil
end end
it 'does not add the blob to the list of transformed blobs' do it 'does not add the blob to the list of transformed blobs' do
CustomDiffHelper.preprocess_before_diff(blob.path, nil, blob) described_class.preprocess_before_diff(blob.path, nil, blob)
expect(CustomDiffHelper.transformed_for_diff?(blob)).to be_falsey expect(described_class.transformed_for_diff?(blob)).to be_falsey
end end
end end
end end
describe "#transformed_blob_data" do describe '#transformed_blob_data' do
it 'transforms blob data if file was processed' do it 'transforms blob data if file was processed' do
CustomDiffHelper.preprocess_before_diff(ipynb_blob.path, nil, ipynb_blob) described_class.preprocess_before_diff(ipynb_blob.path, nil, ipynb_blob)
expect(CustomDiffHelper.transformed_blob_data(ipynb_blob)).not_to include('cells') expect(described_class.transformed_blob_data(ipynb_blob)).not_to include('cells')
end end
it 'transforms blob data if file was not processed' do it 'does not transform blob data if file was not processed' do
expect(CustomDiffHelper.transformed_blob_data(ipynb_blob)).to be_nil expect(described_class.transformed_blob_data(ipynb_blob)).to be_nil
end end
end end
describe "#transformed_blob_language" do describe '#transformed_blob_language' do
it 'is when file was preprocessed' do it 'is md when file was preprocessed' do
CustomDiffHelper.preprocess_before_diff(ipynb_blob.path, nil, ipynb_blob) described_class.preprocess_before_diff(ipynb_blob.path, nil, ipynb_blob)
expect(CustomDiffHelper.transformed_blob_language(ipynb_blob)).to eq('md') expect(described_class.transformed_blob_language(ipynb_blob)).to eq('md')
end end
it 'is nil for .ipynb, but blob when it was not preprocessed' do it 'is nil for a .ipynb blob that was not preprocessed' do
expect(CustomDiffHelper.transformed_blob_language(ipynb_blob)).to be_nil expect(described_class.transformed_blob_language(ipynb_blob)).to be_nil
end end
end end
end end
...@@ -161,7 +161,7 @@ RSpec.describe BlobPresenter do ...@@ -161,7 +161,7 @@ RSpec.describe BlobPresenter do
let(:git_blob) { blob.__getobj__ } let(:git_blob) { blob.__getobj__ }
before do before do
allow(CustomDiffHelper).to receive(:transformed_for_diff?).and_return(true) allow(Gitlab::Diff::CustomDiff).to receive(:transformed_for_diff?).and_return(true)
end end
it 'uses md as the transformed language' do it 'uses md as the transformed language' do
......
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