gitlab-org--gitlab-foss/lib/api/projects.rb

209 lines
7 KiB
Ruby
Raw Normal View History

module API
2012-06-29 06:46:01 -04:00
# Projects API
class Projects < Grape::API
before { authenticate! }
resource :projects do
helpers do
def handle_project_member_errors(errors)
if errors[:project_access].any?
error!(errors[:project_access], 422)
end
not_found!
end
end
2012-06-29 06:46:01 -04:00
# Get a projects list for authenticated user
#
# Example Request:
# GET /projects
get do
@projects = paginate current_user.authorized_projects
present @projects, with: Entities::Project
2012-06-29 06:46:01 -04:00
end
# Get an owned projects list for authenticated user
#
# Example Request:
# GET /projects/owned
get '/owned' do
@projects = paginate current_user.owned_projects
present @projects, with: Entities::Project
end
2012-06-29 06:46:01 -04:00
# Get a single project
#
# Parameters:
# id (required) - The ID of a project
2012-06-29 06:46:01 -04:00
# Example Request:
# GET /projects/:id
get ":id" do
present user_project, with: Entities::Project
2012-06-29 06:46:01 -04:00
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
# default_branch (optional) - 'master' by default
# issues_enabled (optional) - enabled by default
# wall_enabled (optional) - enabled by default
# merge_requests_enabled (optional) - enabled by default
# wiki_enabled (optional) - enabled by default
# namespace_id (optional) - defaults to user namespace
2012-08-31 03:15:37 -04:00
# Example Request
# POST /projects
post do
required_attributes! [:name]
attrs = attributes_for_keys [:name,
2012-09-21 06:22:30 -04:00
:description,
:default_branch,
:issues_enabled,
:wall_enabled,
:merge_requests_enabled,
:wiki_enabled,
:namespace_id]
2013-01-18 13:21:13 -05:00
@project = ::Projects::CreateContext.new(current_user, attrs).execute
2012-08-31 03:15:37 -04:00
if @project.saved?
present @project, with: Entities::Project
else
if @project.errors[:limit_reached].present?
error!(@project.errors[:limit_reached], 403)
end
2012-09-10 03:41:46 -04:00
not_found!
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
# issues_enabled (optional) - enabled by default
# wall_enabled (optional) - enabled by default
# merge_requests_enabled (optional) - enabled by default
# wiki_enabled (optional) - enabled by default
# 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,
:description,
:default_branch,
:issues_enabled,
:wall_enabled,
:merge_requests_enabled,
:wiki_enabled]
@project = ::Projects::CreateContext.new(user, attrs).execute
if @project.saved?
present @project, with: Entities::Project
else
not_found!
end
end
2012-09-21 06:23:17 -04:00
# Get a project team members
2012-09-06 16:49:29 -04:00
#
# Parameters:
# id (required) - The ID of a project
2012-12-18 13:52:18 -05:00
# query - Query string
2012-09-06 16:49:29 -04:00
# Example Request:
2012-09-21 06:23:17 -04:00
# GET /projects/:id/members
get ":id/members" do
2012-12-18 13:52:18 -05:00
if params[:query].present?
@members = paginate user_project.users.where("username LIKE ?", "%#{params[:query]}%")
else
@members = paginate user_project.users
end
2012-09-21 06:23:17 -04:00
present @members, with: Entities::ProjectMember, project: user_project
2012-09-06 16:49:29 -04:00
end
2012-09-21 06:23:17 -04:00
# Get a project team members
#
# Parameters:
# id (required) - The ID of a project
2012-09-21 06:23:17 -04:00
# user_id (required) - The ID of a user
# Example Request:
2012-09-21 06:23:17 -04:00
# GET /projects/:id/members/:user_id
get ":id/members/:user_id" do
@member = user_project.users.find params[:user_id]
present @member, with: Entities::ProjectMember, project: user_project
end
# Add a new project team member
#
# Parameters:
# id (required) - The ID of a project
2012-09-21 06:23:17 -04:00
# user_id (required) - The ID of a user
# access_level (required) - Project access level
# Example Request:
# POST /projects/:id/members
post ":id/members" do
2012-09-10 02:06:11 -04:00
authorize! :admin_project, user_project
required_attributes! [:user_id, :access_level]
# either the user is already a team member or a new one
team_member = user_project.team_member_by_id(params[:user_id])
if team_member.nil?
team_member = user_project.users_projects.new(
user_id: params[:user_id],
project_access: params[:access_level]
)
end
if team_member.save
@member = team_member.user
2012-09-21 06:23:17 -04:00
present @member, with: Entities::ProjectMember, project: user_project
else
handle_project_member_errors team_member.errors
2012-09-21 06:23:17 -04:00
end
end
2012-09-21 06:23:17 -04:00
# Update project team member
2012-09-06 16:49:29 -04:00
#
# Parameters:
# id (required) - The ID of a project
2012-09-21 06:23:17 -04:00
# user_id (required) - The ID of a team member
# access_level (required) - Project access level
2012-09-06 16:49:29 -04:00
# Example Request:
2012-09-21 06:23:17 -04:00
# PUT /projects/:id/members/:user_id
put ":id/members/:user_id" do
2012-09-10 02:06:11 -04:00
authorize! :admin_project, user_project
required_attributes! [:access_level]
2012-09-21 06:23:17 -04:00
team_member = user_project.users_projects.find_by_user_id(params[:user_id])
not_found!("User can not be found") if team_member.nil?
if team_member.update_attributes(project_access: params[:access_level])
@member = team_member.user
2012-09-21 06:23:17 -04:00
present @member, with: Entities::ProjectMember, project: user_project
else
handle_project_member_errors team_member.errors
2012-09-21 06:23:17 -04:00
end
2012-09-06 16:49:29 -04:00
end
2012-09-21 06:23:17 -04:00
# Remove a team member from project
2012-09-06 16:49:29 -04:00
#
# Parameters:
# id (required) - The ID of a project
2012-09-21 06:23:17 -04:00
# user_id (required) - The ID of a team member
2012-09-06 16:49:29 -04:00
# Example Request:
2012-09-21 06:23:17 -04:00
# DELETE /projects/:id/members/:user_id
delete ":id/members/:user_id" do
2012-09-10 02:06:11 -04:00
authorize! :admin_project, user_project
team_member = user_project.users_projects.find_by_user_id(params[:user_id])
unless team_member.nil?
team_member.destroy
else
{message: "Access revoked", id: params[:user_id].to_i}
end
2012-09-06 16:49:29 -04:00
end
2012-06-29 06:46:01 -04:00
end
end
end