debian-mirror-gitlab/lib/api/projects.rb

476 lines
17 KiB
Ruby
Raw Normal View History

2014-09-02 18:07:02 +05:30
module API
# Projects API
class Projects < Grape::API
before { authenticate! }
resource :projects, requirements: { id: /[^\/]+/ } do
2014-09-02 18:07:02 +05:30
helpers do
def map_public_to_visibility_level(attrs)
publik = attrs.delete(:public)
2015-12-23 02:04:40 +05:30
if publik.present? && !attrs[:visibility_level].present?
2016-09-13 17:45:13 +05:30
publik = to_boolean(publik)
2015-12-23 02:04:40 +05:30
# Since setting the public attribute to private could mean either
# private or internal, use the more conservative option, private.
attrs[:visibility_level] = (publik == true) ? Gitlab::VisibilityLevel::PUBLIC : Gitlab::VisibilityLevel::PRIVATE
end
2014-09-02 18:07:02 +05:30
attrs
end
end
# Get a projects list for authenticated user
#
# Example Request:
# GET /projects
get do
2016-11-03 12:29:30 +05:30
projects = current_user.authorized_projects
projects = filter_projects(projects)
projects = paginate projects
entity = params[:simple] ? Entities::BasicProjectDetails : Entities::ProjectWithAccess
present projects, with: entity, user: current_user
end
# Get a list of visible projects for authenticated user
#
# Example Request:
# GET /projects/visible
get '/visible' do
projects = ProjectsFinder.new.execute(current_user)
projects = filter_projects(projects)
projects = paginate projects
entity = params[:simple] ? Entities::BasicProjectDetails : Entities::ProjectWithAccess
present projects, with: entity, user: current_user
2014-09-02 18:07:02 +05:30
end
# Get an owned projects list for authenticated user
#
# Example Request:
# GET /projects/owned
get '/owned' do
2016-11-03 12:29:30 +05:30
projects = current_user.owned_projects
projects = filter_projects(projects)
projects = paginate projects
present projects, with: Entities::ProjectWithAccess, user: current_user
2014-09-02 18:07:02 +05:30
end
2015-12-23 02:04:40 +05:30
# Gets starred project for the authenticated user
#
# Example Request:
# GET /projects/starred
get '/starred' do
2016-11-03 12:29:30 +05:30
projects = current_user.viewable_starred_projects
projects = filter_projects(projects)
projects = paginate projects
present projects, with: Entities::Project, user: current_user
2015-12-23 02:04:40 +05:30
end
2014-09-02 18:07:02 +05:30
# Get all projects for admin user
#
# Example Request:
# GET /projects/all
get '/all' do
authenticated_as_admin!
2016-11-03 12:29:30 +05:30
projects = Project.all
projects = filter_projects(projects)
projects = paginate projects
present projects, with: Entities::ProjectWithAccess, user: current_user
2014-09-02 18:07:02 +05:30
end
# Get a single project
#
# Parameters:
# id (required) - The ID of a project
# Example Request:
# GET /projects/:id
get ":id" do
present user_project, with: Entities::ProjectWithAccess, user: current_user,
user_can_admin_project: can?(current_user, :admin_project, user_project)
2014-09-02 18:07:02 +05:30
end
2015-04-26 12:48:37 +05:30
# Get events for a single project
2014-09-02 18:07:02 +05:30
#
# Parameters:
# id (required) - The ID of a project
# Example Request:
2015-04-26 12:48:37 +05:30
# GET /projects/:id/events
2014-09-02 18:07:02 +05:30
get ":id/events" do
2015-04-26 12:48:37 +05:30
events = paginate user_project.events.recent
2014-09-02 18:07:02 +05:30
present events, with: Entities::Event
end
# Create new project
#
# Parameters:
2016-09-29 09:46:39 +05:30
# name (required) - name for new project
# description (optional) - short project description
2014-09-02 18:07:02 +05:30
# issues_enabled (optional)
# merge_requests_enabled (optional)
2015-11-26 14:37:03 +05:30
# builds_enabled (optional)
2014-09-02 18:07:02 +05:30
# wiki_enabled (optional)
# snippets_enabled (optional)
2016-06-02 11:05:42 +05:30
# container_registry_enabled (optional)
2015-12-23 02:04:40 +05:30
# shared_runners_enabled (optional)
2016-09-29 09:46:39 +05:30
# namespace_id (optional) - defaults to user namespace
# public (optional) - if true same as setting visibility_level = 20
# visibility_level (optional) - 0 by default
2014-09-02 18:07:02 +05:30
# import_url (optional)
2016-04-02 18:10:28 +05:30
# public_builds (optional)
2016-09-29 09:46:39 +05:30
# lfs_enabled (optional)
# request_access_enabled (optional) - Allow users to request member access
2014-09-02 18:07:02 +05:30
# Example Request
# POST /projects
post do
required_attributes! [:name]
2016-09-29 09:46:39 +05:30
attrs = attributes_for_keys [:builds_enabled,
:container_registry_enabled,
2014-09-02 18:07:02 +05:30
:description,
2016-09-29 09:46:39 +05:30
:import_url,
2014-09-02 18:07:02 +05:30
:issues_enabled,
2016-09-29 09:46:39 +05:30
:lfs_enabled,
2014-09-02 18:07:02 +05:30
:merge_requests_enabled,
2016-09-29 09:46:39 +05:30
:name,
2014-09-02 18:07:02 +05:30
:namespace_id,
2016-09-29 09:46:39 +05:30
:only_allow_merge_if_build_succeeds,
:path,
2014-09-02 18:07:02 +05:30
:public,
2016-09-29 09:46:39 +05:30
:public_builds,
:request_access_enabled,
:shared_runners_enabled,
:snippets_enabled,
2014-09-02 18:07:02 +05:30
:visibility_level,
2016-09-29 09:46:39 +05:30
:wiki_enabled]
2014-09-02 18:07:02 +05:30
attrs = map_public_to_visibility_level(attrs)
@project = ::Projects::CreateService.new(current_user, attrs).execute
if @project.saved?
present @project, with: Entities::Project,
user_can_admin_project: can?(current_user, :admin_project, @project)
2014-09-02 18:07:02 +05:30
else
if @project.errors[:limit_reached].present?
error!(@project.errors[:limit_reached], 403)
end
2015-04-26 12:48:37 +05:30
render_validation_error!(@project)
2014-09-02 18:07:02 +05:30
end
end
# Create new project for a specified user. Only available to admin users.
#
# Parameters:
2016-09-29 09:46:39 +05:30
# 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
2014-09-02 18:07:02 +05:30
# issues_enabled (optional)
# merge_requests_enabled (optional)
2015-11-26 14:37:03 +05:30
# builds_enabled (optional)
2014-09-02 18:07:02 +05:30
# wiki_enabled (optional)
# snippets_enabled (optional)
2016-06-02 11:05:42 +05:30
# container_registry_enabled (optional)
2015-12-23 02:04:40 +05:30
# shared_runners_enabled (optional)
2016-09-29 09:46:39 +05:30
# public (optional) - if true same as setting visibility_level = 20
2014-09-02 18:07:02 +05:30
# visibility_level (optional)
# import_url (optional)
2016-04-02 18:10:28 +05:30
# public_builds (optional)
2016-09-29 09:46:39 +05:30
# lfs_enabled (optional)
# request_access_enabled (optional) - Allow users to request member access
2014-09-02 18:07:02 +05:30
# Example Request
# POST /projects/user/:user_id
post "user/:user_id" do
authenticated_as_admin!
user = User.find(params[:user_id])
2016-09-29 09:46:39 +05:30
attrs = attributes_for_keys [:builds_enabled,
2014-09-02 18:07:02 +05:30
:default_branch,
2016-09-29 09:46:39 +05:30
:description,
:import_url,
2014-09-02 18:07:02 +05:30
:issues_enabled,
2016-09-29 09:46:39 +05:30
:lfs_enabled,
2014-09-02 18:07:02 +05:30
:merge_requests_enabled,
2016-09-29 09:46:39 +05:30
:name,
:only_allow_merge_if_build_succeeds,
2014-09-02 18:07:02 +05:30
:public,
2016-09-29 09:46:39 +05:30
:public_builds,
:request_access_enabled,
:shared_runners_enabled,
:snippets_enabled,
2014-09-02 18:07:02 +05:30
:visibility_level,
2016-09-29 09:46:39 +05:30
:wiki_enabled]
2014-09-02 18:07:02 +05:30
attrs = map_public_to_visibility_level(attrs)
@project = ::Projects::CreateService.new(user, attrs).execute
if @project.saved?
present @project, with: Entities::Project,
user_can_admin_project: can?(current_user, :admin_project, @project)
2014-09-02 18:07:02 +05:30
else
2015-04-26 12:48:37 +05:30
render_validation_error!(@project)
end
end
2016-09-29 09:46:39 +05:30
# Fork new project for the current user or provided namespace.
2015-04-26 12:48:37 +05:30
#
# Parameters:
# id (required) - The ID of a project
2016-09-29 09:46:39 +05:30
# namespace (optional) - The ID or name of the namespace that the project will be forked into.
2015-04-26 12:48:37 +05:30
# Example Request
# POST /projects/fork/:id
post 'fork/:id' do
2016-09-29 09:46:39 +05:30
attrs = {}
namespace_id = params[:namespace]
if namespace_id.present?
namespace = Namespace.find_by(id: namespace_id) || Namespace.find_by_path_or_name(namespace_id)
unless namespace && can?(current_user, :create_projects, namespace)
not_found!('Target Namespace')
end
attrs[:namespace] = namespace
end
2015-04-26 12:48:37 +05:30
@forked_project =
::Projects::ForkService.new(user_project,
2016-09-29 09:46:39 +05:30
current_user,
attrs).execute
2015-04-26 12:48:37 +05:30
if @forked_project.errors.any?
conflict!(@forked_project.errors.messages)
else
present @forked_project, with: Entities::Project,
user_can_admin_project: can?(current_user, :admin_project, @forked_project)
2016-04-02 18:10:28 +05:30
end
2015-04-26 12:48:37 +05:30
end
# Update an existing project
#
# Parameters:
# id (required) - the id of a project
# name (optional) - name of a project
# path (optional) - path of a project
# description (optional) - short project description
# issues_enabled (optional)
# merge_requests_enabled (optional)
2015-11-26 14:37:03 +05:30
# builds_enabled (optional)
2015-04-26 12:48:37 +05:30
# wiki_enabled (optional)
# snippets_enabled (optional)
2016-06-02 11:05:42 +05:30
# container_registry_enabled (optional)
2015-12-23 02:04:40 +05:30
# shared_runners_enabled (optional)
2015-04-26 12:48:37 +05:30
# public (optional) - if true same as setting visibility_level = 20
# visibility_level (optional) - visibility level of a project
2016-04-02 18:10:28 +05:30
# public_builds (optional)
2016-09-29 09:46:39 +05:30
# lfs_enabled (optional)
2015-04-26 12:48:37 +05:30
# Example Request
# PUT /projects/:id
put ':id' do
2016-09-29 09:46:39 +05:30
attrs = attributes_for_keys [:builds_enabled,
:container_registry_enabled,
2015-04-26 12:48:37 +05:30
:default_branch,
2016-09-29 09:46:39 +05:30
:description,
2015-04-26 12:48:37 +05:30
:issues_enabled,
2016-09-29 09:46:39 +05:30
:lfs_enabled,
2015-04-26 12:48:37 +05:30
:merge_requests_enabled,
2016-09-29 09:46:39 +05:30
:name,
:only_allow_merge_if_build_succeeds,
:path,
2015-04-26 12:48:37 +05:30
:public,
2016-09-29 09:46:39 +05:30
:public_builds,
:request_access_enabled,
:shared_runners_enabled,
:snippets_enabled,
2016-04-02 18:10:28 +05:30
:visibility_level,
2016-09-29 09:46:39 +05:30
:wiki_enabled]
2015-04-26 12:48:37 +05:30
attrs = map_public_to_visibility_level(attrs)
authorize_admin_project
authorize! :rename_project, user_project if attrs[:name].present?
if attrs[:visibility_level].present?
authorize! :change_visibility_level, user_project
end
::Projects::UpdateService.new(user_project,
current_user, attrs).execute
if user_project.errors.any?
render_validation_error!(user_project)
else
present user_project, with: Entities::Project,
user_can_admin_project: can?(current_user, :admin_project, user_project)
2014-09-02 18:07:02 +05:30
end
end
2016-06-02 11:05:42 +05:30
# Archive project
#
# Parameters:
# id (required) - The ID of a project
# Example Request:
# PUT /projects/:id/archive
post ':id/archive' do
authorize!(:archive_project, user_project)
user_project.archive!
present user_project, with: Entities::Project
end
# Unarchive project
#
# Parameters:
# id (required) - The ID of a project
# Example Request:
# PUT /projects/:id/unarchive
post ':id/unarchive' do
authorize!(:archive_project, user_project)
user_project.unarchive!
present user_project, with: Entities::Project
end
# Star project
#
# Parameters:
# id (required) - The ID of a project
# Example Request:
# POST /projects/:id/star
post ':id/star' do
if current_user.starred?(user_project)
not_modified!
else
current_user.toggle_star(user_project)
user_project.reload
present user_project, with: Entities::Project
end
end
# Unstar project
#
# Parameters:
# id (required) - The ID of a project
# Example Request:
# DELETE /projects/:id/star
delete ':id/star' do
if current_user.starred?(user_project)
current_user.toggle_star(user_project)
user_project.reload
present user_project, with: Entities::Project
else
not_modified!
end
end
2014-09-02 18:07:02 +05:30
# Remove project
#
# Parameters:
# id (required) - The ID of a project
# Example Request:
# DELETE /projects/:id
delete ":id" do
authorize! :remove_project, user_project
2016-09-13 17:45:13 +05:30
::Projects::DestroyService.new(user_project, current_user, {}).async_execute
2014-09-02 18:07:02 +05:30
end
# 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
2015-04-26 12:48:37 +05:30
not_found!("Source Project")
2014-09-02 18:07:02 +05:30
end
end
# Remove a forked_from relationship
#
# Parameters:
# id: (required) - The ID of the project being marked as a fork
2014-09-02 18:07:02 +05:30
# Example Request:
# DELETE /projects/:id/fork
delete ":id/fork" do
2015-11-26 14:37:03 +05:30
authorize! :remove_fork_project, user_project
if user_project.forked?
2014-09-02 18:07:02 +05:30
user_project.forked_project_link.destroy
end
end
2016-06-02 11:05:42 +05:30
# Share project with group
#
# Parameters:
2016-11-03 12:29:30 +05:30
# id (required) - The ID of a project
# group_id (required) - The ID of a group
2016-06-02 11:05:42 +05:30
# group_access (required) - Level of permissions for sharing
2016-11-03 12:29:30 +05:30
# expires_at (optional) - Share expiration date
2016-06-02 11:05:42 +05:30
#
# Example Request:
# POST /projects/:id/share
post ":id/share" do
authorize! :admin_project, user_project
required_attributes! [:group_id, :group_access]
2016-11-03 12:29:30 +05:30
attrs = attributes_for_keys [:group_id, :group_access, :expires_at]
group = Group.find_by_id(attrs[:group_id])
unless group && can?(current_user, :read_group, group)
not_found!('Group')
end
2016-06-02 11:05:42 +05:30
unless user_project.allowed_to_share_with_group?
return render_api_error!("The project sharing with group is disabled", 400)
end
2016-11-03 12:29:30 +05:30
link = user_project.project_group_links.new(attrs)
2016-06-02 11:05:42 +05:30
if link.save
present link, with: Entities::ProjectGroupLink
else
render_api_error!(link.errors.full_messages.first, 409)
end
end
# Upload a file
#
# Parameters:
# id: (required) - The ID of the project
# file: (required) - The file to be uploaded
post ":id/uploads" do
::Projects::UploadService.new(user_project, params[:file]).execute
end
2014-09-02 18:07:02 +05:30
# search for projects current_user has access to
#
# Parameters:
# query (required) - A string contained in the project name
# per_page (optional) - number of projects to return per page
# page (optional) - the page to retrieve
# Example Request:
# GET /projects/search/:query
get "/search/:query" do
2016-09-29 09:46:39 +05:30
search_service = Search::GlobalService.new(current_user, search: params[:query]).execute
projects = search_service.objects('projects', params[:page])
projects = projects.reorder(project_order_by => project_sort)
2015-04-26 12:48:37 +05:30
2014-09-02 18:07:02 +05:30
present paginate(projects), with: Entities::Project
end
# 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
present @users, with: Entities::UserBasic
end
end
end
end