2013-05-14 08:33:31 -04:00
|
|
|
module API
|
2013-01-08 16:05:00 -05:00
|
|
|
# groups API
|
|
|
|
class Groups < Grape::API
|
|
|
|
before { authenticate! }
|
|
|
|
|
2013-02-01 09:00:12 -05:00
|
|
|
resource :groups do
|
|
|
|
# Get a groups list
|
|
|
|
#
|
|
|
|
# Example Request:
|
|
|
|
# GET /groups
|
|
|
|
get do
|
2014-12-30 04:30:56 -05:00
|
|
|
@groups = if current_user.admin
|
|
|
|
Group.all
|
|
|
|
else
|
|
|
|
current_user.groups
|
|
|
|
end
|
|
|
|
|
|
|
|
@groups = @groups.search(params[:search]) if params[:search].present?
|
|
|
|
@groups = paginate @groups
|
2013-02-01 09:00:12 -05:00
|
|
|
present @groups, with: Entities::Group
|
|
|
|
end
|
|
|
|
|
2014-08-18 18:23:02 -04:00
|
|
|
# Create group. Available only for users who can create groups.
|
2013-02-01 09:00:12 -05:00
|
|
|
#
|
|
|
|
# Parameters:
|
2016-09-14 18:04:27 -04:00
|
|
|
# name (required) - The name of the group
|
|
|
|
# path (required) - The path of the group
|
|
|
|
# description (optional) - The description of the group
|
|
|
|
# visibility_level (optional) - The visibility level of the group
|
|
|
|
# lfs_enabled (optional) - Enable/disable LFS for the projects in this group
|
|
|
|
# request_access_enabled (optional) - Allow users to request member access
|
2013-02-01 09:00:12 -05:00
|
|
|
# Example Request:
|
|
|
|
# POST /groups
|
|
|
|
post do
|
2016-08-18 19:12:32 -04:00
|
|
|
authorize! :create_group
|
2013-02-27 11:50:30 -05:00
|
|
|
required_attributes! [:name, :path]
|
2013-02-27 06:34:45 -05:00
|
|
|
|
2016-09-14 18:04:27 -04:00
|
|
|
attrs = attributes_for_keys [:name, :path, :description, :visibility_level, :lfs_enabled, :request_access_enabled]
|
2013-02-01 09:00:12 -05:00
|
|
|
@group = Group.new(attrs)
|
|
|
|
|
|
|
|
if @group.save
|
2015-02-17 19:23:44 -05:00
|
|
|
@group.add_owner(current_user)
|
2013-02-01 09:00:12 -05:00
|
|
|
present @group, with: Entities::Group
|
|
|
|
else
|
2015-01-07 04:46:00 -05:00
|
|
|
render_api_error!("Failed to save group #{@group.errors.messages}", 400)
|
2013-02-01 09:00:12 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-07 04:12:49 -04:00
|
|
|
# Update group. Available only for users who can administrate groups.
|
|
|
|
#
|
|
|
|
# Parameters:
|
2016-09-14 18:04:27 -04:00
|
|
|
# id (required) - The ID of a group
|
|
|
|
# path (optional) - The path of the group
|
|
|
|
# description (optional) - The description of the group
|
|
|
|
# visibility_level (optional) - The visibility level of the group
|
|
|
|
# lfs_enabled (optional) - Enable/disable LFS for the projects in this group
|
|
|
|
# request_access_enabled (optional) - Allow users to request member access
|
2016-04-07 04:12:49 -04:00
|
|
|
# Example Request:
|
|
|
|
# PUT /groups/:id
|
|
|
|
put ':id' do
|
|
|
|
group = find_group(params[:id])
|
|
|
|
authorize! :admin_group, group
|
|
|
|
|
2016-09-14 18:04:27 -04:00
|
|
|
attrs = attributes_for_keys [:name, :path, :description, :visibility_level, :lfs_enabled, :request_access_enabled]
|
2016-04-07 04:12:49 -04:00
|
|
|
|
2016-04-12 13:08:35 -04:00
|
|
|
if ::Groups::UpdateService.new(group, current_user, attrs).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
|
|
|
|
|
2013-02-01 09:00:12 -05:00
|
|
|
# Get a single group, with containing projects
|
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# id (required) - The ID of a group
|
|
|
|
# Example Request:
|
|
|
|
# GET /groups/:id
|
|
|
|
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
|
|
|
|
2013-10-07 06:10:01 -04:00
|
|
|
# Remove group
|
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# id (required) - The ID of a group
|
|
|
|
# Example Request:
|
|
|
|
# DELETE /groups/:id
|
|
|
|
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
|
|
|
|
|
2015-12-07 11:10:40 -05:00
|
|
|
# Get a list of projects in this group
|
|
|
|
#
|
|
|
|
# Example Request:
|
|
|
|
# GET /groups/:id/projects
|
|
|
|
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
|
|
|
|
|
2012-11-14 15:37:52 -05:00
|
|
|
# Transfer a project to the Group namespace
|
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# id - group id
|
|
|
|
# project_id - project id
|
|
|
|
# Example Request:
|
|
|
|
# POST /groups/:id/projects/:project_id
|
|
|
|
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
|
|
|
|
present group
|
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
|