Skip to content

Commit c7d95a0

Browse files
authored
Owner allowance refactor (#1142)
Allowance endpoint for owner
1 parent acda015 commit c7d95a0

File tree

27 files changed

+129
-302
lines changed

27 files changed

+129
-302
lines changed

lib/travis/api/v3/billing_client.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ def allowance(owner_type, owner_id)
1010
response = connection.get("/usage/#{owner_type.downcase}s/#{owner_id}/allowance")
1111
return BillingClient.default_allowance_response unless response.status == 200
1212

13-
Travis::API::V3::Models::Allowance.new(2, response.body)
13+
Travis::API::V3::Models::Allowance.new(2, owner_id, response.body)
1414
end
1515

16-
def self.default_allowance_response
17-
Travis::API::V3::Models::Allowance.new(1, { "public_repos" => true, "private_repos" => false, "concurrency_limit" => 1 }.freeze)
16+
def self.default_allowance_response(id = 0)
17+
Travis::API::V3::Models::Allowance.new(1, id, { "public_repos" => true, "private_repos" => false, "concurrency_limit" => 1 }.freeze)
1818
end
1919

2020
def all

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
module Travis::API::V3
22
class Models::Allowance
3-
attr_reader :subscription_type, :public_repos, :private_repos, :concurrency_limit
3+
attr_reader :subscription_type, :public_repos, :private_repos, :concurrency_limit, :id
44

5-
def initialize(subscription_type, attributes = {})
5+
def initialize(subscription_type, owner_id, attributes = {})
66
@subscription_type = subscription_type
7+
@id = owner_id
78
@public_repos = attributes.fetch('public_repos')
89
@private_repos = attributes.fetch('private_repos')
910
@concurrency_limit = attributes.fetch('concurrency_limit')
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module Travis::API::V3
2+
class Queries::Allowance < Query
3+
params :login, :github_id, :provider
4+
5+
def for_owner(owner, user_id)
6+
client = BillingClient.new(user_id)
7+
client.allowance(owner_type(owner), owner.id)
8+
end
9+
10+
private
11+
12+
def owner_type(owner)
13+
owner.vcs_type =~ /User/ ? 'user' : 'organization'
14+
end
15+
end
16+
end
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module Travis::API::V3
22
class Renderer::Allowance < ModelRenderer
3-
representation(:minimal, :subscription_type, :public_repos, :private_repos, :concurrency_limit)
3+
representation(:minimal, :subscription_type, :public_repos, :private_repos, :concurrency_limit, :id)
4+
representation(:standard, :subscription_type, :public_repos, :private_repos, :concurrency_limit, :id)
45
end
56
end

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module Travis::API::V3
44
class Renderer::Owner < ModelRenderer
55
include Renderer::AvatarURL
66

7-
representation(:minimal, :id, :login, :vcs_type, :allowance)
7+
representation(:minimal, :id, :login, :vcs_type)
88
representation(:standard, :id, :login, :name, :github_id, :vcs_id, :vcs_type, :avatar_url, :education,
99
:allow_migration, :allowance)
1010
representation(:additional, :repositories, :installation)
@@ -31,8 +31,8 @@ def allow_migration
3131
end
3232

3333
def allowance
34-
return BillingClient.default_allowance_response if Travis.config.org?
35-
return BillingClient.default_allowance_response unless access_control.user
34+
return BillingClient.default_allowance_response(id) if Travis.config.org?
35+
return BillingClient.default_allowance_response(id) unless access_control.user
3636

3737
client = BillingClient.new(access_control.user.id)
3838
client.allowance(owner_type, id)

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

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def owner
7575
if included_owner? and owner_href
7676
{ :@href => owner_href }
7777
else
78-
result = { :@type => owner_type, :id => model.owner_id, :login => model.owner_name, :allowance => owner_allowance }
78+
result = { :@type => owner_type, :id => model.owner_id, :login => model.owner_name }
7979
result[:@href] = owner_href if owner_href
8080
result
8181
end
@@ -96,16 +96,6 @@ def owner_type
9696
@owner_type ||= model.owner_type.downcase if model.owner_type
9797
end
9898

99-
def owner_allowance
100-
@owner_allowance ||= begin
101-
return BillingClient.default_allowance_response if Travis.config.org?
102-
return BillingClient.default_allowance_response unless access_control.user
103-
104-
client = BillingClient.new(access_control.user.id)
105-
client.allowance(owner_type, model.owner_id)
106-
end
107-
end
108-
10999
def managed_by_installation
110100
model.managed_by_installation?
111101
end

lib/travis/api/v3/routes.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ module Routes
115115
route '/active'
116116
get :for_owner
117117
end
118+
119+
resource :allowance do
120+
route '/allowance'
121+
get :for_owner
122+
end
118123
end
119124

120125
resource :repositories do
@@ -152,7 +157,7 @@ module Routes
152157

153158
resource :builds do
154159
route '/builds'
155-
get :find
160+
get :find
156161
end
157162

158163
resource :caches do

lib/travis/api/v3/services.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module Services
44

55
Accounts = Module.new { extend Services }
66
Active = Module.new { extend Services }
7+
Allowance = Module.new { extend Services }
78
BetaFeature = Module.new { extend Services }
89
BetaFeatures = Module.new { extend Services }
910
BetaMigrationRequest = Module.new { extend Services }
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module Travis::API::V3
2+
class Services::Allowance::ForOwner < Service
3+
def run!
4+
return result BillingClient.default_allowance_response if Travis.config.org?
5+
raise LoginRequired unless access_control.logged_in?
6+
7+
owner = query(:owner).find
8+
9+
raise NotFound unless owner
10+
raise InsufficientAccess unless access_control.visible?(owner)
11+
12+
result query(:allowance).for_owner(owner, access_control.user.id)
13+
end
14+
end
15+
end

spec/v3/services/installation/find_spec.rb

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,6 @@
1818
"github_id" => installation.github_id,
1919
"owner" => {
2020
"@type"=>"user",
21-
"allowance" => {
22-
"@representation" => "minimal",
23-
"@type" => "allowance",
24-
"concurrency_limit" => 1,
25-
"private_repos" => false,
26-
"public_repos" => true,
27-
"subscription_type" => 1
28-
},
2921
"@href"=>"/v3/user/#{user.id}",
3022
"@representation"=>"minimal",
3123
"id"=>user.id,
@@ -63,15 +55,16 @@
6355
"is_syncing" => nil,
6456
"synced_at" => nil,
6557
"education" => nil,
66-
"allow_migration" => false,
67-
"allowance" => {
68-
"@type" => "allowance",
58+
"allowance" => {
6959
"@representation" => "minimal",
70-
"subscription_type" => 1,
71-
"public_repos" => true,
60+
"@type" => "allowance",
61+
"id" => 1,
62+
"concurrency_limit" => 1,
7263
"private_repos" => false,
73-
"concurrency_limit" => 1
64+
"public_repos" => true,
65+
"subscription_type" => 1
7466
},
67+
"allow_migration" => false,
7568
"recently_signed_up" => false,
7669
"secure_user_hash" => nil,
7770
}

0 commit comments

Comments
 (0)