BigW Consortium Gitlab

projects.rb 14.5 KB
Newer Older
1
module API
2 3
  # Projects API
  class Projects < Grape::API
4 5
    include PaginationParams

6
    before { authenticate_non_get! }
7

8 9 10 11 12 13 14 15 16 17 18
    helpers do
      params :optional_params do
        optional :description, type: String, desc: 'The description of the project'
        optional :issues_enabled, type: Boolean, desc: 'Flag indication if the issue tracker is enabled'
        optional :merge_requests_enabled, type: Boolean, desc: 'Flag indication if merge requests are enabled'
        optional :wiki_enabled, type: Boolean, desc: 'Flag indication if the wiki is enabled'
        optional :builds_enabled, type: Boolean, desc: 'Flag indication if builds are enabled'
        optional :snippets_enabled, type: Boolean, desc: 'Flag indication if snippets are enabled'
        optional :shared_runners_enabled, type: Boolean, desc: 'Flag indication if shared runners are enabled for that project'
        optional :container_registry_enabled, type: Boolean, desc: 'Flag indication if the container registry is enabled for that project'
        optional :lfs_enabled, type: Boolean, desc: 'Flag indication if Git LFS is enabled for that project'
19
        optional :visibility, type: String, values: Gitlab::VisibilityLevel.string_values, desc: 'The visibility of the project.'
20 21
        optional :public_builds, type: Boolean, desc: 'Perform public builds'
        optional :request_access_enabled, type: Boolean, desc: 'Allow users to request member access'
22
        optional :only_allow_merge_if_pipeline_succeeds, type: Boolean, desc: 'Only allow to merge if builds succeed'
23 24 25 26 27
        optional :only_allow_merge_if_all_discussions_are_resolved, type: Boolean, desc: 'Only allow to merge if all discussions are resolved'
      end
    end

    resource :projects do
28
      helpers do
29 30 31 32 33 34 35 36 37
        params :collection_params do
          use :sort_params
          use :filter_params
          use :pagination

          optional :simple, type: Boolean, default: false,
                            desc: 'Return only the ID, URL, name, and path of each project'
        end

38 39 40 41 42 43 44 45 46
        params :sort_params do
          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'
        end

        params :filter_params do
          optional :archived, type: Boolean, default: false, desc: 'Limit by archived status'
47
          optional :visibility, type: String, values: Gitlab::VisibilityLevel.string_values,
48
                                desc: 'Limit by visibility'
49
          optional :search, type: String, desc: 'Return list of projects matching the search criteria'
50 51
          optional :owned, type: Boolean, default: false, desc: 'Limit by owned by authenticated user'
          optional :starred, type: Boolean, default: false, desc: 'Limit by starred status'
52
          optional :membership, type: Boolean, default: false, desc: 'Limit by projects that the current user is a member of'
53 54 55 56
        end

        params :statistics_params do
          optional :statistics, type: Boolean, default: false, desc: 'Include project statistics'
57 58 59 60 61
        end

        params :create_params do
          optional :namespace_id, type: Integer, desc: 'Namespace ID for the new project. Default to the user namespace.'
          optional :import_url, type: String, desc: 'URL from which the project is imported'
62
        end
63 64 65 66 67 68 69 70 71 72 73 74 75 76

        def present_projects(projects, options = {})
          options = options.reverse_merge(
            with: Entities::Project,
            current_user: current_user,
            simple: params[:simple],
          )

          projects = filter_projects(projects)
          projects = projects.with_statistics if options[:statistics]
          options[:with] = Entities::BasicProjectDetails if options[:simple]

          present paginate(projects), options
        end
77 78
      end

79
      desc 'Get a list of visible projects for authenticated user' do
80 81 82
        success Entities::BasicProjectDetails
      end
      params do
83
        use :collection_params
84
      end
85
      get do
86 87
        entity = current_user ? Entities::ProjectWithAccess : Entities::BasicProjectDetails
        present_projects ProjectsFinder.new.execute(current_user), with: entity, statistics: params[:statistics]
88 89
      end

90 91 92 93
      desc 'Create new project' do
        success Entities::Project
      end
      params do
94
        optional :name, type: String, desc: 'The name of the project'
95
        optional :path, type: String, desc: 'The path of the repository'
96
        at_least_one_of :name, :path
97 98 99
        use :optional_params
        use :create_params
      end
100
      post do
101
        attrs = declared_params(include_missing: false)
102 103 104 105 106
        project = ::Projects::CreateService.new(current_user, attrs).execute

        if project.saved?
          present project, with: Entities::Project,
                           user_can_admin_project: can?(current_user, :admin_project, project)
107
        else
108 109
          if project.errors[:limit_reached].present?
            error!(project.errors[:limit_reached], 403)
110
          end
111
          render_validation_error!(project)
112 113 114
        end
      end

115 116 117 118 119 120 121 122 123 124
      desc 'Create new project for a specified user. Only available to admin users.' do
        success Entities::Project
      end
      params do
        requires :name, type: String, desc: 'The name of the project'
        requires :user_id, type: Integer, desc: 'The ID of a user'
        optional :default_branch, type: String, desc: 'The default branch of the project'
        use :optional_params
        use :create_params
      end
Angus MacArthur committed
125 126
      post "user/:user_id" do
        authenticated_as_admin!
127 128 129
        user = User.find_by(id: params.delete(:user_id))
        not_found!('User') unless user

130
        attrs = declared_params(include_missing: false)
131 132 133 134 135
        project = ::Projects::CreateService.new(user, attrs).execute

        if project.saved?
          present project, with: Entities::Project,
                           user_can_admin_project: can?(current_user, :admin_project, project)
Angus MacArthur committed
136
        else
137
          render_validation_error!(project)
Angus MacArthur committed
138 139
        end
      end
140 141 142 143 144
    end

    params do
      requires :id, type: String, desc: 'The ID of a project'
    end
145
    resource :projects, requirements: { id: %r{[^/]+} } do
146 147 148 149
      desc 'Get a single project' do
        success Entities::ProjectWithAccess
      end
      get ":id" do
150
        entity = current_user ? Entities::ProjectWithAccess : Entities::BasicProjectDetails
151
        present user_project, with: entity, current_user: current_user,
152 153 154 155 156 157 158 159 160 161 162 163
                              user_can_admin_project: can?(current_user, :admin_project, user_project)
      end

      desc 'Get events for a single project' do
        success Entities::Event
      end
      params do
        use :pagination
      end
      get ":id/events" do
        present paginate(user_project.events.recent), with: Entities::Event
      end
Angus MacArthur committed
164

165 166 167 168 169 170
      desc 'Fork new project for the current user or provided namespace.' do
        success Entities::Project
      end
      params do
        optional :namespace, type: String, desc: 'The ID or name of the namespace that the project will be forked into'
      end
171
      post ':id/fork' do
172 173
        fork_params = declared_params(include_missing: false)
        namespace_id = fork_params[:namespace]
174

175
        if namespace_id.present?
176 177 178 179 180
          fork_params[:namespace] = if namespace_id =~ /^\d+$/
                                      Namespace.find_by(id: namespace_id)
                                    else
                                      Namespace.find_by_path_or_name(namespace_id)
                                    end
181

182
          unless fork_params[:namespace] && can?(current_user, :create_projects, fork_params[:namespace])
183 184
            not_found!('Target Namespace')
          end
185
        end
186

187
        forked_project = ::Projects::ForkService.new(user_project, current_user, fork_params).execute
188

189 190
        if forked_project.errors.any?
          conflict!(forked_project.errors.messages)
191
        else
192 193
          present forked_project, with: Entities::Project,
                                  user_can_admin_project: can?(current_user, :admin_project, forked_project)
194
        end
195 196
      end

197 198 199 200 201 202 203 204 205 206 207
      desc 'Update an existing project' do
        success Entities::Project
      end
      params do
        optional :name, type: String, desc: 'The name of the project'
        optional :default_branch, type: String, desc: 'The default branch of the project'
        optional :path, type: String, desc: 'The path of the repository'
        use :optional_params
        at_least_one_of :name, :description, :issues_enabled, :merge_requests_enabled,
                        :wiki_enabled, :builds_enabled, :snippets_enabled,
                        :shared_runners_enabled, :container_registry_enabled,
208
                        :lfs_enabled, :visibility, :public_builds,
209
                        :request_access_enabled, :only_allow_merge_if_pipeline_succeeds,
210 211 212
                        :only_allow_merge_if_all_discussions_are_resolved, :path,
                        :default_branch
      end
213 214
      put ':id' do
        authorize_admin_project
215
        attrs = declared_params(include_missing: false)
216
        authorize! :rename_project, user_project if attrs[:name].present?
217
        authorize! :change_visibility_level, user_project if attrs[:visibility].present?
218

219
        result = ::Projects::UpdateService.new(user_project, current_user, attrs).execute
220

221
        if result[:status] == :success
222 223
          present user_project, with: Entities::Project,
                                user_can_admin_project: can?(current_user, :admin_project, user_project)
224 225
        else
          render_validation_error!(user_project)
226 227 228
        end
      end

229 230 231
      desc 'Archive a project' do
        success Entities::Project
      end
232
      post ':id/archive' do
233 234 235 236
        authorize!(:archive_project, user_project)

        user_project.archive!

237
        present user_project, with: Entities::Project
238 239
      end

240 241 242
      desc 'Unarchive a project' do
        success Entities::Project
      end
243
      post ':id/unarchive' do
244 245 246 247
        authorize!(:archive_project, user_project)

        user_project.unarchive!

248
        present user_project, with: Entities::Project
249 250
      end

251 252 253
      desc 'Star a project' do
        success Entities::Project
      end
254
      post ':id/star' do
255 256 257
        if current_user.starred?(user_project)
          not_modified!
        else
258 259
          current_user.toggle_star(user_project)
          user_project.reload
260

261 262 263 264
          present user_project, with: Entities::Project
        end
      end

265 266 267
      desc 'Unstar a project' do
        success Entities::Project
      end
268
      post ':id/unstar' do
269 270 271
        if current_user.starred?(user_project)
          current_user.toggle_star(user_project)
          user_project.reload
272

273 274 275 276 277 278
          present user_project, with: Entities::Project
        else
          not_modified!
        end
      end

279
      desc 'Remove a project'
280 281
      delete ":id" do
        authorize! :remove_project, user_project
Stan Hu committed
282
        ::Projects::DestroyService.new(user_project, current_user, {}).async_execute
283 284

        accepted!
285
      end
Angus MacArthur committed
286

287 288 289 290
      desc 'Mark this project as forked from another'
      params do
        requires :forked_from_id, type: String, desc: 'The ID of the project it was forked from'
      end
291 292
      post ":id/fork/:forked_from_id" do
        authenticated_as_admin!
293

294
        forked_from_project = find_project!(params[:forked_from_id])
295 296 297 298
        not_found!("Source Project") unless forked_from_project

        if user_project.forked_from_project.nil?
          user_project.create_forked_project_link(forked_to_project_id: user_project.id, forked_from_project_id: forked_from_project.id)
299
        else
300
          render_api_error!("Project already forked", 409)
301 302 303
        end
      end

304
      desc 'Remove a forked_from relationship'
305
      delete ":id/fork" do
306
        authorize! :remove_fork_project, user_project
307

308
        if user_project.forked?
309
          user_project.forked_project_link.destroy
310 311
        else
          not_modified!
312 313
        end
      end
314

315 316 317 318 319 320 321 322
      desc 'Share the project with a group' do
        success Entities::ProjectGroupLink
      end
      params do
        requires :group_id, type: Integer, desc: 'The ID of a group'
        requires :group_access, type: Integer, values: Gitlab::Access.values, desc: 'The group access level'
        optional :expires_at, type: Date, desc: 'Share expiration date'
      end
323 324
      post ":id/share" do
        authorize! :admin_project, user_project
325
        group = Group.find_by_id(params[:group_id])
326 327 328 329 330

        unless group && can?(current_user, :read_group, group)
          not_found!('Group')
        end

331 332 333 334
        unless user_project.allowed_to_share_with_group?
          return render_api_error!("The project sharing with group is disabled", 400)
        end

335
        link = user_project.project_group_links.new(declared_params(include_missing: false))
336

337 338 339 340 341 342 343
        if link.save
          present link, with: Entities::ProjectGroupLink
        else
          render_api_error!(link.errors.full_messages.first, 409)
        end
      end

344 345 346 347 348 349 350 351 352 353 354 355
      params do
        requires :group_id, type: Integer, desc: 'The ID of the group'
      end
      delete ":id/share/:group_id" do
        authorize! :admin_project, user_project

        link = user_project.project_group_links.find_by(group_id: params[:group_id])
        not_found!('Group Link') unless link

        link.destroy
      end

356 357 358 359
      desc 'Upload a file'
      params do
        requires :file, type: File, desc: 'The file to be uploaded'
      end
360 361 362 363
      post ":id/uploads" do
        ::Projects::UploadService.new(user_project, params[:file]).execute
      end

364 365 366 367 368 369
      desc 'Get the users list of a project' do
        success Entities::UserBasic
      end
      params do
        optional :search, type: String, desc: 'Return list of users matching the search criteria'
        use :pagination
370
      end
371
      get ':id/users' do
372
        users = user_project.team.users
373 374 375
        users = users.search(params[:search]) if params[:search].present?

        present paginate(users), with: Entities::UserBasic
376
      end
377 378 379 380 381 382 383 384 385 386 387 388 389

      desc 'Start the housekeeping task for a project' do
        detail 'This feature was introduced in GitLab 9.0.'
      end
      post ':id/housekeeping' do
        authorize_admin_project

        begin
          ::Projects::HousekeepingService.new(user_project).execute
        rescue ::Projects::HousekeepingService::LeaseTaken => error
          conflict!(error.message)
        end
      end
390 391 392
    end
  end
end