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

Returns a new instance of CreateService.



5
6
7
8
# File 'app/services/snippets/create_service.rb', line 5

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



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

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
    Gitlab::UsageDataCounters::SnippetCounter.count(:create)

    move_temporary_files

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