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

K
Kirilll Zaitsev 已提交
7

8
    resource :projects do
K
Kirilll Zaitsev 已提交
9
      # Set <service_slug> service for project
10 11
      #
      # Example Request:
K
Kirilll Zaitsev 已提交
12
      #
13 14
      #   PUT /projects/:id/services/gitlab-ci
      #
K
Kirilll Zaitsev 已提交
15 16 17 18 19 20
      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 已提交
21

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

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

K
Kirilll Zaitsev 已提交
33
      # Delete <service_slug> service for project
K
Kevin Houdebert 已提交
34 35
      #
      # Example Request:
K
Kirilll Zaitsev 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49
      #
      #   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
          
          if project_service.update_attributes(attrs.merge(active: false))
            true
          else
            not_found!
          end
K
Kevin Houdebert 已提交
50 51
        end
      end
52 53 54
    end
  end
end