2020-02-18 10:08:51 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 11:08:50 -04:00
|
|
|
RSpec.describe PostReceiveService do
|
2020-02-18 10:08:51 -05:00
|
|
|
include Gitlab::Routing
|
|
|
|
|
|
|
|
let_it_be(:user) { create(:user) }
|
2020-03-04 19:07:49 -05:00
|
|
|
let_it_be(:project) { create(:project, :repository, :wiki_repo, namespace: user.namespace) }
|
|
|
|
let_it_be(:project_snippet) { create(:project_snippet, :repository, project: project, author: user) }
|
|
|
|
let_it_be(:personal_snippet) { create(:personal_snippet, :repository, author: user) }
|
2020-02-18 10:08:51 -05:00
|
|
|
|
|
|
|
let(:identifier) { 'key-123' }
|
|
|
|
let(:gl_repository) { "project-#{project.id}" }
|
|
|
|
let(:branch_name) { 'feature' }
|
|
|
|
let(:secret_token) { Gitlab::Shell.secret_token }
|
|
|
|
let(:reference_counter) { double('ReferenceCounter') }
|
|
|
|
let(:push_options) { ['ci.skip', 'another push option'] }
|
2020-03-04 19:07:49 -05:00
|
|
|
let(:repository) { project.repository }
|
2020-02-18 10:08:51 -05:00
|
|
|
|
|
|
|
let(:changes) do
|
|
|
|
"#{Gitlab::Git::BLANK_SHA} 570e7b2abdd848b95f2f578043fc23bd6f6fd24d refs/heads/#{branch_name}"
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
gl_repository: gl_repository,
|
|
|
|
secret_token: secret_token,
|
|
|
|
identifier: identifier,
|
|
|
|
changes: changes,
|
|
|
|
push_options: push_options
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2020-03-04 19:07:49 -05:00
|
|
|
let(:service) { described_class.new(user, repository, project, params) }
|
|
|
|
let(:response) { service.execute }
|
2020-02-18 10:08:51 -05:00
|
|
|
|
|
|
|
subject { response.messages.as_json }
|
|
|
|
|
2020-03-04 19:07:49 -05:00
|
|
|
context 'when project is nil' do
|
|
|
|
let(:gl_repository) { "snippet-#{personal_snippet.id}" }
|
|
|
|
let(:project) { nil }
|
|
|
|
let(:repository) { personal_snippet.repository }
|
2020-02-18 10:08:51 -05:00
|
|
|
|
2020-03-04 19:07:49 -05:00
|
|
|
it 'does not return error' do
|
|
|
|
expect(subject).to be_empty
|
|
|
|
end
|
2020-02-18 10:08:51 -05:00
|
|
|
end
|
|
|
|
|
2020-03-04 19:07:49 -05:00
|
|
|
context 'when repository is nil' do
|
|
|
|
let(:repository) { nil }
|
2020-02-18 10:08:51 -05:00
|
|
|
|
2020-03-04 19:07:49 -05:00
|
|
|
it 'does not return error' do
|
|
|
|
expect(subject).to be_empty
|
|
|
|
end
|
2020-02-18 10:08:51 -05:00
|
|
|
end
|
|
|
|
|
2020-03-04 19:07:49 -05:00
|
|
|
context 'when both repository and project are nil' do
|
|
|
|
let(:gl_repository) { "snippet-#{personal_snippet.id}" }
|
|
|
|
let(:project) { nil }
|
|
|
|
let(:repository) { nil }
|
2020-02-18 10:08:51 -05:00
|
|
|
|
2020-03-04 19:07:49 -05:00
|
|
|
it 'does not return error' do
|
|
|
|
expect(subject).to be_empty
|
|
|
|
end
|
2020-02-18 10:08:51 -05:00
|
|
|
end
|
|
|
|
|
2020-03-04 19:07:49 -05:00
|
|
|
shared_examples 'post_receive_service actions' do
|
|
|
|
it 'enqueues a PostReceive worker job' do
|
|
|
|
expect(PostReceive).to receive(:perform_async)
|
|
|
|
.with(gl_repository, identifier, changes, { ci: { skip: true } })
|
2020-02-18 10:08:51 -05:00
|
|
|
|
2020-03-04 19:07:49 -05:00
|
|
|
subject
|
|
|
|
end
|
2020-02-18 10:08:51 -05:00
|
|
|
|
2020-03-04 19:07:49 -05:00
|
|
|
it 'decreases the reference counter and returns the result' do
|
|
|
|
expect(Gitlab::ReferenceCounter).to receive(:new).with(gl_repository)
|
|
|
|
.and_return(reference_counter)
|
|
|
|
expect(reference_counter).to receive(:decrease).and_return(true)
|
2020-02-18 10:08:51 -05:00
|
|
|
|
2020-03-04 19:07:49 -05:00
|
|
|
expect(response.reference_counter_decreased).to be(true)
|
2020-02-18 10:08:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-04 19:07:49 -05:00
|
|
|
context 'with Project' do
|
|
|
|
it_behaves_like 'post_receive_service actions'
|
2020-02-18 10:08:51 -05:00
|
|
|
|
2020-03-04 19:07:49 -05:00
|
|
|
it 'returns link to create new merge request' do
|
|
|
|
message = <<~MESSAGE.strip
|
|
|
|
To create a merge request for #{branch_name}, visit:
|
|
|
|
http://#{Gitlab.config.gitlab.host}/#{project.full_path}/-/merge_requests/new?merge_request%5Bsource_branch%5D=#{branch_name}
|
|
|
|
MESSAGE
|
2020-02-18 10:08:51 -05:00
|
|
|
|
2020-03-04 19:07:49 -05:00
|
|
|
expect(subject).to include(build_basic_message(message))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the link to an existing merge request when it exists' do
|
|
|
|
merge_request = create(:merge_request, source_project: project, source_branch: branch_name, target_branch: 'master')
|
|
|
|
message = <<~MESSAGE.strip
|
|
|
|
View merge request for feature:
|
|
|
|
#{project_merge_request_url(project, merge_request)}
|
|
|
|
MESSAGE
|
2020-02-18 10:08:51 -05:00
|
|
|
|
2020-03-04 19:07:49 -05:00
|
|
|
expect(subject).to include(build_basic_message(message))
|
2020-02-18 10:08:51 -05:00
|
|
|
end
|
|
|
|
|
2020-03-04 19:07:49 -05:00
|
|
|
context 'when printing_merge_request_link_enabled is false' do
|
|
|
|
let(:project) { create(:project, printing_merge_request_link_enabled: false) }
|
2020-02-18 10:08:51 -05:00
|
|
|
|
2020-03-04 19:07:49 -05:00
|
|
|
it 'returns no merge request messages' do
|
|
|
|
expect(subject).to be_blank
|
|
|
|
end
|
2020-02-18 10:08:51 -05:00
|
|
|
end
|
|
|
|
|
2020-03-04 19:07:49 -05:00
|
|
|
it 'does not invoke MergeRequests::PushOptionsHandlerService' do
|
|
|
|
expect(MergeRequests::PushOptionsHandlerService).not_to receive(:new)
|
|
|
|
|
|
|
|
subject
|
2020-02-18 10:08:51 -05:00
|
|
|
end
|
|
|
|
|
2020-03-04 19:07:49 -05:00
|
|
|
context 'when there are merge_request push options' do
|
|
|
|
let(:params) { super().merge(push_options: ['merge_request.create']) }
|
2020-02-18 10:08:51 -05:00
|
|
|
|
2020-03-04 19:07:49 -05:00
|
|
|
before do
|
|
|
|
project.add_developer(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'invokes MergeRequests::PushOptionsHandlerService' do
|
|
|
|
expect(MergeRequests::PushOptionsHandlerService).to receive(:new).and_call_original
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a new merge request' do
|
|
|
|
expect { Sidekiq::Testing.fake! { subject } }.to change(MergeRequest, :count).by(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'links to the newly created merge request' do
|
|
|
|
message = <<~MESSAGE.strip
|
|
|
|
View merge request for #{branch_name}:
|
|
|
|
http://#{Gitlab.config.gitlab.host}/#{project.full_path}/-/merge_requests/1
|
|
|
|
MESSAGE
|
|
|
|
|
|
|
|
expect(subject).to include(build_basic_message(message))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds errors on the service instance to warnings' do
|
|
|
|
expect_any_instance_of(
|
|
|
|
MergeRequests::PushOptionsHandlerService
|
|
|
|
).to receive(:errors).at_least(:once).and_return(['my error'])
|
|
|
|
|
|
|
|
message = "WARNINGS:\nError encountered with push options 'merge_request.create': my error"
|
|
|
|
|
|
|
|
expect(subject).to include(build_alert_message(message))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds ActiveRecord errors on invalid MergeRequest records to warnings' do
|
|
|
|
invalid_merge_request = MergeRequest.new
|
|
|
|
invalid_merge_request.errors.add(:base, 'my error')
|
|
|
|
message = "WARNINGS:\nError encountered with push options 'merge_request.create': my error"
|
|
|
|
|
|
|
|
expect_any_instance_of(
|
|
|
|
MergeRequests::CreateService
|
|
|
|
).to receive(:execute).and_return(invalid_merge_request)
|
|
|
|
|
|
|
|
expect(subject).to include(build_alert_message(message))
|
|
|
|
end
|
2020-02-18 10:08:51 -05:00
|
|
|
end
|
2020-03-04 19:07:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'with PersonalSnippet' do
|
|
|
|
let(:gl_repository) { "snippet-#{personal_snippet.id}" }
|
|
|
|
let(:repository) { personal_snippet.repository }
|
2020-02-18 10:08:51 -05:00
|
|
|
|
2020-03-04 19:07:49 -05:00
|
|
|
it_behaves_like 'post_receive_service actions'
|
2020-02-18 10:08:51 -05:00
|
|
|
|
2020-03-04 19:07:49 -05:00
|
|
|
it 'does not return link to create new merge request' do
|
|
|
|
expect(subject).to be_empty
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not return the link to an existing merge request when it exists' do
|
|
|
|
create(:merge_request, source_project: project, source_branch: branch_name, target_branch: 'master')
|
2020-02-18 10:08:51 -05:00
|
|
|
|
2020-03-04 19:07:49 -05:00
|
|
|
expect(subject).to be_empty
|
2020-02-18 10:08:51 -05:00
|
|
|
end
|
2020-03-04 19:07:49 -05:00
|
|
|
end
|
2020-02-18 10:08:51 -05:00
|
|
|
|
2020-03-04 19:07:49 -05:00
|
|
|
context 'with ProjectSnippet' do
|
|
|
|
let(:gl_repository) { "snippet-#{project_snippet.id}" }
|
|
|
|
let(:repository) { project_snippet.repository }
|
2020-02-18 10:08:51 -05:00
|
|
|
|
2020-03-04 19:07:49 -05:00
|
|
|
it_behaves_like 'post_receive_service actions'
|
2020-02-18 10:08:51 -05:00
|
|
|
|
2020-03-04 19:07:49 -05:00
|
|
|
it 'does not return link to create new merge request' do
|
|
|
|
expect(subject).to be_empty
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not return the link to an existing merge request when it exists' do
|
|
|
|
create(:merge_request, source_project: project, source_branch: branch_name, target_branch: 'master')
|
|
|
|
|
|
|
|
expect(subject).to be_empty
|
2020-02-18 10:08:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-04 07:07:52 -05:00
|
|
|
context 'broadcast message banner exists' do
|
2020-02-18 10:08:51 -05:00
|
|
|
it 'outputs a broadcast message' do
|
2020-03-04 07:07:52 -05:00
|
|
|
broadcast_message = create(:broadcast_message)
|
2020-02-18 10:08:51 -05:00
|
|
|
|
|
|
|
expect(subject).to include(build_alert_message(broadcast_message.message))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-04 07:07:52 -05:00
|
|
|
context 'broadcast message notification exists' do
|
|
|
|
it 'does not output a broadcast message' do
|
|
|
|
create(:broadcast_message, :notification)
|
|
|
|
|
|
|
|
expect(has_alert_messages?(subject)).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-18 10:08:51 -05:00
|
|
|
context 'broadcast message does not exist' do
|
|
|
|
it 'does not output a broadcast message' do
|
|
|
|
expect(has_alert_messages?(subject)).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'nil broadcast message' do
|
|
|
|
it 'does not output a broadcast message' do
|
|
|
|
allow(BroadcastMessage).to receive(:current).and_return(nil)
|
|
|
|
|
|
|
|
expect(has_alert_messages?(subject)).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a redirected data' do
|
|
|
|
it 'returns redirected message on the response' do
|
2020-03-17 08:09:52 -04:00
|
|
|
project_moved = Gitlab::Checks::ProjectMoved.new(project.repository, user, 'http', 'foo/baz')
|
2020-02-18 10:08:51 -05:00
|
|
|
project_moved.add_message
|
|
|
|
|
|
|
|
expect(subject).to include(build_basic_message(project_moved.message))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with new project data' do
|
|
|
|
it 'returns new project message on the response' do
|
2020-03-17 08:09:52 -04:00
|
|
|
project_created = Gitlab::Checks::ProjectCreated.new(project.repository, user, 'http')
|
2020-02-18 10:08:51 -05:00
|
|
|
project_created.add_message
|
|
|
|
|
|
|
|
expect(subject).to include(build_basic_message(project_created.message))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-04 19:07:49 -05:00
|
|
|
describe '#process_mr_push_options' do
|
|
|
|
context 'when repository belongs to a snippet' do
|
|
|
|
context 'with PersonalSnippet' do
|
|
|
|
let(:repository) { personal_snippet.repository }
|
|
|
|
|
|
|
|
it 'returns an error message' do
|
|
|
|
result = service.process_mr_push_options(push_options, changes)
|
|
|
|
|
|
|
|
expect(result).to match('Push options are only supported for projects')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with ProjectSnippet' do
|
|
|
|
let(:repository) { project_snippet.repository }
|
|
|
|
|
|
|
|
it 'returns an error message' do
|
|
|
|
result = service.process_mr_push_options(push_options, changes)
|
|
|
|
|
|
|
|
expect(result).to match('Push options are only supported for projects')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#merge_request_urls' do
|
|
|
|
context 'when repository belongs to a snippet' do
|
|
|
|
context 'with PersonalSnippet' do
|
|
|
|
let(:repository) { personal_snippet.repository }
|
|
|
|
|
|
|
|
it 'returns an empty array' do
|
|
|
|
expect(service.merge_request_urls).to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with ProjectSnippet' do
|
|
|
|
let(:repository) { project_snippet.repository }
|
|
|
|
|
|
|
|
it 'returns an empty array' do
|
|
|
|
expect(service.merge_request_urls).to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-18 10:08:51 -05:00
|
|
|
def build_alert_message(message)
|
|
|
|
{ 'type' => 'alert', 'message' => message }
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_basic_message(message)
|
|
|
|
{ 'type' => 'basic', 'message' => message }
|
|
|
|
end
|
|
|
|
|
|
|
|
def has_alert_messages?(messages)
|
|
|
|
messages.any? do |message|
|
|
|
|
message['type'] == 'alert'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|