Merge pull request #37 from travis-ci/plain-log

Plain log
This commit is contained in:
Konstantin Haase 2013-01-15 02:26:19 -08:00
commit b358090376
5 changed files with 65 additions and 2 deletions

View File

@ -29,7 +29,7 @@ class Travis::Api::App
end end
def responders(resource, options) def responders(resource, options)
[:Service, :Json, :Image, :Xml].map do |name| [:Service, :Json, :Image, :Xml, :Plain].map do |name|
Responders.const_get(name) Responders.const_get(name)
end end
end end

View File

@ -3,7 +3,7 @@ require 'travis/api/app'
class Travis::Api::App class Travis::Api::App
class Middleware class Middleware
class Rewrite < Middleware class Rewrite < Middleware
FORMAT = %r(\.(json|xml|png)$) FORMAT = %r(\.(json|xml|png|txt)$)
V1_REPO_URL = %r(^(/[^/]+/[^/]+(?:/builds(?:/[\d]+)?|/cc)?)$) V1_REPO_URL = %r(^(/[^/]+/[^/]+(?:/builds(?:/[\d]+)?|/cc)?)$)
helpers :accept helpers :accept

View File

@ -5,6 +5,7 @@ class Travis::Api::App
autoload :Base, 'travis/api/app/responders/base' autoload :Base, 'travis/api/app/responders/base'
autoload :Image, 'travis/api/app/responders/image' autoload :Image, 'travis/api/app/responders/image'
autoload :Json, 'travis/api/app/responders/json' autoload :Json, 'travis/api/app/responders/json'
autoload :Plain, 'travis/api/app/responders/plain'
autoload :Service, 'travis/api/app/responders/service' autoload :Service, 'travis/api/app/responders/service'
autoload :Xml, 'travis/api/app/responders/xml' autoload :Xml, 'travis/api/app/responders/xml'
end end

View File

@ -0,0 +1,30 @@
module Travis::Api::App::Responders
class Plain < Base
def apply?
# make sure that we don't leak anything by processing only Artifact::Log
# instances here. I don't want to create entire new API builder just
# for log's content for now.
#
# TODO: think how to handle other formats correctly
options[:format] == 'txt' && resource.is_a?(Artifact::Log)
end
def apply
filename = resource.id
disposition = params[:attachment] ? 'attachment' : 'inline'
headers['Content-Disposition'] = %(#{disposition}; filename="#{filename}")
endpoint.content_type 'text/plain'
halt(params[:deansi] ? clear_ansi(resource.content) : resource.content)
end
private
def clear_ansi(content)
content.gsub(/\r\r/, "\r")
.gsub(/^.*\r(?!$)/, '')
.gsub(/\x1b(\[|\(|\))[;?0-9]*[0-9A-Za-z]/m, '')
end
end
end

View File

@ -9,4 +9,36 @@ describe Travis::Api::App::Endpoint::Artifacts do
get("/artifacts/#{id}", {}, 'HTTP_ACCEPT' => 'application/vnd.travis-ci.2+json, */*; q=0.01').should be_ok get("/artifacts/#{id}", {}, 'HTTP_ACCEPT' => 'application/vnd.travis-ci.2+json, */*; q=0.01').should be_ok
end end
end end
describe 'GET /artifacts/:id.txt' do
it 'loads the artifact' do
response = get("/artifacts/#{id}.txt", {})
response.should be_ok
response.body.should == artifact.content
response.headers['Content-Disposition'].should == "inline; filename=\"#{artifact.id}\""
end
it 'sets Content-Disposition to attachment with attachment=true param' do
response = get("/artifacts/#{id}.txt", {'attachment' => true})
response.should be_ok
response.body.should == artifact.content
response.headers['Content-Disposition'].should == "attachment; filename=\"#{artifact.id}\""
end
describe 'with deansi param' do
let(:content) {
"Fetching (0%)\rFetching (10%)\rFetching (100%)\n\e[32m"
}
let(:artifact) { Factory(:log, :content => content) }
it 'clears ansi escape control characters' do
response = get("/artifacts/#{id}.txt", {'deansi' => true})
response.should be_ok
response.body.should == "Fetching (100%)\n"
end
end
end
end end