Commit 3d1bc4a4 authored by Shinya Maeda's avatar Shinya Maeda

Fixed strcture for db change

parent c426763c
......@@ -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,11 +17,9 @@ 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
end
unless branch? || tag?
return error('Reference not found')
......
......@@ -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
......
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
......@@ -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
......@@ -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
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment