2020-01-16 19:09:00 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Snippets
|
|
|
|
class UpdateService < Snippets::BaseService
|
2020-04-21 11:21:10 -04:00
|
|
|
COMMITTABLE_ATTRIBUTES = %w(file_name content).freeze
|
|
|
|
|
2020-03-04 10:08:09 -05:00
|
|
|
UpdateError = Class.new(StandardError)
|
|
|
|
|
2020-01-16 19:09:00 -05:00
|
|
|
def execute(snippet)
|
2020-05-26 14:08:20 -04:00
|
|
|
return invalid_params_error(snippet) unless valid_params?
|
|
|
|
|
2020-05-19 17:08:05 -04:00
|
|
|
if visibility_changed?(snippet) && !visibility_allowed?(snippet, visibility_level)
|
2020-05-22 05:08:09 -04:00
|
|
|
return forbidden_visibility_error(snippet)
|
2020-01-16 19:09:00 -05:00
|
|
|
end
|
|
|
|
|
2020-05-26 14:08:20 -04:00
|
|
|
update_snippet_attributes(snippet)
|
2020-06-08 02:08:19 -04:00
|
|
|
spam_check(snippet, current_user, action: :update)
|
2020-01-16 19:09:00 -05:00
|
|
|
|
2020-03-04 10:08:09 -05:00
|
|
|
if save_and_commit(snippet)
|
2020-01-16 19:09:00 -05:00
|
|
|
Gitlab::UsageDataCounters::SnippetCounter.count(:update)
|
|
|
|
|
|
|
|
ServiceResponse.success(payload: { snippet: snippet } )
|
|
|
|
else
|
|
|
|
snippet_error_response(snippet, 400)
|
|
|
|
end
|
|
|
|
end
|
2020-03-04 10:08:09 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-05-19 17:08:05 -04:00
|
|
|
def visibility_changed?(snippet)
|
|
|
|
visibility_level && visibility_level.to_i != snippet.visibility_level
|
|
|
|
end
|
|
|
|
|
2020-05-26 14:08:20 -04:00
|
|
|
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
|
2020-07-15 05:09:34 -04:00
|
|
|
if snippet_actions.any?
|
|
|
|
params[:content] = snippet_actions[0].content if snippet_actions[0].content
|
|
|
|
params[:file_name] = snippet_actions[0].file_path
|
2020-05-26 14:08:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
snippet.assign_attributes(params)
|
|
|
|
end
|
|
|
|
|
2020-03-04 10:08:09 -05:00
|
|
|
def save_and_commit(snippet)
|
2020-04-06 14:09:37 -04:00
|
|
|
return false unless snippet.save
|
|
|
|
|
2020-04-21 11:21:10 -04:00
|
|
|
# If the updated attributes does not need to update
|
|
|
|
# the repository we can just return
|
|
|
|
return true unless committable_attributes?
|
|
|
|
|
2020-04-23 14:09:46 -04:00
|
|
|
create_repository_for(snippet)
|
|
|
|
create_commit(snippet)
|
2020-04-06 14:09:37 -04:00
|
|
|
|
|
|
|
true
|
|
|
|
rescue => e
|
2020-05-14 08:08:21 -04:00
|
|
|
# Restore old attributes but re-assign changes so they're not lost
|
2020-04-06 14:09:37 -04:00
|
|
|
unless snippet.previous_changes.empty?
|
|
|
|
snippet.previous_changes.each { |attr, value| snippet[attr] = value[0] }
|
|
|
|
snippet.save
|
2020-05-14 08:08:21 -04:00
|
|
|
|
|
|
|
snippet.assign_attributes(params)
|
2020-04-06 14:09:37 -04:00
|
|
|
end
|
2020-03-04 10:08:09 -05:00
|
|
|
|
2020-05-13 17:08:55 -04:00
|
|
|
add_snippet_repository_error(snippet: snippet, error: e)
|
|
|
|
|
2020-04-06 14:09:37 -04:00
|
|
|
log_error(e.message)
|
|
|
|
|
|
|
|
# If the commit action failed we remove it because
|
|
|
|
# we don't want to leave empty repositories
|
|
|
|
# around, to allow cloning them.
|
|
|
|
if repository_empty?(snippet)
|
|
|
|
snippet.repository.remove
|
|
|
|
snippet.snippet_repository&.delete
|
2020-03-04 10:08:09 -05:00
|
|
|
end
|
2020-04-06 14:09:37 -04:00
|
|
|
|
|
|
|
# Purge any existing value for repository_exists?
|
|
|
|
snippet.repository.expire_exists_cache
|
|
|
|
|
|
|
|
false
|
2020-03-04 10:08:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_repository_for(snippet)
|
|
|
|
snippet.create_repository
|
|
|
|
|
|
|
|
raise CreateRepositoryError, 'Repository could not be created' unless snippet.repository_exists?
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_commit(snippet)
|
|
|
|
raise UpdateError unless snippet.snippet_repository
|
|
|
|
|
|
|
|
commit_attrs = {
|
|
|
|
branch_name: 'master',
|
|
|
|
message: 'Update snippet'
|
|
|
|
}
|
|
|
|
|
2020-05-26 14:08:20 -04:00
|
|
|
snippet.snippet_repository.multi_files_action(current_user, files_to_commit(snippet), commit_attrs)
|
2020-03-04 10:08:09 -05:00
|
|
|
end
|
2020-04-06 14:09:37 -04:00
|
|
|
|
|
|
|
# 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-04-21 11:21:10 -04:00
|
|
|
|
|
|
|
def committable_attributes?
|
2020-07-15 05:09:34 -04:00
|
|
|
(params.stringify_keys.keys & COMMITTABLE_ATTRIBUTES).present? || snippet_actions.any?
|
2020-05-26 14:08:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def build_actions_from_params(snippet)
|
2020-05-29 08:08:19 -04:00
|
|
|
file_name_on_repo = snippet.file_name_on_repo
|
|
|
|
|
|
|
|
[{ previous_path: file_name_on_repo,
|
|
|
|
file_path: params[:file_name] || file_name_on_repo,
|
2020-05-26 14:08:20 -04:00
|
|
|
content: params[:content] }]
|
2020-04-21 11:21:10 -04:00
|
|
|
end
|
2020-01-16 19:09:00 -05:00
|
|
|
end
|
|
|
|
end
|