BigW Consortium Gitlab

builds_controller.rb 3.1 KB
Newer Older
1
class Projects::BuildsController < Projects::ApplicationController
Kamil Trzcinski committed
2
  before_action :build, except: [:index, :cancel_all]
3 4 5 6 7 8

  before_action :authorize_read_build!,
    only: [:index, :show, :status, :raw, :trace]
  before_action :authorize_update_build!,
    except: [:index, :show, :status, :raw, :trace, :cancel_all]

9
  layout 'project'
10

Kamil Trzcinski committed
11 12
  def index
    @scope = params[:scope]
13
    @all_builds = project.builds.relevant
14
    @builds = @all_builds.order('created_at DESC')
Kamil Trzcinski committed
15 16
    @builds =
      case @scope
17 18
      when 'pending'
        @builds.pending.reverse_order
19
      when 'running'
20
        @builds.running.reverse_order
Kamil Trzcinski committed
21
      when 'finished'
22
        @builds.finished
Kamil Trzcinski committed
23
      else
24
        @builds
Kamil Trzcinski committed
25
      end
26 27 28 29 30
    @builds = @builds.includes([
      { pipeline: :project },
      :project,
      :tags
    ])
31
    @builds = @builds.page(params[:page]).per(30)
Kamil Trzcinski committed
32 33 34
  end

  def cancel_all
35 36 37 38 39 40
    return access_denied! unless can?(current_user, :update_build, project)

    @project.builds.running_or_pending.each do |build|
      build.cancel if can?(current_user, :update_build, build)
    end

Kamil Trzcinski committed
41 42 43
    redirect_to namespace_project_builds_path(project.namespace, project)
  end

44
  def show
45
    @builds = @project.pipelines.find_by_sha(@build.sha).builds.order('id DESC')
Kamil Trzcinski committed
46
    @builds = @builds.where("id not in (?)", @build.id)
47
    @pipeline = @build.pipeline
48 49
  end

50
  def trace
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
    build.trace.read do |stream|
      respond_to do |format|
        format.json do
          result = {
            id: @build.id, status: @build.status, complete: @build.complete?
          }

          if stream.valid?
            stream.limit
            state = params[:state].presence
            trace = stream.html_with_state(state)
            result.merge!(trace.to_h)
          end

          render json: result
        end
67 68 69 70
      end
    end
  end

71
  def retry
72
    return respond_422 unless @build.retryable?
73

74
    build = Ci::Build.retry(@build, current_user)
Kamil Trzcinski committed
75
    redirect_to build_path(build)
76 77
  end

78
  def play
79
    return respond_422 unless @build.playable?
80 81 82 83 84

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

85
  def cancel
86 87
    return respond_422 unless @build.cancelable?

88 89 90 91
    @build.cancel
    redirect_to build_path(@build)
  end

92
  def status
93
    render json: BuildSerializer
94
      .new(project: @project, current_user: @current_user)
Shinya Maeda committed
95
      .represent_status(@build)
96
  end
97

98
  def erase
99 100
    if @build.erase(erased_by: current_user)
      redirect_to namespace_project_build_path(project.namespace, project, @build),
101
                notice: "Build has been successfully erased!"
102 103 104
    else
      respond_422
    end
105 106
  end

107
  def raw
108 109 110 111 112 113
    build.trace.read do |stream|
      if stream.file?
        send_file stream.path, type: 'text/plain; charset=utf-8', disposition: 'inline'
      else
        render_404
      end
114 115 116
    end
  end

117 118
  private

119 120 121 122
  def authorize_update_build!
    return access_denied! unless can?(current_user, :update_build, build)
  end

123
  def build
124
    @build ||= project.builds.find(params[:id])
125
      .present(current_user: current_user)
126
  end
127 128

  def build_path(build)
129
    namespace_project_build_path(build.project.namespace, build.project, build)
130
  end
131
end