Commit 3f0aae43 authored by Igor Drozdov's avatar Igor Drozdov

Merge branch 'fj-return-proper-file-name-snippet-entity' into 'master'

Return file name from repo in snippet API endpoints

See merge request gitlab-org/gitlab!29785
parents c759589e 632e7d42
......@@ -322,6 +322,12 @@ class Snippet < ApplicationRecord
::Feature.enabled?(:version_snippets, user) && repository_exists?
end
def file_name_on_repo
return if repository.empty?
repository.ls_files(repository.root_ref).first
end
class << self
# Searches for snippets with a matching title, description or file name.
#
......
---
title: Return file name from repo in snippet endpoints
merge_request: 29785
author:
type: fixed
......@@ -3,7 +3,7 @@
module API
module Entities
class Snippet < Grape::Entity
expose :id, :title, :file_name, :description, :visibility
expose :id, :title, :description, :visibility
expose :author, using: Entities::UserBasic
expose :updated_at, :created_at
expose :project_id
......@@ -14,6 +14,10 @@ module API
Gitlab::UrlBuilder.build(snippet, raw: true)
end
expose :ssh_url_to_repo, :http_url_to_repo, if: ->(snippet) { snippet.versioned_enabled_for?(options[:current_user]) }
expose :file_name do |snippet|
(::Feature.enabled?(:version_snippets, options[:current_user]) && snippet.file_name_on_repo) ||
snippet.file_name
end
end
end
end
......@@ -19,9 +19,30 @@ describe ::API::Entities::Snippet do
it { expect(subject[:created_at]).to eq snippet.created_at }
it { expect(subject[:project_id]).to eq snippet.project_id }
it { expect(subject[:visibility]).to eq snippet.visibility }
it { expect(subject[:file_name]).to eq snippet.file_name }
it { expect(subject).to include(:author) }
describe 'file_name' do
it 'returns attribute from repository' do
expect(subject[:file_name]).to eq snippet.blobs.first.path
end
context 'when feature flag :version_snippets is disabled' do
it 'returns attribute from db' do
stub_feature_flags(version_snippets: false)
expect(subject[:file_name]).to eq snippet.file_name
end
end
context 'when repository is empty' do
it 'returns attribute from db' do
allow(snippet.repository).to receive(:empty?).and_return(true)
expect(subject[:file_name]).to eq snippet.file_name
end
end
end
describe 'ssh_url_to_repo' do
it 'returns attribute' do
expect(subject[:ssh_url_to_repo]).to eq snippet.ssh_url_to_repo
......
......@@ -94,7 +94,7 @@ describe API::ProjectSnippets do
expect(json_response['title']).to eq(snippet.title)
expect(json_response['description']).to eq(snippet.description)
expect(json_response['file_name']).to eq(snippet.file_name)
expect(json_response['file_name']).to eq(snippet.file_name_on_repo)
expect(json_response['ssh_url_to_repo']).to eq(snippet.ssh_url_to_repo)
expect(json_response['http_url_to_repo']).to eq(snippet.http_url_to_repo)
end
......
......@@ -160,7 +160,7 @@ describe API::Snippets do
expect(json_response['title']).to eq(private_snippet.title)
expect(json_response['description']).to eq(private_snippet.description)
expect(json_response['file_name']).to eq(private_snippet.file_name)
expect(json_response['file_name']).to eq(private_snippet.file_name_on_repo)
expect(json_response['visibility']).to eq(private_snippet.visibility)
expect(json_response['ssh_url_to_repo']).to eq(private_snippet.ssh_url_to_repo)
expect(json_response['http_url_to_repo']).to eq(private_snippet.http_url_to_repo)
......
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