diff options
-rw-r--r-- | app/services/ci/create_pipeline_service.rb | 10 | ||||
-rw-r--r-- | app/workers/trigger_schedule_worker.rb | 11 | ||||
-rw-r--r-- | spec/factories/ci/trigger_schedules.rb (renamed from spec/factories/ci/scheduled_triggers.rb) | 0 | ||||
-rw-r--r-- | spec/factories/ci/triggers.rb | 2 | ||||
-rw-r--r-- | spec/services/ci/create_pipeline_service_spec.rb | 4 | ||||
-rw-r--r-- | spec/workers/trigger_schedule_worker_spec.rb | 14 |
6 files changed, 22 insertions, 19 deletions
diff --git a/app/services/ci/create_pipeline_service.rb b/app/services/ci/create_pipeline_service.rb index 6e3880e1e63..38a85e9fc42 100644 --- a/app/services/ci/create_pipeline_service.rb +++ b/app/services/ci/create_pipeline_service.rb @@ -2,14 +2,14 @@ module Ci class CreatePipelineService < BaseService attr_reader :pipeline - def execute(ignore_skip_ci: false, save_on_errors: true, trigger_request: nil, scheduled_trigger: false) + def execute(ignore_skip_ci: false, save_on_errors: true, trigger_request: nil) @pipeline = Ci::Pipeline.new( project: project, ref: ref, sha: sha, before_sha: before_sha, tag: tag?, - trigger_requests: (scheduled_trigger) ? [] : Array(trigger_request), + trigger_requests: Array(trigger_request), user: current_user ) @@ -17,10 +17,8 @@ module Ci return error('Pipeline is disabled') end - unless scheduled_trigger - unless trigger_request || can?(current_user, :create_pipeline, project) - return error('Insufficient permissions to create a new pipeline') - end + unless trigger_request || can?(current_user, :create_pipeline, project) + return error('Insufficient permissions to create a new pipeline') end unless branch? || tag? diff --git a/app/workers/trigger_schedule_worker.rb b/app/workers/trigger_schedule_worker.rb index d55e9378e02..04a53a38adb 100644 --- a/app/workers/trigger_schedule_worker.rb +++ b/app/workers/trigger_schedule_worker.rb @@ -3,14 +3,15 @@ class TriggerScheduleWorker include CronjobQueue def perform - Ci::TriggerSchedule.where("next_run_at < ?", Time.now).find_each do |trigger| + Ci::TriggerSchedule.where("next_run_at < ?", Time.now).find_each do |trigger_schedule| begin - Ci::CreatePipelineService.new(trigger.project, trigger.owner, ref: trigger.ref). - execute(ignore_skip_ci: true, scheduled_trigger: true) + Ci::CreateTriggerRequestService.new.execute(trigger_schedule.trigger.project, + trigger_schedule.trigger, + trigger_schedule.trigger.ref) rescue => e - Rails.logger.error "#{trigger.id}: Failed to trigger job: #{e.message}" + Rails.logger.error "#{trigger_schedule.id}: Failed to trigger_schedule job: #{e.message}" ensure - trigger.schedule_next_run! + trigger_schedule.schedule_next_run! end end end diff --git a/spec/factories/ci/scheduled_triggers.rb b/spec/factories/ci/trigger_schedules.rb index f909e343bf2..f909e343bf2 100644 --- a/spec/factories/ci/scheduled_triggers.rb +++ b/spec/factories/ci/trigger_schedules.rb diff --git a/spec/factories/ci/triggers.rb b/spec/factories/ci/triggers.rb index a27b04424e5..1feaa9b9fa1 100644 --- a/spec/factories/ci/triggers.rb +++ b/spec/factories/ci/triggers.rb @@ -1,7 +1,7 @@ FactoryGirl.define do factory :ci_trigger_without_token, class: Ci::Trigger do factory :ci_trigger do - token 'token' + token { SecureRandom.hex(10) } end end end diff --git a/spec/services/ci/create_pipeline_service_spec.rb b/spec/services/ci/create_pipeline_service_spec.rb index 4e34acc3585..d2f0337c260 100644 --- a/spec/services/ci/create_pipeline_service_spec.rb +++ b/spec/services/ci/create_pipeline_service_spec.rb @@ -214,9 +214,5 @@ describe Ci::CreatePipelineService, services: true do expect(Environment.find_by(name: "review/master")).not_to be_nil end end - - context 'when scheduled_trigger' do - # TODO: spec if approved - end end end diff --git a/spec/workers/trigger_schedule_worker_spec.rb b/spec/workers/trigger_schedule_worker_spec.rb index 6c7521e8339..2cf51a31c71 100644 --- a/spec/workers/trigger_schedule_worker_spec.rb +++ b/spec/workers/trigger_schedule_worker_spec.rb @@ -8,18 +8,26 @@ describe TriggerScheduleWorker do end context 'when there is a scheduled trigger within next_run_at' do + let(:user) { create(:user) } + let(:project) { create(:project) } + let(:trigger) { create(:ci_trigger, owner: user, project: project, ref: 'master') } + let!(:trigger_schedule) { create(:ci_trigger_schedule, :cron_nightly_build, :force_triggable, trigger: trigger, project: project) } + before do - create(:ci_trigger_schedule, :cron_nightly_build, :force_triggable) worker.perform end + it 'creates a new trigger request' do + expect(Ci::TriggerRequest.first.trigger_id).to eq(trigger.id) + end + it 'creates a new pipeline' do expect(Ci::Pipeline.last.status).to eq('pending') end it 'schedules next_run_at' do - trigger_schedule2 = create(:ci_trigger_schedule, :cron_nightly_build) - expect(Ci::TriggerSchedule.last.next_run_at).to eq(trigger_schedule2.next_run_at) + next_time = Ci::CronParser.new('0 1 * * *', 'Europe/Istanbul').next_time_from_now + expect(Ci::TriggerSchedule.last.next_run_at).to eq(next_time) end end |