summaryrefslogtreecommitdiff
path: root/app/controllers/admin/integrations_controller.rb
blob: 715aa882bda2e750a1ee74c65095dedc4620c64d (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
# frozen_string_literal: true

class Admin::IntegrationsController < Admin::ApplicationController
  include ServiceParams

  before_action :not_found, unless: :instance_level_integrations_enabled?
  before_action :service, only: [:edit, :update, :test]

  def edit
  end

  def update
    @service.attributes = service_params[:service]

    if @service.save(context: :manual_change)
      redirect_to edit_admin_application_settings_integration_path(@service), notice: success_message
    else
      render :edit
    end
  end

  def test
    if @service.can_test?
      render json: service_test_response, status: :ok
    else
      render json: {}, status: :not_found
    end
  end

  private

  def instance_level_integrations_enabled?
    Feature.enabled?(:instance_level_integrations)
  end

  def project
    # TODO: Change to something more meaningful
    Project.first
  end

  def service
    @service ||= project.find_or_initialize_service(params[:id])
  end

  def success_message
    message = @service.active? ? _('activated') : _('settings saved, but not activated')

    _('%{service_title} %{message}.') % { service_title: @service.title, message: message }
  end

  def service_test_response
    unless @service.update(service_params[:service])
      return { error: true, message: _('Validations failed.'), service_response: @service.errors.full_messages.join(','), test_failed: false }
    end

    data = @service.test_data(project, current_user)
    outcome = @service.test(data)

    unless outcome[:success]
      return { error: true, message: _('Test failed.'), service_response: outcome[:result].to_s, test_failed: true }
    end

    {}
  rescue Gitlab::HTTP::BlockedUrlError => e
    { error: true, message: _('Test failed.'), service_response: e.message, test_failed: true }
  end
end