BigW Consortium Gitlab

artifacts_controller.rb 1.71 KB
Newer Older
1
class Projects::ArtifactsController < Projects::ApplicationController
2 3
  include ExtractsPath

4
  layout 'project'
5
  before_action :authorize_read_build!
6
  before_action :authorize_update_build!, only: [:keep]
7
  before_action :extract_ref_name_and_path
8
  before_action :validate_artifacts!
9 10

  def download
11 12 13 14
    if artifacts_file.file_storage?
      send_file artifacts_file.path, disposition: 'attachment'
    else
      redirect_to artifacts_file.url
15 16 17
    end
  end

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

Lin Jen-Shin committed
22
    render_404 unless @entry.exists?
23 24
  end

25
  def file
26
    entry = build.artifacts_metadata_entry(params[:path])
27

28
    if entry.exists?
29
      send_artifacts_entry(build, entry)
30
    else
31
      render_404
32
    end
33 34
  end

35 36 37 38 39
  def keep
    build.keep_artifacts!
    redirect_to namespace_project_build_path(project.namespace, project, build)
  end

40
  def latest_succeeded
41
    target_path = artifacts_action_path(@path, project, build)
42

43
    if target_path
44
      redirect_to(target_path)
45 46 47 48 49
    else
      render_404
    end
  end

50 51
  private

52 53 54 55 56 57
  def extract_ref_name_and_path
    return unless params[:ref_name_and_path]

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

58
  def validate_artifacts!
59
    render_404 unless build && build.artifacts?
60 61
  end

62
  def build
63 64 65 66 67 68 69 70
    @build ||= build_from_id || build_from_ref
  end

  def build_from_id
    project.builds.find_by(id: params[:build_id]) if params[:build_id]
  end

  def build_from_ref
71
    return unless @ref_name
72

73 74
    builds = project.latest_successful_builds_for(@ref_name)
    builds.find_by(name: params[:job])
75 76 77 78 79 80
  end

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