Commit 1664354c authored by Kamil Trzcinski's avatar Kamil Trzcinski

Update changes

parent a8825f0c
...@@ -54,7 +54,8 @@ class Projects::EnvironmentsController < Projects::ApplicationController ...@@ -54,7 +54,8 @@ class Projects::EnvironmentsController < Projects::ApplicationController
def stop def stop
return render_404 unless @environment.available? return render_404 unless @environment.available?
stop_action = @environment.run_stop!(current_user) stop_action = @environment.stop_with_action!(current_user)
if stop_action if stop_action
redirect_to polymorphic_path([project.namespace.becomes(Namespace), project, stop_action]) redirect_to polymorphic_path([project.namespace.becomes(Namespace), project, stop_action])
else else
......
...@@ -451,7 +451,7 @@ class Projects::MergeRequestsController < Projects::ApplicationController ...@@ -451,7 +451,7 @@ class Projects::MergeRequestsController < Projects::ApplicationController
deployment = environment.first_deployment_for(@merge_request.diff_head_commit) deployment = environment.first_deployment_for(@merge_request.diff_head_commit)
stop_url = stop_url =
if environment.can_run_stop_action? && can?(current_user, :create_deployment, environment) if environment.stoppable? && can?(current_user, :create_deployment, environment)
stop_namespace_project_environment_path(project.namespace, project, environment) stop_namespace_project_environment_path(project.namespace, project, environment)
end end
......
...@@ -110,15 +110,15 @@ class Environment < ActiveRecord::Base ...@@ -110,15 +110,15 @@ class Environment < ActiveRecord::Base
external_url.gsub(/\A.*?:\/\//, '') external_url.gsub(/\A.*?:\/\//, '')
end end
def can_run_stop_action? def stop_action?
available? && stop_action.present? available? && stop_action.present?
end end
def run_stop!(current_user) def stop_with_action!(current_user)
return unless available? return unless available?
stop stop!
stop_action.play(current_user) stop_action.play(current_user) if stop_action
end end
def actions_for(environment) def actions_for(environment)
......
...@@ -7,7 +7,7 @@ class EnvironmentEntity < Grape::Entity ...@@ -7,7 +7,7 @@ class EnvironmentEntity < Grape::Entity
expose :external_url expose :external_url
expose :environment_type expose :environment_type
expose :last_deployment, using: DeploymentEntity expose :last_deployment, using: DeploymentEntity
expose :can_run_stop_action? expose :stoppable?
expose :environment_path do |environment| expose :environment_path do |environment|
namespace_project_environment_path( namespace_project_environment_path(
......
...@@ -8,10 +8,9 @@ module Ci ...@@ -8,10 +8,9 @@ module Ci
return unless has_ref? return unless has_ref?
environments.each do |environment| environments.each do |environment|
next unless environment.stoppable?
next unless can?(current_user, :create_deployment, project) next unless can?(current_user, :create_deployment, project)
environment.stop!(current_user) environment.stop_with_action!(current_user)
end end
end end
......
- if can?(current_user, :create_deployment, environment) && environment.can_run_stop_action? - if can?(current_user, :create_deployment, environment) && environment.stoppable?
.inline .inline
= link_to stop_namespace_project_environment_path(@project.namespace, @project, environment), method: :post, = 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 class: 'btn stop-env-link', rel: 'nofollow', data: { confirm: 'Are you sure you want to stop this environment?' } do
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
= render 'projects/environments/external_url', environment: @environment = render 'projects/environments/external_url', environment: @environment
- if can?(current_user, :update_environment, @environment) - if can?(current_user, :update_environment, @environment)
= link_to 'Edit', edit_namespace_project_environment_path(@project.namespace, @project, @environment), class: 'btn' = link_to 'Edit', edit_namespace_project_environment_path(@project.namespace, @project, @environment), class: 'btn'
- if can?(current_user, :create_deployment, @environment) && @environment.available? - if can?(current_user, :create_deployment, @environment) && @environment.can_stop?
= 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 = 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 .deployments-container
......
...@@ -64,10 +64,6 @@ feature 'Environment', :feature do ...@@ -64,10 +64,6 @@ feature 'Environment', :feature do
expect(page).to have_link('Re-deploy') expect(page).to have_link('Re-deploy')
end end
scenario 'does not show stop button' do
expect(page).not_to have_link('Stop')
end
scenario 'does not show terminal button' do scenario 'does not show terminal button' do
expect(page).not_to have_terminal_button expect(page).not_to have_terminal_button
end end
...@@ -116,6 +112,7 @@ feature 'Environment', :feature do ...@@ -116,6 +112,7 @@ feature 'Environment', :feature do
end end
end end
context 'when environment is available' do
context 'with stop action' do context 'with stop action' do
given(:manual) { create(:ci_build, :manual, pipeline: pipeline, name: 'close_app') } given(:manual) { create(:ci_build, :manual, pipeline: pipeline, name: 'close_app') }
given(:deployment) { create(:deployment, environment: environment, deployable: build, on_stop: 'close_app') } given(:deployment) { create(:deployment, environment: environment, deployable: build, on_stop: 'close_app') }
...@@ -138,6 +135,21 @@ feature 'Environment', :feature do ...@@ -138,6 +135,21 @@ feature 'Environment', :feature do
end end
end end
end end
context 'without stop action' do
scenario 'does allow to stop environment' do
click_link('Stop')
end
end
end
context 'when environment is stopped' do
given(:environment) { create(:environment, project: project, state: :stopped) }
scenario 'does not show stop button' do
expect(page).not_to have_link('Stop')
end
end
end end
end end
end end
......
...@@ -112,8 +112,8 @@ describe Environment, models: true do ...@@ -112,8 +112,8 @@ describe Environment, models: true do
end end
end end
describe '#can_run_stop_action?' do describe '#stoppable?' do
subject { environment.can_run_stop_action? } subject { environment.stoppable? }
context 'when no other actions' do context 'when no other actions' do
it { is_expected.to be_falsey } it { is_expected.to be_falsey }
...@@ -142,10 +142,10 @@ describe Environment, models: true do ...@@ -142,10 +142,10 @@ describe Environment, models: true do
end end
end end
describe '#run_stop!' do describe '#stop_with_action!' do
let(:user) { create(:user) } let(:user) { create(:user) }
subject { environment.run_stop!(user) } subject { environment.stop_with_action!(user) }
before do before do
expect(environment).to receive(:available?).and_call_original expect(environment).to receive(:available?).and_call_original
......
...@@ -42,10 +42,10 @@ describe Ci::StopEnvironmentsService, services: true do ...@@ -42,10 +42,10 @@ describe Ci::StopEnvironmentsService, services: true do
end end
end end
context 'when environment is not stoppable' do context 'when environment is not stopped' do
before do before do
allow_any_instance_of(Environment) allow_any_instance_of(Environment)
.to receive(:stoppable?).and_return(false) .to receive(:state).and_return(:stopped)
end end
it 'does not stop environment' do it 'does not stop environment' 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