debian-mirror-gitlab/app/controllers/projects/pipeline_schedules_controller.rb

124 lines
3.8 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
class Projects::PipelineSchedulesController < Projects::ApplicationController
2017-09-10 17:25:29 +05:30
before_action :schedule, except: [:index, :new, :create]
2022-01-26 12:08:38 +05:30
before_action :check_play_rate_limit!, only: [:play]
2018-03-17 18:26:18 +05:30
before_action :authorize_play_pipeline_schedule!, only: [:play]
2017-08-17 22:00:37 +05:30
before_action :authorize_read_pipeline_schedule!
2017-09-10 17:25:29 +05:30
before_action :authorize_create_pipeline_schedule!, only: [:new, :create]
2022-05-03 16:02:30 +05:30
before_action :authorize_update_pipeline_schedule!, only: [:edit, :update]
before_action :authorize_take_ownership_pipeline_schedule!, only: [:take_ownership]
2017-08-17 22:00:37 +05:30
before_action :authorize_admin_pipeline_schedule!, only: [:destroy]
2022-11-25 23:54:43 +05:30
before_action :push_schedule_feature_flag, only: [:index, :new, :edit]
2017-08-17 22:00:37 +05:30
2021-01-03 14:25:43 +05:30
feature_category :continuous_integration
2022-07-16 23:28:13 +05:30
urgency :low
2021-01-03 14:25:43 +05:30
2017-08-17 22:00:37 +05:30
def index
@scope = params[:scope]
2020-04-08 14:13:33 +05:30
@all_schedules = Ci::PipelineSchedulesFinder.new(@project).execute
@schedules = Ci::PipelineSchedulesFinder.new(@project).execute(scope: params[:scope])
2017-08-17 22:00:37 +05:30
end
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
if schedule.update(schedule_params)
2017-09-10 17:25:29 +05:30
redirect_to project_pipeline_schedules_path(@project)
2017-08-17 22:00:37 +05:30
else
render :edit
end
end
2018-03-17 18:26:18 +05:30
def play
2020-03-13 15:44:24 +05:30
job_id = RunPipelineScheduleWorker.perform_async(schedule.id, current_user.id) # rubocop:disable CodeReuse/Worker
2018-03-17 18:26:18 +05:30
if job_id
2019-07-07 11:18:12 +05:30
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>" }
flash[:notice] = message.html_safe
2018-03-17 18:26:18 +05:30
else
2019-07-07 11:18:12 +05:30
flash[:alert] = _('Unable to schedule a pipeline to run immediately')
2018-03-17 18:26:18 +05:30
end
redirect_to pipeline_schedules_path(@project)
end
2017-08-17 22:00:37 +05:30
def take_ownership
if schedule.update(owner: current_user)
redirect_to pipeline_schedules_path(@project)
else
2017-09-10 17:25:29 +05:30
redirect_to pipeline_schedules_path(@project), alert: _("Failed to change the owner")
2017-08-17 22:00:37 +05:30
end
end
def destroy
if schedule.destroy
2018-11-18 11:00:15 +05:30
redirect_to pipeline_schedules_path(@project), status: :found
2017-08-17 22:00:37 +05:30
else
2017-09-10 17:25:29 +05:30
redirect_to pipeline_schedules_path(@project),
status: :forbidden,
alert: _("Failed to remove the pipeline schedule")
2017-08-17 22:00:37 +05:30
end
end
private
2022-01-26 12:08:38 +05:30
def check_play_rate_limit!
2018-03-17 18:26:18 +05:30
return unless current_user
2022-01-26 12:08:38 +05:30
check_rate_limit!(:play_pipeline_schedule, scope: [current_user, schedule]) do
2020-01-01 13:55:28 +05:30
flash[:alert] = _('You cannot play this scheduled pipeline at the moment. Please wait a minute.')
redirect_to pipeline_schedules_path(@project)
end
end
2018-03-17 18:26:18 +05:30
2017-08-17 22:00:37 +05:30
def schedule
@schedule ||= project.pipeline_schedules.find(params[:id])
end
def schedule_params
params.require(:schedule)
2017-09-10 17:25:29 +05:30
.permit(:description, :cron, :cron_timezone, :ref, :active,
2023-01-13 00:05:48 +05:30
variables_attributes: [:id, :variable_type, :key, :secret_value, :_destroy])
2017-09-10 17:25:29 +05:30
end
2018-03-17 18:26:18 +05:30
def authorize_play_pipeline_schedule!
return access_denied! unless can?(current_user, :play_pipeline_schedule, schedule)
end
2017-09-10 17:25:29 +05:30
def authorize_update_pipeline_schedule!
return access_denied! unless can?(current_user, :update_pipeline_schedule, schedule)
end
2022-05-03 16:02:30 +05:30
def authorize_take_ownership_pipeline_schedule!
return access_denied! unless can?(current_user, :take_ownership_pipeline_schedule, schedule)
end
2017-09-10 17:25:29 +05:30
def authorize_admin_pipeline_schedule!
return access_denied! unless can?(current_user, :admin_pipeline_schedule, schedule)
2017-08-17 22:00:37 +05:30
end
2022-11-25 23:54:43 +05:30
def push_schedule_feature_flag
push_frontend_feature_flag(:pipeline_schedules_vue, @project)
end
2017-08-17 22:00:37 +05:30
end