Commit 9b26edbe authored by Gabriel Mazetto's avatar Gabriel Mazetto

Refactor ValidateNewBranchService into Branches::ValidateNewService

Renamed ValidateNewBranchService into Branches::ValidateNewService and
renamed its usages.

Added spec coverage
parent 4ca43462
......@@ -5,8 +5,7 @@ module Branches
def execute(branch_name, ref, create_master_if_empty: true)
create_master_branch if create_master_if_empty && project.empty_repo?
result = ValidateNewBranchService.new(project, current_user)
.execute(branch_name)
result = ::Branches::ValidateNewService.new(project).execute(branch_name)
return result if result[:status] == :error
......
# frozen_string_literal: true
module Branches
class ValidateNewService < BaseService
def initialize(project)
@project = project
end
def execute(branch_name, force: false)
return error('Branch name is invalid') unless valid_name?(branch_name)
if branch_exist?(branch_name) && !force
return error('Branch already exists')
end
success
rescue Gitlab::Git::PreReceiveError => ex
error(ex.message)
end
private
def valid_name?(branch_name)
Gitlab::GitRefValidator.validate(branch_name)
end
def branch_exist?(branch_name)
project.repository.branch_exists?(branch_name)
end
end
end
......@@ -101,7 +101,7 @@ module Commits
end
def validate_new_branch_name!
result = ValidateNewBranchService.new(project, current_user).execute(@branch_name, force: force?)
result = ::Branches::ValidateNewService.new(project).execute(@branch_name, force: force?)
if result[:status] == :error
raise_error("Something went wrong when we tried to create '#{@branch_name}' for you: #{result[:message]}")
......
# frozen_string_literal: true
require_relative 'base_service'
class ValidateNewBranchService < BaseService
def execute(branch_name, force: false)
valid_branch = Gitlab::GitRefValidator.validate(branch_name)
unless valid_branch
return error('Branch name is invalid')
end
if project.repository.branch_exists?(branch_name) && !force
return error('Branch already exists')
end
success
rescue Gitlab::Git::PreReceiveError => ex
error(ex.message)
end
end
# frozen_string_literal: true
require 'spec_helper'
describe Branches::ValidateNewService do
let(:project) { create(:project, :repository) }
subject(:service) { described_class.new(project) }
describe '#execute' do
context 'validation' do
it 'returns error with an invalid branch name' do
result = service.execute('refs/heads/invalid_branch')
expect(result[:status]).to eq(:error)
expect(result[:message]).to eq('Branch name is invalid')
end
it 'returns success with a valid branch name' do
result = service.execute('valid_branch_name')
expect(result[:status]).to eq(:success)
end
end
context 'branch exist' do
it 'returns error when branch exists' do
result = service.execute('master')
expect(result[:status]).to eq(:error)
expect(result[:message]).to eq('Branch already exists')
end
it 'returns success when branch name is available' do
result = service.execute('valid_branch_name')
expect(result[:status]).to eq(:success)
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment