summaryrefslogtreecommitdiff
path: root/lib/api/services.rb
blob: a3b5d2cc4b7b0c34ad4473614781e210aa7ee605 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# frozen_string_literal: true
module API
  class Services < Grape::API
    services = Helpers::ServicesHelpers.services
    service_classes = Helpers::ServicesHelpers.service_classes

    if Rails.env.development?
      services['mock-ci'] = [
        {
          required: true,
          name: :mock_service_url,
          type: String,
          desc: 'URL to the mock service'
        }
      ]
      services['mock-deployment'] = []
      services['mock-monitoring'] = []

      service_classes += Helpers::ServicesHelpers.development_service_classes
    end

    SERVICES = services.freeze
    SERVICE_CLASSES = service_classes.freeze

    SERVICE_CLASSES.each do |service|
      event_names = service.try(:event_names) || next
      event_names.each do |event_name|
        SERVICES[service.to_param.tr("_", "-")] << {
          required: false,
          name: event_name.to_sym,
          type: String,
          desc: service.event_description(event_name)
        }
      end
    end

    TRIGGER_SERVICES = {
      'mattermost-slash-commands' => [
        {
          name: :token,
          type: String,
          desc: 'The Mattermost token'
        }
      ],
      'slack-slash-commands' => [
        {
          name: :token,
          type: String,
          desc: 'The Slack token'
        }
      ]
    }.freeze

    params do
      requires :id, type: String, desc: 'The ID of a project'
    end
    resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
      before { authenticate! }
      before { authorize_admin_project }

      helpers do
        def service_attributes(service)
          service.fields.inject([]) do |arr, hash|
            arr << hash[:name].to_sym
          end
        end
      end

      desc 'Get all active project services' do
        success Entities::ProjectServiceBasic
      end
      get ":id/services" do
        services = user_project.services.active

        present services, with: Entities::ProjectServiceBasic
      end

      SERVICES.each do |service_slug, settings|
        desc "Set #{service_slug} service for project"
        params do
          settings.each do |setting|
            if setting[:required]
              requires setting[:name], type: setting[:type], desc: setting[:desc]
            else
              optional setting[:name], type: setting[:type], desc: setting[:desc]
            end
          end
        end
        put ":id/services/#{service_slug}" do
          service = user_project.find_or_initialize_service(service_slug.underscore)
          service_params = declared_params(include_missing: false).merge(active: true)

          if service.update(service_params)
            present service, with: Entities::ProjectService
          else
            render_api_error!('400 Bad Request', 400)
          end
        end
      end

      desc "Delete a service for project"
      params do
        requires :service_slug, type: String, values: SERVICES.keys, desc: 'The name of the service'
      end
      delete ":id/services/:service_slug" do
        service = user_project.find_or_initialize_service(params[:service_slug].underscore)

        destroy_conditionally!(service) do
          attrs = service_attributes(service).inject({}) do |hash, key|
            hash.merge!(key => nil)
          end

          unless service.update(attrs.merge(active: false))
            render_api_error!('400 Bad Request', 400)
          end
        end
      end

      desc 'Get the service settings for project' do
        success Entities::ProjectService
      end
      params do
        requires :service_slug, type: String, values: SERVICES.keys, desc: 'The name of the service'
      end
      get ":id/services/:service_slug" do
        service = user_project.find_or_initialize_service(params[:service_slug].underscore)
        present service, with: Entities::ProjectService
      end
    end

    TRIGGER_SERVICES.each do |service_slug, settings|
      helpers do
        # rubocop: disable CodeReuse/ActiveRecord
        def slash_command_service(project, service_slug, params)
          project.services.active.where(template: false).find do |service|
            service.try(:token) == params[:token] && service.to_param == service_slug.underscore
          end
        end
        # rubocop: enable CodeReuse/ActiveRecord
      end

      params do
        requires :id, type: String, desc: 'The ID of a project'
      end
      resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
        desc "Trigger a slash command for #{service_slug}" do
          detail 'Added in GitLab 8.13'
        end
        params do
          settings.each do |setting|
            requires setting[:name], type: setting[:type], desc: setting[:desc]
          end
        end
        post ":id/services/#{service_slug.underscore}/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 = slash_command_service(project, service_slug, params)
          result = service.try(:trigger, params)

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

API::Services.prepend_if_ee('EE::API::Services')