summaryrefslogtreecommitdiff
path: root/lib/api/services.rb
blob: bc427705777fc38ca3856ddfe99dbec23f2736a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
module API
  # Projects API
  class Services < Grape::API
    resource :projects do
      before { authenticate! }
      before { authorize_admin_project }

      # Set <service_slug> service for project
      #
      # Example Request:
      #
      #   PUT /projects/:id/services/gitlab-ci
      #
      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

          required_attributes! validators.map(&:attributes).flatten.uniq
          attrs = attributes_for_keys service_attributes

          if project_service.update_attributes(attrs.merge(active: true))
            true
          else
            not_found!
          end
        end
      end

      # Delete <service_slug> service for project
      #
      # Example Request:
      #
      #   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

          if project_service.update_attributes(attrs.merge(active: false))
            true
          else
            not_found!
          end
        end
      end

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

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

        # This is not accurate, but done to prevent leakage of the project names
        not_found!('Service') unless project

        service = project_service(project)

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

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