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

156 lines
5.1 KiB
Ruby
Raw Normal View History

2020-03-13 15:44:24 +05:30
# frozen_string_literal: true
module Snippets
class UpdateService < Snippets::BaseService
2020-05-24 23:13:21 +05:30
COMMITTABLE_ATTRIBUTES = %w(file_name content).freeze
2020-03-13 15:44:24 +05:30
2020-04-08 14:13:33 +05:30
UpdateError = Class.new(StandardError)
2021-09-30 23:02:18 +05:30
# NOTE: For Snippets::UpdateService, we default the spam_params to nil, because spam_checking is not
# necessary in many cases, and we don't want every caller to have to explicitly pass it as nil
# to disable spam checking.
def initialize(project:, current_user: nil, params: {}, spam_params: nil)
super(project: project, current_user: current_user, params: params)
@spam_params = spam_params
end
2021-03-11 19:13:27 +05:30
2021-09-30 23:02:18 +05:30
def execute(snippet)
2020-06-23 00:09:42 +05:30
return invalid_params_error(snippet) unless valid_params?
2021-09-04 01:27:46 +05:30
if visibility_changed?(snippet) && !visibility_allowed?(visibility_level)
2020-06-23 00:09:42 +05:30
return forbidden_visibility_error(snippet)
2020-03-13 15:44:24 +05:30
end
2020-06-23 00:09:42 +05:30
update_snippet_attributes(snippet)
2021-03-11 19:13:27 +05:30
2022-10-11 01:57:18 +05:30
files = snippet.all_files.map { |f| { path: f } } + file_paths_to_commit
2021-09-30 23:02:18 +05:30
Spam::SpamActionService.new(
spammable: snippet,
spam_params: spam_params,
user: current_user,
2022-10-11 01:57:18 +05:30
action: :update,
extra_features: { files: files }
2021-09-30 23:02:18 +05:30
).execute
2020-03-13 15:44:24 +05:30
2020-04-08 14:13:33 +05:30
if save_and_commit(snippet)
2020-03-13 15:44:24 +05:30
Gitlab::UsageDataCounters::SnippetCounter.count(:update)
2021-03-11 19:13:27 +05:30
ServiceResponse.success(payload: { snippet: snippet })
2020-03-13 15:44:24 +05:30
else
snippet_error_response(snippet, 400)
end
end
2020-04-08 14:13:33 +05:30
private
2021-09-30 23:02:18 +05:30
attr_reader :spam_params
2021-03-11 19:13:27 +05:30
2020-05-24 23:13:21 +05:30
def visibility_changed?(snippet)
visibility_level && visibility_level.to_i != snippet.visibility_level
end
2020-06-23 00:09:42 +05:30
def update_snippet_attributes(snippet)
# We can remove the following condition once
# https://gitlab.com/gitlab-org/gitlab/-/issues/217801
# is implemented.
# Once we can perform different operations through this service
# we won't need to keep track of the `content` and `file_name` fields
2021-01-03 14:25:43 +05:30
#
# If the repository does not exist we don't need to update `params`
# because we need to commit the information from the database
if snippet_actions.any? && snippet.repository_exists?
2020-07-28 23:09:34 +05:30
params[:content] = snippet_actions[0].content if snippet_actions[0].content
params[:file_name] = snippet_actions[0].file_path
2020-06-23 00:09:42 +05:30
end
snippet.assign_attributes(params)
end
2020-04-08 14:13:33 +05:30
def save_and_commit(snippet)
2020-04-22 19:07:51 +05:30
return false unless snippet.save
2020-05-24 23:13:21 +05:30
# If the updated attributes does not need to update
# the repository we can just return
return true unless committable_attributes?
2020-04-22 19:07:51 +05:30
2021-01-03 14:25:43 +05:30
unless snippet.repository_exists?
create_repository_for(snippet)
create_first_commit_using_db_data(snippet)
end
2020-05-24 23:13:21 +05:30
create_commit(snippet)
2020-04-22 19:07:51 +05:30
true
2021-06-08 01:23:25 +05:30
rescue StandardError => e
2020-05-24 23:13:21 +05:30
# Restore old attributes but re-assign changes so they're not lost
2020-04-22 19:07:51 +05:30
unless snippet.previous_changes.empty?
snippet.previous_changes.each { |attr, value| snippet[attr] = value[0] }
snippet.save
2020-05-24 23:13:21 +05:30
snippet.assign_attributes(params)
2020-04-22 19:07:51 +05:30
end
2020-04-08 14:13:33 +05:30
2020-05-24 23:13:21 +05:30
add_snippet_repository_error(snippet: snippet, error: e)
2021-01-29 00:20:46 +05:30
Gitlab::ErrorTracking.log_exception(e, service: 'Snippets::UpdateService')
2020-04-22 19:07:51 +05:30
# If the commit action failed we remove it because
# we don't want to leave empty repositories
# around, to allow cloning them.
2021-01-03 14:25:43 +05:30
delete_repository(snippet) if repository_empty?(snippet)
2020-04-22 19:07:51 +05:30
false
2020-04-08 14:13:33 +05:30
end
def create_repository_for(snippet)
snippet.create_repository
raise CreateRepositoryError, 'Repository could not be created' unless snippet.repository_exists?
end
2021-01-03 14:25:43 +05:30
# If the user provides `snippet_actions` and the repository
# does not exist, we need to commit first the snippet info stored
# in the database. Mostly because the content inside `snippet_actions`
# would assume that the file is already in the repository.
def create_first_commit_using_db_data(snippet)
return if snippet_actions.empty?
attrs = commit_attrs(snippet, INITIAL_COMMIT_MSG)
actions = [{ file_path: snippet.file_name, content: snippet.content }]
snippet.snippet_repository.multi_files_action(current_user, actions, **attrs)
end
2020-04-08 14:13:33 +05:30
def create_commit(snippet)
raise UpdateError unless snippet.snippet_repository
2021-01-03 14:25:43 +05:30
attrs = commit_attrs(snippet, UPDATE_COMMIT_MSG)
2020-04-08 14:13:33 +05:30
2021-01-03 14:25:43 +05:30
snippet.snippet_repository.multi_files_action(current_user, files_to_commit(snippet), **attrs)
2020-04-08 14:13:33 +05:30
end
2020-04-22 19:07:51 +05:30
# Because we are removing repositories we don't want to remove
# any existing repository with data. Therefore, we cannot
# rely on cached methods for that check in order to avoid losing
# data.
def repository_empty?(snippet)
snippet.repository._uncached_exists? && !snippet.repository._uncached_has_visible_content?
end
2020-05-24 23:13:21 +05:30
def committable_attributes?
2020-07-28 23:09:34 +05:30
(params.stringify_keys.keys & COMMITTABLE_ATTRIBUTES).present? || snippet_actions.any?
2020-06-23 00:09:42 +05:30
end
def build_actions_from_params(snippet)
file_name_on_repo = snippet.file_name_on_repo
[{ previous_path: file_name_on_repo,
file_path: params[:file_name] || file_name_on_repo,
content: params[:content] }]
2020-05-24 23:13:21 +05:30
end
2020-03-13 15:44:24 +05:30
end
end
2021-12-11 22:18:48 +05:30
Snippets::UpdateService.prepend_mod_with('Snippets::UpdateService')