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

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

  private

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

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

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