2019-01-06 07:59:10 -05:00
# frozen_string_literal: true
module Projects
module Operations
class UpdateService < BaseService
def execute
Projects :: UpdateService
. new ( project , current_user , project_update_params )
. execute
end
private
def project_update_params
2019-10-03 17:07:29 -04:00
error_tracking_params
2020-03-25 05:08:11 -04:00
. merge ( alerting_setting_params )
2019-10-03 17:07:29 -04:00
. merge ( metrics_setting_params )
. merge ( grafana_integration_params )
2020-02-12 19:08:46 -05:00
. merge ( prometheus_integration_params )
2020-02-15 16:08:49 -05:00
. merge ( incident_management_setting_params )
2020-10-06 08:08:38 -04:00
. merge ( tracing_setting_params )
2019-04-26 13:23:26 -04:00
end
2020-03-25 05:08:11 -04:00
def alerting_setting_params
return { } unless can? ( current_user , :read_prometheus_alerts , project )
attr = params [ :alerting_setting_attributes ]
return { } unless attr
regenerate_token = attr . delete ( :regenerate_token )
if regenerate_token
attr [ :token ] = nil
else
attr = attr . except ( :token )
end
{ alerting_setting_attributes : attr }
end
2019-04-26 13:23:26 -04:00
def metrics_setting_params
attribs = params [ :metrics_setting_attributes ]
return { } unless attribs
2020-06-10 08:08:58 -04:00
attribs [ :external_dashboard_url ] = attribs [ :external_dashboard_url ] . presence
2019-04-26 13:23:26 -04:00
2020-06-10 08:08:58 -04:00
{ metrics_setting_attributes : attribs }
2019-03-01 09:51:54 -05:00
end
def error_tracking_params
settings = params [ :error_tracking_setting_attributes ]
return { } if settings . blank?
2020-02-05 13:09:06 -05:00
if error_tracking_params_partial_updates? ( settings )
error_tracking_params_for_partial_update ( settings )
else
error_tracking_params_for_update ( settings )
end
end
def error_tracking_params_partial_updates? ( settings )
# Help from @splattael :bow:
# Make sure we're converting to symbols because
# * ActionController::Parameters#keys returns a list of strings
# * in specs we're using hashes with symbols as keys
settings . keys . map ( & :to_sym ) == % i [ enabled ]
end
def error_tracking_params_for_partial_update ( settings )
{ error_tracking_setting_attributes : settings }
end
def error_tracking_params_for_update ( settings )
2019-12-18 13:08:04 -05:00
api_url = :: ErrorTracking :: ProjectErrorTrackingSetting . build_api_url_from (
2019-03-01 09:51:54 -05:00
api_host : settings [ :api_host ] ,
project_slug : settings . dig ( :project , :slug ) ,
organization_slug : settings . dig ( :project , :organization_slug )
)
2019-10-07 18:07:18 -04:00
params = {
2019-03-01 09:51:54 -05:00
error_tracking_setting_attributes : {
api_url : api_url ,
enabled : settings [ :enabled ] ,
project_name : settings . dig ( :project , :name ) ,
organization_name : settings . dig ( :project , :organization_name )
}
}
2019-10-07 18:07:18 -04:00
params [ :error_tracking_setting_attributes ] [ :token ] = settings [ :token ] unless / \ A \ *+ \ z / . match? ( settings [ :token ] ) # Don't update token if we receive masked value
params
2019-01-06 07:59:10 -05:00
end
2019-10-03 17:07:29 -04:00
def grafana_integration_params
return { } unless attrs = params [ :grafana_integration_attributes ]
destroy = attrs [ :grafana_url ] . blank? && attrs [ :token ] . blank?
{ grafana_integration_attributes : attrs . merge ( _destroy : destroy ) }
end
2020-02-12 19:08:46 -05:00
def prometheus_integration_params
return { } unless attrs = params [ :prometheus_integration_attributes ]
service = project . find_or_initialize_service ( :: PrometheusService . to_param )
service . assign_attributes ( attrs )
{ prometheus_service_attributes : service . attributes . except ( * %w( id project_id created_at updated_at ) ) }
end
2020-02-15 16:08:49 -05:00
def incident_management_setting_params
2020-07-14 20:09:23 -04:00
attrs = params [ :incident_management_setting_attributes ]
return { } unless attrs
regenerate_token = attrs . delete ( :regenerate_token )
if regenerate_token
attrs [ :pagerduty_token ] = nil
else
attrs = attrs . except ( :pagerduty_token )
end
{ incident_management_setting_attributes : attrs }
2020-02-15 16:08:49 -05:00
end
2020-10-06 08:08:38 -04:00
def tracing_setting_params
attr = params [ :tracing_setting_attributes ]
return { } unless attr
destroy = attr [ :external_url ] . blank?
{ tracing_setting_attributes : attr . merge ( _destroy : destroy ) }
end
2019-01-06 07:59:10 -05:00
end
end
end
2019-09-13 09:26:31 -04:00
Projects :: Operations :: UpdateService . prepend_if_ee ( '::EE::Projects::Operations::UpdateService' )