2018-09-25 23:45:43 -04:00
# frozen_string_literal: true
2017-05-07 18:35:56 -04:00
class Projects :: PipelineSchedulesController < Projects :: ApplicationController
2017-06-28 08:42:13 -04:00
before_action :schedule , except : [ :index , :new , :create ]
2017-06-27 03:23:16 -04:00
2017-12-12 18:46:05 -05:00
before_action :play_rate_limit , only : [ :play ]
2017-12-06 02:23:59 -05:00
before_action :authorize_play_pipeline_schedule! , only : [ :play ]
2017-05-07 18:35:56 -04:00
before_action :authorize_read_pipeline_schedule!
2017-06-01 10:30:12 -04:00
before_action :authorize_create_pipeline_schedule! , only : [ :new , :create ]
2017-12-06 01:24:20 -05:00
before_action :authorize_update_pipeline_schedule! , except : [ :index , :new , :create , :play ]
2017-05-07 18:35:56 -04:00
before_action :authorize_admin_pipeline_schedule! , only : [ :destroy ]
2021-06-14 11:09:48 -04:00
before_action do
push_frontend_feature_flag ( :ci_daily_limit_for_pipeline_schedules , @project , default_enabled : :yaml )
end
2020-10-08 14:08:32 -04:00
feature_category :continuous_integration
2018-08-27 11:31:01 -04:00
# rubocop: disable CodeReuse/ActiveRecord
2017-05-07 18:35:56 -04:00
def index
@scope = params [ :scope ]
2020-03-13 20:09:30 -04:00
@all_schedules = Ci :: PipelineSchedulesFinder . new ( @project ) . execute
@schedules = Ci :: PipelineSchedulesFinder . new ( @project ) . execute ( scope : params [ :scope ] )
2017-05-07 18:35:56 -04:00
end
2018-08-27 11:31:01 -04:00
# rubocop: enable CodeReuse/ActiveRecord
2017-05-07 18:35:56 -04:00
def new
@schedule = project . pipeline_schedules . new
end
def create
@schedule = Ci :: CreatePipelineScheduleService
. new ( @project , current_user , schedule_params )
. execute
if @schedule . persisted?
redirect_to pipeline_schedules_path ( @project )
else
render :new
end
end
def edit
end
def update
2017-07-05 11:23:28 -04:00
if schedule . update ( schedule_params )
2017-06-29 13:06:35 -04:00
redirect_to project_pipeline_schedules_path ( @project )
2017-05-07 18:35:56 -04:00
else
render :edit
end
end
2017-12-06 01:24:20 -05:00
def play
2020-02-14 16:09:08 -05:00
job_id = RunPipelineScheduleWorker . perform_async ( schedule . id , current_user . id ) # rubocop:disable CodeReuse/Worker
2017-12-03 01:55:49 -05:00
2017-12-12 18:46:05 -05:00
if job_id
2019-04-02 05:08:23 -04:00
pipelines_link_start = " <a href= \" #{ project_pipelines_path ( @project ) } \" > "
message = _ ( " Successfully scheduled a pipeline to run. Go to the %{pipelines_link_start}Pipelines page%{pipelines_link_end} for details. " ) % { pipelines_link_start : pipelines_link_start , pipelines_link_end : " </a> " }
2019-03-27 12:52:52 -04:00
flash [ :notice ] = message . html_safe
2017-12-12 18:46:05 -05:00
else
2019-03-27 12:52:52 -04:00
flash [ :alert ] = _ ( 'Unable to schedule a pipeline to run immediately' )
2017-12-12 18:46:05 -05:00
end
2017-12-03 01:55:49 -05:00
redirect_to pipeline_schedules_path ( @project )
end
2017-05-07 18:35:56 -04:00
def take_ownership
if schedule . update ( owner : current_user )
redirect_to pipeline_schedules_path ( @project )
else
2017-06-07 09:46:10 -04:00
redirect_to pipeline_schedules_path ( @project ) , alert : _ ( " Failed to change the owner " )
2017-05-07 18:35:56 -04:00
end
end
def destroy
if schedule . destroy
2018-07-02 06:43:06 -04:00
redirect_to pipeline_schedules_path ( @project ) , status : :found
2017-05-07 18:35:56 -04:00
else
2017-06-06 18:45:16 -04:00
redirect_to pipeline_schedules_path ( @project ) ,
2017-07-04 04:45:07 -04:00
status : :forbidden ,
2017-06-07 09:46:10 -04:00
alert : _ ( " Failed to remove the pipeline schedule " )
2017-05-07 18:35:56 -04:00
end
end
private
2017-12-12 18:46:05 -05:00
def play_rate_limit
return unless current_user
2019-12-05 16:07:40 -05:00
if rate_limiter . throttled? ( :play_pipeline_schedule , scope : [ current_user , schedule ] )
flash [ :alert ] = _ ( 'You cannot play this scheduled pipeline at the moment. Please wait a minute.' )
redirect_to pipeline_schedules_path ( @project )
end
end
2017-12-12 18:46:05 -05:00
2019-12-05 16:07:40 -05:00
def rate_limiter
:: Gitlab :: ApplicationRateLimiter
2017-12-09 04:01:42 -05:00
end
2017-05-07 18:35:56 -04:00
def schedule
@schedule || = project . pipeline_schedules . find ( params [ :id ] )
end
def schedule_params
params . require ( :schedule )
2017-06-22 14:57:13 -04:00
. permit ( :description , :cron , :cron_timezone , :ref , :active ,
2019-05-06 09:11:42 -04:00
variables_attributes : [ :id , :variable_type , :key , :secret_value , :_destroy ] )
2017-06-27 03:23:16 -04:00
end
2017-06-27 03:23:16 -04:00
2017-12-06 02:23:59 -05:00
def authorize_play_pipeline_schedule!
return access_denied! unless can? ( current_user , :play_pipeline_schedule , schedule )
end
2017-06-27 03:23:16 -04:00
def authorize_update_pipeline_schedule!
return access_denied! unless can? ( current_user , :update_pipeline_schedule , schedule )
end
2017-07-07 06:09:35 -04:00
def authorize_admin_pipeline_schedule!
return access_denied! unless can? ( current_user , :admin_pipeline_schedule , schedule )
end
2017-05-07 18:35:56 -04:00
end