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

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

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

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

Kirilll Zaitsev's avatar
Kirilll Zaitsev committed
32
      # Delete <service_slug> service for project
Kevin Houdebert's avatar
Kevin Houdebert committed
33
34
      #
      # Example Request:
Kirilll Zaitsev's avatar
Kirilll Zaitsev committed
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
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
43

Kirilll Zaitsev's avatar
Kirilll Zaitsev committed
44
45
46
47
48
          if project_service.update_attributes(attrs.merge(active: false))
            true
          else
            not_found!
          end
Kevin Houdebert's avatar
Kevin Houdebert committed
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
    end
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84

    resource :projects do
      post ':id/services/:service_slug/trigger' do
        project = Project.find_with_namespace(params[:id]) || Project.find_by(id: params[:id])

        underscored_service = params[:service_slug].underscore

        not_found!('Service') unless Service.available_services_names.include?(underscored_service)
        service_method = "#{underscored_service}_service"

        service = project.public_send(service_method)

        result = if service.try(:active?) && service.respond_to?(:trigger)
          service.trigger(params)
        end

        if result
          present result, status: result[:status] || 200
        else
          not_found!('Service')
        end
      end
    end
85
86
  end
end