BigW Consortium Gitlab

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

6 7 8
    resource :groups do
      # Get a groups list
      #
9 10 11
      # Parameters
      #   page (optional) - The page number of the groups list
      #   per_page (optional) - The number of elements per page
12 13 14 15 16 17 18 19 20 21 22 23 24 25
      # Example Request:
      #  GET /groups
      get do
        if current_user.admin
          @groups = paginate Group
        else
          @groups = paginate current_user.groups
        end
        present @groups, with: Entities::Group
      end

      # Create group. Available only for admin
      #
      # Parameters:
26 27
      #   name (required) - The name of the group
      #   path (required) - The path of the group
28 29 30 31
      # Example Request:
      #   POST /groups
      post do
        authenticated_as_admin!
32 33 34 35

        bad_request!(:name) unless params[:name].present?
        bad_request!(:path) unless params[:path].present?

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
        attrs = attributes_for_keys [:name, :path]
        @group = Group.new(attrs)
        @group.owner = current_user

        if @group.save
          present @group, with: Entities::Group
        else
          not_found!
        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
        @group = Group.find(params[:id])
        if current_user.admin or current_user.groups.include? @group
          present @group, with: Entities::GroupDetail
        else
          not_found!
        end
      end
    end
62 63
  end
end