show next build time for cron
This commit is contained in:
parent
ffcf8985c0
commit
00e15de083
|
@ -3,5 +3,103 @@ module Travis::API::V3
|
|||
|
||||
belongs_to :branch
|
||||
|
||||
def next_build_time
|
||||
|
||||
if disable_by_build
|
||||
if last_non_cron_build_date > last_planned_time
|
||||
return after_next_planned_time
|
||||
else
|
||||
return next_planned_time
|
||||
end
|
||||
else
|
||||
if last_cron_build_date >= last_planned_time
|
||||
return next_planned_time
|
||||
else
|
||||
return Time.now
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def next_planned_time
|
||||
now = DateTime.now
|
||||
created = DateTime.new(created_at.year, created_at.month, created_at.day, created_at.hour)
|
||||
case interval
|
||||
when 'daily'
|
||||
build_today = DateTime.new(now.year, now.month, now.day, created_at.hour)
|
||||
if now > build_today
|
||||
return build_today + 1
|
||||
else
|
||||
return build_today
|
||||
end
|
||||
when 'weekly'
|
||||
build_today = DateTime.new(now.year, now.month, now.day, created_at.hour)
|
||||
in_days = (created_at.wday - now.wday) % 7
|
||||
next_time = build_today + in_days
|
||||
if now > next_time
|
||||
return build_today + 7
|
||||
else
|
||||
return next_time
|
||||
end
|
||||
when 'monthly'
|
||||
month_since_creation = (now.year*12+now.month) - (created_at.year*12+created_at.month)
|
||||
this_month = created >> month_since_creation
|
||||
if now > this_month
|
||||
return created >> (month_since_creation+1)
|
||||
else
|
||||
return this_month
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def last_planned_time
|
||||
now = DateTime.now
|
||||
created = DateTime.new(created_at.year, created_at.month, created_at.day, created_at.hour)
|
||||
case interval
|
||||
when 'daily'
|
||||
return next_planned_time - 1
|
||||
when 'weekly'
|
||||
return next_planned_time - 7
|
||||
when 'monthly'
|
||||
month_since_creation = (now.year*12+now.month) - (created_at.year*12+created_at.month)
|
||||
this_month = created >> month_since_creation
|
||||
if now > this_month
|
||||
return this_month
|
||||
else
|
||||
return created >> (month_since_creation-1)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def after_next_planned_time
|
||||
now = DateTime.now
|
||||
created = DateTime.new(created_at.year, created_at.month, created_at.day, created_at.hour)
|
||||
case interval
|
||||
when 'daily'
|
||||
return next_planned_time + 1
|
||||
when 'weekly'
|
||||
return next_planned_time + 7
|
||||
when 'monthly'
|
||||
month_since_creation = (now.year*12+now.month) - (created_at.year*12+created_at.month)
|
||||
this_month = created >> month_since_creation
|
||||
if now > this_month
|
||||
return created >> (month_since_creation+2)
|
||||
else
|
||||
return created >> (month_since_creation+1)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def last_cron_build_date
|
||||
last_cron_build = Models::Build.where(:repository_id => branch.repository.id, :branch => branch.name, :event_type => 'cron').order("id DESC").first
|
||||
return last_cron_build.created_at unless last_cron_build.nil?
|
||||
return Time.at(0)
|
||||
end
|
||||
|
||||
def last_non_cron_build_date
|
||||
last_build = Models::Build.where(:repository_id => branch.repository.id, :branch => branch.name).where(['event_type NOT IN (?)', ['cron']]).order("id DESC").first
|
||||
return last_build.created_at unless last_build.nil?
|
||||
return Time.at(0)
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
|
|
@ -45,7 +45,7 @@ module Travis::API::V3
|
|||
when Hash then value.map { |k, v| [k, render_value(v, **options)] }.to_h
|
||||
when Array then value.map { |v | render_value(v, **options) }
|
||||
when *PRIMITIVE then value
|
||||
when Time then value.strftime('%Y-%m-%dT%H:%M:%SZ')
|
||||
when Time, DateTime then value.strftime('%Y-%m-%dT%H:%M:%SZ')
|
||||
when Model then render_model(value, **options)
|
||||
when ActiveRecord::Relation then render_value(value.to_a, **options)
|
||||
when ActiveRecord::Associations::CollectionProxy then render_value(value.to_a, **options)
|
||||
|
|
|
@ -3,7 +3,7 @@ require 'travis/api/v3/renderer/model_renderer'
|
|||
module Travis::API::V3
|
||||
class Renderer::Cron < Renderer::ModelRenderer
|
||||
representation(:minimal, :id)
|
||||
representation(:standard, :id, :repository, :branch, :interval, :disable_by_build)
|
||||
representation(:standard, :id, :repository, :branch, :interval, :disable_by_build, :next_build_time)
|
||||
|
||||
def repository
|
||||
model.branch.repository
|
||||
|
|
Loading…
Reference in New Issue
Block a user