From 2891dd640a27c572ce7c5aecdd23a36a1143107c Mon Sep 17 00:00:00 2001 From: Konstantin Haase Date: Thu, 3 Sep 2015 15:03:12 +0200 Subject: [PATCH 1/2] v3: use cached_matrix_ids for build.jobs if only job ids are being loaded --- lib/travis/api/v3/models/build.rb | 10 ++++++++++ lib/travis/api/v3/renderer/build.rb | 22 +++++++++++++++++++++- lib/travis/api/v3/renderer/job.rb | 2 +- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/lib/travis/api/v3/models/build.rb b/lib/travis/api/v3/models/build.rb index cecf242d..16eb4ce4 100644 --- a/lib/travis/api/v3/models/build.rb +++ b/lib/travis/api/v3/models/build.rb @@ -20,6 +20,16 @@ module Travis::API::V3 read_attribute(:branch) end + def job_ids + return super unless cached = cached_matrix_ids + + # AR 3.2 does not handle pg arrays and the plugins supporting them + # do not work well with jdbc drivers + # TODO: remove this once we're on >= 4.0 + cached = cached.gsub(/^{|}$/, '').split(',').map(&:to_i) unless cached.is_a? Array + cached + end + def branch_name=(value) write_attribute(:branch, value) end diff --git a/lib/travis/api/v3/renderer/build.rb b/lib/travis/api/v3/renderer/build.rb index a57e5136..4bca9f68 100644 --- a/lib/travis/api/v3/renderer/build.rb +++ b/lib/travis/api/v3/renderer/build.rb @@ -3,6 +3,26 @@ require 'travis/api/v3/renderer/model_renderer' module Travis::API::V3 class Renderer::Build < Renderer::ModelRenderer representation(:minimal, :id, :number, :state, :duration, :event_type, :previous_state, :started_at, :finished_at, :jobs) - representation(:standard, *representations[:minimal], :repository, :branch, :commit ) + representation(:standard, *representations[:minimal], :repository, :branch, :commit) + + def jobs + return model.jobs if include_full_jobs? + model.job_ids.map { |id| job(id) } + end + + private def include_full_jobs? + return true if include? 'build.job'.freeze + return true if include.any? { |i| i.start_with? 'job.'.freeze } + return true if included.any? { |i| i.is_a? Models::Job and i.source_id == model.id } + end + + private def job(id) + { + "@type" => "job", + :@href => Renderer.href(:job, script_name: script_name, id: id), + "@representation" => "minimal", + "id" => id + } + end end end diff --git a/lib/travis/api/v3/renderer/job.rb b/lib/travis/api/v3/renderer/job.rb index bb04fda0..92441b62 100644 --- a/lib/travis/api/v3/renderer/job.rb +++ b/lib/travis/api/v3/renderer/job.rb @@ -3,6 +3,6 @@ require 'travis/api/v3/renderer/model_renderer' module Travis::API::V3 class Renderer::Job < Renderer::ModelRenderer representation(:minimal, :id) - representation(:standard, *representations[:minimal], :number, :state, :started_at, :finished_at, :build, :queue, :repository, :commit, :owner ) + representation(:standard, *representations[:minimal], :number, :state, :started_at, :finished_at, :build, :queue, :repository, :commit, :owner) end end From 3242189259f5f59aa4ca041fec9f5ad0d67e091a Mon Sep 17 00:00:00 2001 From: Konstantin Haase Date: Thu, 3 Sep 2015 15:30:09 +0200 Subject: [PATCH 2/2] v3: eager load jobs for build history if asked for --- lib/travis/api/v3/queries/builds.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/travis/api/v3/queries/builds.rb b/lib/travis/api/v3/queries/builds.rb index f45d3daf..fbd7e08e 100644 --- a/lib/travis/api/v3/queries/builds.rb +++ b/lib/travis/api/v3/queries/builds.rb @@ -15,6 +15,7 @@ module Travis::API::V3 list = list.includes(:commit).includes(branch: :last_build).includes(:repository) list = list.includes(branch: { last_build: :commit }) if includes? 'build.commit'.freeze + list = list.includes(:jobs) if includes? 'build.jobs'.freeze or includes? 'job'.freeze list end end