Sha256: a38e5ea982a21780ffb44a3d439f4d992d48ed50f6743cce9b231afa9df7c95c

Contents?: true

Size: 1.66 KB

Versions: 20

Compression:

Stored size: 1.66 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)
        new_urls = urls.map do |url|
          if (match = url.match(NGROK_REGEX))
            "#{new_url}#{match[2]}"
          else
            url
          end
        end
        if new_urls.grep(/#{new_url}#{callback_url}/).empty?
          new_urls.push("#{new_url}#{callback_url}")
        end
        new_urls.uniq
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
shopify-cli-2.4.0 lib/shopify-cli/tasks/update_dashboard_urls.rb
shopify-cli-2.3.0 lib/shopify-cli/tasks/update_dashboard_urls.rb
shopify-cli-2.2.2 lib/shopify-cli/tasks/update_dashboard_urls.rb
shopify-cli-2.2.1 lib/shopify-cli/tasks/update_dashboard_urls.rb
shopify-cli-2.2.0 lib/shopify-cli/tasks/update_dashboard_urls.rb
shopify-cli-2.1.0 lib/shopify-cli/tasks/update_dashboard_urls.rb
shopify-cli-2.0.2 lib/shopify-cli/tasks/update_dashboard_urls.rb
shopify-cli-2.0.1 lib/shopify-cli/tasks/update_dashboard_urls.rb
shopify-cli-2.0.0 lib/shopify-cli/tasks/update_dashboard_urls.rb
shopify-cli-1.14.0 lib/shopify-cli/tasks/update_dashboard_urls.rb
shopify-cli-1.13.1 lib/shopify-cli/tasks/update_dashboard_urls.rb
shopify-cli-1.13.0 lib/shopify-cli/tasks/update_dashboard_urls.rb
shopify-cli-1.12.0 lib/shopify-cli/tasks/update_dashboard_urls.rb
shopify-cli-1.11.0 lib/shopify-cli/tasks/update_dashboard_urls.rb
shopify-cli-1.10.0 lib/shopify-cli/tasks/update_dashboard_urls.rb
shopify-cli-1.9.1 lib/shopify-cli/tasks/update_dashboard_urls.rb
shopify-cli-1.9.0 lib/shopify-cli/tasks/update_dashboard_urls.rb
shopify-cli-1.8.0 lib/shopify-cli/tasks/update_dashboard_urls.rb
shopify-cli-1.7.1 lib/shopify-cli/tasks/update_dashboard_urls.rb
shopify-cli-1.7.0 lib/shopify-cli/tasks/update_dashboard_urls.rb