Class: Snippets::UpdateService

Inherits:
BaseService show all
Defined in:
app/services/snippets/update_service.rb

Constant Summary collapse

COMMITTABLE_ATTRIBUTES =
%w[file_name content].freeze
UpdateError =
Class.new(StandardError)

Constants inherited from BaseService

BaseService::CreateRepositoryError, BaseService::INITIAL_COMMIT_MSG, BaseService::UPDATE_COMMIT_MSG

Instance Attribute Summary

Attributes inherited from BaseService

#snippet_actions, #uploaded_assets

Attributes inherited from BaseProjectService

#project

Attributes inherited from BaseContainerService

#container, #current_user, #group, #params, #project

Instance Method Summary collapse

Methods inherited from BaseContainerService

#group_container?, #namespace_container?, #project_container?, #project_group

Methods included from BaseServiceUtility

#deny_visibility_level, #event_service, #log_error, #log_info, #notification_service, #system_hook_service, #todo_service, #visibility_level

Methods included from Gitlab::Allowable

#can?

Constructor Details

#initialize(project:, current_user: nil, params: {}, perform_spam_check: false) ⇒ UpdateService

Returns a new instance of UpdateService.



9
10
11
12
# File 'app/services/snippets/update_service.rb', line 9

def initialize(project:, current_user: nil, params: {}, perform_spam_check: false)
  super(project: project, current_user: current_user, params: params)
  @perform_spam_check = perform_spam_check
end

Instance Method Details

#execute(snippet) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/services/snippets/update_service.rb', line 14

def execute(snippet)
  return invalid_params_error(snippet) unless valid_params?

  if visibility_changed?(snippet) && !visibility_allowed?(visibility_level)
    return forbidden_visibility_error(snippet)
  end

  update_snippet_attributes(snippet)

  files = snippet.all_files.map { |f| { path: f } } + file_paths_to_commit

  if perform_spam_check
    snippet.check_for_spam(user: current_user, action: :update, extra_features: { files: files })
  end

  if save_and_commit(snippet)
    Gitlab::UsageDataCounters::SnippetCounter.count(:update)

    ServiceResponse.success(payload: { snippet: snippet })
  else
    snippet_error_response(snippet, 400)
  end
end