Read about our upcoming Code of Conduct on this issue

base_service.rb 1.83 KB
Newer Older
1
2
# frozen_string_literal: true

3
class BaseService
4
  include Gitlab::Allowable
5

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
6
7
  attr_accessor :project, :current_user, :params

8
  def initialize(project, user = nil, params = {})
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
9
10
    @project, @current_user, @params = project, user, params.dup
  end
randx's avatar
randx committed
11

12
13
14
15
16
17
18
19
  def notification_service
    NotificationService.new
  end

  def event_service
    EventCreateService.new
  end

20
21
  def todo_service
    TodoService.new
22
23
  end

24
  def log_info(message)
25
26
    Gitlab::AppLogger.info message
  end
27

28
29
30
31
  def log_error(message)
    Gitlab::AppLogger.error message
  end

32
33
34
  def system_hook_service
    SystemHooksService.new
  end
35

Douwe Maan's avatar
Douwe Maan committed
36
  delegate :repository, to: :project
37

38
39
40
41
  # 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

Douwe Maan's avatar
Douwe Maan committed
42
    level_name = Gitlab::VisibilityLevel.level_name(denied_visibility_level).downcase
43

Douwe Maan's avatar
Douwe Maan committed
44
    model.errors.add(:visibility_level, "#{level_name} has been restricted by your GitLab administrator")
45
46
  end

47
48
49
50
  def visibility_level
    params[:visibility].is_a?(String) ? Gitlab::VisibilityLevel.level_value(params[:visibility]) : params[:visibility_level]
  end

51
52
  private

53
54
55
56
57
58
  # 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: {})
59
    result = {
60
61
      message: message,
      status: :error
62
    }.reverse_merge(pass_back)
63
64
65

    result[:http_status] = http_status if http_status
    result
66
67
  end

68
69
70
  # Return a Hash with a `success` status
  #
  # pass_back - Additional attributes to be included in the resulting Hash
Marc Siegfriedt's avatar
Marc Siegfriedt committed
71
72
73
  def success(pass_back = {})
    pass_back[:status] = :success
    pass_back
74
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
75
end