Commit 5e29c446 authored by Jan Provaznik's avatar Jan Provaznik

Merge branch 'vij-move-fetch-subscription-service' into 'master'

Move FetchSubscriptionPlansService to namespace

See merge request gitlab-org/gitlab!55991
parents 3895c05f 0ccb6aa0
......@@ -2355,7 +2355,6 @@ Gitlab/NamespacedClass:
- 'ee/app/serializers/vulnerability_note_serializer.rb'
- 'ee/app/serializers/vulnerability_serializer.rb'
- 'ee/app/services/clear_namespace_shared_runners_minutes_service.rb'
- 'ee/app/services/fetch_subscription_plans_service.rb'
- 'ee/app/services/ldap_group_reset_service.rb'
- 'ee/app/services/start_pull_mirroring_service.rb'
- 'ee/app/services/timebox_report_service.rb'
......
......@@ -12,7 +12,9 @@ class Groups::BillingsController < Groups::ApplicationController
@top_most_group = @group.root_ancestor if @group.has_parent?
relevant_group = (@top_most_group || @group)
current_plan = relevant_group.plan_name_for_upgrading
@plans_data = FetchSubscriptionPlansService.new(plan: current_plan, namespace_id: relevant_group.id).execute
@plans_data = GitlabSubscriptions::FetchSubscriptionPlansService
.new(plan: current_plan, namespace_id: relevant_group.id)
.execute
track_experiment_event(:contact_sales_btn_in_app, 'page_view:billing_plans:group')
record_experiment_user(:contact_sales_btn_in_app)
end
......
......@@ -6,7 +6,7 @@ class Profiles::BillingsController < Profiles::ApplicationController
feature_category :purchase
def index
@plans_data = FetchSubscriptionPlansService
@plans_data = GitlabSubscriptions::FetchSubscriptionPlansService
.new(plan: current_user.namespace.plan_name_for_upgrading, namespace_id: current_user.namespace_id)
.execute
track_experiment_event(:contact_sales_btn_in_app, 'page_view:billing_plans:profile')
......
......@@ -31,7 +31,7 @@ module SubscriptionsHelper
end
def plans_data
FetchSubscriptionPlansService.new(plan: :free).execute
GitlabSubscriptions::FetchSubscriptionPlansService.new(plan: :free).execute
.map(&:symbolize_keys)
.reject { |plan_data| plan_data[:free] }
.map { |plan_data| plan_data.slice(:id, :code, :price_per_year, :deprecated, :name) }
......
# frozen_string_literal: true
class FetchSubscriptionPlansService
URL = "#{EE::SUBSCRIPTIONS_URL}/gitlab_plans".freeze
def initialize(plan:, namespace_id: nil)
@plan = plan
@namespace_id = namespace_id
end
def execute
cached { send_request }
end
private
def send_request
response = Gitlab::HTTP.get(
URL,
allow_local_requests: true,
query: { plan: @plan, namespace_id: @namespace_id },
headers: { 'Accept' => 'application/json' }
)
Gitlab::Json.parse(response.body).map { |plan| Hashie::Mash.new(plan) }
rescue => e
Gitlab::AppLogger.info "Unable to connect to GitLab Customers App #{e}"
nil
end
def cached
if plans_data = cache.read(cache_key)
plans_data
else
cache.fetch(cache_key, force: true, expires_in: 1.day) { yield }
end
end
def cache
Rails.cache
end
def cache_key
if Feature.enabled?(:pnp_subscription_plan_cache_key)
if @namespace_id.present?
"pnp-subscription-plan-#{@plan}-#{@namespace_id}"
else
"pnp-subscription-plan-#{@plan}"
end
elsif Feature.enabled?(:subscription_plan_cache_key)
if @namespace_id.present?
"subscription-plan-#{@plan}-#{@namespace_id}"
else
"subscription-plan-#{@plan}"
end
else
if @namespace_id.present?
"subscription-plans-#{@plan}-#{@namespace_id}"
else
"subscription-plans-#{@plan}"
end
end
end
end
# frozen_string_literal: true
module GitlabSubscriptions
class FetchSubscriptionPlansService
URL = "#{EE::SUBSCRIPTIONS_URL}/gitlab_plans".freeze
def initialize(plan:, namespace_id: nil)
@plan = plan
@namespace_id = namespace_id
end
def execute
cached { send_request }
end
private
def send_request
response = Gitlab::HTTP.get(
URL,
allow_local_requests: true,
query: { plan: @plan, namespace_id: @namespace_id },
headers: { 'Accept' => 'application/json' }
)
Gitlab::Json.parse(response.body).map { |plan| Hashie::Mash.new(plan) }
rescue => e
Gitlab::AppLogger.info "Unable to connect to GitLab Customers App #{e}"
nil
end
def cached
if plans_data = cache.read(cache_key)
plans_data
else
cache.fetch(cache_key, force: true, expires_in: 1.day) { yield }
end
end
def cache
Rails.cache
end
def cache_key
if Feature.enabled?(:pnp_subscription_plan_cache_key)
if @namespace_id.present?
"pnp-subscription-plan-#{@plan}-#{@namespace_id}"
else
"pnp-subscription-plan-#{@plan}"
end
elsif Feature.enabled?(:subscription_plan_cache_key)
if @namespace_id.present?
"subscription-plan-#{@plan}-#{@namespace_id}"
else
"subscription-plan-#{@plan}"
end
else
if @namespace_id.present?
"subscription-plans-#{@plan}-#{@namespace_id}"
else
"subscription-plans-#{@plan}"
end
end
end
end
end
......@@ -26,7 +26,7 @@ RSpec.describe Groups::BillingsController do
context 'authorized' do
before do
add_group_owner
allow_next_instance_of(FetchSubscriptionPlansService) do |instance|
allow_next_instance_of(GitlabSubscriptions::FetchSubscriptionPlansService) do |instance|
allow(instance).to receive(:execute)
end
allow(controller).to receive(:track_experiment_event)
......@@ -41,7 +41,7 @@ RSpec.describe Groups::BillingsController do
it 'fetches subscription plans data from customers.gitlab.com' do
data = double
expect_next_instance_of(FetchSubscriptionPlansService) do |instance|
expect_next_instance_of(GitlabSubscriptions::FetchSubscriptionPlansService) do |instance|
expect(instance).to receive(:execute).and_return(data)
end
......
......@@ -10,7 +10,7 @@ RSpec.describe Profiles::BillingsController do
sign_in(user)
stub_application_setting(check_namespace_plan: true)
allow(Gitlab).to receive(:com?) { true }
allow_next_instance_of(FetchSubscriptionPlansService) do |instance|
allow_next_instance_of(GitlabSubscriptions::FetchSubscriptionPlansService) do |instance|
allow(instance).to receive(:execute)
end
allow(controller).to receive(:track_experiment_event)
......@@ -31,7 +31,7 @@ RSpec.describe Profiles::BillingsController do
it 'fetch subscription plans data from customers.gitlab.com' do
data = double
expect_next_instance_of(FetchSubscriptionPlansService) do |instance|
expect_next_instance_of(GitlabSubscriptions::FetchSubscriptionPlansService) do |instance|
expect(instance).to receive(:execute).and_return(data)
end
......
......@@ -26,7 +26,7 @@ RSpec.describe SubscriptionsHelper do
before do
stub_feature_flags(hide_deprecated_billing_plans: false)
allow(helper).to receive(:params).and_return(plan_id: 'bronze_id', namespace_id: nil)
allow_next_instance_of(FetchSubscriptionPlansService) do |instance|
allow_next_instance_of(GitlabSubscriptions::FetchSubscriptionPlansService) do |instance|
allow(instance).to receive(:execute).and_return(raw_plan_data)
end
end
......
......@@ -2,7 +2,7 @@
require 'spec_helper'
RSpec.describe FetchSubscriptionPlansService do
RSpec.describe GitlabSubscriptions::FetchSubscriptionPlansService do
describe '#execute' do
subject(:execute_service) { described_class.new(plan: plan).execute }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment