[Step1|BE] Refactor Snippets::CreateService to allow multiple files
Important: All the new functionality must be implemented behind the feature flag :snippet_multiple_files
. Besides, it has to be scoped for users. Therefore, instead of checking if the feature flag is enabled or not (Feature.enabled?(:snippet_multiple_files)
), we will check if the feature flag is enabled for specific users (Feature.enabled?(:snippet_multiple_files, user)
).
In preparation for having multiple files inside the same snippet we need to update the Snippets::CreateService
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::CreateService
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
[{ file_path: params[:file_name], content: params[:content] }]
end
# Proposal
def execute
filter_spam_check_params
# 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) if Feature.enabled?(:snippet_multiple_files, current_user)
...
end
def snippet_files
@snippet_files || build_files_from_params
end
def build_files_from_params
[{ 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.