Skip to content

Commit

Permalink
Get ImportOrganization spec passing
Browse files Browse the repository at this point in the history
  • Loading branch information
azimux committed Apr 8, 2024
1 parent dee42c2 commit 2d0b080
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 26 deletions.
1 change: 0 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ gemspec

# TODO: move this to .gemspec
gem "foobara", git: "foobara", branch: "main"

gem "foobara-dotenv-loader", github: "foobara/dotenv-loader"
gem "foobara-util", github: "foobara/util"

Expand Down
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ GIT

GIT
remote: https://github.com/foobara/util.git
revision: ebf430ec00a389ec11e7f8e233131b1b36a1b6f1
revision: 659d4e4fd5dd74ca005650fc4bcc2ac1ee7809ec
specs:
foobara-util (0.1.0)

Expand Down Expand Up @@ -176,7 +176,7 @@ GEM
rubocop (~> 1.41)
rubocop-rake (0.6.0)
rubocop (~> 1.0)
rubocop-rspec (2.29.0)
rubocop-rspec (2.29.1)
rubocop (~> 1.40)
rubocop-capybara (~> 2.17)
rubocop-factory_bot (~> 2.22)
Expand Down
11 changes: 9 additions & 2 deletions spec/foobara/remote_imports/import_organization_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,18 @@
let(:errors_hash) { outcome.errors_hash }

let(:inputs) do
{ foo: "bar" }
{
raw_manifest:,
to_import:
}
end
let(:to_import) { "SomeOrg" }
let(:raw_manifest) { JSON.parse(manifest_json) }
let(:manifest_json) { File.read("#{__dir__}/../../fixtures/foobara-manifest.json") }

it "is successful" do
expect(outcome).to be_success
expect(result).to eq("bar")
expect(result).to eq([SomeOrg])
expect(SomeOrg.foobara_organization?).to be true
end
end
2 changes: 1 addition & 1 deletion src/foobara/remote_imports/already_imported.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def <<(manifest)
end

def to_key(manifest)
[manifest.category.to_sym, manifest.reference.to_sym]
[manifest.path.first.to_sym, manifest.reference.to_sym]
end

def already_imported?(manifest)
Expand Down
49 changes: 30 additions & 19 deletions src/foobara/remote_imports/import_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
module Foobara
module RemoteImports
module ImportBase
class BadManifestInputs < RuntimeError
class BadManifestInputsError < RuntimeError
class << self
def context_type_declaration
{}
end
end
end

class NotFound < RuntimeError
# TODO: why do we need this Value:: prefix?
class NotFoundError < Value::DataError
class << self
def context_type_declaration
{ not_found: [:string] }
Expand All @@ -21,16 +22,23 @@ def context_type_declaration

class << self
def included(klass)
cat = klass.name.match(/Import(\w+)$/)[1]
klass.category = Util.underscore_sym(cat)
category = klass.name.match(/Import(\w+)$/)[1]
category = Util.underscore_sym(category)

klass.singleton_class.define_method :category do
category
end

klass.possible_input_error :to_import, NotFoundError
klass.possible_error BadManifestInputsError

klass.inputs(
manifest_url: :string,
raw_manifest: :string,
cache: {type: :boolean, default: true},
cache_path: {type: :string, default: "tmp/cache/foobara-remote-imports"},
raw_manifest: :associative_array,
cache: { type: :boolean, default: true },
cache_path: { type: :string, default: "tmp/cache/foobara-remote-imports" },
to_import: :duck,
already_imported: {type: :duck, :allow_nil}
already_imported: { type: :duck, allow_nil: true }
)

klass.result :duck
Expand All @@ -39,7 +47,8 @@ def included(klass)

def execute
load_manifest
cache_manifest

cache_manifest if should_cache?
determine_manifests_to_import
filter_manifests_to_import
import_objects_from_manifests
Expand Down Expand Up @@ -88,7 +97,7 @@ def load_manifest_from_url
response.body
else
raise "Could not get manifest from #{url}: " \
"#{response.code} #{response.message}"
"#{response.code} #{response.message}"
end

JSON.parse(manifest_json)
Expand All @@ -102,7 +111,7 @@ def filter_manifests_to_import
not_found = filter - manifests_to_import.map(&:reference)

if not_found.any?
add_input_error :to_import, :organization_not_found, not_found:
add_input_error :to_import, :not_found, "Could not find #{not_found}", not_found:
end

self.manifests_to_import = manifests_to_import.select do |manifest|
Expand All @@ -115,10 +124,12 @@ def root_manifest
end

def cache_manifest
if cache
FileUtils.mkdir_p(cache_path)
File.write(cache_file_path, manifest_data.to_json)
end
FileUtils.mkdir_p(cache_path)
File.write(cache_file_path, manifest_data.to_json)
end

def should_cache?
cache && manifest_url && !cached?
end

def cached?
Expand All @@ -131,11 +142,11 @@ def load_cached_manifest

def cache_key
@cache_key ||= begin
hash = Digest::MD5.hexdigest(manifest_url)
escaped_url = manifest_url.gsub(/[^\w]/, "_")
hash = Digest::MD5.hexdigest(manifest_url)
escaped_url = manifest_url.gsub(/[^\w]/, "_")

"#{hash}_#{escaped_url}"
end
"#{hash}_#{escaped_url}"
end
end

def cache_file_path
Expand Down
3 changes: 3 additions & 0 deletions src/foobara/remote_imports/import_command.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
require_relative "import_domain"
require_relative "import_type"

module Foobara
module RemoteImports
class ImportCommand < Command
Expand Down
4 changes: 3 additions & 1 deletion src/foobara/remote_imports/import_organization.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
require_relative "import_base"

module Foobara
module RemoteImports
class ImportOrganization < Command
include ImportBase

def find_manifests_to_import
root_manifest.organizations
root_manifest.organizations
end

def import_object_from_manifest(manifest)
Expand Down

0 comments on commit 2d0b080

Please sign in to comment.