Skip to content

Commit

Permalink
add Import generator to import PRO editor diagrams via the API.
Browse files Browse the repository at this point in the history
  • Loading branch information
apotonick committed Mar 5, 2024
1 parent 22c82d5 commit d8a504e
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ $ rake test_2

Tests if `wtf?` works without any PRO configuration, but PRO is added.

```ruby
$ rake test_3
```

Tests that the PRO editor import generator works.

### Architecture

Expand Down
1 change: 1 addition & 0 deletions lib/trailblazer/pro/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ module Rails
require "trailblazer/pro/rails/railtie"
require "trailblazer/pro/rails/generator"
require "trailblazer/pro/rails/wtf"
require "trailblazer/pro/rails/generator/import"
24 changes: 24 additions & 0 deletions lib/trailblazer/pro/rails/generator/import.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Trailblazer
module Pro
class Import < ::Rails::Generators::Base
# source_root File.expand_path("templates", __dir__)
argument :slug, type: :string, default: nil, banner: "Diagram slug"
argument :target, type: :string, default: nil, banner: "Target filename"

def import_file_via_api
# TODO: use endpoint logic here.
signal, (ctx, _) = Trailblazer::Developer.wtf?(Trailblazer::Pro::Editor::Import, [
{
diagram_slug: slug,
target_filename: target,
session: Trailblazer::Pro::Session.session
},
{}
])

return puts("Diagram #{slug} successfully imported to #{target}.") if signal.to_h[:semantic] == :success
raise "error: #{ctx[:error_message]}"
end
end
end
end
34 changes: 34 additions & 0 deletions test/dummies/uninitialized/test/import_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require_relative "test_helper"

class GeneratorTest < Minitest::Spec
let(:api_key) { "tpka_909ae987_c834_43e4_9869_2eefd2aa9bcf" }
let(:trailblazer_pro_host) { "https://testbackend-pro.trb.to" }

after do
# Dir.chdir("test/dummies/uninitialized") do
# `rm tmp/trb-pro` #if File.exist?("tmp/trb-pro")
# end
`rm /tmp/b0f945.json`
end

it "retrieve document" do
Dir.chdir("test/dummies/uninitialized") do
cli = File.popen({"BUNDLE_GEMFILE" => "../Gemfile"}, "bin/rails.rb g trailblazer:pro:install", "r+")
cli.write "#{api_key}\n"
cli.close

json = File.read("tmp/trb-pro/session")
json = json.sub("https://pro.trailblazer.to", trailblazer_pro_host)
File.write("tmp/trb-pro/session", json) # FIXME: what do you mean? This is super clean :D


cli = File.popen({"BUNDLE_GEMFILE" => "../Gemfile"}, "bin/rails.rb g trailblazer:pro:import b0f945 /tmp/b0f945.json", "r+")
assert_equal cli.readlines.last, %(Diagram b0f945 successfully imported to /tmp/b0f945.json.\n)
cli.close

assert File.exist?("/tmp/b0f945.json")
# TODO: use template from trailblazer-pro test suite.
assert_equal File.read("/tmp/b0f945.json"), %({\"id\":3,\"type\":\"collaboration\",\"lanes\":[{\"id\":\"lifecycle\",\"type\":\"lane\",\"elements\":[{\"id\":\"Activity_0dgrwre\",\"label\":\"Create\",\"type\":\"task\",\"data\":{},\"links\":[{\"target_id\":\"throw-after-Activity_0dgrwre\",\"semantic\":\"success\"}]},{\"id\":\"catch-before-Activity_0dgrwre\",\"label\":null,\"type\":\"catch_event\",\"data\":{\"start_task\":true},\"links\":[{\"target_id\":\"Activity_0dgrwre\",\"semantic\":\"success\"}]},{\"id\":\"throw-after-Activity_0dgrwre\",\"label\":null,\"type\":\"throw_event\",\"data\":{},\"links\":[]},{\"id\":\"suspend-gw-to-catch-before-Activity_0dgrwre\",\"label\":null,\"type\":\"suspend\",\"data\":{\"resumes\":[\"catch-before-Activity_0dgrwre\"]},\"links\":[]}]}],\"messages\":[]})
end
end
end

0 comments on commit d8a504e

Please sign in to comment.