Class: Snippets::UpdateService

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

Constant Summary collapse

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

Constants inherited from BaseService

BaseService::CreateRepositoryError, BaseService::INITIAL_COMMIT_MSG, BaseService::INVALID_PARAMS_ERROR, BaseService::INVALID_PARAMS_MESSAGES, BaseService::SNIPPET_ACCESS_ERROR, 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 included from Gitlab::InternalEventsTracking

#track_internal_event

Methods inherited from BaseContainerService

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

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?, #can_all?, #can_any?

Constructor Details

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

Returns a new instance of UpdateService.



12
13
14
15
# File 'app/services/snippets/update_service.rb', line 12

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



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

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)
    track_internal_event('update_snippet', project: project, user: current_user)

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