pipelines_controller.rb 4.0 KB
Newer Older
K
Kamil Trzcinski 已提交
1
class Projects::PipelinesController < Projects::ApplicationController
2
  before_action :pipeline, except: [:index, :new, :create, :charts]
3
  before_action :commit, only: [:show, :builds, :failures]
K
WIP  
Kamil Trzcinski 已提交
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
K
WIP  
Kamil Trzcinski 已提交
8

9 10
  wrap_parameters Ci::Pipeline

11 12
  POLLING_INTERVAL = 10_000

K
WIP  
Kamil Trzcinski 已提交
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
K
Kamil Trzcinski 已提交
32 33 34 35

    respond_to do |format|
      format.html
      format.json do
36
        Gitlab::PollingInterval.set_header(response, interval: POLLING_INTERVAL)
T
Toon Claes 已提交
37

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

  def new
55
    @pipeline = project.pipelines.new(ref: @project.default_branch)
K
WIP  
Kamil Trzcinski 已提交
56 57 58
  end

  def create
59 60
    @pipeline = Ci::CreatePipelineService
      .new(project, current_user, create_params)
61
      .execute(:web, 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
K
Kamil Trzcinski 已提交
66
      render 'new'
K
WIP  
Kamil Trzcinski 已提交
67 68 69 70
    end
  end

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

        render json: PipelineSerializer
F
Fatih Acet 已提交
77
          .new(project: @project, current_user: @current_user)
78 79 80
          .represent(@pipeline, grouped: true)
      end
    end
K
WIP  
Kamil Trzcinski 已提交
81 82
  end

F
Filipa Lacerda 已提交
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
F
Filipa Lacerda 已提交
93 94
  end

95
  def status
96
    render json: PipelineSerializer
F
Fatih Acet 已提交
97
      .new(project: @project, current_user: @current_user)
S
Shinya Maeda 已提交
98
      .represent_status(@pipeline)
99 100
  end

101
  def stage
102
    @stage = pipeline.legacy_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

K
WIP  
Kamil Trzcinski 已提交
110
  def retry
111
    pipeline.retry_failed(current_user)
K
WIP  
Kamil Trzcinski 已提交
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
K
WIP  
Kamil Trzcinski 已提交
120 121 122
  end

  def cancel
K
Kamil Trzcinski 已提交
123
    pipeline.cancel_running
K
WIP  
Kamil Trzcinski 已提交
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
K
WIP  
Kamil Trzcinski 已提交
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

K
WIP  
Kamil Trzcinski 已提交
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

K
Kamil Trzcinski 已提交
152
  def create_params
153
    params.require(:pipeline).permit(:ref)
K
Kamil Trzcinski 已提交
154 155
  end

K
Kamil Trzcinski 已提交
156
  def pipeline
157
    @pipeline ||= project.pipelines.find_by!(id: params[:id]).present(current_user: current_user)
K
WIP  
Kamil Trzcinski 已提交
158
  end
K
Kamil Trzcinski 已提交
159 160

  def commit
161
    @commit ||= @pipeline.commit
K
Kamil Trzcinski 已提交
162
  end
K
WIP  
Kamil Trzcinski 已提交
163
end