Read about our upcoming Code of Conduct on this issue

pipelines_controller.rb 5.07 KB
Newer Older
1
2
# frozen_string_literal: true

Kamil Trzcinski's avatar
Kamil Trzcinski committed
3
class Projects::PipelinesController < Projects::ApplicationController
4
  before_action :whitelist_query_limiting, only: [:create, :retry]
5
  before_action :pipeline, except: [:index, :new, :create, :charts]
Kamil Trzcinski's avatar
WIP    
Kamil Trzcinski committed
6
7
8
9
  before_action :authorize_read_pipeline!
  before_action :authorize_create_pipeline!, only: [:new, :create]
  before_action :authorize_update_pipeline!, only: [:retry, :cancel]

10
11
  wrap_parameters Ci::Pipeline

12
13
  POLLING_INTERVAL = 10_000

Kamil Trzcinski's avatar
WIP    
Kamil Trzcinski committed
14
15
  def index
    @scope = params[:scope]
16
    @pipelines = PipelinesFinder
17
      .new(project, current_user, scope: @scope)
18
      .execute
19
20
      .page(params[:page])
      .per(30)
21

22
23
24
25
    @running_count = limited_pipelines_count(project, 'running')
    @pending_count = limited_pipelines_count(project, 'pending')
    @finished_count = limited_pipelines_count(project, 'finished')
    @pipelines_count = limited_pipelines_count(project)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
26
27
28
29

    respond_to do |format|
      format.html
      format.json do
30
        Gitlab::PollingInterval.set_header(response, interval: POLLING_INTERVAL)
31

32
33
        render json: {
          pipelines: PipelineSerializer
Fatih Acet's avatar
Fatih Acet committed
34
            .new(project: @project, current_user: @current_user)
35
            .with_pagination(request, response)
36
            .represent(@pipelines, disable_coverage: true, preload: true),
37
38
          count: {
            all: @pipelines_count,
39
40
            running: @running_count,
            pending: @pending_count,
41
            finished: @finished_count
42
43
          }
        }
Kamil Trzcinski's avatar
Kamil Trzcinski committed
44
45
      end
    end
Kamil Trzcinski's avatar
WIP    
Kamil Trzcinski committed
46
47
48
  end

  def new
49
    @pipeline = project.all_pipelines.new(ref: @project.default_branch)
Kamil Trzcinski's avatar
WIP    
Kamil Trzcinski committed
50
51
52
  end

  def create
53
54
    @pipeline = Ci::CreatePipelineService
      .new(project, current_user, create_params)
55
      .execute(:web, ignore_skip_ci: true, save_on_errors: false)
56
57

    if @pipeline.persisted?
58
      redirect_to project_pipeline_path(project, @pipeline)
59
    else
Kamil Trzcinski's avatar
Kamil Trzcinski committed
60
      render 'new'
Kamil Trzcinski's avatar
WIP    
Kamil Trzcinski committed
61
62
63
64
    end
  end

  def show
65
66
67
68
69
70
    respond_to do |format|
      format.html
      format.json do
        Gitlab::PollingInterval.set_header(response, interval: POLLING_INTERVAL)

        render json: PipelineSerializer
Fatih Acet's avatar
Fatih Acet committed
71
          .new(project: @project, current_user: @current_user)
72
73
74
          .represent(@pipeline, grouped: true)
      end
    end
Kamil Trzcinski's avatar
WIP    
Kamil Trzcinski committed
75
76
  end

Filipa Lacerda's avatar
Filipa Lacerda committed
77
  def builds
78
79
80
81
    render_show
  end

  def failures
82
    if @pipeline.failed_builds.present?
83
84
85
      render_show
    else
      redirect_to pipeline_path(@pipeline)
86
    end
Filipa Lacerda's avatar
Filipa Lacerda committed
87
88
  end

89
  def status
90
    render json: PipelineSerializer
Fatih Acet's avatar
Fatih Acet committed
91
      .new(project: @project, current_user: @current_user)
Shinya Maeda's avatar
Shinya Maeda committed
92
      .represent_status(@pipeline)
93
94
  end

95
  def stage
96
    @stage = pipeline.legacy_stage(params[:stage])
97
98
    return not_found unless @stage

Kamil Trzciński's avatar
Kamil Trzciński committed
99
100
    render json: StageSerializer
      .new(project: @project, current_user: @current_user)
101
      .represent(@stage, details: true, retried: params[:retried])
102
103
  end

104
105
106
107
108
109
  # TODO: This endpoint is used by mini-pipeline-graph
  # TODO: This endpoint should be migrated to `stage.json`
  def stage_ajax
    @stage = pipeline.legacy_stage(params[:stage])
    return not_found unless @stage

Kamil Trzciński's avatar
Kamil Trzciński committed
110
    render json: { html: view_to_html_string('projects/pipelines/_stage') }
111
112
  end

Kamil Trzcinski's avatar
WIP    
Kamil Trzcinski committed
113
  def retry
114
    pipeline.retry_failed(current_user)
Kamil Trzcinski's avatar
WIP    
Kamil Trzcinski committed
115

116
117
    respond_to do |format|
      format.html do
118
        redirect_back_or_default default: project_pipelines_path(project)
119
120
      end

121
      format.json { head :no_content }
122
    end
Kamil Trzcinski's avatar
WIP    
Kamil Trzcinski committed
123
124
125
  end

  def cancel
Kamil Trzcinski's avatar
Kamil Trzcinski committed
126
    pipeline.cancel_running
Kamil Trzcinski's avatar
WIP    
Kamil Trzcinski committed
127

128
129
    respond_to do |format|
      format.html do
130
        redirect_back_or_default default: project_pipelines_path(project)
131
132
      end

133
      format.json { head :no_content }
134
    end
Kamil Trzcinski's avatar
WIP    
Kamil Trzcinski committed
135
136
  end

137
138
  def charts
    @charts = {}
139
140
141
142
    @charts[:week] = Gitlab::Ci::Charts::WeekChart.new(project)
    @charts[:month] = Gitlab::Ci::Charts::MonthChart.new(project)
    @charts[:year] = Gitlab::Ci::Charts::YearChart.new(project)
    @charts[:pipeline_times] = Gitlab::Ci::Charts::PipelineTime.new(project)
143
144

    @counts = {}
145
146
147
    @counts[:total] = @project.all_pipelines.count(:all)
    @counts[:success] = @project.all_pipelines.success.count(:all)
    @counts[:failed] = @project.all_pipelines.failed.count(:all)
148
149
  end

Kamil Trzcinski's avatar
WIP    
Kamil Trzcinski committed
150
151
  private

152
153
154
155
156
157
158
159
  def render_show
    respond_to do |format|
      format.html do
        render 'show'
      end
    end
  end

Kamil Trzcinski's avatar
Kamil Trzcinski committed
160
  def create_params
161
    params.require(:pipeline).permit(:ref, variables_attributes: %i[key secret_value])
Kamil Trzcinski's avatar
Kamil Trzcinski committed
162
163
  end

164
  # rubocop: disable CodeReuse/ActiveRecord
Kamil Trzcinski's avatar
Kamil Trzcinski committed
165
  def pipeline
166
    @pipeline ||= project
167
                    .all_pipelines
168
169
170
                    .includes(user: :status)
                    .find_by!(id: params[:id])
                    .present(current_user: current_user)
Kamil Trzcinski's avatar
WIP    
Kamil Trzcinski committed
171
  end
172
  # rubocop: enable CodeReuse/ActiveRecord
Kamil Trzcinski's avatar
Kamil Trzcinski committed
173

174
175
176
177
  def whitelist_query_limiting
    # Also see https://gitlab.com/gitlab-org/gitlab-ce/issues/42343
    Gitlab::QueryLimiting.whitelist('https://gitlab.com/gitlab-org/gitlab-ce/issues/42339')
  end
178
179
180
181

  def authorize_update_pipeline!
    return access_denied! unless can?(current_user, :update_pipeline, @pipeline)
  end
182
183

  def limited_pipelines_count(project, scope = nil)
184
    finder = PipelinesFinder.new(project, current_user, scope: scope)
185
186
187

    view_context.limited_counter_with_delimiter(finder.execute)
  end
Kamil Trzcinski's avatar
WIP    
Kamil Trzcinski committed
188
end