services.rb 2.3 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 }

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
    end
62 63

    resource :projects do
64 65 66
      desc 'Trigger a slash command' do
        detail 'Added in GitLab 8.13'
      end
67
      post ':id/services/:service_slug/trigger' do
68
        project = find_project(params[:id])
69

70 71
        # This is not accurate, but done to prevent leakage of the project names
        not_found!('Service') unless project
Z
Z.J. van de Weg 已提交
72 73

        service = project_service(project)
74

75
        result = service.try(:active?) && service.try(:trigger, params)
76 77

        if result
78 79
          status result[:status] || 200
          present result
80
        else
81
          not_found!('Service')
82 83 84
        end
      end
    end
85 86
  end
end