2020-01-01 13:55:28 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Branches
|
|
|
|
class CreateService < BaseService
|
2022-08-27 11:52:29 +05:30
|
|
|
def initialize(project, user = nil, params = {})
|
|
|
|
super(project, user, params)
|
|
|
|
|
|
|
|
@errors = []
|
|
|
|
end
|
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
def execute(branch_name, ref, create_default_branch_if_empty: true)
|
|
|
|
create_default_branch if create_default_branch_if_empty && project.empty_repo?
|
2020-01-01 13:55:28 +05:30
|
|
|
|
2022-08-27 11:52:29 +05:30
|
|
|
result = branch_validation_service.execute(branch_name)
|
2020-01-01 13:55:28 +05:30
|
|
|
|
|
|
|
return result if result[:status] == :error
|
|
|
|
|
2022-08-27 11:52:29 +05:30
|
|
|
create_branch(branch_name, ref)
|
|
|
|
end
|
|
|
|
|
|
|
|
def bulk_create(branches)
|
|
|
|
reset_errors
|
|
|
|
|
|
|
|
created_branches =
|
|
|
|
branches
|
|
|
|
.then { |branches| only_valid_branches(branches) }
|
|
|
|
.then { |branches| create_branches(branches) }
|
|
|
|
.then { |branches| expire_branches_cache(branches) }
|
|
|
|
|
|
|
|
return error(errors) if errors.present?
|
|
|
|
|
|
|
|
success(branches: created_branches)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :errors
|
|
|
|
|
|
|
|
def reset_errors
|
|
|
|
@errors = []
|
|
|
|
end
|
|
|
|
|
|
|
|
def only_valid_branches(branches)
|
|
|
|
branches.select do |branch_name, _ref|
|
|
|
|
result = branch_validation_service.execute(branch_name)
|
|
|
|
|
|
|
|
if result[:status] == :error
|
|
|
|
errors << result[:message]
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
true
|
2021-09-30 23:02:18 +05:30
|
|
|
end
|
2022-08-27 11:52:29 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def create_branches(branches)
|
|
|
|
branches.filter_map do |branch_name, ref|
|
|
|
|
result = create_branch(branch_name, ref, expire_cache: false)
|
|
|
|
|
|
|
|
if result[:status] == :error
|
|
|
|
errors << result[:message]
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
result[:branch]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def expire_branches_cache(branches)
|
|
|
|
repository.expire_branches_cache if branches.present?
|
|
|
|
|
|
|
|
branches
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_branch(branch_name, ref, expire_cache: true)
|
|
|
|
new_branch = repository.add_branch(current_user, branch_name, ref, expire_cache: expire_cache)
|
2020-01-01 13:55:28 +05:30
|
|
|
|
|
|
|
if new_branch
|
2022-08-27 11:52:29 +05:30
|
|
|
success(branch: new_branch)
|
2020-01-01 13:55:28 +05:30
|
|
|
else
|
2021-09-30 23:02:18 +05:30
|
|
|
error("Failed to create branch '#{branch_name}': invalid reference name '#{ref}'")
|
2020-01-01 13:55:28 +05:30
|
|
|
end
|
2022-08-27 11:52:29 +05:30
|
|
|
rescue Gitlab::Git::CommandError => e
|
|
|
|
error("Failed to create branch '#{branch_name}': #{e}")
|
2020-10-24 23:57:45 +05:30
|
|
|
rescue Gitlab::Git::PreReceiveError => e
|
2022-04-04 11:22:00 +05:30
|
|
|
Gitlab::ErrorTracking.log_exception(e, pre_receive_message: e.raw_message, branch_name: branch_name, ref: ref)
|
2020-10-24 23:57:45 +05:30
|
|
|
error(e.message)
|
2020-01-01 13:55:28 +05:30
|
|
|
end
|
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
def create_default_branch
|
2020-01-01 13:55:28 +05:30
|
|
|
project.repository.create_file(
|
|
|
|
current_user,
|
|
|
|
'/README.md',
|
|
|
|
'',
|
|
|
|
message: 'Add README.md',
|
2021-09-04 01:27:46 +05:30
|
|
|
branch_name: project.default_branch_or_main
|
2020-01-01 13:55:28 +05:30
|
|
|
)
|
|
|
|
end
|
2022-08-27 11:52:29 +05:30
|
|
|
|
|
|
|
def branch_validation_service
|
|
|
|
@branch_validation_service ||= ::Branches::ValidateNewService.new(project)
|
|
|
|
end
|
2020-01-01 13:55:28 +05:30
|
|
|
end
|
|
|
|
end
|