Skip to content

Commit

Permalink
add Task::Import and tests, so we can pull diagrams from the PRO server.
Browse files Browse the repository at this point in the history
  • Loading branch information
apotonick committed Jun 4, 2024
1 parent add1bb4 commit 1aeaa16
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
40 changes: 40 additions & 0 deletions lib/trailblazer/workflow/task/import.rb
Original file line number Diff line number Diff line change
@@ -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
32 changes: 32 additions & 0 deletions test/import_task_test.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 1aeaa16

Please sign in to comment.