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

298 lines
12 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
module API
class Groups < Grape::API
2016-12-04 12:11:19 -05:00
include PaginationParams
include Helpers::CustomAttributes
2016-12-12 03:55:29 -05:00
before { authenticate_non_get! }
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'
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
if Gitlab.ee?
params :optional_params_ee do
optional :membership_lock, type: Boolean, desc: 'Prevent adding new members to project membership within this group'
optional :ldap_cn, type: String, desc: 'LDAP Common Name'
optional :ldap_access, type: Integer, desc: 'A valid access level'
optional :shared_runners_minutes_limit, type: Integer, desc: '(admin-only) Pipeline minutes quota for this group'
optional :extra_shared_runners_minutes_limit, type: Integer, desc: '(admin-only) Extra pipeline minutes quota for this group'
all_or_none_of :ldap_cn, :ldap_access
end
end
2017-04-05 12:31:15 -04:00
params :optional_params do
use :optional_params_ce
use :optional_params_ee if Gitlab.ee?
2017-04-05 12:31:15 -04:00
end
params :statistics_params do
optional :statistics, type: Boolean, default: false, desc: 'Include project statistics'
end
params :group_list_params do
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'
optional :owned, type: Boolean, default: false, desc: 'Limit by owned by authenticated user'
optional :order_by, type: String, values: %w[name path id], default: 'name', desc: 'Order by name, path or id'
optional :sort, type: String, values: %w[asc desc], default: 'asc', desc: 'Sort by asc (ascending) or desc (descending)'
optional :min_access_level, type: Integer, values: Gitlab::Access.all_values, desc: 'Minimum access level of authenticated user'
2016-12-04 12:11:19 -05:00
use :pagination
2016-11-07 10:54:39 -05:00
end
# rubocop: disable CodeReuse/ActiveRecord
def find_groups(params, parent_id = nil)
find_params = params.slice(:all_available, :custom_attributes, :owned, :min_access_level)
find_params[:parent] = find_group!(parent_id) if parent_id
find_params[:all_available] =
find_params.fetch(:all_available, current_user&.full_private_access?)
2017-09-18 11:07:38 -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?
order_options = { params[:order_by] => params[:sort] }
order_options["id"] ||= "asc"
groups = groups.reorder(order_options)
groups
end
# rubocop: enable CodeReuse/ActiveRecord
def create_group
# This is a separate method so that EE can extend its behaviour, without
# having to modify this code directly.
::Groups::CreateService
.new(current_user, declared_params(include_missing: false))
.execute
end
def update_group(group)
# This is a separate method so that EE can extend its behaviour, without
# having to modify this code directly.
::Groups::UpdateService
.new(group, current_user, declared_params(include_missing: false))
.execute
end
def find_group_projects(params)
group = find_group!(params[:id])
options = {
2018-10-26 02:34:31 -04:00
only_owned: !params[:with_shared],
include_subgroups: params[:include_subgroups]
}
projects = GroupProjectsFinder.new(
group: group,
current_user: current_user,
params: project_finder_params,
options: options
).execute
projects = projects.with_issues_available_for_user(current_user) if params[:with_issues_enabled]
projects = projects.with_merge_requests_enabled if params[:with_merge_requests_enabled]
projects = reorder_projects(projects)
paginate(projects)
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]
groups, options = with_custom_attributes(groups, options)
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
use :with_custom_attributes
end
get do
groups = find_groups(declared_params(include_missing: false), params[:id])
present_groups params, groups
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'
if ::Group.supports_nested_objects?
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
post do
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
group = create_group
2016-11-07 10:54:39 -05:00
if group.persisted?
present group, with: Entities::GroupDetail, current_user: current_user
else
2016-11-07 10:54:39 -05:00
render_api_error!("Failed to save group #{group.errors.messages}", 400)
end
end
2016-11-07 10:54:39 -05:00
end
2016-11-07 10:54:39 -05:00
params do
requires :id, type: String, desc: 'The ID of a group'
end
resource :groups, requirements: API::NAMESPACE_OR_PROJECT_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
if Gitlab.ee?
optional :file_template_project_id, type: Integer, desc: 'The ID of a project to use for custom templates in this group'
end
2016-11-07 10:54:39 -05:00
end
2016-04-07 04:12:49 -04:00
put ':id' do
group = find_group!(params[:id])
2016-04-07 04:12:49 -04:00
authorize! :admin_group, group
if update_group(group)
present group, with: Entities::GroupDetail, current_user: current_user
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
params do
use :with_custom_attributes
optional :with_projects, type: Boolean, default: true, desc: 'Omit project details'
end
get ":id" do
group = find_group!(params[:id])
options = {
with: params[:with_projects] ? Entities::GroupDetail : Entities::Group,
current_user: current_user
}
group, options = with_custom_attributes(group, options)
present group, options
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])
authorize! :admin_group, group
Gitlab::QueryLimiting.whitelist('https://gitlab.com/gitlab-org/gitlab-ce/issues/46285')
2017-03-02 07:14:13 -05:00
destroy_conditionally!(group) do |group|
2018-05-17 21:05:11 -04:00
::Groups::DestroyService.new(group, current_user).async_execute
2017-03-02 07:14:13 -05:00
end
2018-05-17 21:05:11 -04:00
accepted!
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'
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'
optional :simple, type: Boolean, default: false,
desc: 'Return only the ID, URL, name, and path of each project'
optional :owned, type: Boolean, default: false, desc: 'Limit by owned by authenticated user'
optional :starred, type: Boolean, default: false, desc: 'Limit by starred status'
optional :with_issues_enabled, type: Boolean, default: false, desc: 'Limit by enabled issues feature'
optional :with_merge_requests_enabled, type: Boolean, default: false, desc: 'Limit by enabled merge requests feature'
2018-10-26 02:34:31 -04:00
optional :with_shared, type: Boolean, default: true, desc: 'Include projects shared to this group'
optional :include_subgroups, type: Boolean, default: false, desc: 'Includes projects in subgroups of this group'
2016-12-04 12:11:19 -05:00
use :pagination
use :with_custom_attributes
2016-12-04 12:11:19 -05:00
end
2015-12-07 11:10:40 -05:00
get ":id/projects" do
projects = find_group_projects(params)
options = {
with: params[:simple] ? Entities::BasicProjectDetails : Entities::Project,
current_user: current_user
}
projects, options = with_custom_attributes(projects, options)
present options[:with].prepare_relation(projects), options
2015-12-07 11:10:40 -05:00
end
desc 'Get a list of subgroups in this group.' do
success Entities::Group
end
params do
use :group_list_params
use :with_custom_attributes
end
get ":id/subgroups" do
groups = find_groups(declared_params(include_missing: false), params[:id])
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
requires :project_id, type: String, desc: 'The ID or path of the project'
2016-11-07 10:54:39 -05:00
end
post ":id/projects/:project_id", requirements: { project_id: /.+/ } do
2012-11-14 15:37:52 -05:00
authenticated_as_admin!
group = find_group!(params[:id])
project = find_project!(params[:project_id])
result = ::Projects::TransferService.new(project, current_user).execute(group)
if result
present group, with: Entities::GroupDetail, current_user: current_user
2012-11-14 15:37:52 -05:00
else
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
end
end
end