Skip to content

Commit 4e713be

Browse files
Merge pull request #1343 from travis-ci/release_241106
Release 2024 11 06
2 parents 38579b8 + e8faac9 commit 4e713be

File tree

22 files changed

+115
-11
lines changed

22 files changed

+115
-11
lines changed

lib/travis/api/v3/billing_client.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,15 @@ def pause_v2_subscription(id, reason_data)
253253
handle_subscription_response(response)
254254
end
255255

256+
def trial_allowed(owner_id, owner_type)
257+
data = connection.post("/usage/stats", owners: [{id: owner_id, type: owner_type}], query: 'trial_allowed')
258+
data = data&.body
259+
data = data.is_a?(String) && data.length > 0 ? JSON.parse(data) : data
260+
data.fetch('trial_allowed') == 'true' if data && data['trial_allowed']
261+
rescue
262+
false
263+
end
264+
256265
def usage_stats(owners)
257266
data = connection.post("/usage/stats", owners: owners, query: 'paid_plan_count')
258267
data = data&.body

lib/travis/api/v3/models/subscription.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ def initialize(attributes = {})
2222
@client_secret = attributes.fetch('client_secret')
2323
@created_at = attributes.fetch('created_at')
2424
@cancellation_requested = attributes.fetch('cancellation_requested')
25+
@on_trial_period = attributes.fetch('on_trial_period', false)
26+
current_trial = attributes.fetch('current_trial', nil)
27+
@current_trial = current_trial && Models::V2Trial.new(current_trial)
2528
end
2629
end
2730

lib/travis/api/v3/models/v2_plan_config.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
module Travis::API::V3
22
class Models::V2PlanConfig
33
attr_reader :id, :name, :private_repos, :starting_price, :starting_users, :plan_type,
4-
:private_credits, :public_credits, :addon_configs, :concurrency_limit, :available_standalone_addons, :auto_refill_enabled, :trial_plan,
5-
:annual, :auto_refill_thresholds, :auto_refill_amounts
4+
:private_credits, :public_credits, :addon_configs, :concurrency_limit, :available_standalone_addons, :auto_refill_enabled, :trial_plan, :trial_config,
5+
:annual, :auto_refill_thresholds, :auto_refill_amounts, :vm_size
66

77
def initialize(attrs)
88
@id = attrs.fetch('id')
@@ -17,9 +17,11 @@ def initialize(attrs)
1717
@concurrency_limit = attrs.fetch('concurrency_limit')
1818
@available_standalone_addons = attrs.fetch('available_standalone_addons')
1919
@annual = attrs.fetch('annual')
20+
@vm_size = attrs.fetch('vm_size', nil)
2021
@auto_refill_thresholds = attrs.fetch('auto_refill_thresholds')
2122
@auto_refill_amounts = attrs.fetch('auto_refill_amounts')
2223
@trial_plan = attrs.fetch('trial_plan', false)
24+
@trial_config = attrs.fetch('trial_config', nil)
2325
end
2426
end
2527
end

lib/travis/api/v3/models/v2_subscription.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class Models::V2Subscription
44

55
attr_reader :id, :plan, :permissions, :source, :billing_info, :credit_card_info, :owner, :status, :valid_to, :canceled_at,
66
:client_secret, :payment_intent, :addons, :auto_refill, :available_standalone_addons, :created_at, :scheduled_plan_name,
7-
:cancellation_requested
7+
:cancellation_requested, :current_trial
88

99
def initialize(attributes = {})
1010
@id = attributes.fetch('id')
@@ -33,6 +33,10 @@ def initialize(attributes = {})
3333
@canceled_at = attributes.fetch('canceled_at')
3434
@scheduled_plan_name = attributes.fetch('scheduled_plan')
3535
@cancellation_requested = attributes.fetch('cancellation_requested')
36+
current_trial = attributes.fetch('current_trial', nil)
37+
if current_trial
38+
@current_trial = Models::V2Trial.new(current_trial)
39+
end
3640
end
3741
end
3842

lib/travis/api/v3/models/v2_trial.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module Travis::API::V3
2+
class Models::V2Trial
3+
4+
attr_reader :concurrency_limit, :max_builds, :max_jobs_per_build, :status, :builds_triggered, :started_at, :finish_time, :credit_usage, :user_usage
5+
6+
def initialize(attributes = {})
7+
@concurrency_limit = attributes.fetch('concurrency_limit')
8+
@max_builds = attributes.fetch('max_builds')
9+
@max_jobs_per_build = attributes.fetch('max_jobs_per_build')
10+
@builds_triggered = attributes.fetch('builds_triggered')
11+
@status = attributes.fetch('status')
12+
@started_at = attributes.fetch('started_at')
13+
@finish_time = attributes.fetch('finish_time')
14+
c_usage = attributes.fetch('credit_usage', nil)
15+
@credit_usage = Models::V2AddonUsage.new(c_usage) if c_usage
16+
u_usage = attributes.fetch('user_usage', nil)
17+
@user_usage = Models::V2AddonUsage.new(u_usage) if u_usage
18+
end
19+
end
20+
end

lib/travis/api/v3/queries/owner.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ def find
66
find!(:organization) || find!(:user)
77
end
88

9+
def trial_allowed(user_id, owner_id, owner_type)
10+
client = BillingClient.new(user_id)
11+
!!client.trial_allowed(owner_id, owner_type)
12+
end
13+
914
private
1015

1116
def query(type, main_type: self.main_type, params: self.params)

lib/travis/api/v3/renderer/owner.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ class Renderer::Owner < ModelRenderer
66

77
representation(:minimal, :id, :login, :name, :vcs_type, :ro_mode)
88
representation(:standard, :id, :login, :name, :github_id, :vcs_id, :vcs_type, :avatar_url, :education,
9-
:allow_migration, :allowance, :ro_mode, :custom_keys)
10-
representation(:additional, :repositories, :installation)
9+
:allow_migration, :allowance, :ro_mode, :custom_keys, :trial_allowed)
10+
representation(:additional, :repositories, :installation, :trial_allowed)
1111

1212
def initialize(model, **options)
1313
super
@@ -39,6 +39,10 @@ def allowance
3939
BillingClient.minimal_allowance_response(id)
4040
end
4141

42+
def trial_allowed
43+
query(:owner).trial_allowed(access_control&.user&.id, @model.id, @model.class.name.split('::').last)
44+
end
45+
4246
def owner_type
4347
vcs_type.match(/User$/) ? 'User' : 'Organization'
4448
end
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
module Travis::API::V3
22
class Renderer::V2PlanConfig < ModelRenderer
33
representation(:standard, :id, :name, :private_repos, :starting_price, :starting_users, :private_credits,
4-
:public_credits, :addon_configs, :plan_type, :concurrency_limit, :available_standalone_addons, :trial_plan, :annual, :auto_refill_thresholds, :auto_refill_amounts)
4+
:public_credits, :addon_configs, :plan_type, :concurrency_limit, :available_standalone_addons, :trial_plan, :annual, :auto_refill_thresholds, :auto_refill_amounts, :trial_config, :vm_size)
55
representation(:minimal, :id, :name, :private_repos, :starting_price, :starting_users, :private_credits,
6-
:public_credits, :addon_configs, :plan_type, :concurrency_limit, :trial_plan, :annual, :auto_refill_thresholds, :auto_refill_amounts)
6+
:public_credits, :addon_configs, :plan_type, :concurrency_limit, :trial_plan, :annual, :auto_refill_thresholds, :auto_refill_amounts, :trial_config, :vm_size)
77
end
88
end

lib/travis/api/v3/renderer/v2_subscription.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module Travis::API::V3
22
class Renderer::V2Subscription < ModelRenderer
3-
representation(:standard, :id, :plan, :addons, :auto_refill, :status, :valid_to, :canceled_at, :source, :owner, :client_secret, :billing_info, :credit_card_info, :payment_intent, :created_at, :scheduled_plan_name, :cancellation_requested)
3+
representation(:standard, :id, :plan, :addons, :auto_refill, :status, :valid_to, :canceled_at, :source, :owner, :client_secret, :billing_info, :credit_card_info, :payment_intent, :created_at, :scheduled_plan_name, :cancellation_requested, :current_trial)
44

55
def billing_info
66
Renderer.render_model(model.billing_info, mode: :standard) unless model.billing_info.nil?
@@ -17,6 +17,10 @@ def plan
1717
def payment_intent
1818
Renderer.render_model(model.payment_intent, mode: :standard) unless model.payment_intent.nil?
1919
end
20+
21+
def current_trial
22+
Renderer.render_model(model.current_trial,mode: :standard) unless model.current_trial.nil?
23+
end
2024
end
2125

2226
class Renderer::V2BillingInfo < ModelRenderer
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module Travis::API::V3
2+
class Renderer::V2Trial < ModelRenderer
3+
representation(:standard, :concurrency_limit, :max_builds, :max_jobs_per_build, :status, :builds_triggered, :started_at, :finish_time, :credit_usage, :user_usage)
4+
representation(:minimal, :concurrency_limit, :max_builds, :max_jobs_per_build, :status, :builds_triggered, :started_at, :finish_time, :credit_usage, :user_usage)
5+
6+
def credit_usage
7+
Renderer.render_model(model.credit_usage, mode: :standard) unless model.credit_usage.nil?
8+
end
9+
10+
def user_usage
11+
Renderer.render_model(model.user_usage, mode: :standard) unless model.user_usage.nil?
12+
end
13+
end
14+
end

0 commit comments

Comments
 (0)