pipeline_schedules_controller.rb 2.16 KB
Newer Older
1
class Projects::PipelineSchedulesController < Projects::ApplicationController
Shinya Maeda's avatar
Shinya Maeda committed
2
  before_action :schedule, except: [:index, :new, :create]
3

4
  before_action :authorize_read_pipeline_schedule!
5
  before_action :authorize_create_pipeline_schedule!, only: [:new, :create]
Shinya Maeda's avatar
Shinya Maeda committed
6
  before_action :authorize_update_pipeline_schedule!, except: [:index, :new, :create]
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]

  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
    if Ci::UpdatePipelineScheduleService
        .new(@project, current_user, schedule_params).execute(schedule)
38 39 40 41 42 43 44 45 46 47
      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
48
      redirect_to pipeline_schedules_path(@project), alert: _("Failed to change the owner")
49 50 51 52 53
    end
  end

  def destroy
    if schedule.destroy
54
      redirect_to pipeline_schedules_path(@project), status: 302
55
    else
56
      redirect_to pipeline_schedules_path(@project),
Shinya Maeda's avatar
Shinya Maeda committed
57
                  status: :forbidden,
58
                  alert: _("Failed to remove the pipeline schedule")
59 60 61 62 63 64 65 66 67 68 69
    end
  end

  private

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

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

  def authorize_update_pipeline_schedule!
    return access_denied! unless can?(current_user, :update_pipeline_schedule, schedule)
  end
77
end