build_artifacts_controller.rb 1.31 KB
Newer Older
Lin Jen-Shin's avatar
Lin Jen-Shin committed
1 2 3 4
class Projects::BuildArtifactsController < Projects::ApplicationController
  include ExtractsPath
  include RendersBlob

5
  before_action :authorize_read_build!
Lin Jen-Shin's avatar
Lin Jen-Shin committed
6
  before_action :extract_ref_name_and_path
7
  before_action :validate_artifacts!
Lin Jen-Shin's avatar
Lin Jen-Shin committed
8 9

  def download
10
    redirect_to download_project_job_artifacts_path(project, job)
Lin Jen-Shin's avatar
Lin Jen-Shin committed
11 12 13
  end

  def browse
14
    redirect_to browse_project_job_artifacts_path(project, job, path: params[:path])
Lin Jen-Shin's avatar
Lin Jen-Shin committed
15 16 17
  end

  def file
18
    redirect_to file_project_job_artifacts_path(project, job, path: params[:path])
Lin Jen-Shin's avatar
Lin Jen-Shin committed
19 20 21
  end

  def raw
22
    redirect_to raw_project_job_artifacts_path(project, job, path: params[:path])
Lin Jen-Shin's avatar
Lin Jen-Shin committed
23 24 25
  end

  def latest_succeeded
26
    redirect_to latest_succeeded_project_artifacts_path(project, job, ref_name_and_path: params[:ref_name_and_path], job: params[:job])
Lin Jen-Shin's avatar
Lin Jen-Shin committed
27 28 29 30
  end

  private

31 32 33 34
  def validate_artifacts!
    render_404 unless job && job.artifacts?
  end

Lin Jen-Shin's avatar
Lin Jen-Shin committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
  def extract_ref_name_and_path
    return unless params[:ref_name_and_path]

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

  def job
    @job ||= job_from_id || job_from_ref
  end

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

  def job_from_ref
    return unless @ref_name

    jobs = project.latest_successful_builds_for(@ref_name)
    jobs.find_by(name: params[:job])
  end
end