Skip to content

Commit 900ba05

Browse files
authored
Usage executions for owner (#1147)
Executions for owner
1 parent abc8d53 commit 900ba05

File tree

11 files changed

+203
-0
lines changed

11 files changed

+203
-0
lines changed

lib/travis/api/v3/billing_client.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ def self.default_allowance_response(id = 0)
1717
Travis::API::V3::Models::Allowance.new(1, id, { "public_repos" => true, "private_repos" => false, "concurrency_limit" => 1 }.freeze)
1818
end
1919

20+
def executions(owner_type, owner_id, page, per_page, from, to)
21+
response = connection.get("/usage/#{owner_type.downcase}s/#{owner_id}/executions?page=#{page}&per_page=#{per_page}&from=#{from}&to=#{to}")
22+
executions = response.body.map do |execution_data|
23+
Travis::API::V3::Models::Execution.new(execution_data)
24+
end
25+
executions
26+
end
27+
2028
def all
2129
data = connection.get('/subscriptions').body
2230
subscriptions = data.fetch('subscriptions').map do |subscription_data|
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module Travis::API::V3
2+
class Models::Execution
3+
attr_reader :id, :os, :instance_size, :arch, :virtualization_type, :queue, :job_id, :repository_id, :owner_id,
4+
:owner_type, :plan_id, :sender_id, :credits_consumed, :started_at, :finished_at, :created_at,
5+
:updated_at
6+
7+
def initialize(attributes = {})
8+
@id = attributes.fetch('id')
9+
@os = attributes.fetch('os')
10+
@instance_size = attributes.fetch('instance_size')
11+
@arch = attributes.fetch('arch')
12+
@virtualization_type = attributes.fetch('virtualization_type')
13+
@queue = attributes.fetch('queue')
14+
@job_id = attributes.fetch('job_id')
15+
@repository_id = attributes.fetch('repository_id')
16+
@owner_id = attributes.fetch('owner_id')
17+
@owner_type = attributes.fetch('owner_type')
18+
@plan_id = attributes.fetch('plan_id')
19+
@sender_id = attributes.fetch('sender_id')
20+
@credits_consumed = attributes.fetch('credits_consumed')
21+
@started_at = attributes.fetch('started_at')
22+
@finished_at = attributes.fetch('finished_at')
23+
@created_at = attributes.fetch('created_at')
24+
@updated_at = attributes.fetch('updated_at')
25+
end
26+
end
27+
end
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 Queries::Executions < Query
3+
4+
def for_owner(owner, user_id, page, per_page, from, to)
5+
client = BillingClient.new(user_id)
6+
client.executions(owner_type(owner), owner.id, page, per_page, from, to)
7+
end
8+
9+
private
10+
11+
def owner_type(owner)
12+
owner.vcs_type =~ /User/ ? 'user' : 'organization'
13+
end
14+
end
15+
end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module Travis::API::V3
2+
class Renderer::Execution < ModelRenderer
3+
representation :minimal, :id, :os, :instance_size, :arch, :virtualization_type, :queue, :job_id,
4+
:repository_id, :owner_id, :owner_type, :plan_id, :sender_id, :credits_consumed, :started_at,
5+
:finished_at, :created_at, :updated_at
6+
representation :standard, *representations[:minimal]
7+
end
8+
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module Travis::API::V3
2+
class Renderer::Executions < CollectionRenderer
3+
type :executions
4+
collection_key :executions
5+
end
6+
end

lib/travis/api/v3/routes.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ module Routes
126126
route '/allowance'
127127
get :for_owner
128128
end
129+
130+
resource :executions do
131+
route '/executions'
132+
get :for_owner
133+
end
129134
end
130135

131136
resource :repositories do

lib/travis/api/v3/services.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ module Services
2424
EnvVar = Module.new { extend Services }
2525
EnvVars = Module.new { extend Services }
2626
EnterpriseLicense = Module.new { extend Services }
27+
Executions = Module.new { extend Services }
2728
Gdpr = Module.new { extend Services }
2829
Insights = Module.new { extend Services }
2930
Installation = Module.new { extend Services }
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module Travis::API::V3
2+
class Services::Executions::ForOwner < Service
3+
params :page, :per_page, :from, :to
4+
result_type :executions
5+
6+
def run!
7+
raise MethodNotAllowed if Travis.config.org?
8+
raise LoginRequired unless access_control.logged_in?
9+
10+
owner = query(:owner).find
11+
12+
raise NotFound unless owner
13+
raise InsufficientAccess unless access_control.visible?(owner)
14+
15+
result query(:executions).for_owner(owner, access_control.user.id, params['page'], params['per_page'],
16+
params['from'], params['to'])
17+
end
18+
end
19+
end

spec/support/billing_spec_helper.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,5 +250,27 @@ def billing_trial_response_body(attributes = {})
250250
'builds_remaining' => 5
251251
}.deep_merge(attributes)
252252
end
253+
254+
def billing_executions_response_body(attributes = {})
255+
{
256+
'id' => 1,
257+
'os' => 'linux',
258+
'instance_size' => 'standard-2',
259+
'arch' => 'amd64',
260+
'virtualization_type' => 'vm',
261+
'queue' => 'builds.gce-oss',
262+
'job_id' => 123,
263+
'repository_id' => 123,
264+
'owner_id' => 1,
265+
'owner_type' => 'User',
266+
'plan_id' => 2,
267+
'sender_id' => 1,
268+
'credits_consumed' => 5,
269+
'started_at' => Time.now,
270+
'finished_at' => Time.now,
271+
'created_at' => Time.now,
272+
'updated_at' => Time.now
273+
}.deep_merge(attributes)
274+
end
253275
end
254276
end

spec/v3/billing_client_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212

1313
let(:subscription_id) { rand(999) }
1414
let(:invoice_id) { "TP#{rand(999)}" }
15+
let(:page) { 1 }
16+
let(:per_page) { 25 }
17+
let(:from) { DateTime.now - 2.months }
18+
let(:to) { DateTime.now }
19+
let(:owner_id) { rand(999) }
20+
let(:owner_type) { 'user' }
1521

1622
describe '#get_subscription' do
1723
subject { billing.get_subscription(subscription_id) }
@@ -414,4 +420,17 @@
414420
expect(stubbed_request).to have_been_made
415421
end
416422
end
423+
424+
describe '#update_organization_billing_permission' do
425+
subject { billing.executions(owner_type, owner_id, page, per_page, from, to) }
426+
427+
it 'returns the list of executions for the subscription' do
428+
stub_request(:get, "#{billing_url}usage/#{owner_type.downcase}s/#{owner_id}/executions?page=#{page}&per_page=#{per_page}&from=#{from}&to=#{to}").with(basic_auth: ['_', auth_key], headers: { 'X-Travis-User-Id' => user_id })
429+
.to_return(body: JSON.dump([billing_executions_response_body]))
430+
431+
expect(subject.first).to be_a(Travis::API::V3::Models::Execution)
432+
expect(subject.first.credits_consumed).to eq 5
433+
expect(subject.first.os).to eq 'linux'
434+
end
435+
end
417436
end

0 commit comments

Comments
 (0)