diff --git a/lib/travis/api/v3/queries/log.rb b/lib/travis/api/v3/queries/log.rb index 9c7035ce..e799c9ed 100644 --- a/lib/travis/api/v3/queries/log.rb +++ b/lib/travis/api/v3/queries/log.rb @@ -1,16 +1,36 @@ module Travis::API::V3 class Queries::Log < Query + require 'net/http' + require 'uri' + def find(job) - #TODO check for the log in the DB, if it's not there then fetch it from S3. + #check for the log in the DB log = Models::Log.find_by_job_id - #if the log exists and has not bee archived yet, then collect the log_parts and return the contents + #if the log exists and has not been archived yet, then collect the log_parts and return the contents unless log.nil? || !log.archived_at.nil? log_parts = Models::Log::Part.where(log_id: log.id) - log_parts.each { |log_part| contents << log_part.content } - contents - else - #go look in S3 + log_data = [] + log_parts.each { |log_part| log_data << log_part.content } + log_data + elsif log.archived_at? + # if it's not there then fetch it from S3. + archived_log_path = archive_url("/jobs/#{params[:job.id]}/log.txt") + + content = open(Net::HTTP.get(URI.parse(archived_log_path))) + archived_log_data = [] + content.each_line do |line| + archived_log_data << line.chop + end + archived_log_data end end + + def archive_url(path) + "https://s3.amazonaws.com/#{hostname('archive')}#{path}" + end + + def hostname(name) + "#{name}#{'-staging' if Travis.env == 'staging'}.#{Travis.config.host.split('.')[-2, 2].join('.')}" + end end end