debian-mirror-gitlab/app/services/base_service.rb

76 lines
1.8 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
class BaseService
2017-08-17 22:00:37 +05:30
include Gitlab::Allowable
2015-04-26 12:48:37 +05:30
2014-09-02 18:07:02 +05:30
attr_accessor :project, :current_user, :params
2018-11-08 19:23:39 +05:30
def initialize(project, user = nil, params = {})
2014-09-02 18:07:02 +05:30
@project, @current_user, @params = project, user, params.dup
end
def notification_service
NotificationService.new
end
def event_service
EventCreateService.new
end
2016-04-02 18:10:28 +05:30
def todo_service
TodoService.new
end
2015-04-26 12:48:37 +05:30
def log_info(message)
2014-09-02 18:07:02 +05:30
Gitlab::AppLogger.info message
end
2017-08-17 22:00:37 +05:30
def log_error(message)
Gitlab::AppLogger.error message
end
2014-09-02 18:07:02 +05:30
def system_hook_service
SystemHooksService.new
end
2015-04-26 12:48:37 +05:30
2017-08-17 22:00:37 +05:30
delegate :repository, to: :project
2015-09-25 12:07:36 +05:30
2015-04-26 12:48:37 +05:30
# 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
2016-06-02 11:05:42 +05:30
level_name = Gitlab::VisibilityLevel.level_name(denied_visibility_level).downcase
2015-04-26 12:48:37 +05:30
2016-06-02 11:05:42 +05:30
model.errors.add(:visibility_level, "#{level_name} has been restricted by your GitLab administrator")
2015-04-26 12:48:37 +05:30
end
2019-10-12 21:52:04 +05:30
def visibility_level
params[:visibility].is_a?(String) ? Gitlab::VisibilityLevel.level_value(params[:visibility]) : params[:visibility_level]
end
2015-04-26 12:48:37 +05:30
private
2019-12-26 22:10:19 +05:30
# 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: {})
2015-04-26 12:48:37 +05:30
result = {
message: message,
status: :error
2019-12-26 22:10:19 +05:30
}.reverse_merge(pass_back)
2015-04-26 12:48:37 +05:30
result[:http_status] = http_status if http_status
result
end
2019-12-26 22:10:19 +05:30
# Return a Hash with a `success` status
#
# pass_back - Additional attributes to be included in the resulting Hash
2016-11-03 12:29:30 +05:30
def success(pass_back = {})
pass_back[:status] = :success
pass_back
2015-04-26 12:48:37 +05:30
end
2014-09-02 18:07:02 +05:30
end