Skip to content
This repository has been archived by the owner on Jun 1, 2023. It is now read-only.

Commit

Permalink
Merge pull request #664 from Shopify/check-for-development-store-befo…
Browse files Browse the repository at this point in the history
…re-populate

Create SelectOrgAndShop Task
  • Loading branch information
carmelal authored Jul 15, 2020
2 parents 4e6d540 + a33438a commit dfad776
Show file tree
Hide file tree
Showing 21 changed files with 354 additions and 157 deletions.
57 changes: 3 additions & 54 deletions lib/project_types/node/forms/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ def ask
self.title ||= CLI::UI::Prompt.ask(ctx.message('node.forms.create.app_name'))
self.type = ask_type
self.name = self.title.downcase.split(" ").join("_")
self.organization_id ||= organization["id"].to_i
self.shop_domain ||= ask_shop_domain
res = ShopifyCli::Tasks::SelectOrgAndShop.call(ctx, organization_id: organization_id, shop_domain: shop_domain)
self.organization_id = res[:organization_id]
self.shop_domain = res[:shop_domain]
end

private
Expand All @@ -30,58 +31,6 @@ def ask_type
ctx.puts(ctx.message('node.forms.create.app_type.selected', type))
type
end

def organizations
@organizations ||= ShopifyCli::PartnersAPI::Organizations.fetch_all(ctx)
end

def organization
@organization ||= if !organization_id.nil?
org = ShopifyCli::PartnersAPI::Organizations.fetch(ctx, id: organization_id)
if org.nil?
ctx.puts(ctx.message('node.forms.create.authentication_issue', ShopifyCli::TOOL_NAME))
ctx.abort(ctx.message('node.forms.create.error.organization_not_found'))
end
org
elsif organizations.count == 0
ctx.puts(ctx.message('node.forms.create.partners_notice'))
ctx.puts(ctx.message('node.forms.create.authentication_issue', ShopifyCli::TOOL_NAME))
ctx.abort(ctx.message('node.forms.create.error.no_organizations'))
elsif organizations.count == 1
org = organizations.first
ctx.puts(ctx.message('node.forms.create.organization',
ctx.message('core.partners_api.org_name_and_id', org['businessName'], org['id'])))
org
else
org_id = CLI::UI::Prompt.ask(ctx.message('node.forms.create.organization_select')) do |handler|
organizations.each do |o|
handler.option(ctx.message('core.partners_api.org_name_and_id', o['businessName'], o['id'])) { o['id'] }
end
end
organizations.find { |o| o['id'] == org_id }
end
end

def ask_shop_domain
valid_stores = organization['stores'].select do |store|
store['transferDisabled'] == true || store['convertableToPartnerTest'] == true
end

if valid_stores.count == 0
ctx.puts(ctx.message('node.forms.create.no_development_stores'))
ctx.puts(ctx.message('node.forms.create.create_store', organization['id']))
ctx.puts(ctx.message('node.forms.create.authentication_issue', ShopifyCli::TOOL_NAME))
elsif valid_stores.count == 1
domain = valid_stores.first['shopDomain']
ctx.puts(ctx.message('node.forms.create.development_store', domain))
domain
else
CLI::UI::Prompt.ask(
ctx.message('node.forms.create.development_store_select'),
options: valid_stores.map { |s| s['shopDomain'] }
)
end
end
end
end
end
13 changes: 1 addition & 12 deletions lib/project_types/node/messages/messages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -233,25 +233,14 @@ module Messages
create: {
error: {
invalid_app_type: "Invalid app type %s",
organization_not_found: "Cannot find a partner organization with that ID",
no_organizations: "No partner organizations available.",
},

authentication_issue: "For authentication issues, run {{command:%s logout}} to clear invalid credentials",
partners_notice: "Please visit https://partners.shopify.com/ to create a partners account",
no_development_stores: "{{x}} No Development Stores available.",
create_store: "Visit {{underline:https://partners.shopify.com/%s/stores}} to create one",
app_name: "App Name",
app_name: "App name",
app_type: {
select: "What type of app are you building?",
select_public: "Public: An app built for a wide merchant audience.",
select_custom: "Custom: An app custom built for a single client.",
selected: "App type {{green:%s}}",
},
organization_select: "Select partner organization",
organization: "Partner organization {{green:%s}}",
development_store_select: "Select a development store",
development_store: "Using development store {{green:%s}}",
},
},
},
Expand Down
55 changes: 3 additions & 52 deletions lib/project_types/rails/forms/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ def ask
self.title ||= CLI::UI::Prompt.ask(ctx.message('rails.forms.create.app_name'))
self.type = ask_type
self.name = self.title.downcase.split(" ").join("_")
self.organization_id ||= organization["id"].to_i
self.shop_domain ||= ask_shop_domain
res = ShopifyCli::Tasks::SelectOrgAndShop.call(ctx, organization_id: organization_id, shop_domain: shop_domain)
self.organization_id = res[:organization_id]
self.shop_domain = res[:shop_domain]
self.db = ask_db
end

Expand All @@ -44,56 +45,6 @@ def ask_type
type
end

def organizations
@organizations ||= ShopifyCli::PartnersAPI::Organizations.fetch_all(ctx)
end

def organization
@organization ||= if !organization_id.nil?
org = ShopifyCli::PartnersAPI::Organizations.fetch(ctx, id: organization_id)
if org.nil?
ctx.puts(ctx.message('rails.forms.create.authentication_issue', ShopifyCli::TOOL_NAME))
ctx.abort(ctx.message('rails.forms.create.error.organization_not_found'))
end
org
elsif organizations.count == 0
ctx.puts(ctx.message('rails.forms.create.partners_notice'))
ctx.puts(ctx.message('rails.forms.create.authentication_issue', ShopifyCli::TOOL_NAME))
ctx.abort(ctx.message('rails.forms.create.error.no_organizations'))
elsif organizations.count == 1
org = organizations.first
ctx.puts(ctx.message('rails.forms.create.organization',
ctx.message('core.partners_api.org_name_and_id', org['businessName'], org['id'])))
org
else
org_id = CLI::UI::Prompt.ask(ctx.message('rails.forms.create.organization_select')) do |handler|
organizations.each { |o| handler.option(o['businessName']) { o['id'] } }
end
organizations.find { |o| o['id'] == org_id }
end
end

def ask_shop_domain
valid_stores = organization['stores'].select do |store|
store['transferDisabled'] == true || store['convertableToPartnerTest'] == true
end

if valid_stores.count == 0
ctx.puts(ctx.message('rails.forms.create.no_development_stores'))
ctx.puts(ctx.message('rails.forms.create.create_store', organization['id']))
ctx.puts(ctx.message('rails.forms.create.authentication_issue', ShopifyCli::TOOL_NAME))
elsif valid_stores.count == 1
domain = valid_stores.first['shopDomain']
ctx.puts(ctx.message('rails.forms.create.development_store', domain))
domain
else
CLI::UI::Prompt.ask(
ctx.message('rails.forms.create.development_store_select'),
options: valid_stores.map { |s| s['shopDomain'] }
)
end
end

def ask_db
if db.nil?
return 'sqlite3' unless CLI::UI::Prompt.confirm(ctx.message('rails.forms.create.db.want_select'),
Expand Down
11 changes: 0 additions & 11 deletions lib/project_types/rails/messages/messages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -236,14 +236,7 @@ module Messages
error: {
invalid_app_type: "Invalid app type %s",
invalid_db_type: "Invalid database type %s",
organization_not_found: "Cannot find a partner organization with that ID",
no_organizations: "No partner organizations available.",
},

authentication_issue: "For authentication issues, run {{command:%s logout}} to clear invalid credentials",
partners_notice: "Please visit https://partners.shopify.com/ to create a partners account",
no_development_stores: "{{x}} No development stores available.",
create_store: "Visit {{underline:https://partners.shopify.com/%s/stores}} to create one",
app_name: "App name",
app_type: {
select: "What type of app are you building?",
Expand Down Expand Up @@ -271,10 +264,6 @@ module Messages
select_jdbc: "JDBC",
selected: "Database Type {{green:%s}}",
},
organization_select: "Select partner organization",
organization: "Partner organization {{green:%s}}",
development_store_select: "Select a development store",
development_store: "Using development store {{green:%s}}",
},
},
},
Expand Down
3 changes: 1 addition & 2 deletions lib/shopify-cli/admin_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ class << self
#
# ShopifyCli::AdminAPI.query(@ctx, 'all_organizations')
#
def query(ctx, query_name, api_version: nil, shop: nil, **variables)
shop ||= Project.current.env.shop
def query(ctx, query_name, shop:, api_version: nil, **variables)
authenticated_req(ctx, shop) do
api_client(ctx, api_version, shop).query(query_name, variables: variables)
end
Expand Down
11 changes: 10 additions & 1 deletion lib/shopify-cli/admin_api/populate_resource_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ def call(args, _)
return @ctx.puts(output)
end

@shop ||= Project.current.env.shop || get_shop(@ctx)

if @silent
spin_group = CLI::UI::SpinGroup.new
spin_group.add(@ctx.message('core.populate.populating', @count, camel_case_resource_type)) do |spinner|
Expand Down Expand Up @@ -114,7 +116,7 @@ def schema

def run_mutation(data)
kwargs = { input: data }
kwargs[:shop] = @shop if @shop
kwargs[:shop] = @shop
resp = AdminAPI.query(
@ctx, "create_#{snake_case_resource_type}", kwargs
)
Expand Down Expand Up @@ -145,6 +147,13 @@ def price

private

def get_shop(ctx)
res = ShopifyCli::Tasks::SelectOrgAndShop.call(ctx)
domain = res[:shop_domain]
Project.current.env.update(ctx, :shop, domain)
domain
end

def camel_case_resource_type
@camel_case_resource_type ||= self.class.to_s.split('::').last
end
Expand Down
14 changes: 14 additions & 0 deletions lib/shopify-cli/messages/messages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,20 @@ module Messages
"{{x}} error: For authentication issues, run {{command:%s logout}} to clear invalid credentials",
update_prompt: "Do you want to update your application url?",
},
select_org_and_shop: {
authentication_issue: "For authentication issues, run {{command:%s logout}} to clear invalid credentials",
create_store: "Visit {{underline:https://partners.shopify.com/%s/stores}} to create one",
development_store: "Using development store {{green:%s}}",
development_store_select: "Select a development store",
error: {
no_development_stores: "{{x}} No Development Stores available.",
no_organizations: "No partner organizations available.",
organization_not_found: "Cannot find a partner organization with that ID",
partners_notice: "Please visit https://partners.shopify.com/ to create a partners account",
},
organization: "Partner organization {{green:%s (%s)}}",
organization_select: "Select partner organization",
},
},

tunnel: {
Expand Down
1 change: 1 addition & 0 deletions lib/shopify-cli/tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def self.register(task, name, path)
register :EnsureEnv, :ensure_env, 'shopify-cli/tasks/ensure_env'
register :EnsureLoopbackURL, :ensure_loopback_url, 'shopify-cli/tasks/ensure_loopback_url'
register :EnsureDevStore, :ensure_dev_store, 'shopify-cli/tasks/ensure_dev_store'
register :SelectOrgAndShop, :select_org_and_shop, 'shopify-cli/tasks/select_org_and_shop'
register :UpdateDashboardURLS, :update_dashboard_urls, 'shopify-cli/tasks/update_dashboard_urls'
end
end
77 changes: 77 additions & 0 deletions lib/shopify-cli/tasks/select_org_and_shop.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
require 'shopify_cli'

module ShopifyCli
module Tasks
class SelectOrgAndShop < ShopifyCli::Task
attr_reader :ctx

def call(ctx, organization_id: nil, shop_domain: nil)
@ctx = ctx
return response(organization_id.to_i, shop_domain) unless organization_id.nil? || shop_domain.nil?
org = get_organization(organization_id)
shop_domain ||= get_shop_domain(org)
response(org["id"].to_i, shop_domain)
end

private

def response(organization_id, shop_domain)
{
organization_id: organization_id,
shop_domain: shop_domain,
}
end

def organizations
@organizations ||= ShopifyCli::PartnersAPI::Organizations.fetch_all(ctx)
end

def get_organization(organization_id)
@organization ||= if !organization_id.nil?
org = ShopifyCli::PartnersAPI::Organizations.fetch(ctx, id: organization_id)
if org.nil?
ctx.puts(ctx.message('core.tasks.select_org_and_shop.error.authentication_issue', ShopifyCli::TOOL_NAME))
ctx.abort(ctx.message('core.tasks.select_org_and_shop.error.organization_not_found'))
end
org
elsif organizations.count == 0
ctx.puts(ctx.message('core.tasks.select_org_and_shop.error.partners_notice'))
ctx.puts(ctx.message('core.tasks.select_org_and_shop.authentication_issue', ShopifyCli::TOOL_NAME))
ctx.abort(ctx.message('core.tasks.select_org_and_shop.error.no_organizations'))
elsif organizations.count == 1
org = organizations.first
ctx.puts(ctx.message('core.tasks.select_org_and_shop.organization', org['businessName'], org['id']))
org
else
org_id = CLI::UI::Prompt.ask(ctx.message('core.tasks.select_org_and_shop.organization_select')) do |handler|
organizations.each do |o|
handler.option(ctx.message('core.partners_api.org_name_and_id', o['businessName'], o['id'])) { o['id'] }
end
end
organizations.find { |o| o['id'] == org_id }
end
end

def get_shop_domain(organization)
valid_stores = organization['stores'].select do |store|
store['transferDisabled'] == true || store['convertableToPartnerTest'] == true
end

if valid_stores.count == 0
ctx.puts(ctx.message('core.tasks.select_org_and_shop.error.no_development_stores'))
ctx.puts(ctx.message('core.tasks.select_org_and_shop.create_store', organization['id']))
ctx.puts(ctx.message('core.tasks.select_org_and_shop.authentication_issue', ShopifyCli::TOOL_NAME))
elsif valid_stores.count == 1
domain = valid_stores.first['shopDomain']
ctx.puts(ctx.message('core.tasks.select_org_and_shop.development_store', domain))
domain
else
CLI::UI::Prompt.ask(
ctx.message('core.tasks.select_org_and_shop.development_store_select'),
options: valid_stores.map { |s| s['shopDomain'] }
)
end
end
end
end
end
2 changes: 1 addition & 1 deletion test/project_types/node/commands/populate/customer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class CustomerTest < MiniTest::Test
def test_populate_calls_api_with_mutation
ShopifyCli::Helpers::Haikunator.stubs(:name).returns(['first', 'last'])
ShopifyCli::AdminAPI.expects(:query)
.with(@context, 'create_customer', input: {
.with(@context, 'create_customer', shop: 'my-test-shop.myshopify.com', input: {
firstName: 'first',
lastName: 'last',
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class DraftOrderTest < MiniTest::Test
def test_populate_calls_api_with_mutation
ShopifyCli::Helpers::Haikunator.stubs(:title).returns('fake order')
ShopifyCli::AdminAPI.expects(:query)
.with(@context, 'create_draft_order', input: {
.with(@context, 'create_draft_order', shop: 'my-test-shop.myshopify.com', input: {
lineItems: [{
originalUnitPrice: "1.00",
quantity: 1,
Expand Down
4 changes: 2 additions & 2 deletions test/project_types/node/commands/populate/product_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ def test_populate_calls_api_with_mutation
ShopifyCli::AdminAPI::PopulateResourceCommand.any_instance.stubs(:price).returns('1.00')
return_data = JSON.parse(File.read(File.join(FIXTURE_DIR, 'populate/product_data.json')))
ShopifyCli::AdminAPI.expects(:query)
.with(@context, 'create_product', input: {
.with(@context, 'create_product', shop: 'my-test-shop.myshopify.com', input: {
'title': 'fake product',
variants: [{ price: '1.00' }],
})
.returns(return_data)
ShopifyCli::AdminAPI.expects(:query)
.with(@context, 'create_product', input: {
.with(@context, 'create_product', shop: 'my-test-shop.myshopify.com', input: {
'title': 'fake producttwo',
variants: [{ price: '1.00' }],
})
Expand Down
Loading

0 comments on commit dfad776

Please sign in to comment.