summaryrefslogtreecommitdiff
path: root/app/services/concerns/base_service_utility.rb
blob: 70b223a0289be56b6fcf43344b3635ebee79023d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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