pipelines_controller.rb 4.12 KB
Newer Older
1
class Projects::PipelinesController < Projects::ApplicationController
2
  before_action :pipeline, except: [:index, :new, :create, :charts]
3
  before_action :commit, only: [:show, :builds, :failures]
Kamil Trzcinski's avatar
Kamil Trzcinski committed
4 5 6 7
  before_action :authorize_read_pipeline!
  before_action :authorize_create_pipeline!, only: [:new, :create]
  before_action :authorize_update_pipeline!, only: [:retry, :cancel]

8 9
  wrap_parameters Ci::Pipeline

10 11
  POLLING_INTERVAL = 10_000

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

20
    @running_count = PipelinesFinder
21
      .new(project, scope: 'running').execute.count
22

23
    @pending_count = PipelinesFinder
24
      .new(project, scope: 'pending').execute.count
25 26

    @finished_count = PipelinesFinder
27
      .new(project, scope: 'finished').execute.count
28

29 30
    @pipelines_count = PipelinesFinder
      .new(project).execute.count
Kamil Trzcinski's avatar
Kamil Trzcinski committed
31

32 33
    @pipelines.map(&:commit) # List commits for batch loading

Kamil Trzcinski's avatar
Kamil Trzcinski committed
34 35 36
    respond_to do |format|
      format.html
      format.json do
37
        Gitlab::PollingInterval.set_header(response, interval: POLLING_INTERVAL)
38

39 40
        render json: {
          pipelines: PipelineSerializer
41
            .new(project: @project, current_user: @current_user)
42 43 44 45
            .with_pagination(request, response)
            .represent(@pipelines),
          count: {
            all: @pipelines_count,
46 47
            running: @running_count,
            pending: @pending_count,
48
            finished: @finished_count
49 50
          }
        }
Kamil Trzcinski's avatar
Kamil Trzcinski committed
51 52
      end
    end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
53 54 55
  end

  def new
56
    @pipeline = project.pipelines.new(ref: @project.default_branch)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
57 58 59
  end

  def create
60 61
    @pipeline = Ci::CreatePipelineService
      .new(project, current_user, create_params)
62
      .execute(:web, ignore_skip_ci: true, save_on_errors: false)
63 64

    if @pipeline.persisted?
65
      redirect_to project_pipeline_path(project, @pipeline)
66
    else
67
      render 'new'
Kamil Trzcinski's avatar
Kamil Trzcinski committed
68 69 70 71
    end
  end

  def show
72 73 74 75 76 77
    respond_to do |format|
      format.html
      format.json do
        Gitlab::PollingInterval.set_header(response, interval: POLLING_INTERVAL)

        render json: PipelineSerializer
78
          .new(project: @project, current_user: @current_user)
79 80 81
          .represent(@pipeline, grouped: true)
      end
    end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
82 83
  end

Filipa Lacerda's avatar
Filipa Lacerda committed
84
  def builds
85 86 87 88
    render_show
  end

  def failures
89 90 91 92
    if @pipeline.statuses.latest.failed.present?
      render_show
    else
      redirect_to pipeline_path(@pipeline)
93
    end
Filipa Lacerda's avatar
Filipa Lacerda committed
94 95
  end

96
  def status
97
    render json: PipelineSerializer
98
      .new(project: @project, current_user: @current_user)
Shinya Maeda's avatar
Shinya Maeda committed
99
      .represent_status(@pipeline)
100 101
  end

102
  def stage
103
    @stage = pipeline.legacy_stage(params[:stage])
104 105 106 107 108 109 110
    return not_found unless @stage

    respond_to do |format|
      format.json { render json: { html: view_to_html_string('projects/pipelines/_stage') } }
    end
  end

Kamil Trzcinski's avatar
Kamil Trzcinski committed
111
  def retry
112
    pipeline.retry_failed(current_user)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
113

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

119
      format.json { head :no_content }
120
    end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
121 122 123
  end

  def cancel
124
    pipeline.cancel_running
Kamil Trzcinski's avatar
Kamil Trzcinski committed
125

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

131
      format.json { head :no_content }
132
    end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
133 134
  end

135 136
  def charts
    @charts = {}
137 138 139 140
    @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)
141 142 143 144 145

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

Kamil Trzcinski's avatar
Kamil Trzcinski committed
148 149
  private

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

158
  def create_params
159
    params.require(:pipeline).permit(:ref)
160 161
  end

Kamil Trzcinski's avatar
Kamil Trzcinski committed
162
  def pipeline
163
    @pipeline ||= project.pipelines.find_by!(id: params[:id]).present(current_user: current_user)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
164
  end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
165 166

  def commit
167
    @commit ||= @pipeline.commit
Kamil Trzcinski's avatar
Kamil Trzcinski committed
168
  end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
169
end