diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-07-20 15:26:25 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-07-20 15:26:25 +0300 |
commit | a09983ae35713f5a2bbb100981116d31ce99826e (patch) | |
tree | 2ee2af7bd104d57086db360a7e6d8c9d5d43667a /app/services/snippets | |
parent | 18c5ab32b738c0b6ecb4d0df3994000482f34bd8 (diff) |
Add latest changes from gitlab-org/gitlab@13-2-stable-ee
Diffstat (limited to 'app/services/snippets')
-rw-r--r-- | app/services/snippets/base_service.rb | 20 | ||||
-rw-r--r-- | app/services/snippets/create_service.rb | 10 | ||||
-rw-r--r-- | app/services/snippets/update_service.rb | 7 | ||||
-rw-r--r-- | app/services/snippets/update_statistics_service.rb | 28 |
4 files changed, 52 insertions, 13 deletions
diff --git a/app/services/snippets/base_service.rb b/app/services/snippets/base_service.rb index 5d1fe815d83..d9e8326f159 100644 --- a/app/services/snippets/base_service.rb +++ b/app/services/snippets/base_service.rb @@ -6,13 +6,15 @@ module Snippets CreateRepositoryError = Class.new(StandardError) - attr_reader :uploaded_assets, :snippet_files + attr_reader :uploaded_assets, :snippet_actions def initialize(project, user = nil, params = {}) super @uploaded_assets = Array(@params.delete(:files).presence) - @snippet_files = SnippetInputActionCollection.new(Array(@params.delete(:snippet_files).presence)) + + input_actions = Array(@params.delete(:snippet_actions).presence) + @snippet_actions = SnippetInputActionCollection.new(input_actions, allowed_actions: restricted_files_actions) filter_spam_check_params end @@ -30,18 +32,18 @@ module Snippets end def valid_params? - return true if snippet_files.empty? + return true if snippet_actions.empty? - (params.keys & [:content, :file_name]).none? && snippet_files.valid? + (params.keys & [:content, :file_name]).none? && snippet_actions.valid? end def invalid_params_error(snippet) - if snippet_files.valid? + if snippet_actions.valid? [:content, :file_name].each do |key| snippet.errors.add(key, 'and snippet files cannot be used together') if params.key?(key) end else - snippet.errors.add(:snippet_files, 'have invalid data') + snippet.errors.add(:snippet_actions, 'have invalid data') end snippet_error_response(snippet, 403) @@ -73,11 +75,15 @@ module Snippets end def files_to_commit(snippet) - snippet_files.to_commit_actions.presence || build_actions_from_params(snippet) + snippet_actions.to_commit_actions.presence || build_actions_from_params(snippet) end def build_actions_from_params(snippet) raise NotImplementedError end + + def restricted_files_actions + nil + end end end diff --git a/app/services/snippets/create_service.rb b/app/services/snippets/create_service.rb index 7b477621da3..dab47de8a36 100644 --- a/app/services/snippets/create_service.rb +++ b/app/services/snippets/create_service.rb @@ -37,13 +37,13 @@ module Snippets end end - # If the snippet_files param is present + # If the snippet_actions param is present # we need to fill content and file_name from # the model def create_params - return params if snippet_files.empty? + return params if snippet_actions.empty? - params.merge(content: snippet_files[0].content, file_name: snippet_files[0].file_path) + params.merge(content: snippet_actions[0].content, file_name: snippet_actions[0].file_path) end def save_and_commit @@ -100,5 +100,9 @@ module Snippets def build_actions_from_params(_snippet) [{ file_path: params[:file_name], content: params[:content] }] end + + def restricted_files_actions + :create + end end end diff --git a/app/services/snippets/update_service.rb b/app/services/snippets/update_service.rb index 6cdc2c374da..00146389e22 100644 --- a/app/services/snippets/update_service.rb +++ b/app/services/snippets/update_service.rb @@ -37,8 +37,9 @@ module Snippets # 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 - if snippet_files.any? - params.merge!(content: snippet_files[0].content, file_name: snippet_files[0].file_path) + if snippet_actions.any? + params[:content] = snippet_actions[0].content if snippet_actions[0].content + params[:file_name] = snippet_actions[0].file_path end snippet.assign_attributes(params) @@ -108,7 +109,7 @@ module Snippets end def committable_attributes? - (params.stringify_keys.keys & COMMITTABLE_ATTRIBUTES).present? || snippet_files.any? + (params.stringify_keys.keys & COMMITTABLE_ATTRIBUTES).present? || snippet_actions.any? end def build_actions_from_params(snippet) diff --git a/app/services/snippets/update_statistics_service.rb b/app/services/snippets/update_statistics_service.rb new file mode 100644 index 00000000000..295cb963ccc --- /dev/null +++ b/app/services/snippets/update_statistics_service.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +module Snippets + class UpdateStatisticsService + attr_reader :snippet + + def initialize(snippet) + @snippet = snippet + end + + def execute + unless snippet.repository_exists? + return ServiceResponse.error(message: 'Invalid snippet repository', http_status: 400) + end + + snippet.repository.expire_statistics_caches + statistics.refresh! + + ServiceResponse.success(message: 'Snippet statistics successfully updated.') + end + + private + + def statistics + @statistics ||= snippet.statistics || snippet.build_statistics + end + end +end |