BigW Consortium Gitlab

Commit 1494abe9 by Kamil Trzcinski

Allow to stop any environment

parent 13a680e3
......@@ -47,10 +47,14 @@ class Projects::EnvironmentsController < Projects::ApplicationController
end
def stop
return render_404 unless @environment.stoppable?
return render_404 unless @environment.available?
new_action = @environment.stop!(current_user)
redirect_to polymorphic_path([project.namespace.becomes(Namespace), project, new_action])
stop_action = @environment.run_stop!(current_user)
if stop_action
redirect_to polymorphic_path([project.namespace.becomes(Namespace), project, stop_action])
else
redirect_to namespace_project_environment_path(project.namespace, project, @environment)
end
end
private
......
......@@ -436,7 +436,7 @@ class Projects::MergeRequestsController < Projects::ApplicationController
deployment = environment.first_deployment_for(@merge_request.diff_head_commit)
stop_url =
if environment.stoppable? && can?(current_user, :create_deployment, environment)
if environment.can_run_stop_action? && can?(current_user, :create_deployment, environment)
stop_namespace_project_environment_path(project.namespace, project, environment)
end
......
......@@ -85,13 +85,18 @@ class Environment < ActiveRecord::Base
external_url.gsub(/\A.*?:\/\//, '')
end
def stoppable?
def can_run_stop_action?
available? && stop_action.present?
end
def stop!(current_user)
return unless stoppable?
def run_stop!(current_user)
return unless available?
stop_action.play(current_user)
if stop_action.present?
stop_action.play(current_user)
else
stop
nil
end
end
end
......@@ -7,7 +7,7 @@ class EnvironmentEntity < Grape::Entity
expose :external_url
expose :environment_type
expose :last_deployment, using: DeploymentEntity
expose :stoppable?
expose :can_run_stop_action?
expose :environment_url do |environment|
namespace_project_environment_url(
......
- if can?(current_user, :create_deployment, environment) && environment.stoppable?
- if can?(current_user, :create_deployment, environment) && environment.can_run_stop_action?
.inline
= link_to stop_namespace_project_environment_path(@project.namespace, @project, environment), method: :post,
class: 'btn stop-env-link', rel: 'nofollow', data: { confirm: 'Are you sure you want to stop this environment?' } do
......
......@@ -11,7 +11,7 @@
= render 'projects/environments/external_url', environment: @environment
- if can?(current_user, :update_environment, @environment)
= link_to 'Edit', edit_namespace_project_environment_path(@project.namespace, @project, @environment), class: 'btn'
- if can?(current_user, :create_deployment, @environment)
- if can?(current_user, :create_deployment, @environment) && @environment.available?
= link_to 'Stop', stop_namespace_project_environment_path(@project.namespace, @project, @environment), data: { confirm: 'Are you sure you want to stop this environment?' }, class: 'btn btn-danger', method: :post
.deployments-container
......
......@@ -149,6 +149,24 @@ feature 'Environments', feature: true do
scenario 'does show no deployments' do
expect(page).to have_content('You don\'t have any deployments right now.')
end
context 'for available environment' do
given(:environment) { create(:environment, project: project, state: :available) }
scenario 'does allow to stop environment' do
click_link('Stop')
expect(page).to have_content(environment.name.titleize)
end
end
context 'for stopped environment' do
given(:environment) { create(:environment, project: project, state: :stopped) }
scenario 'does not shows stop button' do
expect(page).not_to have_link('Stop')
end
end
end
context 'with deployments' do
......@@ -175,10 +193,6 @@ feature 'Environments', feature: true do
expect(page).to have_link('Re-deploy')
end
scenario 'does not show stop button' do
expect(page).not_to have_link('Stop')
end
context 'with manual action' do
given(:manual) { create(:ci_build, :manual, pipeline: pipeline, name: 'deploy to production') }
......
......@@ -99,8 +99,8 @@ describe Environment, models: true do
end
end
describe '#stoppable?' do
subject { environment.stoppable? }
describe '#can_run_stop_action?' do
subject { environment.can_run_stop_action? }
context 'when no other actions' do
it { is_expected.to be_falsey }
......@@ -129,17 +129,39 @@ describe Environment, models: true do
end
end
describe '#stop!' do
describe '#run_stop!' do
let(:user) { create(:user) }
subject { environment.stop!(user) }
subject { environment.run_stop!(user) }
before do
expect(environment).to receive(:stoppable?).and_call_original
expect(environment).to receive(:available?).and_call_original
end
context 'when no other actions' do
it { is_expected.to be_nil }
context 'environment is available' do
before do
environment.update(state: :available)
end
it do
subject
expect(environment).to be_stopped
end
end
context 'environment is already stopped' do
before do
environment.update(state: :stopped)
end
it do
subject
expect(environment).to be_stopped
end
end
end
context 'when matching action is defined' do
......
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