2018-09-29 18:34:47 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-11-15 08:24:10 -05:00
|
|
|
module API
|
2020-10-14 20:08:42 -04:00
|
|
|
class Namespaces < ::API::Base
|
2016-12-04 12:11:19 -05:00
|
|
|
include PaginationParams
|
|
|
|
|
2015-05-25 16:51:37 -04:00
|
|
|
before { authenticate! }
|
2013-11-15 08:24:10 -05:00
|
|
|
|
2018-12-07 08:29:53 -05:00
|
|
|
helpers do
|
|
|
|
params :optional_list_params_ee do
|
|
|
|
# EE::API::Namespaces would override this helper
|
|
|
|
end
|
|
|
|
|
|
|
|
# EE::API::Namespaces would override this method
|
|
|
|
def custom_namespace_present_options
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
prepend_mod_with('API::Namespaces') # rubocop: disable Cop/InjectEnterpriseEditionModule
|
2019-09-13 09:26:31 -04:00
|
|
|
|
2013-11-15 08:24:10 -05:00
|
|
|
resource :namespaces do
|
2016-09-13 14:55:26 -04:00
|
|
|
desc 'Get a namespaces list' do
|
|
|
|
success Entities::Namespace
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
optional :search, type: String, desc: "Search query for namespaces"
|
2021-07-29 17:10:10 -04:00
|
|
|
optional :owned_only, type: Boolean, desc: "Owned namespaces only"
|
2018-12-07 08:29:53 -05:00
|
|
|
|
2016-12-04 12:11:19 -05:00
|
|
|
use :pagination
|
2018-12-07 08:29:53 -05:00
|
|
|
use :optional_list_params_ee
|
2016-09-13 14:55:26 -04:00
|
|
|
end
|
2022-04-22 14:09:22 -04:00
|
|
|
get feature_category: :subgroups, urgency: :low do
|
2021-07-29 17:10:10 -04:00
|
|
|
owned_only = params[:owned_only] == true
|
|
|
|
|
|
|
|
namespaces = current_user.admin ? Namespace.all : current_user.namespaces(owned_only: owned_only)
|
2016-09-13 14:55:26 -04:00
|
|
|
|
2021-10-22 08:09:12 -04:00
|
|
|
namespaces = namespaces.without_project_namespaces.include_route
|
2020-10-11 23:08:20 -04:00
|
|
|
|
|
|
|
namespaces = namespaces.include_gitlab_subscription_with_hosted_plan if Gitlab.ee?
|
2019-12-23 10:07:48 -05:00
|
|
|
|
2016-09-13 14:55:26 -04:00
|
|
|
namespaces = namespaces.search(params[:search]) if params[:search].present?
|
2013-11-15 08:24:10 -05:00
|
|
|
|
2018-12-07 08:29:53 -05:00
|
|
|
options = { with: Entities::Namespace, current_user: current_user }
|
|
|
|
|
|
|
|
present paginate(namespaces), options.reverse_merge(custom_namespace_present_options)
|
2013-11-15 08:24:10 -05:00
|
|
|
end
|
2017-11-16 20:43:55 -05:00
|
|
|
|
|
|
|
desc 'Get a namespace by ID' do
|
|
|
|
success Entities::Namespace
|
|
|
|
end
|
|
|
|
params do
|
2017-11-23 08:32:16 -05:00
|
|
|
requires :id, type: String, desc: "Namespace's ID or path"
|
2017-11-16 20:43:55 -05:00
|
|
|
end
|
2022-05-02 17:09:12 -04:00
|
|
|
get ':id', requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS, feature_category: :subgroups, urgency: :low do
|
2019-04-24 13:39:59 -04:00
|
|
|
user_namespace = find_namespace!(params[:id])
|
|
|
|
|
2017-11-23 08:32:16 -05:00
|
|
|
present user_namespace, with: Entities::Namespace, current_user: current_user
|
2017-11-16 20:43:55 -05:00
|
|
|
end
|
2021-04-19 14:09:09 -04:00
|
|
|
|
|
|
|
desc 'Get existence of a namespace including alternative suggestions' do
|
|
|
|
success Entities::NamespaceExistence
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :namespace, type: String, desc: "Namespace's path"
|
|
|
|
optional :parent_id, type: Integer, desc: "The ID of the parent namespace. If no ID is specified, only top-level namespaces are considered."
|
|
|
|
end
|
2022-04-28 11:10:08 -04:00
|
|
|
get ':namespace/exists', requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS, feature_category: :subgroups, urgency: :low do
|
2022-09-19 05:13:01 -04:00
|
|
|
check_rate_limit!(:namespace_exists, scope: current_user)
|
2022-08-30 17:09:41 -04:00
|
|
|
|
2021-04-19 14:09:09 -04:00
|
|
|
namespace_path = params[:namespace]
|
2022-05-25 08:08:10 -04:00
|
|
|
existing_namespaces_within_the_parent = Namespace.without_project_namespaces.by_parent(params[:parent_id])
|
2021-04-19 14:09:09 -04:00
|
|
|
|
2022-05-25 08:08:10 -04:00
|
|
|
exists = existing_namespaces_within_the_parent.filter_by_path(namespace_path).exists?
|
|
|
|
suggestions = exists ? [Namespace.clean_path(namespace_path, limited_to: existing_namespaces_within_the_parent)] : []
|
2021-04-19 14:09:09 -04:00
|
|
|
|
|
|
|
present :exists, exists
|
|
|
|
present :suggests, suggestions
|
|
|
|
end
|
2013-11-15 08:24:10 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|