services.rb 1.7 KB
Newer Older
1 2 3 4 5 6 7
module API
  # Projects API
  class Services < Grape::API
    before { authenticate! }
    before { authorize_admin_project }

    resource :projects do
K
Kirilll Zaitsev 已提交
8
      # Set <service_slug> service for project
9 10
      #
      # Example Request:
K
Kirilll Zaitsev 已提交
11
      #
12 13
      #   PUT /projects/:id/services/gitlab-ci
      #
K
Kirilll Zaitsev 已提交
14 15 16 17 18 19
      put ':id/services/:service_slug' do
        if project_service
          validators = project_service.class.validators.select do |s|
            s.class == ActiveRecord::Validations::PresenceValidator &&
              s.attributes != [:project_id]
          end
K
Kevin Houdebert 已提交
20

K
Kirilll Zaitsev 已提交
21
          required_attributes! validators.map(&:attributes).flatten.uniq
D
Dmitriy Zaporozhets 已提交
22
          attrs = attributes_for_keys service_attributes
K
Kevin Houdebert 已提交
23

K
Kirilll Zaitsev 已提交
24 25 26 27 28
          if project_service.update_attributes(attrs.merge(active: true))
            true
          else
            not_found!
          end
K
Kevin Houdebert 已提交
29 30 31
        end
      end

K
Kirilll Zaitsev 已提交
32
      # Delete <service_slug> service for project
K
Kevin Houdebert 已提交
33 34
      #
      # Example Request:
K
Kirilll Zaitsev 已提交
35 36 37 38 39 40 41 42
      #
      #   DELETE /project/:id/services/gitlab-ci
      #
      delete ':id/services/:service_slug' do
        if project_service
          attrs = service_attributes.inject({}) do |hash, key|
            hash.merge!(key => nil)
          end
D
Dmitriy Zaporozhets 已提交
43

K
Kirilll Zaitsev 已提交
44 45 46 47 48
          if project_service.update_attributes(attrs.merge(active: false))
            true
          else
            not_found!
          end
K
Kevin Houdebert 已提交
49 50
        end
      end
51 52 53 54 55 56 57 58

      # Get <service_slug> service settings for project
      #
      # Example Request:
      #
      #   GET /project/:id/services/gitlab-ci
      #
      get ':id/services/:service_slug' do
59
        present project_service, with: Entities::ProjectService, include_passwords: current_user.is_admin?
60
      end
61 62 63
    end
  end
end