groups.rb 6.0 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 14 15
    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
    end

16
    resource :groups do
R
Robert Schilling 已提交
17 18 19 20 21 22 23
      desc 'Get a groups list' do
        success Entities::Group
      end
      params do
        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'
S
Sean McGivern 已提交
24 25
        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)'
26
        use :pagination
R
Robert Schilling 已提交
27
      end
28
      get do
R
Robert Schilling 已提交
29 30 31 32 33 34 35
        groups = if current_user.admin
                   Group.all
                 elsif params[:all_available]
                   GroupsFinder.new.execute(current_user)
                 else
                   current_user.groups
                 end
36

R
Robert Schilling 已提交
37 38
        groups = groups.search(params[:search]) if params[:search].present?
        groups = groups.where.not(id: params[:skip_groups]) if params[:skip_groups].present?
39
        groups = groups.reorder(params[:order_by] => params[:sort])
S
Sean McGivern 已提交
40

R
Robert Schilling 已提交
41
        present paginate(groups), with: Entities::Group
42 43
      end

R
Robert Schilling 已提交
44 45 46
      desc 'Get list of owned groups for authenticated user' do
        success Entities::Group
      end
47 48 49
      params do
        use :pagination
      end
B
Borja Aparicio 已提交
50
      get '/owned' do
R
Robert Schilling 已提交
51 52
        groups = current_user.owned_groups
        present paginate(groups), with: Entities::Group, user: current_user
B
Borja Aparicio 已提交
53 54
      end

R
Robert Schilling 已提交
55 56 57 58 59 60 61 62
      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'
        use :optional_params
      end
63
      post do
64
        authorize! :create_group
65

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

R
Robert Schilling 已提交
68 69
        if group.persisted?
          present group, with: Entities::Group
70
        else
R
Robert Schilling 已提交
71
          render_api_error!("Failed to save group #{group.errors.messages}", 400)
72 73
        end
      end
R
Robert Schilling 已提交
74
    end
75

R
Robert Schilling 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89
    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
90
      put ':id' do
91
        group = find_group!(params[:id])
92 93
        authorize! :admin_group, group

R
Robert Schilling 已提交
94
        if ::Groups::UpdateService.new(group, current_user, declared_params(include_missing: false)).execute
95
          present group, with: Entities::GroupDetail
96 97
        else
          render_validation_error!(group)
98 99 100
        end
      end

R
Robert Schilling 已提交
101 102 103
      desc 'Get a single group, with containing projects.' do
        success Entities::GroupDetail
      end
104
      get ":id" do
105
        group = find_group!(params[:id])
I
Izaak Alpert 已提交
106
        present group, with: Entities::GroupDetail
107
      end
A
Angus MacArthur 已提交
108

R
Robert Schilling 已提交
109
      desc 'Remove a group.'
110
      delete ":id" do
111
        group = find_group!(params[:id])
112
        authorize! :admin_group, group
113
        DestroyGroupService.new(group, current_user).execute
114 115
      end

R
Robert Schilling 已提交
116 117 118
      desc 'Get a list of projects in this group.' do
        success Entities::Project
      end
119
      params do
R
Robert Schilling 已提交
120 121 122 123 124 125 126 127
        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'
128 129
        optional :simple, type: Boolean, default: false,
                          desc: 'Return only the ID, URL, name, and path of each project'
130 131
        use :pagination
      end
D
Douwe Maan 已提交
132
      get ":id/projects" do
133
        group = find_group!(params[:id])
134
        projects = GroupProjectsFinder.new(group).execute(current_user)
R
Robert Schilling 已提交
135
        projects = filter_projects(projects)
136 137
        entity = params[:simple] ? Entities::BasicProjectDetails : Entities::Project
        present paginate(projects), with: entity, user: current_user
D
Douwe Maan 已提交
138 139
      end

R
Robert Schilling 已提交
140 141 142 143 144 145
      desc 'Transfer a project to the group namespace. Available only for admin.' do
        success Entities::GroupDetail
      end
      params do
        requires :project_id, type: String, desc: 'The ID of the project'
      end
A
Angus MacArthur 已提交
146 147
      post ":id/projects/:project_id" do
        authenticated_as_admin!
148
        group = Group.find_by(id: params[:id])
A
Angus MacArthur 已提交
149
        project = Project.find(params[:project_id])
150
        result = ::Projects::TransferService.new(project, current_user).execute(group)
151 152

        if result
R
Robert Schilling 已提交
153
          present group, with: Entities::GroupDetail
A
Angus MacArthur 已提交
154
        else
155
          render_api_error!("Failed to transfer project #{project.errors.messages}", 400)
A
Angus MacArthur 已提交
156
        end
A
Andrey Kumanyaev 已提交
157
      end
158
    end
159 160
  end
end