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

Fixed strcture for db change

parent c426763c
...@@ -2,14 +2,14 @@ module Ci ...@@ -2,14 +2,14 @@ module Ci
class CreatePipelineService < BaseService class CreatePipelineService < BaseService
attr_reader :pipeline 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( @pipeline = Ci::Pipeline.new(
project: project, project: project,
ref: ref, ref: ref,
sha: sha, sha: sha,
before_sha: before_sha, before_sha: before_sha,
tag: tag?, tag: tag?,
trigger_requests: (scheduled_trigger) ? [] : Array(trigger_request), trigger_requests: Array(trigger_request),
user: current_user user: current_user
) )
...@@ -17,10 +17,8 @@ module Ci ...@@ -17,10 +17,8 @@ module Ci
return error('Pipeline is disabled') return error('Pipeline is disabled')
end end
unless scheduled_trigger unless trigger_request || can?(current_user, :create_pipeline, project)
unless trigger_request || can?(current_user, :create_pipeline, project) return error('Insufficient permissions to create a new pipeline')
return error('Insufficient permissions to create a new pipeline')
end
end end
unless branch? || tag? unless branch? || tag?
......
...@@ -3,14 +3,15 @@ class TriggerScheduleWorker ...@@ -3,14 +3,15 @@ class TriggerScheduleWorker
include CronjobQueue include CronjobQueue
def perform 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 begin
Ci::CreatePipelineService.new(trigger.project, trigger.owner, ref: trigger.ref). Ci::CreateTriggerRequestService.new.execute(trigger_schedule.trigger.project,
execute(ignore_skip_ci: true, scheduled_trigger: true) trigger_schedule.trigger,
trigger_schedule.trigger.ref)
rescue => e 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 ensure
trigger.schedule_next_run! trigger_schedule.schedule_next_run!
end end
end end
end end
......
FactoryGirl.define do FactoryGirl.define do
factory :ci_trigger_without_token, class: Ci::Trigger do factory :ci_trigger_without_token, class: Ci::Trigger do
factory :ci_trigger do factory :ci_trigger do
token 'token' token { SecureRandom.hex(10) }
end end
end end
end end
...@@ -214,9 +214,5 @@ describe Ci::CreatePipelineService, services: true do ...@@ -214,9 +214,5 @@ describe Ci::CreatePipelineService, services: true do
expect(Environment.find_by(name: "review/master")).not_to be_nil expect(Environment.find_by(name: "review/master")).not_to be_nil
end end
end end
context 'when scheduled_trigger' do
# TODO: spec if approved
end
end end
end end
...@@ -8,18 +8,26 @@ describe TriggerScheduleWorker do ...@@ -8,18 +8,26 @@ describe TriggerScheduleWorker do
end end
context 'when there is a scheduled trigger within next_run_at' do 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 before do
create(:ci_trigger_schedule, :cron_nightly_build, :force_triggable)
worker.perform worker.perform
end 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 it 'creates a new pipeline' do
expect(Ci::Pipeline.last.status).to eq('pending') expect(Ci::Pipeline.last.status).to eq('pending')
end end
it 'schedules next_run_at' do it 'schedules next_run_at' do
trigger_schedule2 = create(:ci_trigger_schedule, :cron_nightly_build) next_time = Ci::CronParser.new('0 1 * * *', 'Europe/Istanbul').next_time_from_now
expect(Ci::TriggerSchedule.last.next_run_at).to eq(trigger_schedule2.next_run_at) expect(Ci::TriggerSchedule.last.next_run_at).to eq(next_time)
end end
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