Skip to content

Commit d9891d7

Browse files
authored
Allowance exposed in owner representation (#1133)
Allowance exposed in owner representation. For security reasons visible only for logged in users.
1 parent 4de3fce commit d9891d7

File tree

11 files changed

+180
-3
lines changed

11 files changed

+180
-3
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
language: ruby
22

33
import:
4-
- travis-ci/build-configs:db-setup.yml@sb-remove-user-travis
4+
- travis-ci/build-configs:db-setup.yml
55

66
rvm: 2.6.5
77

lib/travis/api/v3/billing_client.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@ def initialize(user_id)
66
@user_id = user_id
77
end
88

9+
def allowance(owner_type, owner_id)
10+
response = connection.get("/usage/#{owner_type.downcase}s/#{owner_id}/allowance")
11+
return default_allowance_response unless response.status == 200
12+
13+
Travis::API::V3::Models::Allowance.new(2, response.body)
14+
end
15+
16+
def self.default_allowance_response
17+
Travis::API::V3::Models::Allowance.new(1, { "public_repos" => true, "private_repos" => false, "concurrency_limit" => 1 }.freeze)
18+
end
19+
920
def all
1021
data = connection.get('/subscriptions').body
1122
subscriptions = data.fetch('subscriptions').map do |subscription_data|

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module Travis::API::V3
2+
class Models::Allowance
3+
attr_reader :subscription_type, :public_repos, :private_repos, :concurrency_limit
4+
5+
def initialize(subscription_type, attributes = {})
6+
@subscription_type = subscription_type
7+
@public_repos = attributes.fetch('public_repos')
8+
@private_repos = attributes.fetch('private_repos')
9+
@concurrency_limit = attributes.fetch('concurrency_limit')
10+
end
11+
end
12+
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module Travis::API::V3
2+
class Renderer::Allowance < ModelRenderer
3+
representation(:minimal, :subscription_type, :public_repos, :private_repos, :concurrency_limit)
4+
end
5+
end

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ class Renderer::Owner < ModelRenderer
55
include Renderer::AvatarURL
66

77
representation(:minimal, :id, :login, :vcs_type)
8-
representation(:standard, :id, :login, :name, :github_id, :vcs_id, :vcs_type, :avatar_url, :education, :allow_migration)
8+
representation(:standard, :id, :login, :name, :github_id, :vcs_id, :vcs_type, :avatar_url, :education,
9+
:allow_migration, :allowance)
910
representation(:additional, :repositories, :installation)
1011

1112
def initialize(*)
@@ -28,5 +29,17 @@ def installation
2829
def allow_migration
2930
!!Travis::Features.owner_active?(:allow_migration, @model)
3031
end
32+
33+
def allowance
34+
return BillingClient.default_allowance_response if Travis.config.org?
35+
return BillingClient.default_allowance_response unless access_control.user
36+
37+
client = BillingClient.new(access_control.user.id)
38+
client.allowance(owner_type, id)
39+
end
40+
41+
def owner_type
42+
vcs_type.match(/User$/) ? 'User' : 'Organization'
43+
end
3144
end
3245
end

spec/v3/services/installation/find_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@
5656
"synced_at" => nil,
5757
"education" => nil,
5858
"allow_migration" => false,
59+
"allowance" => {
60+
"@type" => "allowance",
61+
"@representation" => "minimal",
62+
"subscription_type" => 1,
63+
"public_repos" => true,
64+
"private_repos" => false,
65+
"concurrency_limit" => 1
66+
},
5967
"recently_signed_up" => false,
6068
"secure_user_hash" => nil,
6169
}

spec/v3/services/organization/find_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@
2323
"avatar_url" => nil,
2424
"education" => false,
2525
"allow_migration" => false,
26+
"allowance" => {
27+
"@type" => "allowance",
28+
"@representation" => "minimal",
29+
"subscription_type" => 1,
30+
"public_repos" => true,
31+
"private_repos" => false,
32+
"concurrency_limit" => 1
33+
}
2634
}}
2735
end
2836

@@ -52,6 +60,14 @@
5260
"avatar_url" => nil,
5361
"education" => true,
5462
"allow_migration" => true,
63+
"allowance" => {
64+
"@type" => "allowance",
65+
"@representation" => "minimal",
66+
"subscription_type" => 1,
67+
"public_repos" => true,
68+
"private_repos" => false,
69+
"concurrency_limit" => 1
70+
}
5571
}}
5672
end
5773

spec/v3/services/organizations/for_current_user_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@
5959
"avatar_url" => nil,
6060
"education" => false,
6161
"allow_migration" => false,
62+
"allowance" => {
63+
"@type" => "allowance",
64+
"@representation" => "minimal",
65+
"subscription_type" => 1,
66+
"public_repos" => true,
67+
"private_repos" => false,
68+
"concurrency_limit" => 1
69+
}
6270
}]
6371
}}
6472
end

spec/v3/services/owner/find_spec.rb

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@
2323
"avatar_url" => nil,
2424
"education" => false,
2525
"allow_migration" => false,
26+
"allowance" => {
27+
"@type" => "allowance",
28+
"@representation" => "minimal",
29+
"subscription_type" => 1,
30+
"public_repos" => true,
31+
"private_repos" => false,
32+
"concurrency_limit" => 1
33+
}
2634
}}
2735
end
2836

@@ -43,6 +51,14 @@
4351
"avatar_url" => nil,
4452
"education" => false,
4553
"allow_migration" => false,
54+
"allowance" => {
55+
"@type" => "allowance",
56+
"@representation" => "minimal",
57+
"subscription_type" => 1,
58+
"public_repos" => true,
59+
"private_repos" => false,
60+
"concurrency_limit" => 1
61+
}
4662
}}
4763
end
4864

@@ -68,6 +84,14 @@
6884
"avatar_url" => nil,
6985
"education" => false,
7086
"allow_migration" => false,
87+
"allowance" => {
88+
"@type" => "allowance",
89+
"@representation" => "minimal",
90+
"subscription_type" => 1,
91+
"public_repos" => true,
92+
"private_repos" => false,
93+
"concurrency_limit" => 1
94+
},
7195
"repositories" => [{
7296
"@type" => "repository",
7397
"@href" => "/v3/repo/#{repo.id}",
@@ -137,6 +161,14 @@
137161
"avatar_url" => nil,
138162
"education" => false,
139163
"allow_migration" => false,
164+
"allowance" => {
165+
"@type" => "allowance",
166+
"@representation" => "minimal",
167+
"subscription_type" => 1,
168+
"public_repos" => true,
169+
"private_repos" => false,
170+
"concurrency_limit" => 1
171+
},
140172
"repositories" => [{
141173
"@type" => "repository",
142174
"@href" => "/v3/repo/#{repo.id}",
@@ -201,6 +233,14 @@
201233
"avatar_url" => nil,
202234
"education" => false,
203235
"allow_migration" => false,
236+
"allowance" => {
237+
"@type" => "allowance",
238+
"@representation" => "minimal",
239+
"subscription_type" => 1,
240+
"public_repos" => true,
241+
"private_repos" => false,
242+
"concurrency_limit" => 1
243+
}
204244
}}
205245
end
206246

@@ -225,6 +265,14 @@
225265
"avatar_url" => nil,
226266
"education" => false,
227267
"allow_migration"=> false,
268+
"allowance" => {
269+
"@type" => "allowance",
270+
"@representation" => "minimal",
271+
"subscription_type" => 1,
272+
"public_repos" => true,
273+
"private_repos" => false,
274+
"concurrency_limit" => 1
275+
},
228276
"@warnings" => [{
229277
"@type" => "warning",
230278
"message" => "query parameter organization.id not safelisted, ignored",
@@ -259,6 +307,14 @@
259307
"synced_at" => nil,
260308
"education" => nil,
261309
"allow_migration"=> false,
310+
"allowance" => {
311+
"@type" => "allowance",
312+
"@representation" => "minimal",
313+
"subscription_type" => 1,
314+
"public_repos" => true,
315+
"private_repos" => false,
316+
"concurrency_limit" => 1
317+
},
262318
"recently_signed_up"=>false,
263319
"secure_user_hash" => nil,
264320
}}
@@ -284,6 +340,14 @@
284340
"is_syncing" => nil,
285341
"synced_at" => nil,
286342
"allow_migration"=> false,
343+
"allowance" => {
344+
"@type" => "allowance",
345+
"@representation" => "minimal",
346+
"subscription_type" => 1,
347+
"public_repos" => true,
348+
"private_repos" => false,
349+
"concurrency_limit" => 1
350+
},
287351
"recently_signed_up"=>false,
288352
"secure_user_hash" => nil,
289353
}}
@@ -309,6 +373,14 @@
309373
"is_syncing" => nil,
310374
"synced_at" => nil,
311375
"allow_migration" => false,
376+
"allowance" => {
377+
"@type" => "allowance",
378+
"@representation" => "minimal",
379+
"subscription_type" => 1,
380+
"public_repos" => true,
381+
"private_repos" => false,
382+
"concurrency_limit" => 1
383+
},
312384
"recently_signed_up"=>false,
313385
"secure_user_hash" => nil,
314386
}}
@@ -338,6 +410,14 @@
338410
"is_syncing" => nil,
339411
"synced_at" => nil,
340412
"allow_migration" => false,
413+
"allowance" => {
414+
"@type" => "allowance",
415+
"@representation" => "minimal",
416+
"subscription_type" => 1,
417+
"public_repos" => true,
418+
"private_repos" => false,
419+
"concurrency_limit" => 1
420+
},
341421
"recently_signed_up"=>false,
342422
"secure_user_hash" => nil,
343423
"@warnings" => [{

spec/v3/services/user/current_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@
2424
"synced_at" => user.synced_at,
2525
"education" => nil,
2626
"allow_migration" => false,
27+
"allowance" => {
28+
"@type" => "allowance",
29+
"@representation" => "minimal",
30+
"subscription_type" => 1,
31+
"public_repos" => true,
32+
"private_repos" => false,
33+
"concurrency_limit" => 1
34+
},
2735
"recently_signed_up"=>false,
2836
"secure_user_hash" => nil
2937
}}

0 commit comments

Comments
 (0)