2019-08-22 06:57:44 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-11-21 11:26:35 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe Gitlab::SlashCommands::Deploy do
|
2016-11-21 11:26:35 -05:00
|
|
|
describe '#execute' do
|
2019-12-20 22:07:37 -05:00
|
|
|
let(:project) { create(:project, :repository) }
|
2016-11-21 11:26:35 -05:00
|
|
|
let(:user) { create(:user) }
|
2018-02-22 12:34:04 -05:00
|
|
|
let(:chat_name) { double(:chat_name, user: user) }
|
2016-11-21 11:26:35 -05:00
|
|
|
let(:regex_match) { described_class.match('deploy staging to production') }
|
|
|
|
|
|
|
|
before do
|
2017-05-31 05:09:29 -04:00
|
|
|
# Make it possible to trigger protected manual actions for developers.
|
|
|
|
#
|
|
|
|
project.add_developer(user)
|
|
|
|
|
|
|
|
create(:protected_branch, :developers_can_merge,
|
|
|
|
name: 'master', project: project)
|
2016-11-21 11:26:35 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
subject do
|
2018-02-22 12:34:04 -05:00
|
|
|
described_class.new(project, chat_name).execute(regex_match)
|
2016-11-21 11:26:35 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'if no environment is defined' do
|
2017-01-10 13:43:58 -05:00
|
|
|
it 'does not execute an action' do
|
|
|
|
expect(subject[:response_type]).to be(:ephemeral)
|
2017-07-28 06:16:07 -04:00
|
|
|
expect(subject[:text]).to eq "Couldn't find a deployment manual action."
|
2016-11-21 11:26:35 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with environment' do
|
|
|
|
let!(:staging) { create(:environment, name: 'staging', project: project) }
|
2017-04-06 07:40:04 -04:00
|
|
|
let!(:pipeline) { create(:ci_pipeline, project: project) }
|
|
|
|
let!(:build) { create(:ci_build, pipeline: pipeline) }
|
2018-11-04 19:37:40 -05:00
|
|
|
let!(:deployment) { create(:deployment, :success, environment: staging, deployable: build) }
|
2016-11-21 11:26:35 -05:00
|
|
|
|
|
|
|
context 'without actions' do
|
2017-01-10 13:43:58 -05:00
|
|
|
it 'does not execute an action' do
|
|
|
|
expect(subject[:response_type]).to be(:ephemeral)
|
2017-07-28 06:16:07 -04:00
|
|
|
expect(subject[:text]).to eq "Couldn't find a deployment manual action."
|
2016-11-21 11:26:35 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-28 04:56:50 -04:00
|
|
|
context 'when single action has been matched' do
|
|
|
|
before do
|
2017-04-06 07:40:04 -04:00
|
|
|
create(:ci_build, :manual, pipeline: pipeline,
|
|
|
|
name: 'first',
|
|
|
|
environment: 'production')
|
2016-11-21 11:26:35 -05:00
|
|
|
end
|
|
|
|
|
2016-11-21 13:40:04 -05:00
|
|
|
it 'returns success result' do
|
2017-01-10 13:43:58 -05:00
|
|
|
expect(subject[:response_type]).to be(:in_channel)
|
2017-07-28 04:56:50 -04:00
|
|
|
expect(subject[:text])
|
|
|
|
.to start_with('Deployment started from staging to production')
|
2016-11-21 11:26:35 -05:00
|
|
|
end
|
2017-07-28 04:56:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when more than one action has been matched' do
|
|
|
|
context 'when there is no specific actions with a environment name' do
|
|
|
|
before do
|
|
|
|
create(:ci_build, :manual, pipeline: pipeline,
|
|
|
|
name: 'first',
|
|
|
|
environment: 'production')
|
2016-11-21 11:26:35 -05:00
|
|
|
|
2017-04-06 07:40:04 -04:00
|
|
|
create(:ci_build, :manual, pipeline: pipeline,
|
|
|
|
name: 'second',
|
|
|
|
environment: 'production')
|
2016-11-21 11:26:35 -05:00
|
|
|
end
|
|
|
|
|
2017-07-28 04:56:50 -04:00
|
|
|
it 'returns error about too many actions defined' do
|
2017-07-28 06:16:07 -04:00
|
|
|
expect(subject[:text]).to eq("Couldn't find a deployment manual action.")
|
2017-01-10 13:43:58 -05:00
|
|
|
expect(subject[:response_type]).to be(:ephemeral)
|
2016-11-21 11:26:35 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-28 04:56:50 -04:00
|
|
|
context 'when one of the actions is environement specific action' do
|
|
|
|
before do
|
|
|
|
create(:ci_build, :manual, pipeline: pipeline,
|
|
|
|
name: 'first',
|
|
|
|
environment: 'production')
|
|
|
|
|
|
|
|
create(:ci_build, :manual, pipeline: pipeline,
|
|
|
|
name: 'production',
|
|
|
|
environment: 'production')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'deploys to production' do
|
|
|
|
expect(subject[:text])
|
|
|
|
.to start_with('Deployment started from staging to production')
|
|
|
|
expect(subject[:response_type]).to be(:in_channel)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when one of the actions is a teardown action' do
|
|
|
|
before do
|
|
|
|
create(:ci_build, :manual, pipeline: pipeline,
|
|
|
|
name: 'first',
|
|
|
|
environment: 'production')
|
|
|
|
|
2016-11-21 12:22:03 -05:00
|
|
|
create(:ci_build, :manual, :teardown_environment,
|
2017-04-06 07:40:04 -04:00
|
|
|
pipeline: pipeline, name: 'teardown', environment: 'production')
|
2016-11-21 11:26:35 -05:00
|
|
|
end
|
|
|
|
|
2017-07-28 04:56:50 -04:00
|
|
|
it 'deploys to production' do
|
|
|
|
expect(subject[:text])
|
|
|
|
.to start_with('Deployment started from staging to production')
|
2017-01-10 13:43:58 -05:00
|
|
|
expect(subject[:response_type]).to be(:in_channel)
|
2016-11-21 11:26:35 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'self.match' do
|
|
|
|
it 'matches the environment' do
|
|
|
|
match = described_class.match('deploy staging to production')
|
|
|
|
|
|
|
|
expect(match[:from]).to eq('staging')
|
|
|
|
expect(match[:to]).to eq('production')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|