Commit 0aa23270 authored by Kamil Trzcinski's avatar Kamil Trzcinski

Add `stop!` to `environment`

parent 66ff67b0
...@@ -9,8 +9,12 @@ class Projects::EnvironmentsController < Projects::ApplicationController ...@@ -9,8 +9,12 @@ class Projects::EnvironmentsController < Projects::ApplicationController
def index def index
@scope = params[:scope] @scope = params[:scope]
@all_environments = project.environments @all_environments = project.environments
@environments = @scope == 'stopped' ? @environments =
@all_environments.stopped : @all_environments.available if @scope == 'stopped' then
@all_environments.stopped
else
@all_environments.available
end
end end
def show def show
...@@ -45,8 +49,7 @@ class Projects::EnvironmentsController < Projects::ApplicationController ...@@ -45,8 +49,7 @@ class Projects::EnvironmentsController < Projects::ApplicationController
def stop def stop
return render_404 unless @environment.stoppable? return render_404 unless @environment.stoppable?
action = @environment.stop_action new_action = @environment.stop!(current_user)
new_action = action.active? ? action : action.play(current_user)
redirect_to polymorphic_path([project.namespace.becomes(Namespace), project, new_action]) redirect_to polymorphic_path([project.namespace.becomes(Namespace), project, new_action])
end end
......
...@@ -88,4 +88,10 @@ class Environment < ActiveRecord::Base ...@@ -88,4 +88,10 @@ class Environment < ActiveRecord::Base
def stoppable? def stoppable?
available? && stop_action.present? available? && stop_action.present?
end end
def stop!(current_user)
return unless stoppable?
stop_action.play(current_user)
end
end end
...@@ -128,4 +128,41 @@ describe Environment, models: true do ...@@ -128,4 +128,41 @@ describe Environment, models: true do
end end
end end
end end
describe '#stop!' do
let(:user) { create(:user) }
subject { environment.stop!(user) }
before do
expect(environment).to receive(:stoppable?).and_call_original
end
context 'when no other actions' do
it { is_expected.to be_nil }
end
context 'when matching action is defined' do
let(:build) { create(:ci_build) }
let!(:deployment) { create(:deployment, environment: environment, deployable: build, on_stop: 'close_app') }
context 'when action did not yet finish' do
let!(:close_action) { create(:ci_build, :manual, pipeline: build.pipeline, name: 'close_app') }
it 'returns the same action' do
is_expected.to eq(close_action)
is_expected.to include(user: user)
end
end
context 'if action did finish' do
let!(:close_action) { create(:ci_build, :manual, :success, pipeline: build.pipeline, name: 'close_app') }
it 'returns a new action of the same type' do
is_expected.to be_persisted
is_expected.to include(name: close_action.name, user: user)
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