2020-01-16 19:09:00 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Snippets
|
|
|
|
class BaseService < ::BaseService
|
2020-05-19 17:08:05 -04:00
|
|
|
include SpamCheckMethods
|
|
|
|
|
|
|
|
CreateRepositoryError = Class.new(StandardError)
|
|
|
|
|
2020-07-15 05:09:34 -04:00
|
|
|
attr_reader :uploaded_assets, :snippet_actions
|
2020-05-19 17:08:05 -04:00
|
|
|
|
|
|
|
def initialize(project, user = nil, params = {})
|
|
|
|
super
|
|
|
|
|
2020-05-22 05:08:09 -04:00
|
|
|
@uploaded_assets = Array(@params.delete(:files).presence)
|
2020-06-17 17:08:24 -04:00
|
|
|
|
2020-07-15 05:09:34 -04:00
|
|
|
input_actions = Array(@params.delete(:snippet_actions).presence)
|
|
|
|
@snippet_actions = SnippetInputActionCollection.new(input_actions, allowed_actions: restricted_files_actions)
|
2020-05-19 17:08:05 -04:00
|
|
|
|
|
|
|
filter_spam_check_params
|
|
|
|
end
|
|
|
|
|
2020-01-16 19:09:00 -05:00
|
|
|
private
|
|
|
|
|
2020-05-19 17:08:05 -04:00
|
|
|
def visibility_allowed?(snippet, visibility_level)
|
|
|
|
Gitlab::VisibilityLevel.allowed_for?(current_user, visibility_level)
|
|
|
|
end
|
|
|
|
|
2020-05-22 05:08:09 -04:00
|
|
|
def forbidden_visibility_error(snippet)
|
2020-05-19 17:08:05 -04:00
|
|
|
deny_visibility_level(snippet)
|
|
|
|
|
|
|
|
snippet_error_response(snippet, 403)
|
|
|
|
end
|
|
|
|
|
2020-05-22 05:08:09 -04:00
|
|
|
def valid_params?
|
2020-07-15 05:09:34 -04:00
|
|
|
return true if snippet_actions.empty?
|
2020-05-22 05:08:09 -04:00
|
|
|
|
2020-07-15 05:09:34 -04:00
|
|
|
(params.keys & [:content, :file_name]).none? && snippet_actions.valid?
|
2020-05-22 05:08:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def invalid_params_error(snippet)
|
2020-07-15 05:09:34 -04:00
|
|
|
if snippet_actions.valid?
|
2020-05-22 05:08:09 -04:00
|
|
|
[:content, :file_name].each do |key|
|
|
|
|
snippet.errors.add(key, 'and snippet files cannot be used together') if params.key?(key)
|
|
|
|
end
|
|
|
|
else
|
2020-07-15 05:09:34 -04:00
|
|
|
snippet.errors.add(:snippet_actions, 'have invalid data')
|
2020-05-22 05:08:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
snippet_error_response(snippet, 403)
|
|
|
|
end
|
|
|
|
|
2020-01-16 19:09:00 -05:00
|
|
|
def snippet_error_response(snippet, http_status)
|
|
|
|
ServiceResponse.error(
|
|
|
|
message: snippet.errors.full_messages.to_sentence,
|
|
|
|
http_status: http_status,
|
|
|
|
payload: { snippet: snippet }
|
|
|
|
)
|
|
|
|
end
|
2020-05-13 17:08:55 -04:00
|
|
|
|
|
|
|
def add_snippet_repository_error(snippet:, error:)
|
|
|
|
message = repository_error_message(error)
|
|
|
|
|
|
|
|
snippet.errors.add(:repository, message)
|
|
|
|
end
|
|
|
|
|
|
|
|
def repository_error_message(error)
|
|
|
|
message = self.is_a?(Snippets::CreateService) ? _("Error creating the snippet") : _("Error updating the snippet")
|
|
|
|
|
|
|
|
# We only want to include additional error detail in the message
|
|
|
|
# if the error is not a CommitError because we cannot guarantee the message
|
|
|
|
# will be user-friendly
|
|
|
|
message += " - #{error.message}" unless error.instance_of?(SnippetRepository::CommitError)
|
|
|
|
|
|
|
|
message
|
|
|
|
end
|
2020-05-22 05:08:09 -04:00
|
|
|
|
2020-05-26 14:08:20 -04:00
|
|
|
def files_to_commit(snippet)
|
2020-07-15 05:09:34 -04:00
|
|
|
snippet_actions.to_commit_actions.presence || build_actions_from_params(snippet)
|
2020-05-22 05:08:09 -04:00
|
|
|
end
|
|
|
|
|
2020-05-26 14:08:20 -04:00
|
|
|
def build_actions_from_params(snippet)
|
2020-05-22 05:08:09 -04:00
|
|
|
raise NotImplementedError
|
|
|
|
end
|
2020-06-17 17:08:24 -04:00
|
|
|
|
|
|
|
def restricted_files_actions
|
|
|
|
nil
|
|
|
|
end
|
2020-01-16 19:09:00 -05:00
|
|
|
end
|
|
|
|
end
|