Commit e3e1e0b7 authored by Jan Provaznik's avatar Jan Provaznik

Merge branch '216545_fix_branch_creation_error' into 'master'

Correct error message for branch creation service

Closes #216545

See merge request gitlab-org/gitlab!31594
parents ac620b29 2f3f6828
...@@ -14,7 +14,7 @@ module Branches ...@@ -14,7 +14,7 @@ module Branches
if new_branch if new_branch
success(new_branch) success(new_branch)
else else
error("Invalid reference name: #{branch_name}") error("Invalid reference name: #{ref}")
end end
rescue Gitlab::Git::PreReceiveError => ex rescue Gitlab::Git::PreReceiveError => ex
error(ex.message) error(ex.message)
......
...@@ -630,7 +630,7 @@ describe API::Branches do ...@@ -630,7 +630,7 @@ describe API::Branches do
post api(route, user), params: { branch: 'new_design3', ref: 'foo' } post api(route, user), params: { branch: 'new_design3', ref: 'foo' }
expect(response).to have_gitlab_http_status(:bad_request) expect(response).to have_gitlab_http_status(:bad_request)
expect(json_response['message']).to eq('Invalid reference name: new_design3') expect(json_response['message']).to eq('Invalid reference name: foo')
end end
end end
......
...@@ -38,7 +38,7 @@ describe 'Creation of a new branch' do ...@@ -38,7 +38,7 @@ describe 'Creation of a new branch' do
let(:ref) { 'unknown' } let(:ref) { 'unknown' }
it_behaves_like 'a mutation that returns errors in the response', it_behaves_like 'a mutation that returns errors in the response',
errors: ['Invalid reference name: new_branch'] errors: ['Invalid reference name: unknown']
end end
end end
end end
...@@ -3,39 +3,45 @@ ...@@ -3,39 +3,45 @@
require 'spec_helper' require 'spec_helper'
describe Branches::CreateService do describe Branches::CreateService do
let(:user) { create(:user) }
subject(:service) { described_class.new(project, user) } subject(:service) { described_class.new(project, user) }
let_it_be(:project) { create(:project_empty_repo) }
let_it_be(:user) { create(:user) }
describe '#execute' do describe '#execute' do
context 'when repository is empty' do context 'when repository is empty' do
let(:project) { create(:project_empty_repo) }
it 'creates master branch' do it 'creates master branch' do
service.execute('my-feature', 'master') service.execute('my-feature', 'master')
expect(project.repository.branch_exists?('master')).to be_truthy expect(project.repository.branch_exists?('master')).to be_truthy
end end
it 'creates my-feature branch' do it 'creates another-feature branch' do
service.execute('my-feature', 'master') service.execute('another-feature', 'master')
expect(project.repository.branch_exists?('my-feature')).to be_truthy expect(project.repository.branch_exists?('another-feature')).to be_truthy
end end
end end
context 'when creating a branch fails' do context 'when branch already exists' do
let(:project) { create(:project_empty_repo) } it 'returns an error' do
result = service.execute('master', 'master')
expect(result[:status]).to eq(:error)
expect(result[:message]).to eq('Branch already exists')
end
end
context 'when incorrect reference is provided' do
before do before do
allow(project.repository).to receive(:add_branch).and_return(false) allow(project.repository).to receive(:add_branch).and_return(false)
end end
it 'returns an error with the branch name' do it 'returns an error with a reference name' do
result = service.execute('my-feature', 'master') result = service.execute('new-feature', 'unknown')
expect(result[:status]).to eq(:error) expect(result[:status]).to eq(:error)
expect(result[:message]).to eq("Invalid reference name: my-feature") expect(result[:message]).to eq('Invalid reference name: unknown')
end end
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