Class: Snippets::CreateService

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

Constant Summary

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 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: true) ⇒ CreateService

Returns a new instance of CreateService.



7
8
9
10
# File 'app/services/snippets/create_service.rb', line 7

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

Instance Method Details

#executeObject



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

def execute
  @snippet = build_from_params

  return invalid_params_error(@snippet) unless valid_params?

  unless visibility_allowed?(snippet.visibility_level)
    return forbidden_visibility_error(snippet)
  end

  @snippet.author = current_user

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

  if save_and_commit
    UserAgentDetailService.new(spammable: @snippet, perform_spam_check: perform_spam_check).create
    track_internal_event('create_snippet', project: project, user: current_user)

    move_temporary_files

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