Commit 37abb20c authored by Rémy Coutable's avatar Rémy Coutable

Merge branch 'grapify-groups-api' into 'master'

Grapify the group API

## What are the relevant issue numbers?

Related to #22928

See merge request !7359
parents 68978697 ac12c1d2
module API module API
# groups API
class Groups < Grape::API class Groups < Grape::API
before { authenticate! } before { authenticate! }
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
resource :groups do resource :groups do
# Get a groups list desc 'Get a groups list' do
# success Entities::Group
# Parameters: end
# skip_groups (optional) - Array of group ids to exclude from list params do
# all_available (optional, boolean) - Show all group that you have access to optional :skip_groups, type: Array[Integer], desc: 'Array of group ids to exclude from list'
# Example Request: optional :all_available, type: Boolean, desc: 'Show all group that you have access to'
# GET /groups optional :search, type: String, desc: 'Search for a specific group'
end
get do get do
@groups = if current_user.admin groups = if current_user.admin
Group.all Group.all
elsif params[:all_available] elsif params[:all_available]
GroupsFinder.new.execute(current_user) GroupsFinder.new.execute(current_user)
else else
current_user.groups current_user.groups
end end
@groups = @groups.search(params[:search]) if params[:search].present? groups = groups.search(params[:search]) if params[:search].present?
@groups = @groups.where.not(id: params[:skip_groups]) if params[:skip_groups].present? groups = groups.where.not(id: params[:skip_groups]) if params[:skip_groups].present?
@groups = paginate @groups present paginate(groups), with: Entities::Group
present @groups, with: Entities::Group
end end
# Get list of owned groups for authenticated user desc 'Get list of owned groups for authenticated user' do
# success Entities::Group
# Example Request: end
# GET /groups/owned
get '/owned' do get '/owned' do
@groups = current_user.owned_groups groups = current_user.owned_groups
@groups = paginate @groups present paginate(groups), with: Entities::Group, user: current_user
present @groups, with: Entities::Group, user: current_user
end end
# Create group. Available only for users who can create groups. desc 'Create a group. Available only for users who can create groups.' do
# success Entities::Group
# Parameters: end
# name (required) - The name of the group params do
# path (required) - The path of the group requires :name, type: String, desc: 'The name of the group'
# description (optional) - The description of the group requires :path, type: String, desc: 'The path of the group'
# visibility_level (optional) - The visibility level of the group use :optional_params
# lfs_enabled (optional) - Enable/disable LFS for the projects in this group end
# request_access_enabled (optional) - Allow users to request member access
# Example Request:
# POST /groups
post do post do
authorize! :create_group authorize! :create_group
required_attributes! [:name, :path]
attrs = attributes_for_keys [:name, :path, :description, :visibility_level, :lfs_enabled, :request_access_enabled] group = ::Groups::CreateService.new(current_user, declared_params(include_missing: false)).execute
@group = Group.new(attrs)
if @group.save if group.persisted?
@group.add_owner(current_user) present group, with: Entities::Group
present @group, with: Entities::Group
else else
render_api_error!("Failed to save group #{@group.errors.messages}", 400) render_api_error!("Failed to save group #{group.errors.messages}", 400)
end end
end end
end
# Update group. Available only for users who can administrate groups. params do
# requires :id, type: String, desc: 'The ID of a group'
# Parameters: end
# id (required) - The ID of a group resource :groups do
# path (optional) - The path of the group desc 'Update a group. Available only for users who can administrate groups.' do
# description (optional) - The description of the group success Entities::Group
# visibility_level (optional) - The visibility level of the group end
# lfs_enabled (optional) - Enable/disable LFS for the projects in this group params do
# request_access_enabled (optional) - Allow users to request member access optional :name, type: String, desc: 'The name of the group'
# Example Request: optional :path, type: String, desc: 'The path of the group'
# PUT /groups/:id use :optional_params
at_least_one_of :name, :path, :description, :visibility_level,
:lfs_enabled, :request_access_enabled
end
put ':id' do put ':id' do
group = find_group(params[:id]) group = find_group(params[:id])
authorize! :admin_group, group authorize! :admin_group, group
attrs = attributes_for_keys [:name, :path, :description, :visibility_level, :lfs_enabled, :request_access_enabled] if ::Groups::UpdateService.new(group, current_user, declared_params(include_missing: false)).execute
if ::Groups::UpdateService.new(group, current_user, attrs).execute
present group, with: Entities::GroupDetail present group, with: Entities::GroupDetail
else else
render_validation_error!(group) render_validation_error!(group)
end end
end end
# Get a single group, with containing projects desc 'Get a single group, with containing projects.' do
# success Entities::GroupDetail
# Parameters: end
# id (required) - The ID of a group
# Example Request:
# GET /groups/:id
get ":id" do get ":id" do
group = find_group(params[:id]) group = find_group(params[:id])
present group, with: Entities::GroupDetail present group, with: Entities::GroupDetail
end end
# Remove group desc 'Remove a group.'
#
# Parameters:
# id (required) - The ID of a group
# Example Request:
# DELETE /groups/:id
delete ":id" do delete ":id" do
group = find_group(params[:id]) group = find_group(params[:id])
authorize! :admin_group, group authorize! :admin_group, group
DestroyGroupService.new(group, current_user).execute DestroyGroupService.new(group, current_user).execute
end end
# Get a list of projects in this group desc 'Get a list of projects in this group.' do
# success Entities::Project
# Example Request: end
# GET /groups/:id/projects
get ":id/projects" do get ":id/projects" do
group = find_group(params[:id]) group = find_group(params[:id])
projects = GroupProjectsFinder.new(group).execute(current_user) projects = GroupProjectsFinder.new(group).execute(current_user)
...@@ -120,13 +113,12 @@ module API ...@@ -120,13 +113,12 @@ module API
present projects, with: Entities::Project, user: current_user present projects, with: Entities::Project, user: current_user
end end
# Transfer a project to the Group namespace desc 'Transfer a project to the group namespace. Available only for admin.' do
# success Entities::GroupDetail
# Parameters: end
# id - group id params do
# project_id - project id requires :project_id, type: String, desc: 'The ID of the project'
# Example Request: end
# POST /groups/:id/projects/:project_id
post ":id/projects/:project_id" do post ":id/projects/:project_id" do
authenticated_as_admin! authenticated_as_admin!
group = Group.find_by(id: params[:id]) group = Group.find_by(id: params[:id])
...@@ -134,7 +126,7 @@ module API ...@@ -134,7 +126,7 @@ module API
result = ::Projects::TransferService.new(project, current_user).execute(group) result = ::Projects::TransferService.new(project, current_user).execute(group)
if result if result
present group present group, with: Entities::GroupDetail
else else
render_api_error!("Failed to transfer project #{project.errors.messages}", 400) render_api_error!("Failed to transfer project #{project.errors.messages}", 400)
end end
......
...@@ -167,7 +167,7 @@ describe API::API, api: true do ...@@ -167,7 +167,7 @@ describe API::API, api: true do
end end
it 'returns 404 for a non existing group' do it 'returns 404 for a non existing group' do
put api('/groups/1328', user1) put api('/groups/1328', user1), name: new_group_name
expect(response).to have_http_status(404) expect(response).to have_http_status(404)
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment