services.rb 2.4 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 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