jobs_controller.rb 5.6 KB
Newer Older
1 2
# frozen_string_literal: true

3
class Projects::JobsController < Projects::ApplicationController
4
  include SendFileUpload
5
  include ContinueParams
6

7
  before_action :build, except: [:index]
8
  before_action :authorize_read_build!
9
  before_action :authorize_update_build!,
10
    except: [:index, :show, :status, :raw, :trace, :erase]
11
  before_action :authorize_erase_build!, only: [:erase]
12
  before_action :authorize_use_build_terminal!, only: [:terminal, :terminal_websocket_authorize]
13
  before_action :verify_api_request!, only: :terminal_websocket_authorize
14

15
  layout 'project'
16

17
  # rubocop: disable CodeReuse/ActiveRecord
Kamil Trzcinski's avatar
Kamil Trzcinski committed
18 19
  def index
    @scope = params[:scope]
20
    @all_builds = project.builds.relevant
21
    @builds = @all_builds.order('ci_builds.id DESC')
Kamil Trzcinski's avatar
Kamil Trzcinski committed
22 23
    @builds =
      case @scope
24 25
      when 'pending'
        @builds.pending.reverse_order
26
      when 'running'
27
        @builds.running.reverse_order
Kamil Trzcinski's avatar
Kamil Trzcinski committed
28
      when 'finished'
29
        @builds.finished
Kamil Trzcinski's avatar
Kamil Trzcinski committed
30
      else
31
        @builds
Kamil Trzcinski's avatar
Kamil Trzcinski committed
32
      end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
33
    @builds = @builds.includes([
34 35 36 37
      { pipeline: [:project, :user] },
      :job_artifacts_archive,
      :metadata,
      :trigger_request,
Kamil Trzcinski's avatar
Kamil Trzcinski committed
38
      :project,
39
      :user,
Kamil Trzcinski's avatar
Kamil Trzcinski committed
40 41
      :tags
    ])
42
    @builds = @builds.page(params[:page]).per(30).without_count
Kamil Trzcinski's avatar
Kamil Trzcinski committed
43
  end
44
  # rubocop: enable CodeReuse/ActiveRecord
Kamil Trzcinski's avatar
Kamil Trzcinski committed
45

46
  # rubocop: disable CodeReuse/ActiveRecord
47
  def show
48 49
    @pipeline = @build.pipeline
    @builds = @pipeline.builds
50 51
      .order('id DESC')
      .present(current_user: current_user)
52 53 54 55 56 57 58 59

    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
60
          .represent(@build, {}, BuildDetailsEntity)
61 62
      end
    end
63
  end
64
  # rubocop: enable CodeReuse/ActiveRecord
65

66
  def trace
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    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
83 84 85 86
      end
    end
  end

87
  def retry
88
    return respond_422 unless @build.retryable?
89

90
    build = Ci::Build.retry(@build, current_user)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
91
    redirect_to build_path(build)
92 93
  end

94
  def play
95
    return respond_422 unless @build.playable?
96 97 98 99 100

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

101
  def cancel
102 103
    return respond_422 unless @build.cancelable?

104
    @build.cancel
105

106
    if continue_params[:to]
107 108 109 110
      redirect_to continue_params[:to]
    else
      redirect_to builds_project_pipeline_path(@project, @build.pipeline.id)
    end
111 112
  end

113 114 115
  def unschedule
    return respond_422 unless @build.scheduled?

116
    @build.unschedule!
117 118 119
    redirect_to build_path(@build)
  end

120
  def status
121
    render json: BuildSerializer
Fatih Acet's avatar
Fatih Acet committed
122
      .new(project: @project, current_user: @current_user)
Shinya Maeda's avatar
Shinya Maeda committed
123
      .represent_status(@build)
124
  end
125

126
  def erase
127
    if @build.erase(erased_by: current_user)
128
      redirect_to project_job_path(project, @build),
129
                notice: _("Job has been successfully erased!")
130 131 132
    else
      respond_422
    end
133 134
  end

135
  def raw
Shinya Maeda's avatar
Fix  
Shinya Maeda committed
136
    if trace_artifact_file
137
      workhorse_set_content_type!
Shinya Maeda's avatar
Fix  
Shinya Maeda committed
138 139 140 141 142 143
      send_upload(trace_artifact_file,
                  send_params: raw_send_params,
                  redirect_params: raw_redirect_params)
    else
      build.trace.read do |stream|
        if stream.file?
144
          workhorse_set_content_type!
Shinya Maeda's avatar
Fix  
Shinya Maeda committed
145 146
          send_file stream.path, type: 'text/plain; charset=utf-8', disposition: 'inline'
        else
147 148 149 150 151 152
          # In this case we can't use workhorse_set_content_type! and let
          # Workhorse handle the response because the data is streamed directly
          # to the user but, because we have the trace content, we can calculate
          # the proper content type and disposition here.
          raw_data = stream.raw
          send_data raw_data, type: 'text/plain; charset=utf-8', disposition: raw_trace_content_disposition(raw_data), filename: 'job.log'
Shinya Maeda's avatar
Fix  
Shinya Maeda committed
153
        end
154
      end
155 156 157
    end
  end

158 159 160 161 162 163
  def terminal
  end

  # GET .../terminal.ws : implemented in gitlab-workhorse
  def terminal_websocket_authorize
    set_workhorse_internal_api_content_type
164
    render json: Gitlab::Workhorse.channel_websocket(@build.terminal_specification)
165 166
  end

167 168
  private

169 170 171 172
  def authorize_update_build!
    return access_denied! unless can?(current_user, :update_build, build)
  end

173 174 175 176
  def authorize_erase_build!
    return access_denied! unless can?(current_user, :erase_build, build)
  end

177 178 179 180 181 182 183 184
  def authorize_use_build_terminal!
    return access_denied! unless can?(current_user, :create_build_terminal, build)
  end

  def verify_api_request!
    Gitlab::Workhorse.verify_api_request!(request.headers)
  end

Shinya Maeda's avatar
Fix  
Shinya Maeda committed
185 186 187 188 189 190 191 192
  def raw_send_params
    { type: 'text/plain; charset=utf-8', disposition: 'inline' }
  end

  def raw_redirect_params
    { query: { 'response-content-type' => 'text/plain; charset=utf-8', 'response-content-disposition' => 'inline' } }
  end

193 194 195 196
  def trace_artifact_file
    @trace_artifact_file ||= build.job_artifacts_trace&.file
  end

197
  def build
198
    @build ||= project.builds.find(params[:id])
Lin Jen-Shin's avatar
Lin Jen-Shin committed
199
      .present(current_user: current_user)
200
  end
201 202

  def build_path(build)
203
    project_job_path(build.project, build)
204
  end
205 206 207 208 209 210 211 212 213

  def raw_trace_content_disposition(raw_data)
    mime_type = MimeMagic.by_magic(raw_data)

    # if mime_type is nil can also represent 'text/plain'
    return 'inline' if mime_type.nil? || mime_type.type == 'text/plain'

    'attachment'
  end
214
end