2013-05-14 08:33:31 -04:00
|
|
|
module API
|
2013-01-08 16:05:00 -05:00
|
|
|
class Groups < Grape::API
|
2016-12-04 12:11:19 -05:00
|
|
|
include PaginationParams
|
2016-12-12 03:55:29 -05:00
|
|
|
|
2017-08-23 08:01:11 -04:00
|
|
|
before { authenticate_non_get! }
|
2013-01-08 16:05:00 -05:00
|
|
|
|
2016-11-07 10:54:39 -05:00
|
|
|
helpers do
|
2017-04-05 12:31:15 -04:00
|
|
|
params :optional_params_ce do
|
2016-11-07 10:54:39 -05:00
|
|
|
optional :description, type: String, desc: 'The description of the group'
|
2017-08-30 04:11:24 -04:00
|
|
|
optional :visibility, type: String,
|
|
|
|
values: Gitlab::VisibilityLevel.string_values,
|
|
|
|
default: Gitlab::VisibilityLevel.string_level(
|
|
|
|
Gitlab::CurrentSettings.current_application_settings.default_group_visibility),
|
|
|
|
desc: 'The visibility of the group'
|
2016-11-07 10:54:39 -05:00
|
|
|
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'
|
2017-04-10 15:22:48 -04:00
|
|
|
optional :share_with_group_lock, type: Boolean, desc: 'Prevent sharing a project with another group within this group'
|
2016-11-07 10:54:39 -05:00
|
|
|
end
|
2016-11-22 11:58:10 -05:00
|
|
|
|
2017-04-05 12:31:15 -04:00
|
|
|
params :optional_params do
|
|
|
|
use :optional_params_ce
|
|
|
|
end
|
|
|
|
|
2016-11-22 11:58:10 -05:00
|
|
|
params :statistics_params do
|
|
|
|
optional :statistics, type: Boolean, default: false, desc: 'Include project statistics'
|
|
|
|
end
|
|
|
|
|
2017-11-09 11:07:04 -05:00
|
|
|
params :group_list_params do
|
2016-11-22 11:58:10 -05:00
|
|
|
use :statistics_params
|
2016-11-07 10:54:39 -05:00
|
|
|
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'
|
2017-02-24 03:25:53 -05:00
|
|
|
optional :owned, type: Boolean, default: false, desc: 'Limit by owned by authenticated user'
|
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-12-04 12:11:19 -05:00
|
|
|
use :pagination
|
2016-11-07 10:54:39 -05:00
|
|
|
end
|
2017-11-09 11:07:04 -05:00
|
|
|
|
|
|
|
def find_groups(params)
|
2017-09-18 11:07:38 -04:00
|
|
|
find_params = {
|
|
|
|
all_available: params[:all_available],
|
2017-11-09 11:07:04 -05:00
|
|
|
custom_attributes: params[:custom_attributes],
|
|
|
|
owned: params[:owned]
|
2017-09-18 11:07:38 -04:00
|
|
|
}
|
2017-11-09 11:07:04 -05:00
|
|
|
find_params[:parent] = find_group!(params[:id]) if params[:id]
|
2017-09-18 11:07:38 -04:00
|
|
|
|
2017-08-24 06:33:06 -04:00
|
|
|
groups = GroupsFinder.new(current_user, find_params).execute
|
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-24 07:30:53 -05:00
|
|
|
groups = groups.reorder(params[:order_by] => params[:sort])
|
2016-11-17 07:41:48 -05:00
|
|
|
|
2017-11-09 11:07:04 -05:00
|
|
|
groups
|
|
|
|
end
|
|
|
|
|
|
|
|
def present_groups(params, groups)
|
|
|
|
options = {
|
|
|
|
with: Entities::Group,
|
|
|
|
current_user: current_user,
|
|
|
|
statistics: params[:statistics] && current_user.admin?
|
|
|
|
}
|
|
|
|
|
|
|
|
groups = groups.with_statistics if options[:statistics]
|
|
|
|
present paginate(groups), options
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
resource :groups do
|
|
|
|
include CustomAttributesEndpoints
|
|
|
|
|
|
|
|
desc 'Get a groups list' do
|
|
|
|
success Entities::Group
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
use :group_list_params
|
|
|
|
end
|
|
|
|
get do
|
|
|
|
groups = find_groups(params)
|
|
|
|
present_groups params, groups
|
2013-02-01 09:00:12 -05: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'
|
2017-05-03 08:49:37 -04:00
|
|
|
|
|
|
|
if ::Group.supports_nested_groups?
|
|
|
|
optional :parent_id, type: Integer, desc: 'The parent group id for creating nested group'
|
|
|
|
end
|
|
|
|
|
2016-11-07 10:54:39 -05:00
|
|
|
use :optional_params
|
|
|
|
end
|
2013-02-01 09:00:12 -05:00
|
|
|
post do
|
2017-09-07 14:35:45 -04:00
|
|
|
parent_group = find_group!(params[:parent_id]) if params[:parent_id].present?
|
|
|
|
if parent_group
|
|
|
|
authorize! :create_subgroup, parent_group
|
|
|
|
else
|
|
|
|
authorize! :create_group
|
|
|
|
end
|
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?
|
2017-06-06 09:42:45 -04:00
|
|
|
present group, with: Entities::GroupDetail, current_user: current_user
|
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
|
2017-08-31 07:44:49 -04:00
|
|
|
resource :groups, requirements: API::PROJECT_ENDPOINT_REQUIREMENTS do
|
2016-11-07 10:54:39 -05:00
|
|
|
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
|
|
|
|
end
|
2016-04-07 04:12:49 -04:00
|
|
|
put ':id' do
|
2016-11-24 10:58:32 -05:00
|
|
|
group = find_group!(params[:id])
|
2016-04-07 04:12:49 -04:00
|
|
|
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-11-29 14:21:39 -05:00
|
|
|
present group, with: Entities::GroupDetail, current_user: current_user
|
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
|
2016-11-24 10:58:32 -05:00
|
|
|
group = find_group!(params[:id])
|
2016-11-29 14:21:39 -05:00
|
|
|
present group, with: Entities::GroupDetail, current_user: current_user
|
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
|
2016-11-24 10:58:32 -05:00
|
|
|
group = find_group!(params[:id])
|
2015-04-10 08:39:10 -04:00
|
|
|
authorize! :admin_group, group
|
2017-07-20 09:33:18 -04:00
|
|
|
|
2017-03-02 07:14:13 -05:00
|
|
|
destroy_conditionally!(group) do |group|
|
|
|
|
::Groups::DestroyService.new(group, current_user).execute
|
|
|
|
end
|
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
|
2016-12-04 12:11:19 -05:00
|
|
|
params do
|
2016-12-12 03:55:29 -05:00
|
|
|
optional :archived, type: Boolean, default: false, desc: 'Limit by archived status'
|
2017-02-16 08:56:14 -05:00
|
|
|
optional :visibility, type: String, values: Gitlab::VisibilityLevel.string_values,
|
2016-12-12 03:55:29 -05:00
|
|
|
desc: 'Limit by visibility'
|
|
|
|
optional :search, type: String, desc: 'Return list of authorized projects matching the search criteria'
|
|
|
|
optional :order_by, type: String, values: %w[id name path created_at updated_at last_activity_at],
|
|
|
|
default: 'created_at', desc: 'Return projects ordered by field'
|
|
|
|
optional :sort, type: String, values: %w[asc desc], default: 'desc',
|
|
|
|
desc: 'Return projects sorted in ascending and descending order'
|
2016-12-13 07:51:30 -05:00
|
|
|
optional :simple, type: Boolean, default: false,
|
|
|
|
desc: 'Return only the ID, URL, name, and path of each project'
|
2017-02-01 05:23:57 -05:00
|
|
|
optional :owned, type: Boolean, default: false, desc: 'Limit by owned by authenticated user'
|
|
|
|
optional :starred, type: Boolean, default: false, desc: 'Limit by starred status'
|
|
|
|
|
2016-12-04 12:11:19 -05:00
|
|
|
use :pagination
|
|
|
|
end
|
2015-12-07 11:10:40 -05:00
|
|
|
get ":id/projects" do
|
2016-11-24 10:58:32 -05:00
|
|
|
group = find_group!(params[:id])
|
2017-05-24 16:12:40 -04:00
|
|
|
projects = GroupProjectsFinder.new(group: group, current_user: current_user, params: project_finder_params).execute
|
2017-05-26 10:31:37 -04:00
|
|
|
projects = reorder_projects(projects)
|
2016-12-13 07:51:30 -05:00
|
|
|
entity = params[:simple] ? Entities::BasicProjectDetails : Entities::Project
|
2016-11-29 14:21:39 -05:00
|
|
|
present paginate(projects), with: entity, current_user: current_user
|
2015-12-07 11:10:40 -05:00
|
|
|
end
|
|
|
|
|
2017-11-09 11:07:04 -05:00
|
|
|
desc 'Get a list of subgroups in this group.' do
|
|
|
|
success Entities::Group
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
use :group_list_params
|
|
|
|
end
|
|
|
|
get ":id/subgroups" do
|
|
|
|
groups = find_groups(params)
|
|
|
|
present_groups params, groups
|
|
|
|
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
|
2017-01-02 13:19:47 -05:00
|
|
|
requires :project_id, type: String, desc: 'The ID or path of the project'
|
2016-11-07 10:54:39 -05:00
|
|
|
end
|
2017-03-15 14:09:24 -04:00
|
|
|
post ":id/projects/:project_id", requirements: { project_id: /.+/ } do
|
2012-11-14 15:37:52 -05:00
|
|
|
authenticated_as_admin!
|
2017-01-02 13:19:47 -05:00
|
|
|
group = find_group!(params[:id])
|
|
|
|
project = find_project!(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-29 14:21:39 -05:00
|
|
|
present group, with: Entities::GroupDetail, current_user: current_user
|
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
|