groups.rb 3.75 KB
Newer Older
1
module API
2 3 4 5
  # groups API
  class Groups < Grape::API
    before { authenticate! }

6 7 8 9 10 11
    resource :groups do
      # Get a groups list
      #
      # Example Request:
      #  GET /groups
      get do
12 13 14 15 16 17 18 19
        @groups = if current_user.admin
                    Group.all
                  else
                    current_user.groups
                  end

        @groups = @groups.search(params[:search]) if params[:search].present?
        @groups = paginate @groups
20 21 22
        present @groups, with: Entities::Group
      end

23
      # Create group. Available only for users who can create groups.
24 25
      #
      # Parameters:
26 27 28 29
      #   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
30 31 32
      # Example Request:
      #   POST /groups
      post do
33
        authorize! :create_group, current_user
34
        required_attributes! [:name, :path]
35

36
        attrs = attributes_for_keys [:name, :path, :description, :visibility_level]
37 38 39
        @group = Group.new(attrs)

        if @group.save
40
          @group.add_owner(current_user)
41 42
          present @group, with: Entities::Group
        else
43
          render_api_error!("Failed to save group #{@group.errors.messages}", 400)
44 45 46
        end
      end

47 48 49
      # Update group. Available only for users who can administrate groups.
      #
      # Parameters:
50
      #   id (required)               - The ID of a group
51 52 53 54 55 56 57 58 59 60 61
      #   path (optional)             - The path of the group
      #   description (optional)      - The description of the group
      #   visibility_level (optional) - The visibility level of the group
      # Example Request:
      #   PUT /groups/:id
      put ':id' do
        group = find_group(params[:id])
        authorize! :admin_group, group

        attrs = attributes_for_keys [:name, :path, :description, :visibility_level]

62
        if ::Groups::UpdateService.new(group, current_user, attrs).execute
63
          present group, with: Entities::GroupDetail
64 65
        else
          render_validation_error!(group)
66 67 68
        end
      end

69 70 71 72 73 74 75
      # Get a single group, with containing projects
      #
      # Parameters:
      #   id (required) - The ID of a group
      # Example Request:
      #   GET /groups/:id
      get ":id" do
Izaak Alpert's avatar
Izaak Alpert committed
76 77
        group = find_group(params[:id])
        present group, with: Entities::GroupDetail
78
      end
Angus MacArthur's avatar
Angus MacArthur committed
79

80 81 82 83 84 85 86 87
      # Remove group
      #
      # Parameters:
      #   id (required) - The ID of a group
      # Example Request:
      #   DELETE /groups/:id
      delete ":id" do
        group = find_group(params[:id])
88
        authorize! :admin_group, group
89
        DestroyGroupService.new(group, current_user).execute
90 91
      end

92 93 94 95 96 97 98 99 100 101 102 103
      # 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

Angus MacArthur's avatar
Angus MacArthur committed
104 105 106 107 108 109 110 111 112
      # 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!
113
        group = Group.find_by(id: params[:id])
Angus MacArthur's avatar
Angus MacArthur committed
114
        project = Project.find(params[:project_id])
115
        result = ::Projects::TransferService.new(project, current_user).execute(group)
116 117 118

        if result
          present group
Angus MacArthur's avatar
Angus MacArthur committed
119
        else
120
          render_api_error!("Failed to transfer project #{project.errors.messages}", 400)
Angus MacArthur's avatar
Angus MacArthur committed
121
        end
Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
122
      end
123
    end
124 125
  end
end