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 ...@@ -16,6 +16,10 @@ module Types
description: 'Blob plain highlighted data.', description: 'Blob plain highlighted data.',
null: true 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, field :raw_path, GraphQL::STRING_TYPE,
description: 'Blob raw content endpoint path.', description: 'Blob raw content endpoint path.',
null: false null: false
......
...@@ -26,6 +26,10 @@ class BlobPresenter < Gitlab::View::Presenter::Delegated ...@@ -26,6 +26,10 @@ class BlobPresenter < Gitlab::View::Presenter::Delegated
highlight(plain: false) highlight(plain: false)
end end
def raw_plain_data
blob.data unless blob.binary?
end
def web_url def web_url
url_helpers.project_blob_url(project, ref_qualified_path) url_helpers.project_blob_url(project, ref_qualified_path)
end end
......
...@@ -17,6 +17,10 @@ class SnippetBlobPresenter < BlobPresenter ...@@ -17,6 +17,10 @@ class SnippetBlobPresenter < BlobPresenter
snippet_blob_raw_route snippet_blob_raw_route
end end
def raw_plain_data
blob.data unless blob.binary?
end
private private
def snippet def snippet
......
...@@ -12880,6 +12880,7 @@ Represents the snippet blob. ...@@ -12880,6 +12880,7 @@ Represents the snippet blob.
| <a id="snippetblobpath"></a>`path` | [`String`](#string) | Blob path. | | <a id="snippetblobpath"></a>`path` | [`String`](#string) | Blob path. |
| <a id="snippetblobplaindata"></a>`plainData` | [`String`](#string) | Blob plain highlighted data. | | <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="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="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="snippetblobrichdata"></a>`richData` | [`String`](#string) | Blob highlighted data. |
| <a id="snippetblobrichviewer"></a>`richViewer` | [`SnippetBlobViewer`](#snippetblobviewer) | Blob content rich viewer. | | <a id="snippetblobrichviewer"></a>`richViewer` | [`SnippetBlobViewer`](#snippetblobviewer) | Blob content rich viewer. |
......
...@@ -6,7 +6,7 @@ RSpec.describe GitlabSchema.types['SnippetBlob'] do ...@@ -6,7 +6,7 @@ RSpec.describe GitlabSchema.types['SnippetBlob'] do
include GraphqlHelpers include GraphqlHelpers
it 'has the correct fields' do 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, :raw_path, :size, :binary, :name, :path,
:simple_viewer, :rich_viewer, :mode, :external_storage, :simple_viewer, :rich_viewer, :mode, :external_storage,
:rendered_as_text] :rendered_as_text]
...@@ -18,6 +18,7 @@ RSpec.describe GitlabSchema.types['SnippetBlob'] do ...@@ -18,6 +18,7 @@ RSpec.describe GitlabSchema.types['SnippetBlob'] do
{ {
'richData' => be_nullable, 'richData' => be_nullable,
'plainData' => be_nullable, 'plainData' => be_nullable,
'rawPlainData' => be_nullable,
'rawPath' => be_non_null, 'rawPath' => be_non_null,
'size' => be_non_null, 'size' => be_non_null,
'binary' => be_non_null, 'binary' => be_non_null,
......
...@@ -121,16 +121,26 @@ RSpec.describe BlobPresenter do ...@@ -121,16 +121,26 @@ RSpec.describe BlobPresenter do
end end
end end
describe '#plain_data' do describe '#raw_plain_data' do
let(:blob) { repository.blob_at('HEAD', file) } 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 context 'when blob is binary' do
let(:file) { 'files/images/logo-black.png' } let(:file) { 'files/images/logo-black.png' }
it 'returns nil' do it 'returns nil' do
expect(subject).to be_nil expect(presenter.plain_data).to be_nil
end end
end end
...@@ -138,7 +148,7 @@ RSpec.describe BlobPresenter do ...@@ -138,7 +148,7 @@ RSpec.describe BlobPresenter do
let(:file) { 'README.md' } let(:file) { 'README.md' }
it 'returns plain content' do 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
end end
...@@ -146,7 +156,7 @@ RSpec.describe BlobPresenter do ...@@ -146,7 +156,7 @@ RSpec.describe BlobPresenter do
let(:file) { 'files/ruby/regex.rb' } let(:file) { 'files/ruby/regex.rb' }
it 'returns highlighted syntax content' do 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>' .to include '<span id="LC1" class="line" lang="ruby"><span class="k">module</span> <span class="nn">Gitlab</span>'
end end
end end
...@@ -155,7 +165,7 @@ RSpec.describe BlobPresenter do ...@@ -155,7 +165,7 @@ RSpec.describe BlobPresenter do
let(:file) { 'LICENSE' } let(:file) { 'LICENSE' }
it 'returns plain text highlighted content' do 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 end
end end
......
...@@ -120,6 +120,27 @@ RSpec.describe SnippetBlobPresenter do ...@@ -120,6 +120,27 @@ RSpec.describe SnippetBlobPresenter do
end end
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 describe '#raw_url' do
subject { described_class.new(blob, current_user: user).raw_url } 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