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

Kirilll Zaitsev's avatar
Kirilll Zaitsev committed
7

8
    resource :projects do
Kirilll Zaitsev's avatar
Kirilll Zaitsev committed
9
      # Set <service_slug> service for project
10
11
      #
      # Example Request:
Kirilll Zaitsev's avatar
Kirilll Zaitsev committed
12
      #
13
14
      #   PUT /projects/:id/services/gitlab-ci
      #
Kirilll Zaitsev's avatar
Kirilll Zaitsev committed
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
Kevin Houdebert's avatar
Kevin Houdebert committed
21

Kirilll Zaitsev's avatar
Kirilll Zaitsev committed
22
          required_attributes! validators.map(&:attributes).flatten.uniq
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
23
          attrs = attributes_for_keys service_attributes
Kevin Houdebert's avatar
Kevin Houdebert committed
24

Kirilll Zaitsev's avatar
Kirilll Zaitsev committed
25
26
27
28
29
          if project_service.update_attributes(attrs.merge(active: true))
            true
          else
            not_found!
          end
Kevin Houdebert's avatar
Kevin Houdebert committed
30
31
32
        end
      end

Kirilll Zaitsev's avatar
Kirilll Zaitsev committed
33
      # Delete <service_slug> service for project
Kevin Houdebert's avatar
Kevin Houdebert committed
34
35
      #
      # Example Request:
Kirilll Zaitsev's avatar
Kirilll Zaitsev committed
36
37
38
39
40
41
42
43
      #
      #   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
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
44

Kirilll Zaitsev's avatar
Kirilll Zaitsev committed
45
46
47
48
49
          if project_service.update_attributes(attrs.merge(active: false))
            true
          else
            not_found!
          end
Kevin Houdebert's avatar
Kevin Houdebert committed
50
51
        end
      end
52
53
54
55
56
57
58
59

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