From 6846d2f78305155b0b024461856b5cebda0976f8 Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Fri, 9 Jan 2015 14:39:57 +0100 Subject: [PATCH] Return log with chunks if chunked response is requested for removed log --- lib/travis/api/app/endpoint/jobs.rb | 2 +- lib/travis/api/v2/http.rb | 1 - lib/travis/api/v2/http/log.rb | 33 ++++++++++++++++++--------- lib/travis/api/v2/http/removed_log.rb | 20 ---------------- spec/integration/v2/jobs_spec.rb | 5 +++- 5 files changed, 27 insertions(+), 34 deletions(-) delete mode 100644 lib/travis/api/v2/http/removed_log.rb diff --git a/lib/travis/api/app/endpoint/jobs.rb b/lib/travis/api/app/endpoint/jobs.rb index f14a4d5c..0d65e78e 100644 --- a/lib/travis/api/app/endpoint/jobs.rb +++ b/lib/travis/api/app/endpoint/jobs.rb @@ -59,7 +59,7 @@ class Travis::Api::App get '/:job_id/log' do resource = service(:find_log, params).run if (resource && resource.removed_at) && accepts?('application/json') - respond_with resource, type: 'removed_log', root: 'log' + respond_with resource elsif (!resource || resource.archived?) # the way we use responders makes it hard to validate proper format # automatically here, so we need to check it explicitly diff --git a/lib/travis/api/v2/http.rb b/lib/travis/api/v2/http.rb index 2e5427aa..a733b364 100644 --- a/lib/travis/api/v2/http.rb +++ b/lib/travis/api/v2/http.rb @@ -16,7 +16,6 @@ module Travis require 'travis/api/v2/http/job' require 'travis/api/v2/http/jobs' require 'travis/api/v2/http/log' - require 'travis/api/v2/http/removed_log' require 'travis/api/v2/http/permissions' require 'travis/api/v2/http/repositories' require 'travis/api/v2/http/repository' diff --git a/lib/travis/api/v2/http/log.rb b/lib/travis/api/v2/http/log.rb index 38af5417..96cb408a 100644 --- a/lib/travis/api/v2/http/log.rb +++ b/lib/travis/api/v2/http/log.rb @@ -11,8 +11,14 @@ module Travis end def data + log_hash = options[:chunked] ? chunked_log_data : log_data + if log.removed_at + log_hash['removed_at'] = log.removed_at + log_hash['removed_by'] = log.removed_by.name || object.removed_by.login + end + { - 'log' => options[:chunked] ? chunked_log_data : log_data, + 'log' => log_hash, } end @@ -37,16 +43,21 @@ module Travis end def log_parts - parts = log.parts - parts = parts.where(number: part_numbers) if part_numbers - parts = parts.where(["number > ?", after]) if after - parts.sort_by(&:number).map do |part| - { - 'id' => part.id, - 'number' => part.number, - 'content' => part.content, - 'final' => part.final - } + if log.removed_at + # if log is removed we don't have actual parts + parts = [{ 'number' => 1, 'content' => log.content, 'final' => true }] + else + parts = log.parts + parts = parts.where(number: part_numbers) if part_numbers + parts = parts.where(["number > ?", after]) if after + parts.sort_by(&:number).map do |part| + { + 'id' => part.id, + 'number' => part.number, + 'content' => part.content, + 'final' => part.final + } + end end end diff --git a/lib/travis/api/v2/http/removed_log.rb b/lib/travis/api/v2/http/removed_log.rb deleted file mode 100644 index 11fa032a..00000000 --- a/lib/travis/api/v2/http/removed_log.rb +++ /dev/null @@ -1,20 +0,0 @@ -module Travis - module Api - module V2 - module Http - class RemovedLog < Travis::Api::Serializer - attributes :id, :job_id, :body, :removed_at, :removed_by - - def body - object.content - end - - def removed_by - object.removed_by.name || object.removed_by.login if object.removed_by - end - - end - end - end - end -end diff --git a/spec/integration/v2/jobs_spec.rb b/spec/integration/v2/jobs_spec.rb index 55cc3ba6..128a2a72 100644 --- a/spec/integration/v2/jobs_spec.rb +++ b/spec/integration/v2/jobs_spec.rb @@ -107,13 +107,16 @@ describe 'Jobs' do it 'adds removed info if the log is removed' do time = Time.new(2015, 1, 9, 12, 57, 31) job.log.update_attributes(removed_at: time, removed_by: User.first) - headers = { 'HTTP_ACCEPT' => 'application/vnd.travis-ci.2+json' } + headers = { 'HTTP_ACCEPT' => 'application/json; chunked=true; version=2' } response = get "/jobs/#{job.id}/log", {}, headers body = JSON.parse(response.body) body['log']['removed_by'].should == 'Sven Fuchs' body['log']['removed_at'].should == "2015-01-09T11:57:31Z" body['log']['id'].should == job.log.id + + # make sure we return parts as chunked=true + body['log']['parts'].length.should == 1 end end