2016-07-28 00:04:57 -04:00
|
|
|
require "spec_helper"
|
|
|
|
|
|
|
|
describe MergeRequests::GetUrlsService do
|
2017-09-29 04:04:50 -04:00
|
|
|
include ProjectForksHelper
|
|
|
|
|
2017-03-27 17:14:01 -04:00
|
|
|
let(:project) { create(:project, :public, :repository) }
|
2017-05-01 11:13:33 -04:00
|
|
|
let(:service) { described_class.new(project) }
|
2017-08-10 13:44:17 -04:00
|
|
|
let(:source_branch) { "merge-test" }
|
2016-11-18 07:17:10 -05:00
|
|
|
let(:new_merge_request_url) { "http://#{Gitlab.config.gitlab.host}/#{project.namespace.name}/#{project.path}/merge_requests/new?merge_request%5Bsource_branch%5D=#{source_branch}" }
|
|
|
|
let(:show_merge_request_url) { "http://#{Gitlab.config.gitlab.host}/#{project.namespace.name}/#{project.path}/merge_requests/#{merge_request.iid}" }
|
2016-07-28 00:04:57 -04:00
|
|
|
let(:new_branch_changes) { "#{Gitlab::Git::BLANK_SHA} 570e7b2abdd848b95f2f578043fc23bd6f6fd24d refs/heads/#{source_branch}" }
|
2016-08-11 20:39:18 -04:00
|
|
|
let(:deleted_branch_changes) { "d14d6c0abdd253381df51a723d58691b2ee1ab08 #{Gitlab::Git::BLANK_SHA} refs/heads/#{source_branch}" }
|
2016-07-28 00:04:57 -04:00
|
|
|
let(:existing_branch_changes) { "d14d6c0abdd253381df51a723d58691b2ee1ab08 570e7b2abdd848b95f2f578043fc23bd6f6fd24d refs/heads/#{source_branch}" }
|
2016-08-11 20:39:18 -04:00
|
|
|
let(:default_branch_changes) { "d14d6c0abdd253381df51a723d58691b2ee1ab08 570e7b2abdd848b95f2f578043fc23bd6f6fd24d refs/heads/master" }
|
2016-07-28 00:04:57 -04:00
|
|
|
|
|
|
|
describe "#execute" do
|
|
|
|
shared_examples 'new_merge_request_link' do
|
|
|
|
it 'returns url to create new merge request' do
|
|
|
|
result = service.execute(changes)
|
|
|
|
expect(result).to match([{
|
|
|
|
branch_name: source_branch,
|
|
|
|
url: new_merge_request_url,
|
|
|
|
new_merge_request: true
|
|
|
|
}])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'show_merge_request_url' do
|
|
|
|
it 'returns url to view merge request' do
|
|
|
|
result = service.execute(changes)
|
|
|
|
expect(result).to match([{
|
|
|
|
branch_name: source_branch,
|
|
|
|
url: show_merge_request_url,
|
|
|
|
new_merge_request: false
|
|
|
|
}])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-11 20:39:18 -04:00
|
|
|
shared_examples 'no_merge_request_url' do
|
|
|
|
it 'returns no URL' do
|
|
|
|
result = service.execute(changes)
|
|
|
|
expect(result).to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'pushing to default branch' do
|
|
|
|
let(:changes) { default_branch_changes }
|
|
|
|
it_behaves_like 'no_merge_request_url'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'pushing to project with MRs disabled' do
|
|
|
|
let(:changes) { new_branch_changes }
|
|
|
|
|
|
|
|
before do
|
2016-08-01 18:31:21 -04:00
|
|
|
project.project_feature.update_attribute(:merge_requests_access_level, ProjectFeature::DISABLED)
|
2016-08-11 20:39:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'no_merge_request_url'
|
|
|
|
end
|
|
|
|
|
2016-07-28 00:04:57 -04:00
|
|
|
context 'pushing one completely new branch' do
|
|
|
|
let(:changes) { new_branch_changes }
|
|
|
|
it_behaves_like 'new_merge_request_link'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'pushing to existing branch but no merge request' do
|
|
|
|
let(:changes) { existing_branch_changes }
|
|
|
|
it_behaves_like 'new_merge_request_link'
|
|
|
|
end
|
|
|
|
|
2016-08-11 20:39:18 -04:00
|
|
|
context 'pushing to deleted branch' do
|
|
|
|
let(:changes) { deleted_branch_changes }
|
|
|
|
it_behaves_like 'no_merge_request_url'
|
|
|
|
end
|
|
|
|
|
2016-07-28 00:04:57 -04:00
|
|
|
context 'pushing to existing branch and merge request opened' do
|
|
|
|
let!(:merge_request) { create(:merge_request, source_project: project, source_branch: source_branch) }
|
|
|
|
let(:changes) { existing_branch_changes }
|
|
|
|
it_behaves_like 'show_merge_request_url'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'pushing to existing branch and merge request is reopened' do
|
2017-07-19 10:03:50 -04:00
|
|
|
let!(:merge_request) { create(:merge_request, :opened, source_project: project, source_branch: source_branch) }
|
2016-07-28 00:04:57 -04:00
|
|
|
let(:changes) { existing_branch_changes }
|
|
|
|
it_behaves_like 'show_merge_request_url'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'pushing to existing branch from forked project' do
|
|
|
|
let(:user) { create(:user) }
|
2017-09-29 04:04:50 -04:00
|
|
|
let!(:forked_project) { fork_project(project, user, repository: true) }
|
2016-07-28 00:04:57 -04:00
|
|
|
let!(:merge_request) { create(:merge_request, source_project: forked_project, target_project: project, source_branch: source_branch) }
|
|
|
|
let(:changes) { existing_branch_changes }
|
|
|
|
# Source project is now the forked one
|
2017-05-01 11:13:33 -04:00
|
|
|
let(:service) { described_class.new(forked_project) }
|
2016-08-11 20:39:18 -04:00
|
|
|
|
|
|
|
before do
|
|
|
|
allow(forked_project).to receive(:empty_repo?).and_return(false)
|
|
|
|
end
|
|
|
|
|
2016-07-28 00:04:57 -04:00
|
|
|
it_behaves_like 'show_merge_request_url'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'pushing to existing branch and merge request is closed' do
|
|
|
|
let!(:merge_request) { create(:merge_request, :closed, source_project: project, source_branch: source_branch) }
|
|
|
|
let(:changes) { existing_branch_changes }
|
|
|
|
it_behaves_like 'new_merge_request_link'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'pushing to existing branch and merge request is merged' do
|
|
|
|
let!(:merge_request) { create(:merge_request, :merged, source_project: project, source_branch: source_branch) }
|
|
|
|
let(:changes) { existing_branch_changes }
|
|
|
|
it_behaves_like 'new_merge_request_link'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'pushing new branch and existing branch (with merge request created) at once' do
|
2017-08-10 13:44:17 -04:00
|
|
|
let!(:merge_request) { create(:merge_request, source_project: project, source_branch: "markdown") }
|
2016-07-28 00:04:57 -04:00
|
|
|
let(:new_branch_changes) { "#{Gitlab::Git::BLANK_SHA} 570e7b2abdd848b95f2f578043fc23bd6f6fd24d refs/heads/new_branch" }
|
2017-08-10 13:44:17 -04:00
|
|
|
let(:existing_branch_changes) { "d14d6c0abdd253381df51a723d58691b2ee1ab08 570e7b2abdd848b95f2f578043fc23bd6f6fd24d refs/heads/markdown" }
|
2016-07-28 00:04:57 -04:00
|
|
|
let(:changes) { "#{new_branch_changes}\n#{existing_branch_changes}" }
|
2016-11-18 07:17:10 -05:00
|
|
|
let(:new_merge_request_url) { "http://#{Gitlab.config.gitlab.host}/#{project.namespace.name}/#{project.path}/merge_requests/new?merge_request%5Bsource_branch%5D=new_branch" }
|
2016-07-28 00:04:57 -04:00
|
|
|
|
|
|
|
it 'returns 2 urls for both creating new and showing merge request' do
|
|
|
|
result = service.execute(changes)
|
|
|
|
expect(result).to match([{
|
|
|
|
branch_name: "new_branch",
|
|
|
|
url: new_merge_request_url,
|
|
|
|
new_merge_request: true
|
|
|
|
}, {
|
2017-08-10 13:44:17 -04:00
|
|
|
branch_name: "markdown",
|
2016-07-28 00:04:57 -04:00
|
|
|
url: show_merge_request_url,
|
|
|
|
new_merge_request: false
|
|
|
|
}])
|
|
|
|
end
|
|
|
|
end
|
2017-03-17 08:35:39 -04:00
|
|
|
|
|
|
|
context 'when printing_merge_request_link_enabled is false' do
|
|
|
|
it 'returns empty array' do
|
|
|
|
project.update!(printing_merge_request_link_enabled: false)
|
|
|
|
|
|
|
|
result = service.execute(existing_branch_changes)
|
|
|
|
|
|
|
|
expect(result).to eq([])
|
|
|
|
end
|
|
|
|
end
|
2016-07-28 00:04:57 -04:00
|
|
|
end
|
|
|
|
end
|