artifacts_controller.rb 2.34 KB
Newer Older
1
class Projects::ArtifactsController < Projects::ApplicationController
2
  include ExtractsPath
3
  include RendersBlob
4
  include SendFileUpload
5

6
  layout 'project'
7
  before_action :authorize_read_build!
8
  before_action :authorize_update_build!, only: [:keep]
9
  before_action :extract_ref_name_and_path
10
  before_action :set_request_format, only: [:file]
11
  before_action :validate_artifacts!
12
  before_action :entry, only: [:file]
13 14

  def download
15
    send_upload(artifacts_file, attachment: artifacts_file.filename)
16 17
  end

18
  def browse
19 20
    @path = params[:path]
    directory = @path ? "#{@path}/" : ''
21
    @entry = build.artifacts_metadata_entry(directory)
22

Lin Jen-Shin's avatar
Lin Jen-Shin committed
23
    render_404 unless @entry.exists?
24 25
  end

26
  def file
27
    blob = @entry.blob
28
    conditionally_expand_blob(blob)
29

30
    if blob.external_link?(build)
31 32 33 34 35 36 37 38 39 40
      redirect_to blob.external_url(@project, build)
    else
      respond_to do |format|
        format.html do
          render 'file'
        end

        format.json do
          render_blob_json(blob)
        end
41
      end
42
    end
43 44
  end

45
  def raw
46
    path = Gitlab::Ci::Build::Artifacts::Path.new(params[:path])
47 48

    send_artifacts_entry(build, path)
49 50
  end

51 52
  def keep
    build.keep_artifacts!
53
    redirect_to project_job_path(project, build)
54 55
  end

56
  def latest_succeeded
57
    target_path = artifacts_action_path(@path, project, build)
58

59
    if target_path
60
      redirect_to(target_path)
61 62 63 64 65
    else
      render_404
    end
  end

66 67
  private

68 69 70 71 72 73
  def extract_ref_name_and_path
    return unless params[:ref_name_and_path]

    @ref_name, @path = extract_ref(params[:ref_name_and_path])
  end

74
  def validate_artifacts!
75
    render_404 unless build&.artifacts?
76 77
  end

78
  def build
79 80 81 82
    @build ||= begin
      build = build_from_id || build_from_ref
      build&.present(current_user: current_user)
    end
83 84 85
  end

  def build_from_id
86
    project.builds.find_by(id: params[:job_id]) if params[:job_id]
87 88 89
  end

  def build_from_ref
90
    return unless @ref_name
91

92 93
    builds = project.latest_successful_builds_for(@ref_name)
    builds.find_by(name: params[:job])
94 95 96 97 98
  end

  def artifacts_file
    @artifacts_file ||= build.artifacts_file
  end
99

100 101
  def entry
    @entry = build.artifacts_metadata_entry(params[:path])
102 103 104

    render_404 unless @entry.exists?
  end
105 106 107 108 109 110 111 112

  def set_request_format
    request.format = :html if set_request_format?
  end

  def set_request_format?
    request.format != :json
  end
113
end