Welcome to mirror list, hosted at ThFree Co, Russian Federation.

pipeline_schedules_controller.rb « projects « controllers « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 96c9aa899539d01dcb04089c2a20a404276357b5 (plain)
1
2
3
4
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# frozen_string_literal: true

class Projects::PipelineSchedulesController < Projects::ApplicationController
  before_action :schedule, except: [:index, :new, :create]

  before_action :check_play_rate_limit!, only: [:play]
  before_action :authorize_play_pipeline_schedule!, only: [:play]
  before_action :authorize_read_pipeline_schedule!
  before_action :authorize_create_pipeline_schedule!, only: [:new, :create]
  before_action :authorize_update_pipeline_schedule!, only: [:edit, :update]
  before_action :authorize_admin_pipeline_schedule!, only: [:take_ownership, :destroy]
  before_action :push_schedule_feature_flag, only: [:index, :new, :edit]

  feature_category :continuous_integration
  urgency :low

  def index
    @scope = params[:scope]
    @all_schedules = Ci::PipelineSchedulesFinder.new(@project).execute
    @schedules = Ci::PipelineSchedulesFinder.new(@project).execute(scope: params[:scope])
  end

  def new
  end

  def create
    if ::Feature.enabled?(:ci_refactoring_pipeline_schedule_create_service, @project)
      response = Ci::PipelineSchedules::CreateService.new(@project, current_user, schedule_params).execute
      @schedule = response.payload

      if response.success?
        redirect_to pipeline_schedules_path(@project)
      else
        render :new
      end
    else
      @schedule = Ci::CreatePipelineScheduleService
        .new(@project, current_user, schedule_params)
        .execute

      if @schedule.persisted?
        redirect_to pipeline_schedules_path(@project)
      else
        render :new
      end
    end
  end

  def edit
  end

  def update
    response = Ci::PipelineSchedules::UpdateService.new(schedule, current_user, schedule_params).execute

    if response.success?
      redirect_to project_pipeline_schedules_path(@project)
    else
      render :edit
    end
  end

  def play
    job_id = RunPipelineScheduleWorker.perform_async(schedule.id, current_user.id) # rubocop:disable CodeReuse/Worker

    if job_id
      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
    else
      flash[:alert] = _('Unable to schedule a pipeline to run immediately')
    end

    redirect_to pipeline_schedules_path(@project)
  end

  def take_ownership
    response = Ci::PipelineSchedules::TakeOwnershipService.new(schedule, current_user).execute

    if response.success?
      redirect_to pipeline_schedules_path(@project)
    else
      redirect_to pipeline_schedules_path(@project), alert: _("Failed to change the owner")
    end
  end

  def destroy
    if schedule.destroy
      redirect_to pipeline_schedules_path(@project), status: :found
    else
      redirect_to pipeline_schedules_path(@project), status: :forbidden, alert: _("Failed to remove the pipeline schedule")
    end
  end

  private

  def check_play_rate_limit!
    return unless current_user

    check_rate_limit!(:play_pipeline_schedule, scope: [current_user, schedule]) do
      flash[:alert] = _('You cannot play this scheduled pipeline at the moment. Please wait a minute.')
      redirect_to pipeline_schedules_path(@project)
    end
  end

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

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

  def new_schedule
    # We need the `ref` here for `authorize_create_pipeline_schedule!`
    @schedule ||= project.pipeline_schedules.new(ref: params.dig(:schedule, :ref))
  end

  def authorize_create_pipeline_schedule!
    return access_denied! unless can?(current_user, :create_pipeline_schedule, new_schedule)
  end

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

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

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

  def push_schedule_feature_flag
    push_frontend_feature_flag(:pipeline_schedules_vue, @project)
  end
end