Commit 34bd1bfb authored by Rémy Coutable's avatar Rémy Coutable

Fetch environment before checking its `state`

This is because `state` isn't exposed from the nested `environment` in
the deployments endpoint. We need to fetch the environment details in
order to access its state.
Signed-off-by: default avatarRémy Coutable <remy@rymai.me>
parent 0765ffdc
...@@ -81,10 +81,13 @@ class AutomatedCleanup ...@@ -81,10 +81,13 @@ class AutomatedCleanup
release = Quality::HelmClient::Release.new(environment.slug, 1, deployed_at.to_s, nil, nil, review_apps_namespace) release = Quality::HelmClient::Release.new(environment.slug, 1, deployed_at.to_s, nil, nil, review_apps_namespace)
releases_to_delete << release releases_to_delete << release
end end
elsif environment.state != 'stopped' && deployed_at < stop_threshold
stop_environment(environment, deployment)
else else
print_release_state(subject: 'Review App', release_name: environment.slug, release_date: last_deploy, action: 'leaving') if deployed_at >= stop_threshold
print_release_state(subject: 'Review App', release_name: environment.slug, release_date: last_deploy, action: 'leaving')
else
environment_state = fetch_environment(environment)&.state
stop_environment(environment, deployment) if environment_state && environment_state != 'stopped'
end
end end
checked_environments << environment.slug checked_environments << environment.slug
...@@ -116,12 +119,19 @@ class AutomatedCleanup ...@@ -116,12 +119,19 @@ class AutomatedCleanup
private private
def fetch_environment(environment)
gitlab.environment(project_path, environment.id)
rescue Errno::ETIMEDOUT => ex
puts "Failed to fetch '#{environment.name}' / '#{environment.slug}' (##{environment.id}):\n#{ex.message}"
nil
end
def delete_environment(environment, deployment) def delete_environment(environment, deployment)
print_release_state(subject: 'Review app', release_name: environment.slug, release_date: deployment.created_at, action: 'deleting') print_release_state(subject: 'Review app', release_name: environment.slug, release_date: deployment.created_at, action: 'deleting')
gitlab.delete_environment(project_path, environment.id) gitlab.delete_environment(project_path, environment.id)
rescue Gitlab::Error::Forbidden rescue Gitlab::Error::Forbidden
puts "Review app '#{environment.slug}' is forbidden: skipping it" puts "Review app '#{environment.name}' / '#{environment.slug}' (##{environment.id}) is forbidden: skipping it"
end end
def stop_environment(environment, deployment) def stop_environment(environment, deployment)
...@@ -129,7 +139,7 @@ class AutomatedCleanup ...@@ -129,7 +139,7 @@ class AutomatedCleanup
gitlab.stop_environment(project_path, environment.id) gitlab.stop_environment(project_path, environment.id)
rescue Gitlab::Error::Forbidden rescue Gitlab::Error::Forbidden
puts "Review app '#{environment.slug}' is forbidden: skipping it" puts "Review app '#{environment.name}' / '#{environment.slug}' (##{environment.id}) is forbidden: skipping it"
end end
def helm_releases def helm_releases
......
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