2018-09-25 23:45:43 -04:00
# frozen_string_literal: true
2013-06-23 12:47:22 -04:00
class Projects :: ServicesController < Projects :: ApplicationController
2021-05-04 02:10:20 -04:00
include Integrations :: Params
2020-06-02 17:08:00 -04:00
include InternalRedirect
2015-10-20 03:28:28 -04:00
2012-11-19 14:34:05 -05:00
# Authorize
2015-04-16 08:03:37 -04:00
before_action :authorize_admin_project!
2018-02-18 18:47:37 -05:00
before_action :ensure_service_enabled
2021-05-04 02:10:20 -04:00
before_action :integration
2021-07-28 14:10:23 -04:00
before_action :default_integration , only : [ :edit , :update ]
2019-12-17 10:08:15 -05:00
before_action :web_hook_logs , only : [ :edit , :update ]
2021-06-18 17:10:06 -04:00
before_action :set_deprecation_notice_for_prometheus_integration , only : [ :edit , :update ]
before_action :redirect_deprecated_prometheus_integration , only : [ :update ]
2022-01-19 13:14:01 -05:00
before_action do
push_frontend_feature_flag ( :vue_integration_form , current_user , default_enabled : :yaml )
end
2012-11-19 14:34:05 -05:00
respond_to :html
2013-06-19 15:44:57 -04:00
layout " project_settings "
2020-10-08 14:08:32 -04:00
feature_category :integrations
2021-10-27 14:13:16 -04:00
urgency :low , [ :test ]
2020-10-08 14:08:32 -04:00
2012-11-19 14:34:05 -05:00
def edit
end
def update
2021-07-28 14:10:23 -04:00
attributes = integration_params [ :integration ]
2017-08-22 12:05:02 -04:00
2021-07-28 14:10:23 -04:00
if use_inherited_settings? ( attributes )
2021-08-25 20:09:31 -04:00
integration . inherit_from_id = default_integration . id
2021-07-28 14:10:23 -04:00
2021-08-25 20:09:31 -04:00
if saved = integration . save ( context : :manual_change )
BulkUpdateIntegrationService . new ( default_integration , [ integration ] ) . execute
2021-07-28 14:10:23 -04:00
end
else
attributes [ :inherit_from_id ] = nil
2021-08-25 20:09:31 -04:00
integration . attributes = attributes
saved = integration . save ( context : :manual_change )
2021-07-28 14:10:23 -04:00
end
2019-09-09 16:22:00 -04:00
respond_to do | format |
format . html do
if saved
2021-05-04 02:10:20 -04:00
redirect_to redirect_path , notice : success_message
2019-09-09 16:22:00 -04:00
else
render 'edit'
end
end
format . json do
status = saved ? :ok : :unprocessable_entity
render json : serialize_as_json , status : status
end
2012-11-19 14:34:05 -05:00
end
end
def test
2021-07-08 14:09:32 -04:00
if integration . testable?
2018-02-23 22:18:14 -05:00
render json : service_test_response , status : :ok
else
render json : { } , status : :not_found
end
end
private
2016-11-14 09:10:35 -05:00
2021-05-04 02:10:20 -04:00
def redirect_path
2021-12-30 13:13:34 -05:00
safe_redirect_path ( params [ :redirect_to ] ) . presence || edit_project_integration_path ( project , integration )
2021-05-04 02:10:20 -04:00
end
2018-02-23 22:18:14 -05:00
def service_test_response
2021-08-25 20:09:31 -04:00
unless integration . update ( integration_params [ :integration ] )
return { error : true , message : _ ( 'Validations failed.' ) , service_response : integration . errors . full_messages . join ( ',' ) , test_failed : false }
2020-03-13 05:09:23 -04:00
end
2021-08-25 20:09:31 -04:00
result = :: Integrations :: Test :: ProjectService . new ( integration , current_user , params [ :event ] ) . execute
2020-03-13 05:09:23 -04:00
2020-06-09 08:08:55 -04:00
unless result [ :success ]
2020-09-15 14:09:43 -04:00
return { error : true , message : s_ ( 'Integrations|Connection failed. Please check your settings.' ) , service_response : result [ :message ] . to_s , test_failed : true }
2015-01-23 23:50:17 -05:00
end
2020-03-13 05:09:23 -04:00
2020-11-11 22:09:17 -05:00
result [ :data ] . presence || { }
2021-03-17 11:09:03 -04:00
rescue * Gitlab :: HTTP :: HTTP_ERRORS = > e
2020-09-15 14:09:43 -04:00
{ error : true , message : s_ ( 'Integrations|Connection failed. Please check your settings.' ) , service_response : e . message , test_failed : true }
2012-11-19 14:34:05 -05:00
end
2013-05-22 09:58:44 -04:00
2017-05-22 06:07:12 -04:00
def success_message
2021-05-04 02:10:20 -04:00
if integration . active?
s_ ( 'Integrations|%{integration} settings saved and active.' ) % { integration : integration . title }
2020-09-15 14:09:43 -04:00
else
2021-05-04 02:10:20 -04:00
s_ ( 'Integrations|%{integration} settings saved, but not active.' ) % { integration : integration . title }
2020-09-15 14:09:43 -04:00
end
2017-05-22 06:07:12 -04:00
end
2021-05-04 02:10:20 -04:00
def integration
2021-08-25 20:09:31 -04:00
@integration || = project . find_or_initialize_integration ( params [ :id ] )
2013-05-22 09:58:44 -04:00
end
2021-05-04 02:10:20 -04:00
alias_method :service , :integration
2018-02-18 18:47:37 -05:00
2021-07-28 14:10:23 -04:00
def default_integration
@default_integration || = Integration . default_integration ( integration . type , project )
end
2019-12-17 10:08:15 -05:00
def web_hook_logs
2021-05-27 17:10:59 -04:00
return unless integration . service_hook . present?
2019-12-17 10:08:15 -05:00
2021-05-27 17:10:59 -04:00
@web_hook_logs || = integration . service_hook . web_hook_logs . recent . page ( params [ :page ] )
2019-12-17 10:08:15 -05:00
end
2018-02-18 18:47:37 -05:00
def ensure_service_enabled
render_404 unless service
end
2019-09-09 16:22:00 -04:00
def serialize_as_json
2021-05-04 02:10:20 -04:00
integration
2021-05-27 17:10:59 -04:00
. as_json ( only : integration . json_fields )
. merge ( errors : integration . errors . as_json )
2019-09-09 16:22:00 -04:00
end
2020-02-10 13:09:00 -05:00
2021-06-18 17:10:06 -04:00
def redirect_deprecated_prometheus_integration
2021-12-30 13:13:34 -05:00
redirect_to edit_project_integration_path ( project , integration ) if integration . is_a? ( :: Integrations :: Prometheus ) && Feature . enabled? ( :settings_operations_prometheus_service , project )
2020-02-10 13:09:00 -05:00
end
2021-06-18 17:10:06 -04:00
def set_deprecation_notice_for_prometheus_integration
2021-06-18 11:10:16 -04:00
return if ! integration . is_a? ( :: Integrations :: Prometheus ) || ! Feature . enabled? ( :settings_operations_prometheus_service , project )
2020-02-10 13:09:00 -05:00
operations_link_start = " <a href= \" #{ project_settings_operations_path ( project ) } \" > "
2021-07-13 14:08:57 -04:00
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> " }
2020-02-10 13:09:00 -05:00
flash . now [ :alert ] = message . html_safe
end
2021-07-28 14:10:23 -04:00
def use_inherited_settings? ( attributes )
default_integration && attributes [ :inherit_from_id ] == default_integration . id . to_s
end
2012-11-19 14:34:05 -05:00
end