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

  def index
    @scope = params[:scope]
    all_pipelines = project.ci_commits
    @pipelines_count = all_pipelines.count
    @running_or_pending_count = all_pipelines.running_or_pending.count
    @pipelines = PipelinesFinder.new(project).execute(all_pipelines, @scope)
    @pipelines = @pipelines.order(id: :desc).page(params[:page]).per(30)
  end

  def new
  end

  def create
    begin
      pipeline = Ci::CreatePipelineService.new(project, current_user, create_params).execute
      redirect_to namespace_project_pipeline_path(project.namespace, project, pipeline)
    rescue ArgumentError => e
      flash[:alert] = e.message
      render 'new'
    rescue
      flash[:alert] = 'The pipeline could not be created. Please try again.'
      render 'new'
    end
  end

  def show
  end

  def retry
    pipeline.retry_failed

    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

  private

  def create_params
    params.permit(:ref)
  end

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

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