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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

113 lines
3 KiB
Ruby
Raw Normal View History

2020-03-13 15:44:24 +05:30
# frozen_string_literal: true
module Snippets
2021-09-04 01:27:46 +05:30
class BaseService < ::BaseProjectService
2021-01-03 14:25:43 +05:30
UPDATE_COMMIT_MSG = 'Update snippet'
INITIAL_COMMIT_MSG = 'Initial commit'
2020-05-24 23:13:21 +05:30
CreateRepositoryError = Class.new(StandardError)
2020-07-28 23:09:34 +05:30
attr_reader :uploaded_assets, :snippet_actions
2020-05-24 23:13:21 +05:30
2021-09-04 01:27:46 +05:30
def initialize(project: nil, current_user: nil, params: {})
2020-05-24 23:13:21 +05:30
super
2020-06-23 00:09:42 +05:30
@uploaded_assets = Array(@params.delete(:files).presence)
2020-07-28 23:09:34 +05:30
input_actions = Array(@params.delete(:snippet_actions).presence)
@snippet_actions = SnippetInputActionCollection.new(input_actions, allowed_actions: restricted_files_actions)
2020-05-24 23:13:21 +05:30
end
2020-03-13 15:44:24 +05:30
private
2021-09-04 01:27:46 +05:30
def visibility_allowed?(visibility_level)
2020-05-24 23:13:21 +05:30
Gitlab::VisibilityLevel.allowed_for?(current_user, visibility_level)
end
2020-06-23 00:09:42 +05:30
def forbidden_visibility_error(snippet)
2020-05-24 23:13:21 +05:30
deny_visibility_level(snippet)
snippet_error_response(snippet, 403)
end
2020-06-23 00:09:42 +05:30
def valid_params?
2020-07-28 23:09:34 +05:30
return true if snippet_actions.empty?
2020-06-23 00:09:42 +05:30
2020-07-28 23:09:34 +05:30
(params.keys & [:content, :file_name]).none? && snippet_actions.valid?
2020-06-23 00:09:42 +05:30
end
def invalid_params_error(snippet)
2020-07-28 23:09:34 +05:30
if snippet_actions.valid?
2020-06-23 00:09:42 +05:30
[: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-28 23:09:34 +05:30
snippet.errors.add(:snippet_actions, 'have invalid data')
2020-06-23 00:09:42 +05:30
end
2020-11-24 15:15:51 +05:30
snippet_error_response(snippet, 422)
2020-06-23 00:09:42 +05:30
end
2020-03-13 15:44:24 +05:30
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-24 23:13:21 +05:30
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-06-23 00:09:42 +05:30
2022-10-11 01:57:18 +05:30
def file_paths_to_commit
paths = []
snippet_actions.to_commit_actions.each do |action|
paths << { path: action[:file_path] }
end
paths
end
2020-06-23 00:09:42 +05:30
def files_to_commit(snippet)
2020-07-28 23:09:34 +05:30
snippet_actions.to_commit_actions.presence || build_actions_from_params(snippet)
2020-06-23 00:09:42 +05:30
end
def build_actions_from_params(snippet)
raise NotImplementedError
end
2020-07-28 23:09:34 +05:30
def restricted_files_actions
nil
end
2021-01-03 14:25:43 +05:30
def commit_attrs(snippet, msg)
{
branch_name: snippet.default_branch,
message: msg
}
end
def delete_repository(snippet)
snippet.repository.remove
snippet.snippet_repository&.delete
# Purge any existing value for repository_exists?
snippet.repository.expire_exists_cache
end
2020-03-13 15:44:24 +05:30
end
end