pipeline_schedules_controller.rb 3.2 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 :play_rate_limit, only: [:play]
5
  before_action :authorize_play_pipeline_schedule!, only: [:play]
6
  before_action :authorize_read_pipeline_schedule!
7
  before_action :authorize_create_pipeline_schedule!, only: [:new, :create]
8
  before_action :authorize_update_pipeline_schedule!, except: [:index, :new, :create, :play]
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 36 37
  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
38
    if schedule.update(schedule_params)
39
      redirect_to project_pipeline_schedules_path(@project)
40 41 42 43 44
    else
      render :edit
    end
  end

45
  def play
46 47
    job_id = RunPipelineScheduleWorker.perform_async(schedule.id, current_user.id)

48 49 50 51 52
    if job_id
      flash[:notice] = "Successfully scheduled a pipeline to run. Go to the <a href=\"#{project_pipelines_path(@project)}\">Pipelines page</a> for details.".html_safe
    else
      flash[:alert] = 'Unable to schedule a pipeline to run immediately'
    end
53 54 55 56

    redirect_to pipeline_schedules_path(@project)
  end

57 58 59 60
  def take_ownership
    if schedule.update(owner: current_user)
      redirect_to pipeline_schedules_path(@project)
    else
61
      redirect_to pipeline_schedules_path(@project), alert: _("Failed to change the owner")
62 63 64 65 66
    end
  end

  def destroy
    if schedule.destroy
Lin Jen-Shin's avatar
Lin Jen-Shin committed
67
      redirect_to pipeline_schedules_path(@project), status: :found
68
    else
69
      redirect_to pipeline_schedules_path(@project),
Shinya Maeda's avatar
Shinya Maeda committed
70
                  status: :forbidden,
71
                  alert: _("Failed to remove the pipeline schedule")
72 73 74 75 76
    end
  end

  private

77 78 79 80 81 82 83 84 85
  def play_rate_limit
    return unless current_user

    limiter = ::Gitlab::ActionRateLimiter.new(action: :play_pipeline_schedule)

    return unless limiter.throttled?([current_user, schedule], 1)

    flash[:alert] = 'You cannot play this scheduled pipeline at the moment. Please wait a minute.'
    redirect_to pipeline_schedules_path(@project)
86 87
  end

88 89 90 91 92 93
  def schedule
    @schedule ||= project.pipeline_schedules.find(params[:id])
  end

  def schedule_params
    params.require(:schedule)
94
      .permit(:description, :cron, :cron_timezone, :ref, :active,
95
        variables_attributes: [:id, :key, :secret_value, :_destroy] )
96
  end
97

98 99 100 101
  def authorize_play_pipeline_schedule!
    return access_denied! unless can?(current_user, :play_pipeline_schedule, schedule)
  end

102 103 104
  def authorize_update_pipeline_schedule!
    return access_denied! unless can?(current_user, :update_pipeline_schedule, schedule)
  end
105 106 107 108

  def authorize_admin_pipeline_schedule!
    return access_denied! unless can?(current_user, :admin_pipeline_schedule, schedule)
  end
109
end