2020-01-31 16:08:52 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module API
|
|
|
|
module Entities
|
|
|
|
class Snippet < Grape::Entity
|
2020-04-21 11:21:10 -04:00
|
|
|
expose :id, :title, :description, :visibility
|
2020-01-31 16:08:52 -05:00
|
|
|
expose :author, using: Entities::UserBasic
|
|
|
|
expose :updated_at, :created_at
|
|
|
|
expose :project_id
|
|
|
|
expose :web_url do |snippet|
|
|
|
|
Gitlab::UrlBuilder.build(snippet)
|
|
|
|
end
|
2020-04-21 11:21:10 -04:00
|
|
|
expose :raw_url do |snippet|
|
|
|
|
Gitlab::UrlBuilder.build(snippet, raw: true)
|
|
|
|
end
|
2020-04-23 14:09:46 -04:00
|
|
|
expose :ssh_url_to_repo, :http_url_to_repo, if: ->(snippet) { snippet.repository_exists? }
|
2020-04-21 11:21:10 -04:00
|
|
|
expose :file_name do |snippet|
|
2020-04-23 14:09:46 -04:00
|
|
|
snippet.file_name_on_repo || snippet.file_name
|
2020-04-21 11:21:10 -04:00
|
|
|
end
|
2020-07-01 11:08:45 -04:00
|
|
|
expose :files, if: ->(snippet, options) { snippet_multiple_files?(snippet, options[:current_user]) } do |snippet, options|
|
|
|
|
snippet.list_files.map do |file|
|
|
|
|
{
|
|
|
|
path: file,
|
|
|
|
raw_url: Gitlab::UrlBuilder.build(snippet, file: file, ref: snippet.repository.root_ref)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def snippet_multiple_files?(snippet, current_user)
|
|
|
|
::Feature.enabled?(:snippet_multiple_files, current_user) && snippet.repository_exists?
|
|
|
|
end
|
2020-01-31 16:08:52 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|