/app/controllers/projects/services_controller.rb

https://gitlab.com/AnhHoaHuynh93/gitlab-foss · Ruby · 136 lines · 104 code · 30 blank · 2 comment · 13 complexity · 4d61154f1540cc379a4714de6db27f2a MD5 · raw file

  1. # frozen_string_literal: true
  2. class Projects::ServicesController < Projects::ApplicationController
  3. include Integrations::Params
  4. include InternalRedirect
  5. # Authorize
  6. before_action :authorize_admin_project!
  7. before_action :ensure_service_enabled
  8. before_action :integration
  9. before_action :default_integration, only: [:edit, :update]
  10. before_action :web_hook_logs, only: [:edit, :update]
  11. before_action :set_deprecation_notice_for_prometheus_integration, only: [:edit, :update]
  12. before_action :redirect_deprecated_prometheus_integration, only: [:update]
  13. respond_to :html
  14. layout "project_settings"
  15. feature_category :integrations
  16. urgency :low, [:test]
  17. def edit
  18. end
  19. def update
  20. attributes = integration_params[:integration]
  21. if use_inherited_settings?(attributes)
  22. integration.inherit_from_id = default_integration.id
  23. if saved = integration.save(context: :manual_change)
  24. BulkUpdateIntegrationService.new(default_integration, [integration]).execute
  25. end
  26. else
  27. attributes[:inherit_from_id] = nil
  28. integration.attributes = attributes
  29. saved = integration.save(context: :manual_change)
  30. end
  31. respond_to do |format|
  32. format.html do
  33. if saved
  34. redirect_to redirect_path, notice: success_message
  35. else
  36. render 'edit'
  37. end
  38. end
  39. format.json do
  40. status = saved ? :ok : :unprocessable_entity
  41. render json: serialize_as_json, status: status
  42. end
  43. end
  44. end
  45. def test
  46. if integration.testable?
  47. render json: service_test_response, status: :ok
  48. else
  49. render json: {}, status: :not_found
  50. end
  51. end
  52. private
  53. def redirect_path
  54. safe_redirect_path(params[:redirect_to]).presence || edit_project_integration_path(project, integration)
  55. end
  56. def service_test_response
  57. unless integration.update(integration_params[:integration])
  58. return { error: true, message: _('Validations failed.'), service_response: integration.errors.full_messages.join(','), test_failed: false }
  59. end
  60. result = ::Integrations::Test::ProjectService.new(integration, current_user, params[:event]).execute
  61. unless result[:success]
  62. return { error: true, message: s_('Integrations|Connection failed. Please check your settings.'), service_response: result[:message].to_s, test_failed: true }
  63. end
  64. result[:data].presence || {}
  65. rescue *Gitlab::HTTP::HTTP_ERRORS => e
  66. { error: true, message: s_('Integrations|Connection failed. Please check your settings.'), service_response: e.message, test_failed: true }
  67. end
  68. def success_message
  69. if integration.active?
  70. s_('Integrations|%{integration} settings saved and active.') % { integration: integration.title }
  71. else
  72. s_('Integrations|%{integration} settings saved, but not active.') % { integration: integration.title }
  73. end
  74. end
  75. def integration
  76. @integration ||= project.find_or_initialize_integration(params[:id])
  77. end
  78. alias_method :service, :integration
  79. def default_integration
  80. @default_integration ||= Integration.default_integration(integration.type, project)
  81. end
  82. def web_hook_logs
  83. return unless integration.service_hook.present?
  84. @web_hook_logs ||= integration.service_hook.web_hook_logs.recent.page(params[:page])
  85. end
  86. def ensure_service_enabled
  87. render_404 unless service
  88. end
  89. def serialize_as_json
  90. integration
  91. .as_json(only: integration.json_fields)
  92. .merge(errors: integration.errors.as_json)
  93. end
  94. def redirect_deprecated_prometheus_integration
  95. redirect_to edit_project_integration_path(project, integration) if integration.is_a?(::Integrations::Prometheus) && Feature.enabled?(:settings_operations_prometheus_service, project)
  96. end
  97. def set_deprecation_notice_for_prometheus_integration
  98. return if !integration.is_a?(::Integrations::Prometheus) || !Feature.enabled?(:settings_operations_prometheus_service, project)
  99. operations_link_start = "<a href=\"#{project_settings_operations_path(project)}\">"
  100. message = s_('PrometheusService|You can now manage your Prometheus settings on the %{operations_link_start}Operations%{operations_link_end} page. Fields on this page have been deprecated.') % { operations_link_start: operations_link_start, operations_link_end: "</a>" }
  101. flash.now[:alert] = message.html_safe
  102. end
  103. def use_inherited_settings?(attributes)
  104. default_integration && attributes[:inherit_from_id] == default_integration.id.to_s
  105. end
  106. end