Add experimental JSON format for tree controller

This commit is contained in:
Douwe Maan 2017-05-26 13:51:24 -05:00
parent 33b622e353
commit 2ae9c890e7
9 changed files with 79 additions and 11 deletions

View file

@ -24,12 +24,19 @@ class Projects::TreeController < Projects::ApplicationController
end
end
@last_commit = @repository.last_commit_for_path(@commit.id, @tree.path) || @commit
respond_to do |format|
format.html
# Disable cache so browser history works
format.js { no_cache_headers }
format.html do
@last_commit = @repository.last_commit_for_path(@commit.id, @tree.path) || @commit
end
format.js do
# Disable cache so browser history works
no_cache_headers
end
format.json do
render json: TreeSerializer.new(project: @project, repository: @repository, ref: @ref).represent(@tree)
end
end
end

View file

@ -88,15 +88,15 @@ module DiffHelper
end
def submodule_link(blob, ref, repository = @repository)
tree, commit = submodule_links(blob, ref, repository)
commit_id = if commit.nil?
project_url, tree_url = submodule_links(blob, ref, repository)
commit_id = if tree_url.nil?
Commit.truncate_sha(blob.id)
else
link_to Commit.truncate_sha(blob.id), commit
link_to Commit.truncate_sha(blob.id), tree_url
end
[
content_tag(:span, link_to(truncate(blob.name, length: 40), tree)),
content_tag(:span, link_to(truncate(blob.name, length: 40), project_url)),
'@',
content_tag(:span, commit_id, class: 'commit-sha')
].join(' ').html_safe

View file

@ -1,4 +1,5 @@
module IconsHelper
extend self
include FontAwesome::Rails::IconHelper
# Creates an icon tag given icon name(s) and possible icon modifiers.

View file

@ -1,5 +1,5 @@
module SubmoduleHelper
include Gitlab::ShellAdapter
extend self
VALID_SUBMODULE_PROTOCOLS = %w[http https git ssh].freeze
@ -47,7 +47,7 @@ module SubmoduleHelper
return true if url_no_dotgit == [Gitlab.config.gitlab.url, '/', namespace, '/',
project].join('')
url_with_dotgit = url_no_dotgit + '.git'
url_with_dotgit == gitlab_shell.url_to_repo([namespace, '/', project].join(''))
url_with_dotgit == Gitlab::Shell.new.url_to_repo([namespace, '/', project].join(''))
end
def relative_self_url?(url)

View file

@ -0,0 +1,13 @@
class BlobEntity < Grape::Entity
include RequestAwareEntity
expose :id, :path, :name, :mode
expose :icon do |blob|
IconsHelper.file_type_icon_class('file', blob.mode, blob.name)
end
expose :url do |blob|
namespace_project_blob_path(request.project.namespace, request.project, File.join(request.ref, blob.path))
end
end

View file

@ -0,0 +1,23 @@
class SubmoduleEntity < Grape::Entity
include RequestAwareEntity
expose :id, :path, :name, :mode
expose :icon do |blob|
'archive'
end
expose :project_url do |blob|
submodule_links(blob, request).first
end
expose :tree_url do |blob|
submodule_links(blob, request).last
end
private
def submodule_links(blob, request)
@submodule_links ||= SubmoduleHelper.submodule_links(blob, request.ref, request.repository)
end
end

View file

@ -0,0 +1,13 @@
class TreeEntity < Grape::Entity
include RequestAwareEntity
expose :id, :path, :name, :mode
expose :icon do |tree|
IconsHelper.file_type_icon_class('folder', tree.mode, tree.name)
end
expose :url do |tree|
namespace_project_tree_path(request.project.namespace, request.project, File.join(request.ref, tree.path))
end
end

View file

@ -0,0 +1,8 @@
# TODO: Inherit from TreeEntity, when `Tree` implements `id` and `name` like `Gitlab::Git::Tree`.
class TreeRootEntity < Grape::Entity
expose :path
expose :trees, using: TreeEntity
expose :blobs, using: BlobEntity
expose :submodules, using: SubmoduleEntity
end

View file

@ -0,0 +1,3 @@
class TreeSerializer < BaseSerializer
entity TreeRootEntity
end