Sha256: a092e28c4f24a3e52f16ca06e73143a5fdbdc8d4c5e9a2210f2f3c0b8c91818e

Contents?: true

Size: 1.62 KB

Versions: 7

Compression:

Stored size: 1.62 KB

Contents

module ShopifyCli
  module Tasks
    class UpdateDashboardURLS < ShopifyCli::Task
      NGROK_REGEX = /https:\/\/([a-z0-9\-]+\.ngrok\.io)(.*)/

      def call(ctx, url:, callback_url:)
        @ctx = ctx
        project = ShopifyCli::Project.current
        api_key = project.env.api_key
        result = ShopifyCli::PartnersAPI.query(ctx, 'get_app_urls', apiKey: api_key)
        app = result['data']['app']
        consent = check_application_url(app['applicationUrl'], url)
        constructed_urls = construct_redirect_urls(app['redirectUrlWhitelist'], url, callback_url)
        return if url == app['applicationUrl']
        ShopifyCli::PartnersAPI.query(@ctx, 'update_dashboard_urls', input: {
          applicationUrl: consent ? url : app['applicationUrl'],
          redirectUrlWhitelist: constructed_urls, apiKey: api_key
        })
        @ctx.puts(@ctx.message('core.tasks.update_dashboard_urls.updated'))
      rescue
        @ctx.puts(@ctx.message('core.tasks.update_dashboard_urls.update_error', ShopifyCli::TOOL_NAME))
        raise
      end

      def check_application_url(application_url, new_url)
        return false if application_url.match(new_url)
        CLI::UI::Prompt.confirm(@ctx.message('core.tasks.update_dashboard_urls.update_prompt'))
      end

      def construct_redirect_urls(urls, new_url, callback_url)
        urls.map do |url|
          if (match = url.match(NGROK_REGEX))
            "#{new_url}#{match[2]}"
          else
            url
          end
        end
        if urls.grep(/#{new_url}#{callback_url}/).empty?
          urls.push("#{new_url}#{callback_url}")
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
shopify-cli-1.0.2 lib/shopify-cli/tasks/update_dashboard_urls.rb
shopify-cli-1.0.1 lib/shopify-cli/tasks/update_dashboard_urls.rb
shopify-cli-1.0.0 lib/shopify-cli/tasks/update_dashboard_urls.rb
shopify-cli-0.9.3 lib/shopify-cli/tasks/update_dashboard_urls.rb
shopify-cli-0.9.2 lib/shopify-cli/tasks/update_dashboard_urls.rb
shopify-cli-0.9.1 lib/shopify-cli/tasks/update_dashboard_urls.rb
shopify-cli-0.9.0 lib/shopify-cli/tasks/update_dashboard_urls.rb