2013-05-14 12:33:31 +00:00
|
|
|
module API
|
2013-01-08 21:05:00 +00:00
|
|
|
# groups API
|
|
|
|
class Groups < Grape::API
|
|
|
|
before { authenticate! }
|
|
|
|
|
2013-02-01 14:00:12 +00:00
|
|
|
resource :groups do
|
|
|
|
# Get a groups list
|
|
|
|
#
|
|
|
|
# Example Request:
|
|
|
|
# GET /groups
|
|
|
|
get do
|
2014-12-30 09:30:56 +00: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 14:00:12 +00:00
|
|
|
present @groups, with: Entities::Group
|
|
|
|
end
|
|
|
|
|
2014-08-18 22:23:02 +00:00
|
|
|
# Create group. Available only for users who can create groups.
|
2013-02-01 14:00:12 +00:00
|
|
|
#
|
|
|
|
# Parameters:
|
2013-02-27 11:34:45 +00:00
|
|
|
# name (required) - The name of the group
|
|
|
|
# path (required) - The path of the group
|
2013-02-01 14:00:12 +00:00
|
|
|
# Example Request:
|
|
|
|
# POST /groups
|
|
|
|
post do
|
2014-08-18 22:23:02 +00:00
|
|
|
authorize! :create_group, current_user
|
2013-02-27 16:50:30 +00:00
|
|
|
required_attributes! [:name, :path]
|
2013-02-27 11:34:45 +00:00
|
|
|
|
2014-12-16 22:03:28 +00:00
|
|
|
attrs = attributes_for_keys [:name, :path, :description]
|
2013-02-01 14:00:12 +00:00
|
|
|
@group = Group.new(attrs)
|
|
|
|
|
|
|
|
if @group.save
|
2015-02-18 00:23:44 +00:00
|
|
|
@group.add_owner(current_user)
|
2013-02-01 14:00:12 +00:00
|
|
|
present @group, with: Entities::Group
|
|
|
|
else
|
2015-01-07 09:46:00 +00:00
|
|
|
render_api_error!("Failed to save group #{@group.errors.messages}", 400)
|
2013-02-01 14:00:12 +00: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 15:19:03 +00:00
|
|
|
group = find_group(params[:id])
|
|
|
|
present group, with: Entities::GroupDetail
|
2013-02-01 14:00:12 +00:00
|
|
|
end
|
2012-11-14 20:37:52 +00:00
|
|
|
|
2013-10-07 10:10:01 +00: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 12:39:10 +00:00
|
|
|
authorize! :admin_group, group
|
2015-06-03 12:07:20 +00:00
|
|
|
DestroyGroupService.new(group, current_user).execute
|
2013-10-07 10:10:01 +00:00
|
|
|
end
|
|
|
|
|
2015-12-07 16:10:40 +00:00
|
|
|
# Get a list of projects in this group
|
|
|
|
#
|
|
|
|
# Example Request:
|
|
|
|
# GET /groups/:id/projects
|
|
|
|
get ":id/projects" do
|
|
|
|
group = find_group(params[:id])
|
|
|
|
projects = group.projects
|
|
|
|
projects = filter_projects(projects)
|
|
|
|
projects = paginate projects
|
|
|
|
present projects, with: Entities::Project
|
|
|
|
end
|
|
|
|
|
2012-11-14 20:37:52 +00: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 12:31:25 +00:00
|
|
|
group = Group.find_by(id: params[:id])
|
2012-11-14 20:37:52 +00:00
|
|
|
project = Project.find(params[:project_id])
|
2015-07-02 12:31:25 +00:00
|
|
|
result = ::Projects::TransferService.new(project, current_user).execute(group)
|
2014-05-28 16:03:45 +00:00
|
|
|
|
|
|
|
if result
|
|
|
|
present group
|
2012-11-14 20:37:52 +00:00
|
|
|
else
|
2015-01-07 09:46:00 +00:00
|
|
|
render_api_error!("Failed to transfer project #{project.errors.messages}", 400)
|
2012-11-14 20:37:52 +00:00
|
|
|
end
|
2013-03-20 21:46:30 +00:00
|
|
|
end
|
2013-02-01 14:00:12 +00:00
|
|
|
end
|
2013-01-08 21:05:00 +00:00
|
|
|
end
|
|
|
|
end
|