Class: Security::CiConfiguration::BaseCreateService

Inherits:
Object
  • Object
show all
Defined in:
app/services/security/ci_configuration/base_create_service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project, current_user) ⇒ BaseCreateService

Returns a new instance of BaseCreateService.



10
11
12
13
14
# File 'app/services/security/ci_configuration/base_create_service.rb', line 10

def initialize(project, current_user)
  @project = project
  @current_user = current_user
  @branch_name = project.repository.next_branch(next_branch)
end

Instance Attribute Details

#branch_nameObject (readonly)

Returns the value of attribute branch_name.



8
9
10
# File 'app/services/security/ci_configuration/base_create_service.rb', line 8

def branch_name
  @branch_name
end

#current_userObject (readonly)

Returns the value of attribute current_user.



8
9
10
# File 'app/services/security/ci_configuration/base_create_service.rb', line 8

def current_user
  @current_user
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'app/services/security/ci_configuration/base_create_service.rb', line 8

def name
  @name
end

#projectObject (readonly)

Returns the value of attribute project.



8
9
10
# File 'app/services/security/ci_configuration/base_create_service.rb', line 8

def project
  @project
end

Instance Method Details

#executeObject



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
41
42
43
44
45
46
47
48
# File 'app/services/security/ci_configuration/base_create_service.rb', line 16

def execute
  if project.repository.empty? && !(@params && @params[:initialize_with_sast])
    docs_link = ActionController::Base.helpers.link_to _('add at least one file to the repository'),
                        Rails.application.routes.url_helpers.help_page_url('user/project/repository/index.md',
                                                                          anchor: 'add-files-to-a-repository'),
                        target: '_blank',
                        rel: 'noopener noreferrer'

    return ServiceResponse.error(
      message: _(format('You must %s before using Security features.', docs_link)).html_safe
    )
  end

  project.repository.add_branch(current_user, branch_name, project.default_branch)

  attributes_for_commit = attributes

  result = ::Files::MultiService.new(project, current_user, attributes_for_commit).execute

  return ServiceResponse.error(message: result[:message]) unless result[:status] == :success

  track_event(attributes_for_commit)
  ServiceResponse.success(payload: { branch: branch_name, success_path: successful_change_path })
rescue CiContentParseError => e
  Gitlab::ErrorTracking.track_exception(e)

  ServiceResponse.error(message: e.message)
rescue Gitlab::Git::PreReceiveError => e
  ServiceResponse.error(message: e.message)
rescue StandardError
  remove_branch_on_exception
  raise
end