Commit 2c1b05c5 authored by Imre Farkas's avatar Imre Farkas

Merge branch '323195-add-blob-plain-data-attribute' into 'master'

Expose blob plain data in GraphQL

See merge request gitlab-org/gitlab!61016
parents 67969908 2fe4dff8
...@@ -67,6 +67,11 @@ module Types ...@@ -67,6 +67,11 @@ module Types
description: 'Blob content rich viewer.', description: 'Blob content rich viewer.',
null: true null: true
field :plain_data, GraphQL::STRING_TYPE,
description: 'Blob plain highlighted data.',
null: true,
calls_gitaly: true
def raw_text_blob def raw_text_blob
object.data unless object.binary? object.data unless object.binary?
end end
......
...@@ -14,6 +14,12 @@ class BlobPresenter < Gitlab::View::Presenter::Delegated ...@@ -14,6 +14,12 @@ class BlobPresenter < Gitlab::View::Presenter::Delegated
) )
end end
def plain_data
return if blob.binary?
highlight(plain: false)
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
......
...@@ -9,12 +9,6 @@ class SnippetBlobPresenter < BlobPresenter ...@@ -9,12 +9,6 @@ class SnippetBlobPresenter < BlobPresenter
render_rich_partial render_rich_partial
end end
def plain_data
return if blob.binary?
highlight(plain: false)
end
def raw_path def raw_path
snippet_blob_raw_route(only_path: true) snippet_blob_raw_route(only_path: true)
end end
......
---
title: Expose blob plain data in GraphQL
merge_request: 61016
author:
type: added
...@@ -11790,6 +11790,7 @@ Returns [`Tree`](#tree). ...@@ -11790,6 +11790,7 @@ Returns [`Tree`](#tree).
| <a id="repositoryblobname"></a>`name` | [`String`](#string) | Blob name. | | <a id="repositoryblobname"></a>`name` | [`String`](#string) | Blob name. |
| <a id="repositorybloboid"></a>`oid` | [`String!`](#string) | OID of the blob. | | <a id="repositorybloboid"></a>`oid` | [`String!`](#string) | OID of the blob. |
| <a id="repositoryblobpath"></a>`path` | [`String!`](#string) | Path of the blob. | | <a id="repositoryblobpath"></a>`path` | [`String!`](#string) | Path of the blob. |
| <a id="repositoryblobplaindata"></a>`plainData` | [`String`](#string) | Blob plain highlighted data. |
| <a id="repositoryblobrawblob"></a>`rawBlob` | [`String`](#string) | The raw content of the blob. | | <a id="repositoryblobrawblob"></a>`rawBlob` | [`String`](#string) | The raw content of the blob. |
| <a id="repositoryblobrawpath"></a>`rawPath` | [`String`](#string) | Web path to download the raw blob. | | <a id="repositoryblobrawpath"></a>`rawPath` | [`String`](#string) | Web path to download the raw blob. |
| <a id="repositoryblobrawsize"></a>`rawSize` | [`Int`](#int) | Size (in bytes) of the blob, or the blob target if stored externally. | | <a id="repositoryblobrawsize"></a>`rawSize` | [`Int`](#int) | Size (in bytes) of the blob, or the blob target if stored externally. |
......
...@@ -24,7 +24,8 @@ RSpec.describe Types::Repository::BlobType do ...@@ -24,7 +24,8 @@ RSpec.describe Types::Repository::BlobType do
:raw_path, :raw_path,
:replace_path, :replace_path,
:simple_viewer, :simple_viewer,
:rich_viewer :rich_viewer,
:plain_data
) )
end end
end end
...@@ -83,4 +83,43 @@ RSpec.describe BlobPresenter do ...@@ -83,4 +83,43 @@ RSpec.describe BlobPresenter do
end end
end end
end end
describe '#plain_data' do
let(:blob) { repository.blob_at('HEAD', file) }
subject { described_class.new(blob).plain_data }
context 'when blob is binary' do
let(:file) { 'files/images/logo-black.png' }
it 'returns nil' do
expect(subject).to be_nil
end
end
context 'when blob is markup' do
let(:file) { 'README.md' }
it 'returns plain content' do
expect(subject).to include('<span id="LC1" class="line" lang="markdown">')
end
end
context 'when blob has syntax' do
let(:file) { 'files/ruby/regex.rb' }
it 'returns highlighted syntax content' do
expect(subject)
.to include '<span id="LC1" class="line" lang="ruby"><span class="k">module</span> <span class="nn">Gitlab</span>'
end
end
context 'when blob has plain data' 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>')
end
end
end
end end
...@@ -80,45 +80,6 @@ RSpec.describe SnippetBlobPresenter do ...@@ -80,45 +80,6 @@ RSpec.describe SnippetBlobPresenter do
end end
end end
describe '#plain_data' do
let(:blob) { blob_at(file) }
subject { described_class.new(blob).plain_data }
context 'when blob is binary' do
let(:file) { 'files/images/logo-black.png' }
it 'returns nil' do
expect(subject).to be_nil
end
end
context 'when blob is markup' do
let(:file) { 'README.md' }
it 'returns plain content' do
expect(subject).to include('<span id="LC1" class="line" lang="markdown">')
end
end
context 'when blob has syntax' do
let(:file) { 'files/ruby/regex.rb' }
it 'returns highlighted syntax content' do
expect(subject)
.to include '<span id="LC1" class="line" lang="ruby"><span class="k">module</span> <span class="nn">Gitlab</span>'
end
end
context 'when blob has plain data' 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>')
end
end
end
describe 'route helpers' do describe 'route helpers' do
let_it_be(:project) { create(:project) } let_it_be(:project) { create(:project) }
let_it_be(:user) { create(:user) } let_it_be(:user) { create(:user) }
......
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