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