diff --git a/lib/trailblazer/workflow/task/import.rb b/lib/trailblazer/workflow/task/import.rb new file mode 100644 index 0000000..7756bb1 --- /dev/null +++ b/lib/trailblazer/workflow/task/import.rb @@ -0,0 +1,40 @@ +require "trailblazer/pro" + +module Trailblazer + module Workflow + module Task + class Import < Trailblazer::Activity::Railway + step Subprocess(Pro::Client::Connect), id: :connect, + Output(:failure) => End(:failure) + step :retrieve_document + fail :error_for_retrieve + step :store_document + + def retrieve_document(ctx, session:, diagram_slug:, **) + id_token = session.id_token + + ctx[:response] = response = Faraday.get( + "#{session.trailblazer_pro_host}/api/v1/diagrams/#{diagram_slug}/export", + {}, + {'Content-Type'=>'application/json', "Accept": "application/json", + "Authorization": "Bearer #{id_token}" + } + ) + + return false unless response.status == 200 # TODO: abstract this for other users, and use "endpoint" paths. + + ctx[:pro_json_document] = ctx[:response].body + end + + def store_document(ctx, pro_json_document:, target_filename:, **) + File.write(target_filename, pro_json_document) > 0 + end + + def error_for_retrieve(ctx, response:, diagram_slug:, **) + ctx[:error_message] = %(Diagram #{diagram_slug.inspect} couldn't be retrieved. HTTP status: #{response.status}) + end + end + + end # Task + end +end diff --git a/test/import_task_test.rb b/test/import_task_test.rb new file mode 100644 index 0000000..337a432 --- /dev/null +++ b/test/import_task_test.rb @@ -0,0 +1,32 @@ +require "test_helper" +require "trailblazer/workflow/task/import" # FIXME. + +class ImportTaskTest < Minitest::Spec + let(:api_key) { "tpka_909ae987_c834_43e4_9869_2eefd2aa9bcf" } + let(:trailblazer_pro_host) { "https://testbackend-pro.trb.to" } # NOTE: it's a bit clumsy to share PRO testing knowledge across several gems, but this code belongs here. + + let(:session_static_options) do + { + api_key: api_key, + trailblazer_pro_host: trailblazer_pro_host, + firebase_refresh_url: "https://securetoken.googleapis.com/v1/token?key=AIzaSyDVZOdUrI6wOji774hGU0yY_cQw9OAVwzs", + } + end # FIXME: do we need this? + + + it "Import retrieves editor JSON file" do + #@ Uninitialized sigin + initial_session = Trailblazer::Pro::Session::Uninitialized.new(trailblazer_pro_host: trailblazer_pro_host, api_key: api_key) + + # 79d163 /tmp/79d163.json + signal, (ctx, _) = Trailblazer::Developer.wtf?(Trailblazer::Workflow::Task::Import, [{ + session: initial_session, + diagram_slug: "79d163", + target_filename: "/tmp/79d163.json", + }, {}]) + + assert_equal signal.to_h[:semantic], :success + assert_equal File.read("/tmp/79d163.json").size, 4635 + # output, _ = capture_io do + end +end