Commit 9c72c83a authored by Dmytro Zaporozhets's avatar Dmytro Zaporozhets

Merge branch 'add-BaseContainerService' into 'master'

Add BaseContainerService

See merge request gitlab-org/gitlab!30979
parents a958d529 a1eabcf9
# frozen_string_literal: true
# Base class, scoped by container (project or group)
class BaseContainerService
include BaseServiceUtility
attr_reader :container, :current_user, :params
def initialize(container:, current_user: nil, params: {})
@container, @current_user, @params = container, current_user, params.dup
end
end
# frozen_string_literal: true
# This is the original root class for service related classes,
# and due to historical reason takes a project as scope.
# Later separate base classes for different scopes will be created,
# and existing service will use these one by one.
# After all are migrated, we can remove this class.
#
# TODO: New services should consider inheriting from
# BaseContainerService, or create new base class:
# https://gitlab.com/gitlab-org/gitlab/-/issues/216672
class BaseService
include Gitlab::Allowable
include BaseServiceUtility
attr_accessor :project, :current_user, :params
......@@ -9,67 +18,5 @@ class BaseService
@project, @current_user, @params = project, user, params.dup
end
def notification_service
NotificationService.new
end
def event_service
EventCreateService.new
end
def todo_service
TodoService.new
end
def log_info(message)
Gitlab::AppLogger.info message
end
def log_error(message)
Gitlab::AppLogger.error message
end
def system_hook_service
SystemHooksService.new
end
delegate :repository, to: :project
# 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
# 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
# frozen_string_literal: true
require 'spec_helper'
describe BaseContainerService do
let(:project) { Project.new }
let(:user) { User.new }
describe '#initialize' do
it 'accepts container and current_user' do
subject = described_class.new(container: project, current_user: user)
expect(subject.container).to eq(project)
expect(subject.current_user).to eq(user)
end
it 'treats current_user as optional' do
subject = described_class.new(container: project)
expect(subject.current_user).to be_nil
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment