2013-05-14 08:33:31 -04:00
|
|
|
module API
|
2013-01-08 16:05:00 -05:00
|
|
|
class Groups < Grape::API
|
|
|
|
before { authenticate! }
|
|
|
|
|
2016-11-07 10:54:39 -05:00
|
|
|
helpers do
|
|
|
|
params :optional_params do
|
|
|
|
optional :description, type: String, desc: 'The description of the group'
|
|
|
|
optional :visibility_level, type: Integer, desc: 'The visibility level of the group'
|
|
|
|
optional :lfs_enabled, type: Boolean, desc: 'Enable/disable LFS for the projects in this group'
|
|
|
|
optional :request_access_enabled, type: Boolean, desc: 'Allow users to request member access'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-01 09:00:12 -05:00
|
|
|
resource :groups do
|
2016-11-07 10:54:39 -05:00
|
|
|
desc 'Get a groups list' do
|
|
|
|
success Entities::Group
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
optional :skip_groups, type: Array[Integer], desc: 'Array of group ids to exclude from list'
|
|
|
|
optional :all_available, type: Boolean, desc: 'Show all group that you have access to'
|
|
|
|
optional :search, type: String, desc: 'Search for a specific group'
|
2016-11-17 07:41:48 -05:00
|
|
|
optional :order_by, type: String, values: %w[name path], default: 'name', desc: 'Order by name or path'
|
|
|
|
optional :sort, type: String, values: %w[asc desc], default: 'asc', desc: 'Sort by asc (ascending) or desc (descending)'
|
2016-11-07 10:54:39 -05:00
|
|
|
end
|
2013-02-01 09:00:12 -05:00
|
|
|
get do
|
2016-11-07 10:54:39 -05:00
|
|
|
groups = if current_user.admin
|
|
|
|
Group.all
|
|
|
|
elsif params[:all_available]
|
|
|
|
GroupsFinder.new.execute(current_user)
|
|
|
|
else
|
|
|
|
current_user.groups
|
|
|
|
end
|
2014-12-30 04:30:56 -05:00
|
|
|
|
2016-11-07 10:54:39 -05:00
|
|
|
groups = groups.search(params[:search]) if params[:search].present?
|
|
|
|
groups = groups.where.not(id: params[:skip_groups]) if params[:skip_groups].present?
|
2016-11-17 07:41:48 -05:00
|
|
|
groups = groups.reorder(params[:order_by] => params[:sort].to_sym)
|
|
|
|
|
2016-11-07 10:54:39 -05:00
|
|
|
present paginate(groups), with: Entities::Group
|
2013-02-01 09:00:12 -05:00
|
|
|
end
|
|
|
|
|
2016-11-07 10:54:39 -05:00
|
|
|
desc 'Get list of owned groups for authenticated user' do
|
|
|
|
success Entities::Group
|
|
|
|
end
|
2016-10-25 09:22:12 -04:00
|
|
|
get '/owned' do
|
2016-11-07 10:54:39 -05:00
|
|
|
groups = current_user.owned_groups
|
|
|
|
present paginate(groups), with: Entities::Group, user: current_user
|
2016-10-25 09:22:12 -04:00
|
|
|
end
|
|
|
|
|
2016-11-07 10:54:39 -05:00
|
|
|
desc 'Create a group. Available only for users who can create groups.' do
|
|
|
|
success Entities::Group
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :name, type: String, desc: 'The name of the group'
|
|
|
|
requires :path, type: String, desc: 'The path of the group'
|
|
|
|
use :optional_params
|
|
|
|
end
|
2013-02-01 09:00:12 -05:00
|
|
|
post do
|
2016-08-18 19:12:32 -04:00
|
|
|
authorize! :create_group
|
2013-02-27 06:34:45 -05:00
|
|
|
|
2016-11-07 10:54:39 -05:00
|
|
|
group = ::Groups::CreateService.new(current_user, declared_params(include_missing: false)).execute
|
2013-02-01 09:00:12 -05:00
|
|
|
|
2016-11-07 10:54:39 -05:00
|
|
|
if group.persisted?
|
|
|
|
present group, with: Entities::Group
|
2013-02-01 09:00:12 -05:00
|
|
|
else
|
2016-11-07 10:54:39 -05:00
|
|
|
render_api_error!("Failed to save group #{group.errors.messages}", 400)
|
2013-02-01 09:00:12 -05:00
|
|
|
end
|
|
|
|
end
|
2016-11-07 10:54:39 -05:00
|
|
|
end
|
2013-02-01 09:00:12 -05:00
|
|
|
|
2016-11-07 10:54:39 -05:00
|
|
|
params do
|
|
|
|
requires :id, type: String, desc: 'The ID of a group'
|
|
|
|
end
|
|
|
|
resource :groups do
|
|
|
|
desc 'Update a group. Available only for users who can administrate groups.' do
|
|
|
|
success Entities::Group
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
optional :name, type: String, desc: 'The name of the group'
|
|
|
|
optional :path, type: String, desc: 'The path of the group'
|
|
|
|
use :optional_params
|
|
|
|
at_least_one_of :name, :path, :description, :visibility_level,
|
|
|
|
:lfs_enabled, :request_access_enabled
|
|
|
|
end
|
2016-04-07 04:12:49 -04:00
|
|
|
put ':id' do
|
|
|
|
group = find_group(params[:id])
|
|
|
|
authorize! :admin_group, group
|
|
|
|
|
2016-11-07 10:54:39 -05:00
|
|
|
if ::Groups::UpdateService.new(group, current_user, declared_params(include_missing: false)).execute
|
2016-04-07 04:12:49 -04:00
|
|
|
present group, with: Entities::GroupDetail
|
2016-04-12 13:08:35 -04:00
|
|
|
else
|
|
|
|
render_validation_error!(group)
|
2016-04-07 04:12:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-07 10:54:39 -05:00
|
|
|
desc 'Get a single group, with containing projects.' do
|
|
|
|
success Entities::GroupDetail
|
|
|
|
end
|
2013-02-01 09:00:12 -05:00
|
|
|
get ":id" do
|
2013-09-04 11:19:03 -04:00
|
|
|
group = find_group(params[:id])
|
|
|
|
present group, with: Entities::GroupDetail
|
2013-02-01 09:00:12 -05:00
|
|
|
end
|
2012-11-14 15:37:52 -05:00
|
|
|
|
2016-11-07 10:54:39 -05:00
|
|
|
desc 'Remove a group.'
|
2013-10-07 06:10:01 -04:00
|
|
|
delete ":id" do
|
|
|
|
group = find_group(params[:id])
|
2015-04-10 08:39:10 -04:00
|
|
|
authorize! :admin_group, group
|
2015-06-03 08:07:20 -04:00
|
|
|
DestroyGroupService.new(group, current_user).execute
|
2013-10-07 06:10:01 -04:00
|
|
|
end
|
|
|
|
|
2016-11-07 10:54:39 -05:00
|
|
|
desc 'Get a list of projects in this group.' do
|
|
|
|
success Entities::Project
|
|
|
|
end
|
2015-12-07 11:10:40 -05:00
|
|
|
get ":id/projects" do
|
|
|
|
group = find_group(params[:id])
|
2016-05-24 20:55:57 -04:00
|
|
|
projects = GroupProjectsFinder.new(group).execute(current_user)
|
2015-12-07 11:10:40 -05:00
|
|
|
projects = paginate projects
|
2016-08-01 18:31:21 -04:00
|
|
|
present projects, with: Entities::Project, user: current_user
|
2015-12-07 11:10:40 -05:00
|
|
|
end
|
|
|
|
|
2016-11-07 10:54:39 -05:00
|
|
|
desc 'Transfer a project to the group namespace. Available only for admin.' do
|
|
|
|
success Entities::GroupDetail
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :project_id, type: String, desc: 'The ID of the project'
|
|
|
|
end
|
2012-11-14 15:37:52 -05:00
|
|
|
post ":id/projects/:project_id" do
|
|
|
|
authenticated_as_admin!
|
2015-07-02 08:31:25 -04:00
|
|
|
group = Group.find_by(id: params[:id])
|
2012-11-14 15:37:52 -05:00
|
|
|
project = Project.find(params[:project_id])
|
2015-07-02 08:31:25 -04:00
|
|
|
result = ::Projects::TransferService.new(project, current_user).execute(group)
|
2014-05-28 12:03:45 -04:00
|
|
|
|
|
|
|
if result
|
2016-11-07 10:54:39 -05:00
|
|
|
present group, with: Entities::GroupDetail
|
2012-11-14 15:37:52 -05:00
|
|
|
else
|
2015-01-07 04:46:00 -05:00
|
|
|
render_api_error!("Failed to transfer project #{project.errors.messages}", 400)
|
2012-11-14 15:37:52 -05:00
|
|
|
end
|
2013-03-20 17:46:30 -04:00
|
|
|
end
|
2013-02-01 09:00:12 -05:00
|
|
|
end
|
2013-01-08 16:05:00 -05:00
|
|
|
end
|
|
|
|
end
|