builds_controller.rb 2.26 KB
Newer Older
1
class Projects::BuildsController < Projects::ApplicationController
Kamil Trzciński's avatar
Kamil Trzciński committed
2
  before_action :build, except: [:index, :cancel_all]
3
  before_action :authorize_read_build!, except: [:cancel, :cancel_all, :retry]
4
  before_action :authorize_update_build!, except: [:index, :show, :status, :raw]
5
  layout 'project'
6

Kamil Trzciński's avatar
Kamil Trzciński committed
7 8
  def index
    @scope = params[:scope]
9
    @all_builds = project.builds
10
    @builds = @all_builds.order('created_at DESC')
Kamil Trzciński's avatar
Kamil Trzciński committed
11 12
    @builds =
      case @scope
13 14
      when 'pending'
        @builds.pending.reverse_order
15
      when 'running'
16
        @builds.running.reverse_order
Kamil Trzciński's avatar
Kamil Trzciński committed
17
      when 'finished'
18
        @builds.finished
Kamil Trzciński's avatar
Kamil Trzciński committed
19
      else
20
        @builds
Kamil Trzciński's avatar
Kamil Trzciński committed
21
      end
22
    @builds = @builds.page(params[:page]).per(30)
Kamil Trzciński's avatar
Kamil Trzciński committed
23 24 25
  end

  def cancel_all
26
    @project.builds.running_or_pending.each(&:cancel)
Kamil Trzciński's avatar
Kamil Trzciński committed
27 28 29
    redirect_to namespace_project_builds_path(project.namespace, project)
  end

30
  def show
31
    @builds = @project.pipelines.find_by_sha(@build.sha).builds.order('id DESC')
Kamil Trzciński's avatar
Kamil Trzciński committed
32
    @builds = @builds.where("id not in (?)", @build.id)
33
    @pipeline = @build.pipeline
34 35 36 37 38 39 40 41 42

    respond_to do |format|
      format.html
      format.json do
        render json: @build.to_json(methods: :trace_html)
      end
    end
  end

43 44 45
  def trace
    respond_to do |format|
      format.json do
Phil Hughes's avatar
Phil Hughes committed
46
        render json: @build.trace_with_state(params[:state].presence).merge!(id: @build.id, status: @build.status)
47 48 49 50
      end
    end
  end

51
  def retry
Kamil Trzciński's avatar
Kamil Trzciński committed
52
    unless @build.retryable?
53
      return render_404
54 55
    end

56
    build = Ci::Build.retry(@build, current_user)
Kamil Trzciński's avatar
Kamil Trzciński committed
57
    redirect_to build_path(build)
58 59 60 61 62 63 64
  end

  def cancel
    @build.cancel
    redirect_to build_path(@build)
  end

65 66 67
  def status
    render json: @build.to_json(only: [:status, :id, :sha, :coverage], methods: :sha)
  end
68

69
  def erase
70
    @build.erase(erased_by: current_user)
71
    redirect_to namespace_project_build_path(project.namespace, project, @build),
72
                notice: "Build has been sucessfully erased!"
73 74
  end

75 76
  def raw
    if @build.has_trace?
77
      send_file @build.path_to_trace, type: 'text/plain; charset=utf-8', disposition: 'inline'
78
    else
79
      render_404
80 81 82
    end
  end

83 84 85
  private

  def build
86
    @build ||= project.builds.find_by!(id: params[:id])
87
  end
88 89

  def build_path(build)
90
    namespace_project_build_path(build.project.namespace, build.project, build)
91
  end
92
end