pipeline_schedules_controller.rb 2.13 KB
Newer Older
1 2
class Projects::PipelineSchedulesController < Projects::ApplicationController
  before_action :authorize_read_pipeline_schedule!
3 4
  before_action :authorize_create_pipeline_schedule!, only: [:new, :create]
  before_action :authorize_update_pipeline_schedule!, only: [:edit, :take_ownership, :update]
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
  before_action :authorize_admin_pipeline_schedule!, only: [:destroy]

  before_action :schedule, only: [:edit, :update, :destroy, :take_ownership]

  def index
    @scope = params[:scope]
    @all_schedules = PipelineSchedulesFinder.new(@project).execute
    @schedules = PipelineSchedulesFinder.new(@project).execute(scope: params[:scope])
      .includes(:last_pipeline)
  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
36 37
    return access_denied! unless can?(current_user, :update_pipeline_schedule, schedule)

38 39
    if Ci::CreatePipelineScheduleService
        .new(@project, current_user, schedule_params).update(schedule)
40 41 42 43 44 45 46 47 48 49
      redirect_to namespace_project_pipeline_schedules_path(@project.namespace.becomes(Namespace), @project)
    else
      render :edit
    end
  end

  def take_ownership
    if schedule.update(owner: current_user)
      redirect_to pipeline_schedules_path(@project)
    else
50
      redirect_to pipeline_schedules_path(@project), alert: _("Failed to change the owner")
51 52 53 54 55
    end
  end

  def destroy
    if schedule.destroy
56
      redirect_to pipeline_schedules_path(@project), status: 302
57
    else
58 59
      redirect_to pipeline_schedules_path(@project),
                  status: 302,
60
                  alert: _("Failed to remove the pipeline schedule")
61 62 63 64 65 66 67 68 69 70 71
    end
  end

  private

  def schedule
    @schedule ||= project.pipeline_schedules.find(params[:id])
  end

  def schedule_params
    params.require(:schedule)
72
      .permit(:description, :cron, :cron_timezone, :ref, :active,
73
        variables_attributes: [:id, :key, :value, :_destroy] )
74 75
  end
end