services.rb 2.2 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 68 69
      post ':id/services/:service_slug/trigger' do
        project = Project.find_with_namespace(params[:id]) || Project.find_by(id: params[:id])

Z
Z.J. van de Weg 已提交
70
        service = service_by_slug(project, params[:service_slug])
71

72
        result = service.try(:active?) && service.try(:trigger, params)
73 74

        if result
75 76
          status result[:status] || 200
          present result
77 78 79 80 81
        else
          not_found!('Service')
        end
      end
    end
82 83
  end
end