lib/uffizzi/services/preview_service.rb in uffizzi-cli-0.7.3 vs lib/uffizzi/services/preview_service.rb in uffizzi-cli-0.8.0
- old
+ new
@@ -3,37 +3,38 @@
require 'uffizzi/clients/api/api_client'
class PreviewService
class << self
include ApiClient
+
def read_deployment_id(deployment_name)
return nil unless deployment_name.start_with?('deployment-')
return nil unless deployment_name.split('-').size == 2
deployment_id = deployment_name.split('-').last
return nil if deployment_id.to_i.to_s != deployment_id
deployment_id
end
- def start_deploy_containers(project_slug, deployment, success_message)
+ def run_containers_deploy(project_slug, deployment)
deployment_id = deployment[:id]
params = { id: deployment_id }
response = deploy_containers(Uffizzi::ConfigFile.read_option(:server), project_slug, deployment_id, params)
- if Uffizzi::ResponseHelper.no_content?(response)
- Uffizzi.ui.say(success_message)
- create_deployment(deployment, project_slug)
- else
+ if !Uffizzi::ResponseHelper.no_content?(response)
Uffizzi::ResponseHelper.handle_failed_response(response)
end
+
+ activity_items = wait_containers_creation(deployment, project_slug)
+ wait_containers_deploy(deployment, project_slug, activity_items)
end
private
- def create_deployment(deployment, project_slug)
+ def wait_containers_creation(deployment, project_slug)
spinner = TTY::Spinner.new('[:spinner] Creating containers...', format: :dots)
spinner.auto_spin
activity_items = []
loop do
@@ -47,14 +48,14 @@
spinner.success
Uffizzi.ui.say('Done')
- display_containers_deploying_status(deployment, project_slug, activity_items)
+ activity_items
end
- def display_containers_deploying_status(deployment, project_slug, activity_items)
+ def wait_containers_deploy(deployment, project_slug, activity_items)
spinner = TTY::Spinner::Multi.new('[:spinner] Deploying preview...', format: :dots, style: {
middle: ' ',
bottom: ' ',
})
@@ -62,24 +63,17 @@
loop do
response = get_activity_items(Uffizzi::ConfigFile.read_option(:server), project_slug, deployment[:id])
handle_activity_items_response(response, spinner)
activity_items = response[:body][:activity_items]
- check_activity_items_state(activity_items, containers_spinners)
+ update_containers_spinners!(activity_items, containers_spinners)
break if activity_items.all? { |activity_item| activity_item[:state] == 'deployed' || activity_item[:state] == 'failed' }
sleep(5)
end
- if Uffizzi.ui.output_format.nil?
- Uffizzi.ui.say('Done')
- preview_url = "https://#{deployment[:preview_url]}"
- Uffizzi.ui.say(preview_url) if spinner.success?
- else
- output_data = build_output_data(deployment)
- Uffizzi.ui.output(output_data)
- end
+ spinner.success?
end
def create_containers_spinners(activity_items, spinner)
activity_items.map do |activity_item|
container_spinner = spinner.register("[:spinner] #{activity_item[:name]}")
@@ -89,14 +83,15 @@
spinner: container_spinner,
}
end
end
- def check_activity_items_state(activity_items, containers_spinners)
+ def update_containers_spinners!(activity_items, containers_spinners)
finished_activity_items = activity_items.filter do |activity_item|
activity_item[:state] == 'deployed' || activity_item[:state] == 'failed'
end
+
finished_activity_items.each do |activity_item|
container_spinner = containers_spinners.detect { |spinner| spinner[:name] == activity_item[:name] }
spinner = container_spinner[:spinner]
case activity_item[:state]
when 'deployed'
@@ -110,15 +105,8 @@
def handle_activity_items_response(response, spinner)
unless Uffizzi::ResponseHelper.ok?(response)
spinner.error
Uffizzi::ResponseHelper.handle_failed_response(response)
end
- end
-
- def build_output_data(output_data)
- {
- id: "deployment-#{output_data[:id]}",
- url: "https://#{output_data[:preview_url]}",
- }
end
end
end