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
|
|
|
|
def handle_project_member_errors(errors)
|
|
|
|
if errors[:project_access].any?
|
|
|
|
error!(errors[:project_access], 422)
|
|
|
|
end
|
|
|
|
not_found!
|
|
|
|
end
|
2013-11-06 10:13:21 -05:00
|
|
|
|
|
|
|
def map_public_to_visibility_level(attrs)
|
|
|
|
publik = attrs.delete(:public)
|
|
|
|
publik = [ true, 1, '1', 't', 'T', 'true', 'TRUE', 'on', 'ON' ].include?(publik)
|
|
|
|
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
|
2013-01-02 12:32:34 -05:00
|
|
|
@projects = paginate current_user.authorized_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
|
|
|
|
@projects = paginate current_user.owned_projects
|
|
|
|
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!
|
|
|
|
@projects = paginate Project
|
|
|
|
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
|
2012-08-10 18:07:50 -04:00
|
|
|
present user_project, with: Entities::Project
|
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:
|
|
|
|
# GET /projects/:id
|
|
|
|
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)
|
|
|
|
# wall_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
|
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,
|
|
|
|
:wall_enabled,
|
|
|
|
:merge_requests_enabled,
|
|
|
|
:wiki_enabled,
|
|
|
|
:snippets_enabled,
|
|
|
|
:namespace_id,
|
2013-11-06 10:13:21 -05:00
|
|
|
:public,
|
|
|
|
:visibility_level]
|
|
|
|
attrs = map_public_to_visibility_level(attrs)
|
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
|
2013-02-14 09:51:56 -05:00
|
|
|
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
|
2013-08-14 06:41:06 -04:00
|
|
|
# issues_enabled (optional)
|
|
|
|
# wall_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)
|
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,
|
|
|
|
:wall_enabled,
|
|
|
|
:merge_requests_enabled,
|
|
|
|
:wiki_enabled,
|
|
|
|
:snippets_enabled,
|
2013-11-06 10:13:21 -05:00
|
|
|
:public,
|
|
|
|
:visibility_level]
|
|
|
|
attrs = map_public_to_visibility_level(attrs)
|
2012-11-14 15:37:52 -05:00
|
|
|
@project = ::Projects::CreateContext.new(user, attrs).execute
|
|
|
|
if @project.saved?
|
|
|
|
present @project, with: Entities::Project
|
|
|
|
else
|
|
|
|
not_found!
|
|
|
|
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
|
|
|
|
user_project.destroy
|
|
|
|
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
|
|
|
|
not_found!
|
|
|
|
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
|
|
|
|
|
2012-09-21 06:23:17 -04:00
|
|
|
# Get a project team members
|
2012-09-06 16:49:29 -04:00
|
|
|
#
|
|
|
|
# Parameters:
|
2012-12-21 12:47:04 -05:00
|
|
|
# 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
|
2012-09-05 16:01:03 -04:00
|
|
|
#
|
|
|
|
# Parameters:
|
2012-12-21 12:47:04 -05:00
|
|
|
# id (required) - The ID of a project
|
2012-09-21 06:23:17 -04:00
|
|
|
# user_id (required) - The ID of a user
|
2012-09-05 16:01:03 -04:00
|
|
|
# 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:
|
2012-12-21 12:47:04 -05:00
|
|
|
# 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
|
2013-02-27 11:50:30 -05:00
|
|
|
required_attributes! [:user_id, :access_level]
|
2013-02-08 08:33:29 -05:00
|
|
|
|
|
|
|
# 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
|
2013-02-08 10:33:15 -05:00
|
|
|
handle_project_member_errors team_member.errors
|
2012-09-21 06:23:17 -04:00
|
|
|
end
|
2012-09-05 16:01:03 -04:00
|
|
|
end
|
|
|
|
|
2012-09-21 06:23:17 -04:00
|
|
|
# Update project team member
|
2012-09-06 16:49:29 -04:00
|
|
|
#
|
|
|
|
# Parameters:
|
2012-12-21 12:47:04 -05:00
|
|
|
# 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
|
2013-02-27 11:50:30 -05:00
|
|
|
required_attributes! [:access_level]
|
2012-09-21 06:23:17 -04:00
|
|
|
|
2013-02-08 08:33:29 -05:00
|
|
|
team_member = user_project.users_projects.find_by_user_id(params[:user_id])
|
2013-02-13 09:48:52 -05:00
|
|
|
not_found!("User can not be found") if team_member.nil?
|
2013-02-08 08:33:29 -05:00
|
|
|
|
|
|
|
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
|
2013-02-08 10:33:15 -05:00
|
|
|
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:
|
2012-12-21 12:47:04 -05:00
|
|
|
# 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
|
2013-02-14 10:55:33 -05:00
|
|
|
team_member = user_project.users_projects.find_by_user_id(params[:user_id])
|
|
|
|
unless team_member.nil?
|
|
|
|
team_member.destroy
|
2013-02-01 08:53:35 -05:00
|
|
|
else
|
2013-03-18 16:11:28 -04:00
|
|
|
{message: "Access revoked", id: params[:user_id].to_i}
|
2013-02-01 08:53:35 -05:00
|
|
|
end
|
2012-09-06 16:49:29 -04:00
|
|
|
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]}%")
|
2013-09-24 09:22:46 -04:00
|
|
|
present paginate(projects), with: Entities::Project
|
2013-09-22 00:50:18 -04:00
|
|
|
end
|
2012-06-29 06:46:01 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|