groups.rb 6.8 KB
Newer Older
1
module API
2
  class Groups < Grape::API
3
    include PaginationParams
R
Robert Schilling 已提交
4

5 6
    before { authenticate! }

R
Robert Schilling 已提交
7 8 9 10 11 12 13
    helpers do
      params :optional_params do
        optional :description, type: String, desc: 'The description of the group'
        optional :visibility_level, type: Integer, desc: 'The visibility level of the group'
        optional :lfs_enabled, type: Boolean, desc: 'Enable/disable LFS for the projects in this group'
        optional :request_access_enabled, type: Boolean, desc: 'Allow users to request member access'
      end
M
Markus Koller 已提交
14 15 16 17 18 19 20 21 22 23 24 25 26 27

      params :statistics_params do
        optional :statistics, type: Boolean, default: false, desc: 'Include project statistics'
      end

      def present_groups(groups, options = {})
        options = options.reverse_merge(
          with: Entities::Group,
          current_user: current_user,
        )

        groups = groups.with_statistics if options[:statistics]
        present paginate(groups), options
      end
R
Robert Schilling 已提交
28 29
    end

30
    resource :groups do
R
Robert Schilling 已提交
31 32 33 34
      desc 'Get a groups list' do
        success Entities::Group
      end
      params do
M
Markus Koller 已提交
35
        use :statistics_params
R
Robert Schilling 已提交
36 37 38
        optional :skip_groups, type: Array[Integer], desc: 'Array of group ids to exclude from list'
        optional :all_available, type: Boolean, desc: 'Show all group that you have access to'
        optional :search, type: String, desc: 'Search for a specific group'
39
        optional :owned, type: Boolean, default: false, desc: 'Limit by owned by authenticated user'
S
Sean McGivern 已提交
40 41
        optional :order_by, type: String, values: %w[name path], default: 'name', desc: 'Order by name or path'
        optional :sort, type: String, values: %w[asc desc], default: 'asc', desc: 'Sort by asc (ascending) or desc (descending)'
42
        use :pagination
R
Robert Schilling 已提交
43
      end
44
      get do
45 46 47
        groups = if params[:owned]
                   current_user.owned_groups
                 elsif current_user.admin
R
Robert Schilling 已提交
48 49 50 51 52 53
                   Group.all
                 elsif params[:all_available]
                   GroupsFinder.new.execute(current_user)
                 else
                   current_user.groups
                 end
54

R
Robert Schilling 已提交
55 56
        groups = groups.search(params[:search]) if params[:search].present?
        groups = groups.where.not(id: params[:skip_groups]) if params[:skip_groups].present?
57
        groups = groups.reorder(params[:order_by] => params[:sort])
S
Sean McGivern 已提交
58

M
Markus Koller 已提交
59
        present_groups groups, statistics: params[:statistics] && current_user.is_admin?
60 61
      end

R
Robert Schilling 已提交
62 63 64 65 66 67
      desc 'Create a group. Available only for users who can create groups.' do
        success Entities::Group
      end
      params do
        requires :name, type: String, desc: 'The name of the group'
        requires :path, type: String, desc: 'The path of the group'
68
        optional :parent_id, type: Integer, desc: 'The parent group id for creating nested group'
R
Robert Schilling 已提交
69 70
        use :optional_params
      end
71
      post do
72
        authorize! :create_group
73

R
Robert Schilling 已提交
74
        group = ::Groups::CreateService.new(current_user, declared_params(include_missing: false)).execute
75

R
Robert Schilling 已提交
76
        if group.persisted?
77
          present group, with: Entities::Group, current_user: current_user
78
        else
R
Robert Schilling 已提交
79
          render_api_error!("Failed to save group #{group.errors.messages}", 400)
80 81
        end
      end
R
Robert Schilling 已提交
82
    end
83

R
Robert Schilling 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97
    params do
      requires :id, type: String, desc: 'The ID of a group'
    end
    resource :groups do
      desc 'Update a group. Available only for users who can administrate groups.' do
        success Entities::Group
      end
      params do
        optional :name, type: String, desc: 'The name of the group'
        optional :path, type: String, desc: 'The path of the group'
        use :optional_params
        at_least_one_of :name, :path, :description, :visibility_level,
                        :lfs_enabled, :request_access_enabled
      end
98
      put ':id' do
99
        group = find_group!(params[:id])
100 101
        authorize! :admin_group, group

R
Robert Schilling 已提交
102
        if ::Groups::UpdateService.new(group, current_user, declared_params(include_missing: false)).execute
103
          present group, with: Entities::GroupDetail, current_user: current_user
104 105
        else
          render_validation_error!(group)
106 107 108
        end
      end

R
Robert Schilling 已提交
109 110 111
      desc 'Get a single group, with containing projects.' do
        success Entities::GroupDetail
      end
112
      get ":id" do
113
        group = find_group!(params[:id])
114
        present group, with: Entities::GroupDetail, current_user: current_user
115
      end
A
Angus MacArthur 已提交
116

R
Robert Schilling 已提交
117
      desc 'Remove a group.'
118
      delete ":id" do
119
        group = find_group!(params[:id])
120
        authorize! :admin_group, group
121
        ::Groups::DestroyService.new(group, current_user).execute
122 123
      end

R
Robert Schilling 已提交
124 125 126
      desc 'Get a list of projects in this group.' do
        success Entities::Project
      end
127
      params do
R
Robert Schilling 已提交
128 129 130 131 132 133 134 135
        optional :archived, type: Boolean, default: false, desc: 'Limit by archived status'
        optional :visibility, type: String, values: %w[public internal private],
                              desc: 'Limit by visibility'
        optional :search, type: String, desc: 'Return list of authorized projects matching the search criteria'
        optional :order_by, type: String, values: %w[id name path created_at updated_at last_activity_at],
                            default: 'created_at', desc: 'Return projects ordered by field'
        optional :sort, type: String, values: %w[asc desc], default: 'desc',
                        desc: 'Return projects sorted in ascending and descending order'
136 137
        optional :simple, type: Boolean, default: false,
                          desc: 'Return only the ID, URL, name, and path of each project'
T
Toon Claes 已提交
138 139 140
        optional :owned, type: Boolean, default: false, desc: 'Limit by owned by authenticated user'
        optional :starred, type: Boolean, default: false, desc: 'Limit by starred status'

141 142
        use :pagination
      end
D
Douwe Maan 已提交
143
      get ":id/projects" do
144
        group = find_group!(params[:id])
145
        projects = GroupProjectsFinder.new(group).execute(current_user)
R
Robert Schilling 已提交
146
        projects = filter_projects(projects)
147
        entity = params[:simple] ? Entities::BasicProjectDetails : Entities::Project
148
        present paginate(projects), with: entity, current_user: current_user
D
Douwe Maan 已提交
149 150
      end

R
Robert Schilling 已提交
151 152 153 154
      desc 'Transfer a project to the group namespace. Available only for admin.' do
        success Entities::GroupDetail
      end
      params do
155
        requires :project_id, type: String, desc: 'The ID or path of the project'
R
Robert Schilling 已提交
156
      end
A
Angus MacArthur 已提交
157 158
      post ":id/projects/:project_id" do
        authenticated_as_admin!
159 160
        group = find_group!(params[:id])
        project = find_project!(params[:project_id])
161
        result = ::Projects::TransferService.new(project, current_user).execute(group)
162 163

        if result
164
          present group, with: Entities::GroupDetail, current_user: current_user
A
Angus MacArthur 已提交
165
        else
166
          render_api_error!("Failed to transfer project #{project.errors.messages}", 400)
A
Angus MacArthur 已提交
167
        end
A
Andrey Kumanyaev 已提交
168
      end
169
    end
170 171
  end
end