Commit b57261d1 authored by Douglas Barbosa Alexandre's avatar Douglas Barbosa Alexandre

Merge branch '260316-graphql-snippetblob-should-expose-unaltered-text-content' into 'master'

Add unaltered text content to GraphQL's SnippetBlob

See merge request gitlab-org/gitlab!64243
parents 35b17e3e d7cb66c0
......@@ -16,6 +16,10 @@ module Types
description: 'Blob plain highlighted data.',
null: true
field :raw_plain_data, GraphQL::STRING_TYPE,
description: 'The raw content of the blob, if the blob is text data.',
null: true
field :raw_path, GraphQL::STRING_TYPE,
description: 'Blob raw content endpoint path.',
null: false
......
......@@ -26,6 +26,10 @@ class BlobPresenter < Gitlab::View::Presenter::Delegated
highlight(plain: false)
end
def raw_plain_data
blob.data unless blob.binary?
end
def web_url
url_helpers.project_blob_url(project, ref_qualified_path)
end
......
......@@ -17,6 +17,10 @@ class SnippetBlobPresenter < BlobPresenter
snippet_blob_raw_route
end
def raw_plain_data
blob.data unless blob.binary?
end
private
def snippet
......
......@@ -12880,6 +12880,7 @@ Represents the snippet blob.
| <a id="snippetblobpath"></a>`path` | [`String`](#string) | Blob path. |
| <a id="snippetblobplaindata"></a>`plainData` | [`String`](#string) | Blob plain highlighted data. |
| <a id="snippetblobrawpath"></a>`rawPath` | [`String!`](#string) | Blob raw content endpoint path. |
| <a id="snippetblobrawplaindata"></a>`rawPlainData` | [`String`](#string) | The raw content of the blob, if the blob is text data. |
| <a id="snippetblobrenderedastext"></a>`renderedAsText` | [`Boolean!`](#boolean) | Shows whether the blob is rendered as text. |
| <a id="snippetblobrichdata"></a>`richData` | [`String`](#string) | Blob highlighted data. |
| <a id="snippetblobrichviewer"></a>`richViewer` | [`SnippetBlobViewer`](#snippetblobviewer) | Blob content rich viewer. |
......
......@@ -6,7 +6,7 @@ RSpec.describe GitlabSchema.types['SnippetBlob'] do
include GraphqlHelpers
it 'has the correct fields' do
expected_fields = [:rich_data, :plain_data,
expected_fields = [:rich_data, :plain_data, :raw_plain_data,
:raw_path, :size, :binary, :name, :path,
:simple_viewer, :rich_viewer, :mode, :external_storage,
:rendered_as_text]
......@@ -18,6 +18,7 @@ RSpec.describe GitlabSchema.types['SnippetBlob'] do
{
'richData' => be_nullable,
'plainData' => be_nullable,
'rawPlainData' => be_nullable,
'rawPath' => be_non_null,
'size' => be_non_null,
'binary' => be_non_null,
......
......@@ -121,16 +121,26 @@ RSpec.describe BlobPresenter do
end
end
describe '#plain_data' do
describe '#raw_plain_data' do
let(:blob) { repository.blob_at('HEAD', file) }
subject { described_class.new(blob).plain_data }
context 'when blob is text' do
let(:file) { 'files/ruby/popen.rb' }
it 'does not include html in the content' do
expect(presenter.raw_plain_data.include?('</span>')).to be_falsey
end
end
end
describe '#plain_data' do
let(:blob) { repository.blob_at('HEAD', file) }
context 'when blob is binary' do
let(:file) { 'files/images/logo-black.png' }
it 'returns nil' do
expect(subject).to be_nil
expect(presenter.plain_data).to be_nil
end
end
......@@ -138,7 +148,7 @@ RSpec.describe BlobPresenter do
let(:file) { 'README.md' }
it 'returns plain content' do
expect(subject).to include('<span id="LC1" class="line" lang="markdown">')
expect(presenter.plain_data).to include('<span id="LC1" class="line" lang="markdown">')
end
end
......@@ -146,7 +156,7 @@ RSpec.describe BlobPresenter do
let(:file) { 'files/ruby/regex.rb' }
it 'returns highlighted syntax content' do
expect(subject)
expect(presenter.plain_data)
.to include '<span id="LC1" class="line" lang="ruby"><span class="k">module</span> <span class="nn">Gitlab</span>'
end
end
......@@ -155,7 +165,7 @@ RSpec.describe BlobPresenter do
let(:file) { 'LICENSE' }
it 'returns plain text highlighted content' do
expect(subject).to include('<span id="LC1" class="line" lang="plaintext">The MIT License (MIT)</span>')
expect(presenter.plain_data).to include('<span id="LC1" class="line" lang="plaintext">The MIT License (MIT)</span>')
end
end
end
......
......@@ -120,6 +120,27 @@ RSpec.describe SnippetBlobPresenter do
end
end
describe '#raw_plain_data' do
context "with a plain file" do
subject { described_class.new(blob, current_user: user) }
it 'shows raw data for non binary files' do
expect(subject.raw_plain_data).to eq(blob.data)
end
end
context "with a binary file" do
let(:file) { 'files/images/logo-black.png' }
let(:blob) { blob_at(file) }
subject { described_class.new(blob, current_user: user) }
it 'returns nil' do
expect(subject.raw_plain_data).to be_nil
end
end
end
describe '#raw_url' do
subject { described_class.new(blob, current_user: user).raw_url }
......
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