2013-05-14 08:33:31 -04:00
|
|
|
module API
|
2012-06-29 06:46:01 -04:00
|
|
|
# Projects API
|
|
|
|
class Projects < Grape::API
|
|
|
|
before { authenticate! }
|
|
|
|
|
|
|
|
resource :projects do
|
2013-02-08 10:33:15 -05:00
|
|
|
helpers do
|
2013-11-06 10:13:21 -05:00
|
|
|
def map_public_to_visibility_level(attrs)
|
|
|
|
publik = attrs.delete(:public)
|
2014-07-28 01:01:01 -04:00
|
|
|
publik = parse_boolean(publik)
|
2013-11-06 10:13:21 -05:00
|
|
|
attrs[:visibility_level] = Gitlab::VisibilityLevel::PUBLIC if !attrs[:visibility_level].present? && publik == true
|
|
|
|
attrs
|
|
|
|
end
|
2013-02-08 10:33:15 -05:00
|
|
|
end
|
|
|
|
|
2012-06-29 06:46:01 -04:00
|
|
|
# Get a projects list for authenticated user
|
|
|
|
#
|
|
|
|
# Example Request:
|
|
|
|
# GET /projects
|
|
|
|
get do
|
2014-08-11 08:25:25 -04:00
|
|
|
@projects = current_user.authorized_projects
|
2014-12-19 13:26:19 -05:00
|
|
|
sort = params[:sort] == 'desc' ? 'desc' : 'asc'
|
2014-12-19 07:27:27 -05:00
|
|
|
|
|
|
|
@projects = case params["order_by"]
|
2014-12-22 07:48:00 -05:00
|
|
|
when 'id' then @projects.reorder("id #{sort}")
|
|
|
|
when 'name' then @projects.reorder("name #{sort}")
|
|
|
|
when 'created_at' then @projects.reorder("created_at #{sort}")
|
|
|
|
when 'last_activity_at' then @projects.reorder("last_activity_at #{sort}")
|
|
|
|
else @projects
|
2014-12-19 07:27:27 -05:00
|
|
|
end
|
|
|
|
|
2014-07-28 01:01:01 -04:00
|
|
|
# If the archived parameter is passed, limit results accordingly
|
|
|
|
if params[:archived].present?
|
2014-08-11 08:25:25 -04:00
|
|
|
@projects = @projects.where(archived: parse_boolean(params[:archived]))
|
2014-07-28 01:01:01 -04:00
|
|
|
end
|
2014-08-11 08:25:25 -04:00
|
|
|
|
2015-01-12 05:08:53 -05:00
|
|
|
if params[:search].present?
|
|
|
|
@projects = @projects.search(params[:search])
|
|
|
|
end
|
|
|
|
|
2014-08-11 08:25:25 -04:00
|
|
|
@projects = paginate @projects
|
2012-08-10 18:07:50 -04:00
|
|
|
present @projects, with: Entities::Project
|
2012-06-29 06:46:01 -04:00
|
|
|
end
|
|
|
|
|
2013-06-03 12:49:04 -04:00
|
|
|
# Get an owned projects list for authenticated user
|
|
|
|
#
|
|
|
|
# Example Request:
|
|
|
|
# GET /projects/owned
|
|
|
|
get '/owned' do
|
2014-12-19 13:26:19 -05:00
|
|
|
sort = params[:sort] == 'desc' ? 'desc' : 'asc'
|
2014-12-19 07:27:27 -05:00
|
|
|
@projects = current_user.owned_projects
|
|
|
|
@projects = case params["order_by"]
|
2014-12-22 07:48:00 -05:00
|
|
|
when 'id' then @projects.reorder("id #{sort}")
|
|
|
|
when 'name' then @projects.reorder("name #{sort}")
|
|
|
|
when 'created_at' then @projects.reorder("created_at #{sort}")
|
|
|
|
when 'last_activity_at' then @projects.reorder("last_activity_at #{sort}")
|
|
|
|
else @projects
|
2014-12-19 07:27:27 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
@projects = paginate @projects
|
2013-06-03 12:49:04 -04:00
|
|
|
present @projects, with: Entities::Project
|
|
|
|
end
|
|
|
|
|
2013-11-18 07:15:59 -05:00
|
|
|
# Get all projects for admin user
|
|
|
|
#
|
|
|
|
# Example Request:
|
|
|
|
# GET /projects/all
|
|
|
|
get '/all' do
|
|
|
|
authenticated_as_admin!
|
2014-12-19 13:26:19 -05:00
|
|
|
sort = params[:sort] == 'desc' ? 'desc' : 'asc'
|
2014-12-19 07:27:27 -05:00
|
|
|
|
|
|
|
@projects = case params["order_by"]
|
2014-12-22 07:48:00 -05:00
|
|
|
when 'id' then Project.order("id #{sort}")
|
|
|
|
when 'name' then Project.order("name #{sort}")
|
|
|
|
when 'created_at' then Project.order("created_at #{sort}")
|
|
|
|
when 'last_activity_at' then Project.order("last_activity_at #{sort}")
|
|
|
|
else Project
|
2014-12-19 07:27:27 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
@projects = paginate @projects
|
2013-11-18 07:15:59 -05:00
|
|
|
present @projects, with: Entities::Project
|
|
|
|
end
|
|
|
|
|
2012-06-29 06:46:01 -04:00
|
|
|
# Get a single project
|
|
|
|
#
|
|
|
|
# Parameters:
|
2012-12-21 12:47:04 -05:00
|
|
|
# id (required) - The ID of a project
|
2012-06-29 06:46:01 -04:00
|
|
|
# Example Request:
|
|
|
|
# GET /projects/:id
|
|
|
|
get ":id" do
|
2014-03-17 08:33:41 -04:00
|
|
|
present user_project, with: Entities::ProjectWithAccess, user: current_user
|
2012-06-29 06:46:01 -04:00
|
|
|
end
|
|
|
|
|
2013-06-06 12:19:17 -04:00
|
|
|
# Get a single project events
|
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# id (required) - The ID of a project
|
|
|
|
# Example Request:
|
2014-11-14 03:32:49 -05:00
|
|
|
# GET /projects/:id/events
|
2013-06-06 12:19:17 -04:00
|
|
|
get ":id/events" do
|
|
|
|
limit = (params[:per_page] || 20).to_i
|
|
|
|
offset = (params[:page] || 0).to_i * limit
|
|
|
|
events = user_project.events.recent.limit(limit).offset(offset)
|
|
|
|
|
|
|
|
present events, with: Entities::Event
|
|
|
|
end
|
|
|
|
|
2012-08-31 03:15:37 -04:00
|
|
|
# Create new project
|
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# name (required) - name for new project
|
2012-09-03 11:00:24 -04:00
|
|
|
# description (optional) - short project description
|
2013-08-14 06:41:06 -04:00
|
|
|
# issues_enabled (optional)
|
|
|
|
# merge_requests_enabled (optional)
|
|
|
|
# wiki_enabled (optional)
|
2013-05-19 14:38:53 -04:00
|
|
|
# snippets_enabled (optional)
|
2013-03-07 09:11:33 -05:00
|
|
|
# namespace_id (optional) - defaults to user namespace
|
2013-11-06 10:13:21 -05:00
|
|
|
# public (optional) - if true same as setting visibility_level = 20
|
|
|
|
# visibility_level (optional) - 0 by default
|
2014-06-05 21:31:55 -04:00
|
|
|
# import_url (optional)
|
2012-08-31 03:15:37 -04:00
|
|
|
# Example Request
|
|
|
|
# POST /projects
|
|
|
|
post do
|
2013-02-27 11:50:30 -05:00
|
|
|
required_attributes! [:name]
|
2012-11-23 15:25:28 -05:00
|
|
|
attrs = attributes_for_keys [:name,
|
2013-09-22 00:50:18 -04:00
|
|
|
:path,
|
|
|
|
:description,
|
|
|
|
:issues_enabled,
|
|
|
|
:merge_requests_enabled,
|
|
|
|
:wiki_enabled,
|
|
|
|
:snippets_enabled,
|
|
|
|
:namespace_id,
|
2013-11-06 10:13:21 -05:00
|
|
|
:public,
|
2013-12-09 05:07:32 -05:00
|
|
|
:visibility_level,
|
|
|
|
:import_url]
|
2013-11-06 10:13:21 -05:00
|
|
|
attrs = map_public_to_visibility_level(attrs)
|
2014-01-16 13:35:21 -05:00
|
|
|
@project = ::Projects::CreateService.new(current_user, attrs).execute
|
2012-08-31 03:15:37 -04:00
|
|
|
if @project.saved?
|
|
|
|
present @project, with: Entities::Project
|
|
|
|
else
|
2013-02-14 09:51:56 -05:00
|
|
|
if @project.errors[:limit_reached].present?
|
|
|
|
error!(@project.errors[:limit_reached], 403)
|
|
|
|
end
|
2014-08-18 14:09:09 -04:00
|
|
|
render_validation_error!(@project)
|
2012-08-31 03:15:37 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-11-14 15:37:52 -05:00
|
|
|
# Create new project for a specified user. Only available to admin users.
|
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# user_id (required) - The ID of a user
|
|
|
|
# name (required) - name for new project
|
|
|
|
# description (optional) - short project description
|
|
|
|
# default_branch (optional) - 'master' by default
|
2013-08-14 06:41:06 -04:00
|
|
|
# issues_enabled (optional)
|
|
|
|
# merge_requests_enabled (optional)
|
2013-05-19 14:38:53 -04:00
|
|
|
# wiki_enabled (optional)
|
|
|
|
# snippets_enabled (optional)
|
2013-11-06 10:13:21 -05:00
|
|
|
# public (optional) - if true same as setting visibility_level = 20
|
|
|
|
# visibility_level (optional)
|
2014-06-05 21:31:55 -04:00
|
|
|
# import_url (optional)
|
2012-11-14 15:37:52 -05:00
|
|
|
# Example Request
|
|
|
|
# POST /projects/user/:user_id
|
|
|
|
post "user/:user_id" do
|
|
|
|
authenticated_as_admin!
|
|
|
|
user = User.find(params[:user_id])
|
|
|
|
attrs = attributes_for_keys [:name,
|
2013-09-22 00:50:18 -04:00
|
|
|
:description,
|
|
|
|
:default_branch,
|
|
|
|
:issues_enabled,
|
|
|
|
:merge_requests_enabled,
|
|
|
|
:wiki_enabled,
|
|
|
|
:snippets_enabled,
|
2013-11-06 10:13:21 -05:00
|
|
|
:public,
|
2014-06-05 21:31:55 -04:00
|
|
|
:visibility_level,
|
|
|
|
:import_url]
|
2013-11-06 10:13:21 -05:00
|
|
|
attrs = map_public_to_visibility_level(attrs)
|
2014-01-16 13:35:21 -05:00
|
|
|
@project = ::Projects::CreateService.new(user, attrs).execute
|
2012-11-14 15:37:52 -05:00
|
|
|
if @project.saved?
|
|
|
|
present @project, with: Entities::Project
|
|
|
|
else
|
2014-08-18 14:09:09 -04:00
|
|
|
render_validation_error!(@project)
|
2012-11-14 15:37:52 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-10-01 04:20:40 -04:00
|
|
|
# Fork new project for the current user.
|
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# id (required) - The ID of a project
|
|
|
|
# Example Request
|
|
|
|
# POST /projects/fork/:id
|
|
|
|
post 'fork/:id' do
|
|
|
|
@forked_project =
|
|
|
|
::Projects::ForkService.new(user_project,
|
|
|
|
current_user).execute
|
|
|
|
if @forked_project.errors.any?
|
|
|
|
conflict!(@forked_project.errors.messages)
|
|
|
|
else
|
|
|
|
present @forked_project, with: Entities::Project
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-10-09 07:41:41 -04:00
|
|
|
# Remove project
|
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# id (required) - The ID of a project
|
|
|
|
# Example Request:
|
|
|
|
# DELETE /projects/:id
|
|
|
|
delete ":id" do
|
|
|
|
authorize! :remove_project, user_project
|
2014-10-22 22:08:19 -04:00
|
|
|
::Projects::DestroyService.new(user_project, current_user, {}).execute
|
2013-10-09 07:41:41 -04:00
|
|
|
end
|
2012-11-14 15:37:52 -05:00
|
|
|
|
2013-06-27 17:49:26 -04:00
|
|
|
# Mark this project as forked from another
|
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# id: (required) - The ID of the project being marked as a fork
|
|
|
|
# forked_from_id: (required) - The ID of the project it was forked from
|
|
|
|
# Example Request:
|
|
|
|
# POST /projects/:id/fork/:forked_from_id
|
|
|
|
post ":id/fork/:forked_from_id" do
|
|
|
|
authenticated_as_admin!
|
|
|
|
forked_from_project = find_project(params[:forked_from_id])
|
|
|
|
unless forked_from_project.nil?
|
|
|
|
if user_project.forked_from_project.nil?
|
|
|
|
user_project.create_forked_project_link(forked_to_project_id: user_project.id, forked_from_project_id: forked_from_project.id)
|
|
|
|
else
|
|
|
|
render_api_error!("Project already forked", 409)
|
|
|
|
end
|
|
|
|
else
|
2014-12-30 09:17:46 -05:00
|
|
|
not_found!("Source Project")
|
2013-06-27 17:49:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
# Remove a forked_from relationship
|
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# id: (required) - The ID of the project being marked as a fork
|
|
|
|
# Example Request:
|
|
|
|
# DELETE /projects/:id/fork
|
|
|
|
delete ":id/fork" do
|
|
|
|
authenticated_as_admin!
|
|
|
|
unless user_project.forked_project_link.nil?
|
|
|
|
user_project.forked_project_link.destroy
|
|
|
|
end
|
|
|
|
end
|
2013-09-22 00:50:18 -04:00
|
|
|
# search for projects current_user has access to
|
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# query (required) - A string contained in the project name
|
2013-09-24 09:22:46 -04:00
|
|
|
# per_page (optional) - number of projects to return per page
|
|
|
|
# page (optional) - the page to retrieve
|
2013-09-22 00:50:18 -04:00
|
|
|
# Example Request:
|
|
|
|
# GET /projects/search/:query
|
|
|
|
get "/search/:query" do
|
|
|
|
ids = current_user.authorized_projects.map(&:id)
|
2013-11-06 10:13:21 -05:00
|
|
|
visibility_levels = [ Gitlab::VisibilityLevel::INTERNAL, Gitlab::VisibilityLevel::PUBLIC ]
|
|
|
|
projects = Project.where("(id in (?) OR visibility_level in (?)) AND (name LIKE (?))", ids, visibility_levels, "%#{params[:query]}%")
|
2014-12-19 13:26:19 -05:00
|
|
|
sort = params[:sort] == 'desc' ? 'desc' : 'asc'
|
2014-12-19 07:27:27 -05:00
|
|
|
|
|
|
|
projects = case params["order_by"]
|
2014-12-22 07:48:00 -05:00
|
|
|
when 'id' then projects.order("id #{sort}")
|
|
|
|
when 'name' then projects.order("name #{sort}")
|
|
|
|
when 'created_at' then projects.order("created_at #{sort}")
|
|
|
|
when 'last_activity_at' then projects.order("last_activity_at #{sort}")
|
|
|
|
else projects
|
2014-12-19 07:27:27 -05:00
|
|
|
end
|
|
|
|
|
2013-09-24 09:22:46 -04:00
|
|
|
present paginate(projects), with: Entities::Project
|
2013-09-22 00:50:18 -04:00
|
|
|
end
|
2014-02-13 09:08:26 -05:00
|
|
|
|
|
|
|
|
|
|
|
# Get a users list
|
|
|
|
#
|
|
|
|
# Example Request:
|
|
|
|
# GET /users
|
|
|
|
get ':id/users' do
|
|
|
|
@users = User.where(id: user_project.team.users.map(&:id))
|
|
|
|
@users = @users.search(params[:search]) if params[:search].present?
|
|
|
|
@users = paginate @users
|
2014-06-13 10:46:48 -04:00
|
|
|
present @users, with: Entities::UserBasic
|
2014-02-13 09:08:26 -05:00
|
|
|
end
|
2012-06-29 06:46:01 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|