diff --git a/lib/travis/api/v3/models/cron.rb b/lib/travis/api/v3/models/cron.rb index d3d4065f..b784634a 100644 --- a/lib/travis/api/v3/models/cron.rb +++ b/lib/travis/api/v3/models/cron.rb @@ -3,20 +3,36 @@ module Travis::API::V3 belongs_to :branch + def self.start_all + self.all.each do |cron| + cron.start if cron.next_build_time <= Time.now + end + end + + def start + raise ServerError, 'repository does not have a github_id'.freeze unless branch.repository.github_id + + payload = { + repository: { id: branch.repository.github_id, owner_name: branch.repository.owner_name, name: branch.repository.name }, + user: { id: user.id }, + message: '', + branch: branch, + config: {} + } + + token = "TODO: Figure out where to get this (branch.repository.owner ?)" + perform_async(:build_request, type: 'cron'.freeze, credentials: { token: token }, payload: JSON.dump(payload)) + payload + end + 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 + if (disable_by_build) && (last_non_cron_build_date > last_planned_time) + return after_next_planned_time + elsif last_cron_build_date >= last_planned_time + return next_planned_time else - if last_cron_build_date >= last_planned_time - return next_planned_time - else - return Time.now - end + return Time.now end end diff --git a/spec/v3/models/cron_spec.rb b/spec/v3/models/cron_spec.rb index 21a800db..a991301c 100644 --- a/spec/v3/models/cron_spec.rb +++ b/spec/v3/models/cron_spec.rb @@ -155,5 +155,77 @@ describe Travis::API::V3::Models::Cron do end + describe "build starts now if next build time is in the past" do + + before do + # nothing, this time + # time freeze is performed in examples + end + + after do + Timecop.return + end + + it "for daily builds with disable_by_build true" do + Timecop.travel(DateTime.new(2015, 12, 31, 16)) + cron = Travis::API::V3::Models::Cron.create(branch_id: branch.id, interval: 'daily', disable_by_build: true) + build = Travis::API::V3::Models::Build.create(:repository_id => repo.id, :branch_name => branch.name, :event_type => 'cron') + Timecop.freeze(DateTime.new(2016, 1, 1, 19)) + expect(cron.next_build_time).to be == DateTime.now + build.destroy + cron.destroy + end + + it "for daily builds with disable_by_build false" do + Timecop.travel(DateTime.new(2015, 12, 31, 16)) + cron = Travis::API::V3::Models::Cron.create(branch_id: branch.id, interval: 'daily', disable_by_build: false) + build = Travis::API::V3::Models::Build.create(:repository_id => repo.id, :branch_name => branch.name, :event_type => 'cron') + Timecop.freeze(DateTime.new(2016, 1, 1, 19)) + expect(cron.next_build_time).to be == DateTime.now + build.destroy + cron.destroy + end + + it "for weekly builds with disable_by_build true" do + Timecop.travel(DateTime.new(2015, 12, 31, 16)) + cron = Travis::API::V3::Models::Cron.create(branch_id: branch.id, interval: 'weekly', disable_by_build: true) + build = Travis::API::V3::Models::Build.create(:repository_id => repo.id, :branch_name => branch.name, :event_type => 'cron') + Timecop.freeze(DateTime.new(2016, 1, 7, 19)) + expect(cron.next_build_time).to be == DateTime.now + build.destroy + cron.destroy + end + + it "for weekly builds with disable_by_build false" do + Timecop.travel(DateTime.new(2015, 12, 31, 16)) + cron = Travis::API::V3::Models::Cron.create(branch_id: branch.id, interval: 'weekly', disable_by_build: false) + build = Travis::API::V3::Models::Build.create(:repository_id => repo.id, :branch_name => branch.name, :event_type => 'cron') + Timecop.freeze(DateTime.new(2016, 1, 7, 19)) + expect(cron.next_build_time).to be == DateTime.now + build.destroy + cron.destroy + end + + it "for monthly builds with disable_by_build true" do + Timecop.travel(DateTime.new(2015, 12, 31, 16)) + cron = Travis::API::V3::Models::Cron.create(branch_id: branch.id, interval: 'monthly', disable_by_build: true) + build = Travis::API::V3::Models::Build.create(:repository_id => repo.id, :branch_name => branch.name, :event_type => 'cron') + Timecop.freeze(DateTime.new(2016, 1, 31, 19)) + expect(cron.next_build_time).to be == DateTime.now + build.destroy + cron.destroy + end + + it "for monthly builds with disable_by_build false" do + Timecop.travel(DateTime.new(2015, 12, 31, 16)) + cron = Travis::API::V3::Models::Cron.create(branch_id: branch.id, interval: 'monthly', disable_by_build: false) + build = Travis::API::V3::Models::Build.create(:repository_id => repo.id, :branch_name => branch.name, :event_type => 'cron') + Timecop.freeze(DateTime.new(2016, 1, 31, 19)) + expect(cron.next_build_time).to be == DateTime.now + build.destroy + cron.destroy + end + + end end