Commit 83e7fa47 authored by Vasilii Iakliushin's avatar Vasilii Iakliushin

Merge branch '348145_add_language_field_to_blob_type' into 'master'

Expose language field in GraphQL blob type

See merge request gitlab-org/gitlab!78895
parents 2f009075 a33ef767
...@@ -125,6 +125,12 @@ module Types ...@@ -125,6 +125,12 @@ module Types
field :archived, GraphQL::Types::Boolean, null: true, method: :archived?, field :archived, GraphQL::Types::Boolean, null: true, method: :archived?,
description: 'Whether the current project is archived.' description: 'Whether the current project is archived.'
field :language, GraphQL::Types::String,
description: 'Blob language.',
method: :blob_language,
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
......
...@@ -32,7 +32,7 @@ class BlobPresenter < Gitlab::View::Presenter::Delegated ...@@ -32,7 +32,7 @@ class BlobPresenter < Gitlab::View::Presenter::Delegated
end end
def blob_language def blob_language
@_blob_language ||= Gitlab::Diff::CustomDiff.transformed_blob_language(blob) || language @_blob_language ||= Gitlab::Diff::CustomDiff.transformed_blob_language(blob) || gitattr_language || detect_language
end end
def raw_plain_data def raw_plain_data
...@@ -166,9 +166,15 @@ class BlobPresenter < Gitlab::View::Presenter::Delegated ...@@ -166,9 +166,15 @@ class BlobPresenter < Gitlab::View::Presenter::Delegated
@all_lines ||= blob.data.lines @all_lines ||= blob.data.lines
end end
def language def gitattr_language
blob.language_from_gitattributes blob.language_from_gitattributes
end end
def detect_language
return if blob.binary?
Rouge::Lexer.guess(filename: blob.path, source: blob_data(nil)) { |lex| lex.min_by(&:tag) }.tag
end
end end
BlobPresenter.prepend_mod_with('BlobPresenter') BlobPresenter.prepend_mod_with('BlobPresenter')
...@@ -33,7 +33,7 @@ class SnippetBlobPresenter < BlobPresenter ...@@ -33,7 +33,7 @@ class SnippetBlobPresenter < BlobPresenter
blob.container blob.container
end end
def language def gitattr_language
nil nil
end end
......
...@@ -14759,6 +14759,7 @@ Returns [`Tree`](#tree). ...@@ -14759,6 +14759,7 @@ Returns [`Tree`](#tree).
| <a id="repositoryblobid"></a>`id` | [`ID!`](#id) | ID of the blob. | | <a id="repositoryblobid"></a>`id` | [`ID!`](#id) | ID of the blob. |
| <a id="repositoryblobideeditpath"></a>`ideEditPath` | [`String`](#string) | Web path to edit this blob in the Web IDE. | | <a id="repositoryblobideeditpath"></a>`ideEditPath` | [`String`](#string) | Web path to edit this blob in the Web IDE. |
| <a id="repositoryblobideforkandeditpath"></a>`ideForkAndEditPath` | [`String`](#string) | Web path to edit this blob in the Web IDE using a forked project. | | <a id="repositoryblobideforkandeditpath"></a>`ideForkAndEditPath` | [`String`](#string) | Web path to edit this blob in the Web IDE using a forked project. |
| <a id="repositorybloblanguage"></a>`language` | [`String`](#string) | Blob language. |
| <a id="repositorybloblfsoid"></a>`lfsOid` | [`String`](#string) | LFS OID of the blob. | | <a id="repositorybloblfsoid"></a>`lfsOid` | [`String`](#string) | LFS OID of the blob. |
| <a id="repositoryblobmode"></a>`mode` | [`String`](#string) | Blob mode. | | <a id="repositoryblobmode"></a>`mode` | [`String`](#string) | Blob mode. |
| <a id="repositoryblobname"></a>`name` | [`String`](#string) | Blob name. | | <a id="repositoryblobname"></a>`name` | [`String`](#string) | Blob name. |
...@@ -80,6 +80,10 @@ module Gitlab ...@@ -80,6 +80,10 @@ module Gitlab
super(presenter_class: BlobPresenter) super(presenter_class: BlobPresenter)
end end
def binary?
false
end
def fetch_blob def fetch_blob
path = [ref, blob_path] path = [ref, blob_path]
missing_blob = { binary_path: blob_path } missing_blob = { binary_path: blob_path }
......
...@@ -41,7 +41,8 @@ RSpec.describe Types::Repository::BlobType do ...@@ -41,7 +41,8 @@ RSpec.describe Types::Repository::BlobType do
:ide_edit_path, :ide_edit_path,
:external_storage_url, :external_storage_url,
:fork_and_edit_path, :fork_and_edit_path,
:ide_fork_and_edit_path :ide_fork_and_edit_path,
:language
) )
end end
end end
...@@ -170,13 +170,13 @@ RSpec.describe BlobPresenter do ...@@ -170,13 +170,13 @@ RSpec.describe BlobPresenter do
let(:git_blob) { blob.__getobj__ } let(:git_blob) { blob.__getobj__ }
it 'returns highlighted content' do it 'returns highlighted content' do
expect(Gitlab::Highlight).to receive(:highlight).with('files/ruby/regex.rb', git_blob.data, plain: nil, language: nil) expect(Gitlab::Highlight).to receive(:highlight).with('files/ruby/regex.rb', git_blob.data, plain: nil, language: 'ruby')
presenter.highlight presenter.highlight
end end
it 'returns plain content when :plain is true' do it 'returns plain content when :plain is true' do
expect(Gitlab::Highlight).to receive(:highlight).with('files/ruby/regex.rb', git_blob.data, plain: true, language: nil) expect(Gitlab::Highlight).to receive(:highlight).with('files/ruby/regex.rb', git_blob.data, plain: true, language: 'ruby')
presenter.highlight(plain: true) presenter.highlight(plain: true)
end end
...@@ -189,7 +189,7 @@ RSpec.describe BlobPresenter do ...@@ -189,7 +189,7 @@ RSpec.describe BlobPresenter do
end end
it 'returns limited highlighted content' do it 'returns limited highlighted content' do
expect(Gitlab::Highlight).to receive(:highlight).with('files/ruby/regex.rb', "line one\n", plain: nil, language: nil) expect(Gitlab::Highlight).to receive(:highlight).with('files/ruby/regex.rb', "line one\n", plain: nil, language: 'ruby')
presenter.highlight(to: 1) presenter.highlight(to: 1)
end end
...@@ -247,6 +247,36 @@ RSpec.describe BlobPresenter do ...@@ -247,6 +247,36 @@ RSpec.describe BlobPresenter do
end end
end end
describe '#blob_language' do
subject { presenter.blob_language }
it { is_expected.to eq('ruby') }
context 'gitlab-language contains a match' do
before do
allow(blob).to receive(:language_from_gitattributes).and_return('cpp')
end
it { is_expected.to eq('cpp') }
end
context 'when blob is ipynb' do
let(:blob) { repository.blob_at('f6b7a707', 'files/ipython/markdown-table.ipynb') }
before do
allow(Gitlab::Diff::CustomDiff).to receive(:transformed_for_diff?).and_return(true)
end
it { is_expected.to eq('md') }
end
context 'when blob is binary' do
let(:blob) { repository.blob_at('HEAD', 'Gemfile.zip') }
it { is_expected.to be_nil }
end
end
describe '#raw_plain_data' do describe '#raw_plain_data' do
let(:blob) { repository.blob_at('HEAD', file) } let(:blob) { repository.blob_at('HEAD', file) }
......
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