[Step1|BE] Refactor Snippets::UpdateService to allow multiple files
In preparation for having multiple files inside the same snippet we need to update the Snippets::UpdateService
to make it work with multiple files.
This change can be done in different ways. The most obvious one is to assume that the Snippets::UpdateService
receives already the snippet_files
param and refactor it. Nevertheless, this would force us to change every other place where we call this service to adapt it. Therefore, we would have to update the controllers, the views, the REST API, and GraphQL. For the sake of iteration, we're going to take a small approach in this issue and work on those other places later.
What I propose in this issue is to assume that we are going to receive the same params
as we do now. Since this service is almost prepared for handling multiple files, we're only going to make a small change in snippet_files
.
# Current state
def snippet_files(snippet)
[{ previous_path: snippet.file_name_on_repo,
file_path: params[:file_name],
content: params[:content] }]
end
# Proposal
def execute
# check that user is allowed to set specified visibility_level
new_visibility = visibility_level
# We need to remove the snippet_files from the params
# because it would raise an error if we pass them
# when building the snippet
@snippet_files = params.delete(:snippet_files)
...
end
def snippet_files(snippet)
@snippet_files || build_files_from_params
end
def build_files_from_params
[{ previous_path: snippet.file_name_on_repo,
file_path: params[:file_name],
content: params[:content] }]
end
This small change allows us to be prepared when we really pass the snippet_files
param directly to the service. Besides, it will also allow us to keep backward compatibility for the REST API since we should maintain the API specification we have at the moment. When we add the new argument to the REST endpoint this service will be prepared for both scenarios.
We don't need to put this change behind a feature flag because it doesn't break the single file scenario.