diff options
Diffstat (limited to 'app/services/concerns')
-rw-r--r-- | app/services/concerns/base_service_utility.rb | 72 | ||||
-rw-r--r-- | app/services/concerns/git/logger.rb | 10 | ||||
-rw-r--r-- | app/services/concerns/measurable.rb | 61 | ||||
-rw-r--r-- | app/services/concerns/spam_check_methods.rb | 4 |
4 files changed, 135 insertions, 12 deletions
diff --git a/app/services/concerns/base_service_utility.rb b/app/services/concerns/base_service_utility.rb new file mode 100644 index 00000000000..70b223a0289 --- /dev/null +++ b/app/services/concerns/base_service_utility.rb @@ -0,0 +1,72 @@ +# frozen_string_literal: true + +module BaseServiceUtility + extend ActiveSupport::Concern + include Gitlab::Allowable + + ### Convenience service methods + + def notification_service + NotificationService.new + end + + def event_service + EventCreateService.new + end + + def todo_service + TodoService.new + end + + def system_hook_service + SystemHooksService.new + end + + # Logging + + def log_info(message) + Gitlab::AppLogger.info message + end + + def log_error(message) + Gitlab::AppLogger.error message + end + + # Add an error to the specified model for restricted visibility levels + def deny_visibility_level(model, denied_visibility_level = nil) + denied_visibility_level ||= model.visibility_level + + level_name = Gitlab::VisibilityLevel.level_name(denied_visibility_level).downcase + + model.errors.add(:visibility_level, "#{level_name} has been restricted by your GitLab administrator") + end + + def visibility_level + params[:visibility].is_a?(String) ? Gitlab::VisibilityLevel.level_value(params[:visibility]) : params[:visibility_level] + end + + private + + # Return a Hash with an `error` status + # + # message - Error message to include in the Hash + # http_status - Optional HTTP status code override (default: nil) + # pass_back - Additional attributes to be included in the resulting Hash + def error(message, http_status = nil, pass_back: {}) + result = { + message: message, + status: :error + }.reverse_merge(pass_back) + + result[:http_status] = http_status if http_status + result + end + + # Return a Hash with a `success` status + # + # pass_back - Additional attributes to be included in the resulting Hash + def success(pass_back = {}) + pass_back[:status] = :success + pass_back + end +end diff --git a/app/services/concerns/git/logger.rb b/app/services/concerns/git/logger.rb deleted file mode 100644 index 7c036212e66..00000000000 --- a/app/services/concerns/git/logger.rb +++ /dev/null @@ -1,10 +0,0 @@ -# frozen_string_literal: true - -module Git - module Logger - def log_error(message, save_message_on_model: false) - Gitlab::GitLogger.error("#{self.class.name} error (#{merge_request.to_reference(full: true)}): #{message}") - merge_request.update(merge_error: message) if save_message_on_model - end - end -end diff --git a/app/services/concerns/measurable.rb b/app/services/concerns/measurable.rb new file mode 100644 index 00000000000..5a74f15506e --- /dev/null +++ b/app/services/concerns/measurable.rb @@ -0,0 +1,61 @@ +# frozen_string_literal: true + +# In order to measure and log execution of our service, we just need to 'prepend Measurable' module +# Example: +# ``` +# class DummyService +# prepend Measurable +# +# def execute +# # ... +# end +# end + +# DummyService.prepend(Measurable) +# ``` +# +# In case when we are prepending a module from the `EE` namespace with EE features +# we need to prepend Measurable after prepending `EE` module. +# This way Measurable will be at the bottom of the ancestor chain, +# in order to measure execution of `EE` features as well +# ``` +# class DummyService +# def execute +# # ... +# end +# end +# +# DummyService.prepend_if_ee('EE::DummyService') +# DummyService.prepend(Measurable) +# ``` +# +module Measurable + extend ::Gitlab::Utils::Override + + override :execute + def execute(*args) + measuring? ? ::Gitlab::Utils::Measuring.new(base_log_data).with_measuring { super(*args) } : super(*args) + end + + protected + + # You can set extra attributes for performance measurement log. + def extra_attributes_for_measurement + defined?(super) ? super : {} + end + + private + + def measuring? + Feature.enabled?("gitlab_service_measuring_#{service_class}") + end + + # These attributes are always present in log. + def base_log_data + extra_attributes_for_measurement.merge({ class: self.class.name }) + end + + def service_class + self.class.name.underscore.tr('/', '_') + end +end diff --git a/app/services/concerns/spam_check_methods.rb b/app/services/concerns/spam_check_methods.rb index 695bdf92b49..53e9e001463 100644 --- a/app/services/concerns/spam_check_methods.rb +++ b/app/services/concerns/spam_check_methods.rb @@ -23,14 +23,14 @@ module SpamCheckMethods # attribute values. # rubocop:disable Gitlab/ModuleWithInstanceVariables def spam_check(spammable, user) - Spam::SpamCheckService.new( + Spam::SpamActionService.new( spammable: spammable, request: @request ).execute( api: @api, recaptcha_verified: @recaptcha_verified, spam_log_id: @spam_log_id, - user_id: user.id) + user: user) end # rubocop:enable Gitlab/ModuleWithInstanceVariables end |