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

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

  def cancel_all
26
    @project.builds.running_or_pending.each(&:cancel)
Kamil Trzcinski's avatar
Kamil Trzcinski 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 Trzcinski's avatar
Kamil Trzcinski 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 Trzcinski's avatar
Kamil Trzcinski committed
52
    return render_404 unless @build.retryable?
53

54
    build = Ci::Build.retry(@build, current_user)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
55
    redirect_to build_path(build)
56
57
  end

58
  def play
Kamil Trzcinski's avatar
Kamil Trzcinski committed
59
    return render_404 unless @build.playable?
60
61
62
63
64

    build = @build.play(current_user)
    redirect_to build_path(build)
  end

65
66
67
68
69
  def cancel
    @build.cancel
    redirect_to build_path(@build)
  end

70
71
72
  def status
    render json: @build.to_json(only: [:status, :id, :sha, :coverage], methods: :sha)
  end
73

74
  def erase
75
    @build.erase(erased_by: current_user)
76
    redirect_to namespace_project_build_path(project.namespace, project, @build),
77
                notice: "Build has been sucessfully erased!"
78
79
  end

80
  def raw
81
    if @build.has_trace_file?
82
      send_file @build.path_to_trace, type: 'text/plain; charset=utf-8', disposition: 'inline'
83
    else
84
      render_404
85
86
87
    end
  end

88
89
90
  private

  def build
91
    @build ||= project.builds.find_by!(id: params[:id])
92
  end
93
94

  def build_path(build)
95
    namespace_project_build_path(build.project.namespace, build.project, build)
96
  end
97
end