2019-02-15 15:39:39 +05:30
# 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-12-21 20:55:43 +05:30
error_tracking_params
2020-04-22 19:07:51 +05:30
. merge ( alerting_setting_params )
2019-12-21 20:55:43 +05:30
. merge ( metrics_setting_params )
. merge ( grafana_integration_params )
2020-03-13 15:44:24 +05:30
. merge ( prometheus_integration_params )
. merge ( incident_management_setting_params )
2019-07-31 22:56:46 +05:30
end
2020-04-22 19:07:51 +05:30
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-07-31 22:56:46 +05:30
def metrics_setting_params
attribs = params [ :metrics_setting_attributes ]
return { } unless attribs
2020-06-23 00:09:42 +05:30
attribs [ :external_dashboard_url ] = attribs [ :external_dashboard_url ] . presence
2019-07-31 22:56:46 +05:30
2020-06-23 00:09:42 +05:30
{ metrics_setting_attributes : attribs }
2019-07-07 11:18:12 +05:30
end
def error_tracking_params
settings = params [ :error_tracking_setting_attributes ]
return { } if settings . blank?
2020-03-13 15:44:24 +05:30
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
2021-10-27 15:23:28 +05:30
update_keys = settings . keys . map ( & :to_sym )
2020-03-13 15:44:24 +05:30
2021-10-27 15:23:28 +05:30
# Integrated error tracking works without Sentry integration,
# so we don't need to update all those values from error_tracking_params_for_update method.
# Instead we turn it on/off with partial update together with "enabled" attribute.
# But since its optional, we exclude it from the condition below.
update_keys . delete ( :integrated )
update_keys == % i [ enabled ]
2020-03-13 15:44:24 +05:30
end
def error_tracking_params_for_partial_update ( settings )
{ error_tracking_setting_attributes : settings }
end
def error_tracking_params_for_update ( settings )
api_url = :: ErrorTracking :: ProjectErrorTrackingSetting . build_api_url_from (
2019-07-07 11:18:12 +05:30
api_host : settings [ :api_host ] ,
project_slug : settings . dig ( :project , :slug ) ,
organization_slug : settings . dig ( :project , :organization_slug )
)
2019-10-31 01:37:42 +05:30
params = {
2019-07-07 11:18:12 +05:30
error_tracking_setting_attributes : {
api_url : api_url ,
enabled : settings [ :enabled ] ,
project_name : settings . dig ( :project , :name ) ,
2022-07-01 11:34:44 +05:30
organization_name : settings . dig ( :project , :organization_name ) ,
sentry_project_id : settings . dig ( :project , :sentry_project_id )
2019-07-07 11:18:12 +05:30
}
}
2019-10-31 01:37:42 +05:30
params [ :error_tracking_setting_attributes ] [ :token ] = settings [ :token ] unless / \ A \ *+ \ z / . match? ( settings [ :token ] ) # Don't update token if we receive masked value
2021-11-11 11:23:49 +05:30
params [ :error_tracking_setting_attributes ] [ :integrated ] = settings [ :integrated ] unless settings [ :integrated ] . nil?
2019-10-31 01:37:42 +05:30
params
2019-02-15 15:39:39 +05:30
end
2019-12-21 20:55:43 +05:30
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-03-13 15:44:24 +05:30
def prometheus_integration_params
return { } unless attrs = params [ :prometheus_integration_attributes ]
2021-09-30 23:02:18 +05:30
integration = project . find_or_initialize_integration ( :: Integrations :: Prometheus . to_param )
integration . assign_attributes ( attrs )
2022-07-23 23:45:48 +05:30
attrs = integration . to_database_hash
2020-03-13 15:44:24 +05:30
2022-06-21 17:19:12 +05:30
{ prometheus_integration_attributes : attrs }
2020-03-13 15:44:24 +05:30
end
def incident_management_setting_params
2020-07-28 23:09:34 +05:30
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-03-13 15:44:24 +05:30
end
2019-02-15 15:39:39 +05:30
end
end
end
2019-12-04 20:38:33 +05:30
2021-06-08 01:23:25 +05:30
Projects :: Operations :: UpdateService . prepend_mod_with ( 'Projects::Operations::UpdateService' )