summaryrefslogtreecommitdiff
path: root/app/controllers/projects/pipelines_controller.rb
blob: 43a1abaa662f757cd949050035b182b47151dba7 (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
class Projects::PipelinesController < Projects::ApplicationController
  before_action :pipeline, except: [:index, :new, :create, :charts]
  before_action :commit, only: [:show, :builds]
  before_action :authorize_read_pipeline!
  before_action :authorize_create_pipeline!, only: [:new, :create]
  before_action :authorize_update_pipeline!, only: [:retry, :cancel]
  before_action :builds_enabled, only: :charts

  def index
    @scope = params[:scope]
    @pipelines = PipelinesFinder
      .new(project)
      .execute(scope: @scope)
      .page(params[:page])
      .per(30)

    @running_count = PipelinesFinder
      .new(project).execute(scope: 'running').count

    @pending_count = PipelinesFinder
      .new(project).execute(scope: 'pending').count

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

    @pipelines_count = PipelinesFinder
      .new(project).execute.count

    respond_to do |format|
      format.html
      format.json do
        render json: {
          pipelines: PipelineSerializer
            .new(project: @project, user: @current_user)
            .with_pagination(request, response)
            .represent(@pipelines),
          count: {
            all: @pipelines_count,
            running: @running_count,
            pending: @pending_count,
            finished: @finished_count,
          }
        }
      end
    end
  end

  def new
    @pipeline = project.pipelines.new(ref: @project.default_branch)
  end

  def create
    @pipeline = Ci::CreatePipelineService
      .new(project, current_user, create_params)
      .execute(ignore_skip_ci: true, save_on_errors: false)
    unless @pipeline.persisted?
      render 'new'
      return
    end

    redirect_to namespace_project_pipeline_path(project.namespace, project, @pipeline)
  end

  def show
  end

  def builds
    respond_to do |format|
      format.html do
        render 'show'
      end
    end
  end

  def status
    render json: PipelineSerializer
      .new(project: @project, user: @current_user)
      .represent_status(@pipeline)
  end

  def stage
    @stage = pipeline.stage(params[:stage])
    return not_found unless @stage

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

  def retry
    pipeline.retry_failed(current_user)

    redirect_back_or_default default: namespace_project_pipelines_path(project.namespace, project)
  end

  def cancel
    pipeline.cancel_running

    redirect_back_or_default default: namespace_project_pipelines_path(project.namespace, project)
  end

  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

  private

  def create_params
    params.require(:pipeline).permit(:ref)
  end

  def pipeline
    @pipeline ||= project.pipelines.find_by!(id: params[:id])
  end

  def commit
    @commit ||= @pipeline.commit
  end
end