jobs_controller.rb 3.48 KB
Newer Older
1
class Projects::JobsController < Projects::ApplicationController
Kamil Trzcinski's avatar
Kamil Trzcinski committed
2
  before_action :build, except: [:index, :cancel_all]
3 4 5 6

  before_action :authorize_read_build!,
    only: [:index, :show, :status, :raw, :trace]
  before_action :authorize_update_build!,
7
    except: [:index, :show, :status, :raw, :trace, :cancel_all, :erase]
8
  before_action :authorize_erase_build!, only: [:erase]
9

10
  layout 'project'
11

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

  def cancel_all
36 37 38 39 40 41
    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

42
    redirect_to project_jobs_path(project)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
43 44
  end

45
  def show
46
    @builds = @project.pipelines.find_by_sha(@build.sha).builds.order('id DESC')
Kamil Trzcinski's avatar
Kamil Trzcinski committed
47
    @builds = @builds.where("id not in (?)", @build.id)
48
    @pipeline = @build.pipeline
49 50 51 52 53 54 55 56

    respond_to do |format|
      format.html
      format.json do
        Gitlab::PollingInterval.set_header(response, interval: 10_000)

        render json: BuildSerializer
          .new(project: @project, current_user: @current_user)
Z.J. van de Weg's avatar
Z.J. van de Weg committed
57
          .represent(@build, {}, BuildDetailsEntity)
58 59
      end
    end
60 61
  end

62
  def trace
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    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
79 80 81 82
      end
    end
  end

83
  def retry
84
    return respond_422 unless @build.retryable?
85

86
    build = Ci::Build.retry(@build, current_user)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
87
    redirect_to build_path(build)
88 89
  end

90
  def play
91
    return respond_422 unless @build.playable?
92 93 94 95 96

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

97
  def cancel
98 99
    return respond_422 unless @build.cancelable?

100 101 102 103
    @build.cancel
    redirect_to build_path(@build)
  end

104
  def status
105
    render json: BuildSerializer
Fatih Acet's avatar
Fatih Acet committed
106
      .new(project: @project, current_user: @current_user)
Shinya Maeda's avatar
Shinya Maeda committed
107
      .represent_status(@build)
108
  end
109

110
  def erase
111
    if @build.erase(erased_by: current_user)
112
      redirect_to project_job_path(project, @build),
113
                notice: "Job has been successfully erased!"
114 115 116
    else
      respond_422
    end
117 118
  end

119
  def raw
120 121 122 123 124 125
    build.trace.read do |stream|
      if stream.file?
        send_file stream.path, type: 'text/plain; charset=utf-8', disposition: 'inline'
      else
        render_404
      end
126 127 128
    end
  end

129 130
  private

131 132 133 134
  def authorize_update_build!
    return access_denied! unless can?(current_user, :update_build, build)
  end

135 136 137 138
  def authorize_erase_build!
    return access_denied! unless can?(current_user, :erase_build, build)
  end

139
  def build
140
    @build ||= project.builds.find(params[:id])
141
      .present(current_user: current_user)
142
  end
143 144

  def build_path(build)
145
    project_job_path(build.project, build)
146
  end
147
end