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:
|
2013-02-27 06:34:45 -05:00
|
|
|
# name (required) - The name of the group
|
|
|
|
# path (required) - The path of the group
|
2013-02-01 09:00:12 -05:00
|
|
|
# Example Request:
|
|
|
|
# POST /groups
|
|
|
|
post do
|
2014-08-18 18:23:02 -04:00
|
|
|
authorize! :create_group, current_user
|
2013-02-27 11:50:30 -05:00
|
|
|
required_attributes! [:name, :path]
|
2013-02-27 06:34:45 -05:00
|
|
|
|
2014-12-16 17:03:28 -05:00
|
|
|
attrs = attributes_for_keys [:name, :path, :description]
|
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
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
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!
|
2014-05-28 12:03:45 -04:00
|
|
|
group = Group.find(params[:id])
|
2012-11-14 15:37:52 -05:00
|
|
|
project = Project.find(params[:project_id])
|
2014-05-28 12:03:45 -04:00
|
|
|
result = ::Projects::TransferService.new(project, current_user, namespace_id: group.id).execute
|
|
|
|
|
|
|
|
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
|