Class: Snippets::CreateService

Inherits:
BaseService show all
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, #params

Instance Method Summary collapse

Methods included from Gitlab::Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Methods inherited from BaseContainerService

#group_container?, #namespace_container?, #project_container?

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: {}, spam_params:) ⇒ CreateService

NOTE: For Issues::CreateService, we require the spam_params and do not default it to nil, because spam_checking is likely to be necessary.


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

def initialize(project:, current_user: nil, params: {}, spam_params:)
  super(project: project, current_user: current_user, params: params)
  @spam_params = spam_params
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
38
39
40
# 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

  Spam::SpamActionService.new(
    spammable: @snippet,
    spam_params: spam_params,
    user: current_user,
    action: :create
  ).execute

  if save_and_commit
    UserAgentDetailService.new(spammable: @snippet, spam_params: spam_params).create
    Gitlab::UsageDataCounters::SnippetCounter.count(:create)

    move_temporary_files

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