Commit 2fe4dff8 authored by Nick Thomas's avatar Nick Thomas

Expose blob plain data in GraphQL

This is required for the repository viewer to GraphQL work

Changelog: added
parent 933bc278
......@@ -67,6 +67,11 @@ module Types
description: 'Blob content rich viewer.',
null: true
field :plain_data, GraphQL::STRING_TYPE,
description: 'Blob plain highlighted data.',
null: true,
calls_gitaly: true
def raw_text_blob
object.data unless object.binary?
end
......
......@@ -14,6 +14,12 @@ class BlobPresenter < Gitlab::View::Presenter::Delegated
)
end
def plain_data
return if blob.binary?
highlight(plain: false)
end
def web_url
url_helpers.project_blob_url(project, ref_qualified_path)
end
......
......@@ -9,12 +9,6 @@ class SnippetBlobPresenter < BlobPresenter
render_rich_partial
end
def plain_data
return if blob.binary?
highlight(plain: false)
end
def raw_path
snippet_blob_raw_route(only_path: true)
end
......
---
title: Expose blob plain data in GraphQL
merge_request: 61016
author:
type: added
......@@ -11744,6 +11744,7 @@ Returns [`Tree`](#tree).
| <a id="repositoryblobname"></a>`name` | [`String`](#string) | Blob name. |
| <a id="repositorybloboid"></a>`oid` | [`String!`](#string) | OID 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="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. |
......
......@@ -24,7 +24,8 @@ RSpec.describe Types::Repository::BlobType do
:raw_path,
:replace_path,
:simple_viewer,
:rich_viewer
:rich_viewer,
:plain_data
)
end
end
......@@ -83,4 +83,43 @@ RSpec.describe BlobPresenter do
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
......@@ -80,45 +80,6 @@ RSpec.describe SnippetBlobPresenter do
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
let_it_be(:project) { create(:project) }
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