Commit 0f604e62 authored by Nihad Abbasov's avatar Nihad Abbasov

refactor projects API

parent 0a6b64e6
...@@ -16,11 +16,7 @@ module Gitlab ...@@ -16,11 +16,7 @@ module Gitlab
expose :issues_enabled, :merge_requests_enabled, :wall_enabled, :wiki_enabled, :created_at expose :issues_enabled, :merge_requests_enabled, :wall_enabled, :wiki_enabled, :created_at
end end
class ProjectRepositoryBranches < Grape::Entity class RepoObject < Grape::Entity
expose :name, :commit
end
class ProjectRepositoryTags < Grape::Entity
expose :name, :commit expose :name, :commit
end end
......
...@@ -4,6 +4,10 @@ module Gitlab ...@@ -4,6 +4,10 @@ module Gitlab
@current_user ||= User.find_by_authentication_token(params[:private_token]) @current_user ||= User.find_by_authentication_token(params[:private_token])
end end
def user_project
@project ||= current_user.projects.find_by_code(params[:id])
end
def authenticate! def authenticate!
error!({'message' => '401 Unauthorized'}, 401) unless current_user error!({'message' => '401 Unauthorized'}, 401) unless current_user
end end
......
...@@ -20,8 +20,7 @@ module Gitlab ...@@ -20,8 +20,7 @@ module Gitlab
# Example Request: # Example Request:
# GET /projects/:id # GET /projects/:id
get ":id" do get ":id" do
@project = current_user.projects.find_by_code(params[:id]) present user_project, :with => Entities::Project
present @project, :with => Entities::Project
end end
# Get a project repository branches # Get a project repository branches
...@@ -31,8 +30,7 @@ module Gitlab ...@@ -31,8 +30,7 @@ module Gitlab
# Example Request: # Example Request:
# GET /projects/:id/repository/branches # GET /projects/:id/repository/branches
get ":id/repository/branches" do get ":id/repository/branches" do
@project = current_user.projects.find_by_code(params[:id]) present user_project.repo.heads.sort_by(&:name), :with => Entities::RepoObject
present @project.repo.heads.sort_by(&:name), :with => Entities::ProjectRepositoryBranches
end end
# Get a project repository tags # Get a project repository tags
...@@ -42,8 +40,7 @@ module Gitlab ...@@ -42,8 +40,7 @@ module Gitlab
# Example Request: # Example Request:
# GET /projects/:id/repository/tags # GET /projects/:id/repository/tags
get ":id/repository/tags" do get ":id/repository/tags" do
@project = current_user.projects.find_by_code(params[:id]) present user_project.repo.tags.sort_by(&:name).reverse, :with => Entities::RepoObject
present @project.repo.tags.sort_by(&:name).reverse, :with => Entities::ProjectRepositoryTags
end end
# Get a project snippet # Get a project snippet
...@@ -54,8 +51,7 @@ module Gitlab ...@@ -54,8 +51,7 @@ module Gitlab
# Example Request: # Example Request:
# GET /projects/:id/snippets/:snippet_id # GET /projects/:id/snippets/:snippet_id
get ":id/snippets/:snippet_id" do get ":id/snippets/:snippet_id" do
@project = current_user.projects.find_by_code(params[:id]) @snippet = user_project.snippets.find(params[:snippet_id])
@snippet = @project.snippets.find(params[:snippet_id])
present @snippet, :with => Entities::ProjectSnippet present @snippet, :with => Entities::ProjectSnippet
end end
...@@ -70,8 +66,7 @@ module Gitlab ...@@ -70,8 +66,7 @@ module Gitlab
# Example Request: # Example Request:
# POST /projects/:id/snippets # POST /projects/:id/snippets
post ":id/snippets" do post ":id/snippets" do
@project = current_user.projects.find_by_code(params[:id]) @snippet = user_project.snippets.new(
@snippet = @project.snippets.new(
:title => params[:title], :title => params[:title],
:file_name => params[:file_name], :file_name => params[:file_name],
:expires_at => params[:lifetime], :expires_at => params[:lifetime],
...@@ -98,8 +93,7 @@ module Gitlab ...@@ -98,8 +93,7 @@ module Gitlab
# Example Request: # Example Request:
# PUT /projects/:id/snippets/:snippet_id # PUT /projects/:id/snippets/:snippet_id
put ":id/snippets/:snippet_id" do put ":id/snippets/:snippet_id" do
@project = current_user.projects.find_by_code(params[:id]) @snippet = user_project.snippets.find(params[:snippet_id])
@snippet = @project.snippets.find(params[:snippet_id])
parameters = { parameters = {
:title => (params[:title] || @snippet.title), :title => (params[:title] || @snippet.title),
:file_name => (params[:file_name] || @snippet.file_name), :file_name => (params[:file_name] || @snippet.file_name),
...@@ -122,8 +116,7 @@ module Gitlab ...@@ -122,8 +116,7 @@ module Gitlab
# Example Request: # Example Request:
# DELETE /projects/:id/snippets/:snippet_id # DELETE /projects/:id/snippets/:snippet_id
delete ":id/snippets/:snippet_id" do delete ":id/snippets/:snippet_id" do
@project = current_user.projects.find_by_code(params[:id]) @snippet = user_project.snippets.find(params[:snippet_id])
@snippet = @project.snippets.find(params[:snippet_id])
@snippet.destroy @snippet.destroy
end end
...@@ -135,8 +128,7 @@ module Gitlab ...@@ -135,8 +128,7 @@ module Gitlab
# Example Request: # Example Request:
# GET /projects/:id/snippets/:snippet_id/raw # GET /projects/:id/snippets/:snippet_id/raw
get ":id/snippets/:snippet_id/raw" do get ":id/snippets/:snippet_id/raw" do
@project = current_user.projects.find_by_code(params[:id]) @snippet = user_project.snippets.find(params[:snippet_id])
@snippet = @project.snippets.find(params[:snippet_id])
present @snippet.content present @snippet.content
end end
end end
......
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