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

refactor projects API

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