Commit 04a8372c authored by Grzegorz Bizon's avatar Grzegorz Bizon

Add environment teardown service

parent e671cd89
module Ci
class StopEnvironmentService < BaseService
def execute(ref)
@ref = ref
@commit = project.commit(ref)
return unless has_ref_sha_pair?
return unless has_environments?
environments.each do |environment|
next unless environment.stoppable?
environment.stop!(current_user)
end
end
private
def has_ref_sha_pair?
@ref && @commit
end
def has_environments?
environments.any?
end
def environments
@environments ||= project.environments_for(@ref, @commit)
end
end
end
...@@ -21,6 +21,8 @@ class DeleteBranchService < BaseService ...@@ -21,6 +21,8 @@ class DeleteBranchService < BaseService
return error('You dont have push access to repo', 405) return error('You dont have push access to repo', 405)
end end
# StopEnvironmentService
if repository.rm_branch(current_user, branch_name) if repository.rm_branch(current_user, branch_name)
success('Branch was removed') success('Branch was removed')
else else
......
require 'spec_helper'
describe Ci::StopEnvironmentService, services: true do
let(:project) { create(:project) }
let(:user) { create(:user) }
let(:service) { described_class.new(project, user) }
describe '#execute' do
context 'when environment exists' do
let(:environment) { create(:environment, project: project) }
let(:deployable) { create(:ci_build) }
let(:stop_build) do
create(:ci_build, :manual, name: 'environment/teardown',
pipeline: deployable.pipeline)
end
before do
create(:deployment, environment: environment,
deployable: deployable,
on_stop: stop_build.name,
user: user,
project: project,
sha: project.commit.id)
end
it 'stops environment' do
expect_any_instance_of(Environment).to receive(:stop!)
service.execute('master')
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