2018-12-05 23:21:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-01-19 16:12:03 +05:30
|
|
|
class Projects::ArtifactsController < Projects::ApplicationController
|
2016-09-29 09:46:39 +05:30
|
|
|
include ExtractsPath
|
2017-08-17 22:00:37 +05:30
|
|
|
include RendersBlob
|
2018-05-09 12:01:36 +05:30
|
|
|
include SendFileUpload
|
2023-01-13 00:05:48 +05:30
|
|
|
include Gitlab::Ci::Artifacts::Logger
|
2016-09-29 09:46:39 +05:30
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
urgency :low, [:browse, :file, :latest_succeeded]
|
|
|
|
|
2016-01-19 16:12:03 +05:30
|
|
|
layout 'project'
|
2016-04-02 18:10:28 +05:30
|
|
|
before_action :authorize_read_build!
|
2022-05-03 16:02:30 +05:30
|
|
|
before_action :authorize_read_build_trace!, only: [:download]
|
2016-06-16 23:09:34 +05:30
|
|
|
before_action :authorize_update_build!, only: [:keep]
|
2019-12-21 20:55:43 +05:30
|
|
|
before_action :authorize_destroy_artifacts!, only: [:destroy]
|
2016-09-29 09:46:39 +05:30
|
|
|
before_action :extract_ref_name_and_path
|
2020-05-24 23:13:21 +05:30
|
|
|
before_action :validate_artifacts!, except: [:index, :download, :raw, :destroy]
|
2022-11-25 23:54:43 +05:30
|
|
|
before_action :entry, only: [:external_file, :file]
|
2016-01-19 16:12:03 +05:30
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
MAX_PER_PAGE = 20
|
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
feature_category :build_artifacts
|
2021-01-03 14:25:43 +05:30
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
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)
|
|
|
|
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
|
|
|
|
|
2016-01-19 16:12:03 +05:30
|
|
|
def download
|
2018-12-05 23:21:45 +05:30
|
|
|
return render_404 unless artifacts_file
|
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
log_artifacts_filesize(artifacts_file.model)
|
2018-12-13 13:39:08 +05:30
|
|
|
send_upload(artifacts_file, attachment: artifacts_file.filename, proxy: params[:proxy])
|
2016-01-19 16:12:03 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def browse
|
2017-08-17 22:00:37 +05:30
|
|
|
@path = params[:path]
|
|
|
|
directory = @path ? "#{@path}/" : ''
|
2016-01-19 16:12:03 +05:30
|
|
|
@entry = build.artifacts_metadata_entry(directory)
|
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
render_404 unless @entry.exists?
|
2016-01-19 16:12:03 +05:30
|
|
|
end
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
# External files are redirected to Gitlab Pages and might have unsecure content
|
|
|
|
# To warn the user about the possible unsecure content, we show a warning page
|
|
|
|
# before redirecting the user.
|
|
|
|
def external_file
|
|
|
|
@blob = @entry.blob
|
|
|
|
end
|
|
|
|
|
2016-01-19 16:12:03 +05:30
|
|
|
def file
|
2017-08-17 22:00:37 +05:30
|
|
|
blob = @entry.blob
|
2017-09-10 17:25:29 +05:30
|
|
|
conditionally_expand_blob(blob)
|
2016-01-19 16:12:03 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
if blob.external_link?(build)
|
2022-11-25 23:54:43 +05:30
|
|
|
redirect_to external_file_project_job_artifacts_path(@project, @build, path: params[:path])
|
2018-03-17 18:26:18 +05:30
|
|
|
else
|
|
|
|
respond_to do |format|
|
|
|
|
format.html do
|
|
|
|
render 'file'
|
|
|
|
end
|
|
|
|
|
|
|
|
format.json do
|
|
|
|
render_blob_json(blob)
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2016-01-19 16:12:03 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def raw
|
2020-05-24 23:13:21 +05:30
|
|
|
return render_404 unless zip_artifact?
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
path = Gitlab::Ci::Build::Artifacts::Path.new(params[:path])
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
send_artifacts_entry(artifacts_file, path)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
def keep
|
|
|
|
build.keep_artifacts!
|
2017-09-10 17:25:29 +05:30
|
|
|
redirect_to project_job_path(project, build)
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
def latest_succeeded
|
|
|
|
target_path = artifacts_action_path(@path, project, build)
|
|
|
|
|
|
|
|
if target_path
|
|
|
|
redirect_to(target_path)
|
|
|
|
else
|
|
|
|
render_404
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-19 16:12:03 +05:30
|
|
|
private
|
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
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-12-21 20:55:43 +05:30
|
|
|
def artifacts_params
|
|
|
|
params.permit(:sort)
|
|
|
|
end
|
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
def validate_artifacts!
|
2020-10-24 23:57:45 +05:30
|
|
|
render_404 unless build&.available_artifacts?
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
2016-01-19 16:12:03 +05:30
|
|
|
def build
|
2017-08-17 22:00:37 +05:30
|
|
|
@build ||= begin
|
2020-05-30 21:06:31 +05:30
|
|
|
build = build_from_id || build_from_sha || build_from_ref
|
2017-08-17 22:00:37 +05:30
|
|
|
build&.present(current_user: current_user)
|
|
|
|
end
|
2016-09-29 09:46:39 +05:30
|
|
|
end
|
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
def artifact
|
|
|
|
@artifact ||=
|
|
|
|
project.job_artifacts.find(params[:id])
|
|
|
|
end
|
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
def build_from_id
|
2019-03-02 22:35:43 +05:30
|
|
|
project.builds.find_by_id(params[:job_id]) if params[:job_id]
|
2016-09-29 09:46:39 +05:30
|
|
|
end
|
|
|
|
|
2020-05-30 21:06:31 +05:30
|
|
|
def build_from_sha
|
|
|
|
return if params[:job].blank?
|
2016-09-29 09:46:39 +05:30
|
|
|
return unless @ref_name
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
commit = project.commit(@ref_name)
|
|
|
|
return unless commit
|
|
|
|
|
|
|
|
project.latest_successful_build_for_sha(params[:job], commit.id)
|
2016-01-19 16:12:03 +05:30
|
|
|
end
|
|
|
|
|
2020-05-30 21:06:31 +05:30
|
|
|
def build_from_ref
|
|
|
|
return if params[:job].blank?
|
|
|
|
return unless @ref_name
|
|
|
|
|
|
|
|
project.latest_successful_build_for_ref(params[:job], @ref_name)
|
|
|
|
end
|
|
|
|
|
2016-01-19 16:12:03 +05:30
|
|
|
def artifacts_file
|
2018-12-13 13:39:08 +05:30
|
|
|
@artifacts_file ||= build&.artifacts_file_for_type(params[:file_type] || :archive)
|
2016-01-19 16:12:03 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
def zip_artifact?
|
|
|
|
types = HashWithIndifferentAccess.new(Ci::JobArtifact::TYPE_AND_FORMAT_PAIRS)
|
|
|
|
file_type = params[:file_type] || :archive
|
|
|
|
|
|
|
|
types[file_type] == :zip
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def entry
|
|
|
|
@entry = build.artifacts_metadata_entry(params[:path])
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
render_404 unless @entry.exists?
|
|
|
|
end
|
2022-05-03 16:02:30 +05:30
|
|
|
|
|
|
|
def authorize_read_build_trace!
|
|
|
|
return unless params[:file_type] == 'trace'
|
|
|
|
|
|
|
|
super
|
|
|
|
end
|
2016-01-19 16:12:03 +05:30
|
|
|
end
|