BigW Consortium Gitlab

jobs_controller.rb 3.31 KB
Newer Older
1
class Projects::JobsController < 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

41
    redirect_to project_jobs_path(project)
Kamil Trzcinski committed
42 43
  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 50 51 52 53 54 55

    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 committed
56
          .represent(@build, {}, BuildDetailsEntity)
57 58
      end
    end
59 60
  end

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

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

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

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

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

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

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

103
  def status
104
    render json: BuildSerializer
105
      .new(project: @project, current_user: @current_user)
Shinya Maeda committed
106
      .represent_status(@build)
107
  end
108

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

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

128 129
  private

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

134
  def build
135
    @build ||= project.builds.find(params[:id])
136
      .present(current_user: current_user)
137
  end
138 139

  def build_path(build)
140
    project_job_path(build.project, build)
141
  end
142
end