pipelines_controller.rb 3.93 KB
Newer Older
Kamil Trzcinski's avatar
Kamil Trzcinski committed
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
WIP  
Kamil Trzcinski committed
4 5 6
  before_action :authorize_read_pipeline!
  before_action :authorize_create_pipeline!, only: [:new, :create]
  before_action :authorize_update_pipeline!, only: [:retry, :cancel]
7
  before_action :builds_enabled, only: :charts
Kamil Trzcinski's avatar
WIP  
Kamil Trzcinski committed
8

9 10
  wrap_parameters Ci::Pipeline

11 12
  POLLING_INTERVAL = 10_000

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

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

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

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

30 31
    @pipelines_count = PipelinesFinder
      .new(project).execute.count
Kamil Trzcinski's avatar
Kamil Trzcinski committed
32 33 34 35

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

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

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

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

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

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

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

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

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

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

101
  def stage
102
    @stage = pipeline.stage(params[:stage])
103 104 105 106 107 108 109
    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
WIP  
Kamil Trzcinski committed
110
  def retry
111
    pipeline.retry_failed(current_user)
Kamil Trzcinski's avatar
WIP  
Kamil Trzcinski committed
112

113 114 115 116 117
    respond_to do |format|
      format.html do
        redirect_back_or_default default: namespace_project_pipelines_path(project.namespace, project)
      end

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

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

125 126 127 128 129
    respond_to do |format|
      format.html do
        redirect_back_or_default default: namespace_project_pipelines_path(project.namespace, project)
      end

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

134 135 136 137 138 139 140 141
  def charts
    @charts = {}
    @charts[:week] = Ci::Charts::WeekChart.new(project)
    @charts[:month] = Ci::Charts::MonthChart.new(project)
    @charts[:year] = Ci::Charts::YearChart.new(project)
    @charts[:build_times] = Ci::Charts::BuildTime.new(project)
  end

Kamil Trzcinski's avatar
WIP  
Kamil Trzcinski committed
142 143
  private

144 145 146 147 148 149 150 151
  def render_show
    respond_to do |format|
      format.html do
        render 'show'
      end
    end
  end

Kamil Trzcinski's avatar
Kamil Trzcinski committed
152
  def create_params
153
    params.require(:pipeline).permit(:ref)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
154 155
  end

Kamil Trzcinski's avatar
Kamil Trzcinski committed
156
  def pipeline
157
    @pipeline ||= project.pipelines.find_by!(id: params[:id]).present(current_user: current_user)
Kamil Trzcinski's avatar
WIP  
Kamil Trzcinski committed
158
  end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
159 160

  def commit
161
    @commit ||= @pipeline.commit
Kamil Trzcinski's avatar
Kamil Trzcinski committed
162
  end
Kamil Trzcinski's avatar
WIP  
Kamil Trzcinski committed
163
end