gitlabhq/lib/api/projects.rb

137 lines
4.4 KiB
Ruby
Raw Normal View History

2012-06-29 12:46:01 +02:00
module Gitlab
# Projects API
class Projects < Grape::API
before { authenticate! }
resource :projects do
# Get a projects list for authenticated user
#
# Example Request:
# GET /projects
get do
@projects = current_user.projects
present @projects, :with => Entities::Project
end
# Get a single project
#
# Parameters:
2012-07-25 11:35:41 +02:00
# id (required) - The ID or code name of a project
2012-06-29 12:46:01 +02:00
# Example Request:
# GET /projects/:id
get ":id" do
2012-07-06 15:08:17 +02:00
present user_project, :with => Entities::Project
2012-06-29 12:46:01 +02:00
end
# Get a project repository branches
#
# Parameters:
2012-07-25 11:35:41 +02:00
# id (required) - The ID or code name of a project
2012-06-29 12:46:01 +02:00
# Example Request:
# GET /projects/:id/repository/branches
get ":id/repository/branches" do
2012-07-06 15:08:17 +02:00
present user_project.repo.heads.sort_by(&:name), :with => Entities::RepoObject
2012-06-29 12:46:01 +02:00
end
# Get a project repository tags
#
# Parameters:
2012-07-25 11:35:41 +02:00
# id (required) - The ID or code name of a project
2012-06-29 12:46:01 +02:00
# Example Request:
# GET /projects/:id/repository/tags
get ":id/repository/tags" do
2012-07-06 15:08:17 +02:00
present user_project.repo.tags.sort_by(&:name).reverse, :with => Entities::RepoObject
2012-06-29 12:46:01 +02:00
end
2012-06-29 15:34:08 +02:00
# Get a project snippet
#
# Parameters:
2012-07-25 11:35:41 +02:00
# id (required) - The ID or code name of a project
2012-06-29 15:34:08 +02:00
# snippet_id (required) - The ID of a project snippet
# Example Request:
# GET /projects/:id/snippets/:snippet_id
get ":id/snippets/:snippet_id" do
2012-07-06 15:08:17 +02:00
@snippet = user_project.snippets.find(params[:snippet_id])
2012-06-29 15:34:08 +02:00
present @snippet, :with => Entities::ProjectSnippet
end
# Create a new project snippet
#
# Parameters:
2012-07-25 11:35:41 +02:00
# id (required) - The ID or code name of a project
2012-06-29 15:34:08 +02:00
# title (required) - The title of a snippet
# file_name (required) - The name of a snippet file
# lifetime (optional) - The expiration date of a snippet
# code (required) - The content of a snippet
# Example Request:
# POST /projects/:id/snippets
post ":id/snippets" do
2012-07-06 15:08:17 +02:00
@snippet = user_project.snippets.new(
2012-06-29 15:34:08 +02:00
:title => params[:title],
:file_name => params[:file_name],
:expires_at => params[:lifetime],
:content => params[:code]
)
@snippet.author = current_user
if @snippet.save
present @snippet, :with => Entities::ProjectSnippet
else
error!({'message' => '404 Not found'}, 404)
end
end
# Update an existing project snippet
#
# Parameters:
2012-07-25 11:35:41 +02:00
# id (required) - The ID or code name of a project
# snippet_id (required) - The ID of a project snippet
# title (optional) - The title of a snippet
# file_name (optional) - The name of a snippet file
# lifetime (optional) - The expiration date of a snippet
# code (optional) - The content of a snippet
# Example Request:
# PUT /projects/:id/snippets/:snippet_id
put ":id/snippets/:snippet_id" do
2012-07-06 15:08:17 +02:00
@snippet = user_project.snippets.find(params[:snippet_id])
parameters = {
:title => (params[:title] || @snippet.title),
:file_name => (params[:file_name] || @snippet.file_name),
:expires_at => (params[:lifetime] || @snippet.expires_at),
:content => (params[:code] || @snippet.content)
}
if @snippet.update_attributes(parameters)
present @snippet, :with => Entities::ProjectSnippet
else
error!({'message' => '404 Not found'}, 404)
end
end
2012-06-29 15:34:08 +02:00
# Delete a project snippet
#
# Parameters:
2012-07-25 11:35:41 +02:00
# id (required) - The ID or code name of a project
2012-06-29 15:34:08 +02:00
# snippet_id (required) - The ID of a project snippet
# Example Request:
# DELETE /projects/:id/snippets/:snippet_id
delete ":id/snippets/:snippet_id" do
2012-07-06 15:08:17 +02:00
@snippet = user_project.snippets.find(params[:snippet_id])
2012-06-29 15:34:08 +02:00
@snippet.destroy
end
# Get a raw project snippet
#
# Parameters:
2012-07-25 11:35:41 +02:00
# id (required) - The ID or code name of a project
# snippet_id (required) - The ID of a project snippet
# Example Request:
# GET /projects/:id/snippets/:snippet_id/raw
get ":id/snippets/:snippet_id/raw" do
2012-07-06 15:08:17 +02:00
@snippet = user_project.snippets.find(params[:snippet_id])
present @snippet.content
end
2012-06-29 12:46:01 +02:00
end
end
end