2018-09-25 23:45:43 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-12-18 02:41:16 -05:00
|
|
|
class Projects::ArtifactsController < Projects::ApplicationController
|
2016-08-24 04:02:56 -04:00
|
|
|
include ExtractsPath
|
2017-05-02 18:42:37 -04:00
|
|
|
include RendersBlob
|
2017-09-07 17:27:04 -04:00
|
|
|
include SendFileUpload
|
2016-08-24 04:02:56 -04:00
|
|
|
|
2015-12-17 08:24:43 -05:00
|
|
|
layout 'project'
|
2016-02-01 17:58:04 -05:00
|
|
|
before_action :authorize_read_build!
|
2016-06-10 10:20:11 -04:00
|
|
|
before_action :authorize_update_build!, only: [:keep]
|
2019-09-18 17:06:34 -04:00
|
|
|
before_action :authorize_destroy_artifacts!, only: [:destroy]
|
2016-08-26 01:10:03 -04:00
|
|
|
before_action :extract_ref_name_and_path
|
2020-05-15 14:07:52 -04:00
|
|
|
before_action :validate_artifacts!, except: [:index, :download, :raw, :destroy]
|
2017-09-05 06:27:40 -04:00
|
|
|
before_action :entry, only: [:file]
|
2015-12-17 08:24:43 -05:00
|
|
|
|
2019-09-18 17:06:34 -04:00
|
|
|
MAX_PER_PAGE = 20
|
|
|
|
|
2020-10-08 14:08:32 -04:00
|
|
|
feature_category :continuous_integration
|
|
|
|
|
2019-09-18 17:06:34 -04:00
|
|
|
def index
|
|
|
|
# Loading artifacts is very expensive in projects with a lot of artifacts.
|
|
|
|
# This feature flag prevents a DOS attack vector.
|
|
|
|
# It should be removed only after resolving the underlying performance
|
|
|
|
# issues: https://gitlab.com/gitlab-org/gitlab/issues/32281
|
|
|
|
return head :no_content unless Feature.enabled?(:artifacts_management_page, @project)
|
|
|
|
|
2020-05-19 11:08:04 -04:00
|
|
|
finder = Ci::JobArtifactsFinder.new(@project, artifacts_params)
|
2019-09-18 17:06:34 -04:00
|
|
|
all_artifacts = finder.execute
|
|
|
|
|
|
|
|
@artifacts = all_artifacts.page(params[:page]).per(MAX_PER_PAGE)
|
|
|
|
@total_size = all_artifacts.total_size
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
notice = if artifact.destroy
|
|
|
|
_('Artifact was successfully deleted.')
|
|
|
|
else
|
|
|
|
_('Artifact could not be deleted.')
|
|
|
|
end
|
|
|
|
|
|
|
|
redirect_to project_artifacts_path(@project), status: :see_other, notice: notice
|
|
|
|
end
|
|
|
|
|
2015-12-17 08:24:43 -05:00
|
|
|
def download
|
2018-10-02 13:01:26 -04:00
|
|
|
return render_404 unless artifacts_file
|
|
|
|
|
2018-11-07 10:54:37 -05:00
|
|
|
send_upload(artifacts_file, attachment: artifacts_file.filename, proxy: params[:proxy])
|
2015-12-17 08:24:43 -05:00
|
|
|
end
|
|
|
|
|
2015-12-18 02:41:16 -05:00
|
|
|
def browse
|
2017-05-02 18:47:28 -04:00
|
|
|
@path = params[:path]
|
|
|
|
directory = @path ? "#{@path}/" : ''
|
2016-01-13 15:17:28 -05:00
|
|
|
@entry = build.artifacts_metadata_entry(directory)
|
2016-01-11 03:57:03 -05:00
|
|
|
|
2016-07-11 07:06:40 -04:00
|
|
|
render_404 unless @entry.exists?
|
2015-12-17 09:17:00 -05:00
|
|
|
end
|
|
|
|
|
2016-01-09 08:41:43 -05:00
|
|
|
def file
|
2017-05-02 18:42:37 -04:00
|
|
|
blob = @entry.blob
|
2017-05-26 19:27:30 -04:00
|
|
|
conditionally_expand_blob(blob)
|
2016-01-11 04:01:18 -05:00
|
|
|
|
2017-10-05 06:41:22 -04:00
|
|
|
if blob.external_link?(build)
|
2017-10-03 06:34:24 -04:00
|
|
|
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
|
2017-05-02 18:42:37 -04:00
|
|
|
end
|
2016-01-11 04:01:18 -05:00
|
|
|
end
|
2016-01-09 08:41:43 -05:00
|
|
|
end
|
|
|
|
|
2017-05-02 18:42:37 -04:00
|
|
|
def raw
|
2020-05-15 14:07:52 -04:00
|
|
|
return render_404 unless zip_artifact?
|
|
|
|
|
2017-12-05 09:31:33 -05:00
|
|
|
path = Gitlab::Ci::Build::Artifacts::Path.new(params[:path])
|
2017-09-05 06:27:40 -04:00
|
|
|
|
2020-05-15 14:07:52 -04:00
|
|
|
send_artifacts_entry(artifacts_file, path)
|
2017-05-02 18:42:37 -04:00
|
|
|
end
|
|
|
|
|
2016-06-08 11:18:54 -04:00
|
|
|
def keep
|
|
|
|
build.keep_artifacts!
|
2017-06-29 13:06:35 -04:00
|
|
|
redirect_to project_job_path(project, build)
|
2016-06-08 11:18:54 -04:00
|
|
|
end
|
|
|
|
|
2016-08-16 10:10:10 -04:00
|
|
|
def latest_succeeded
|
2016-08-25 03:04:15 -04:00
|
|
|
target_path = artifacts_action_path(@path, project, build)
|
2016-07-12 11:26:30 -04:00
|
|
|
|
2016-08-18 07:30:20 -04:00
|
|
|
if target_path
|
2016-08-18 03:31:20 -04:00
|
|
|
redirect_to(target_path)
|
2016-07-11 06:17:32 -04:00
|
|
|
else
|
|
|
|
render_404
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-12-17 08:24:43 -05:00
|
|
|
private
|
|
|
|
|
2016-08-26 01:10:03 -04:00
|
|
|
def extract_ref_name_and_path
|
|
|
|
return unless params[:ref_name_and_path]
|
|
|
|
|
|
|
|
@ref_name, @path = extract_ref(params[:ref_name_and_path])
|
|
|
|
end
|
|
|
|
|
2019-09-18 17:06:34 -04:00
|
|
|
def artifacts_params
|
|
|
|
params.permit(:sort)
|
|
|
|
end
|
|
|
|
|
2016-06-10 08:39:36 -04:00
|
|
|
def validate_artifacts!
|
2020-08-11 14:10:06 -04:00
|
|
|
render_404 unless build&.available_artifacts?
|
2016-06-10 08:39:36 -04:00
|
|
|
end
|
|
|
|
|
2015-12-17 08:24:43 -05:00
|
|
|
def build
|
2017-05-02 18:47:28 -04:00
|
|
|
@build ||= begin
|
2020-05-22 05:08:09 -04:00
|
|
|
build = build_from_id || build_from_sha || build_from_ref
|
2017-05-02 18:47:28 -04:00
|
|
|
build&.present(current_user: current_user)
|
|
|
|
end
|
2016-07-11 06:17:32 -04:00
|
|
|
end
|
|
|
|
|
2019-09-18 17:06:34 -04:00
|
|
|
def artifact
|
|
|
|
@artifact ||=
|
|
|
|
project.job_artifacts.find(params[:id])
|
|
|
|
end
|
|
|
|
|
2016-07-11 06:17:32 -04:00
|
|
|
def build_from_id
|
2019-01-09 10:33:22 -05:00
|
|
|
project.builds.find_by_id(params[:job_id]) if params[:job_id]
|
2016-07-11 06:17:32 -04:00
|
|
|
end
|
|
|
|
|
2020-05-22 05:08:09 -04:00
|
|
|
def build_from_sha
|
|
|
|
return if params[:job].blank?
|
2016-08-26 01:10:03 -04:00
|
|
|
return unless @ref_name
|
2016-07-11 06:51:23 -04:00
|
|
|
|
2019-07-17 09:33:52 -04:00
|
|
|
commit = project.commit(@ref_name)
|
|
|
|
return unless commit
|
|
|
|
|
|
|
|
project.latest_successful_build_for_sha(params[:job], commit.id)
|
2015-12-17 08:24:43 -05:00
|
|
|
end
|
|
|
|
|
2020-05-22 05:08:09 -04:00
|
|
|
def build_from_ref
|
|
|
|
return if params[:job].blank?
|
|
|
|
return unless @ref_name
|
|
|
|
|
|
|
|
project.latest_successful_build_for_ref(params[:job], @ref_name)
|
|
|
|
end
|
|
|
|
|
2015-12-17 08:24:43 -05:00
|
|
|
def artifacts_file
|
2018-10-16 09:01:48 -04:00
|
|
|
@artifacts_file ||= build&.artifacts_file_for_type(params[:file_type] || :archive)
|
2015-12-17 08:24:43 -05:00
|
|
|
end
|
2017-05-02 18:42:37 -04:00
|
|
|
|
2020-05-15 14:07:52 -04:00
|
|
|
def zip_artifact?
|
|
|
|
types = HashWithIndifferentAccess.new(Ci::JobArtifact::TYPE_AND_FORMAT_PAIRS)
|
|
|
|
file_type = params[:file_type] || :archive
|
|
|
|
|
|
|
|
types[file_type] == :zip
|
|
|
|
end
|
|
|
|
|
2017-09-05 06:27:40 -04:00
|
|
|
def entry
|
|
|
|
@entry = build.artifacts_metadata_entry(params[:path])
|
2017-05-02 18:42:37 -04:00
|
|
|
|
|
|
|
render_404 unless @entry.exists?
|
|
|
|
end
|
2015-12-17 08:24:43 -05:00
|
|
|
end
|