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

5
    before { authenticate_non_get! }
6

R
Robert Schilling 已提交
7
    helpers do
8
      params :optional_params_ce do
R
Robert Schilling 已提交
9
        optional :description, type: String, desc: 'The description of the group'
10
        optional :visibility, type: String, values: Gitlab::VisibilityLevel.string_values, desc: 'The visibility of the group'
R
Robert Schilling 已提交
11 12
        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'
13
        optional :share_with_group_lock, type: Boolean, desc: 'Prevent sharing a project with another group within this group'
R
Robert Schilling 已提交
14
      end
M
Markus Koller 已提交
15

16 17 18 19
      params :optional_params do
        use :optional_params_ce
      end

M
Markus Koller 已提交
20 21 22 23 24 25 26
      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,
27
          current_user: current_user
M
Markus Koller 已提交
28 29 30 31 32
        )

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

35
    resource :groups do
R
Robert Schilling 已提交
36 37 38 39
      desc 'Get a groups list' do
        success Entities::Group
      end
      params do
M
Markus Koller 已提交
40
        use :statistics_params
R
Robert Schilling 已提交
41 42 43
        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'
44
        optional :owned, type: Boolean, default: false, desc: 'Limit by owned by authenticated user'
S
Sean McGivern 已提交
45 46
        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)'
47
        use :pagination
R
Robert Schilling 已提交
48
      end
49
      get do
50 51
        find_params = { all_available: params[:all_available], owned: params[:owned] }
        groups = GroupsFinder.new(current_user, find_params).execute
R
Robert Schilling 已提交
52 53
        groups = groups.search(params[:search]) if params[:search].present?
        groups = groups.where.not(id: params[:skip_groups]) if params[:skip_groups].present?
54
        groups = groups.reorder(params[:order_by] => params[:sort])
S
Sean McGivern 已提交
55

B
blackst0ne 已提交
56
        present_groups groups, statistics: params[:statistics] && current_user.admin?
57 58
      end

R
Robert Schilling 已提交
59 60 61 62 63 64
      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'
65 66 67 68 69

        if ::Group.supports_nested_groups?
          optional :parent_id, type: Integer, desc: 'The parent group id for creating nested group'
        end

R
Robert Schilling 已提交
70 71
        use :optional_params
      end
72
      post do
73
        authorize! :create_group
74

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

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

R
Robert Schilling 已提交
85 86 87
    params do
      requires :id, type: String, desc: 'The ID of a group'
    end
88
    resource :groups, requirements: { id: %r{[^/]+} } do
R
Robert Schilling 已提交
89 90 91 92 93 94 95 96
      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
      end
97
      put ':id' do
98
        group = find_group!(params[:id])
99 100
        authorize! :admin_group, group

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

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

R
Robert Schilling 已提交
116
      desc 'Remove a group.'
117
      delete ":id" do
118
        group = find_group!(params[:id])
119
        authorize! :admin_group, group
D
Dmitriy Zaporozhets 已提交
120

121 122 123
        destroy_conditionally!(group) do |group|
          ::Groups::DestroyService.new(group, current_user).execute
        end
124 125
      end

R
Robert Schilling 已提交
126 127 128
      desc 'Get a list of projects in this group.' do
        success Entities::Project
      end
129
      params do
R
Robert Schilling 已提交
130
        optional :archived, type: Boolean, default: false, desc: 'Limit by archived status'
131
        optional :visibility, type: String, values: Gitlab::VisibilityLevel.string_values,
R
Robert Schilling 已提交
132 133 134 135 136 137
                              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'
138 139
        optional :simple, type: Boolean, default: false,
                          desc: 'Return only the ID, URL, name, and path of each project'
T
Toon Claes 已提交
140 141 142
        optional :owned, type: Boolean, default: false, desc: 'Limit by owned by authenticated user'
        optional :starred, type: Boolean, default: false, desc: 'Limit by starred status'

143 144
        use :pagination
      end
D
Douwe Maan 已提交
145
      get ":id/projects" do
146
        group = find_group!(params[:id])
147
        projects = GroupProjectsFinder.new(group: group, current_user: current_user, params: project_finder_params).execute
T
Toon Claes 已提交
148
        projects = reorder_projects(projects)
149
        entity = params[:simple] ? Entities::BasicProjectDetails : Entities::Project
150
        present paginate(projects), with: entity, current_user: current_user
D
Douwe Maan 已提交
151 152
      end

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

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