summaryrefslogtreecommitdiff
path: root/app/services/ci/find_exposed_artifacts_service.rb
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2019-10-21 15:05:58 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2019-10-21 15:05:58 +0000
commitad1e4b8fb8104b642fa79ed34fd144bc2bed8a19 (patch)
tree78f95d63d4ea5ed0b1a8b3c83c38d9cbd682f884 /app/services/ci/find_exposed_artifacts_service.rb
parent664c4c7b49c6056136299817eb79e9f1de83e567 (diff)
downloadgitlab-ce-ad1e4b8fb8104b642fa79ed34fd144bc2bed8a19.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/services/ci/find_exposed_artifacts_service.rb')
-rw-r--r--app/services/ci/find_exposed_artifacts_service.rb70
1 files changed, 70 insertions, 0 deletions
diff --git a/app/services/ci/find_exposed_artifacts_service.rb b/app/services/ci/find_exposed_artifacts_service.rb
new file mode 100644
index 00000000000..5c75af294bf
--- /dev/null
+++ b/app/services/ci/find_exposed_artifacts_service.rb
@@ -0,0 +1,70 @@
+# frozen_string_literal: true
+
+module Ci
+ # This class loops through all builds with exposed artifacts and returns
+ # basic information about exposed artifacts for given jobs for the frontend
+ # to display them as custom links in the merge request.
+ #
+ # This service must be used with care.
+ # Looking for exposed artifacts is very slow and should be done asynchronously.
+ class FindExposedArtifactsService < ::BaseService
+ include Gitlab::Routing
+
+ MAX_EXPOSED_ARTIFACTS = 10
+
+ def for_pipeline(pipeline, limit: MAX_EXPOSED_ARTIFACTS)
+ results = []
+
+ pipeline.builds.latest.with_exposed_artifacts.find_each do |job|
+ if job_exposed_artifacts = for_job(job)
+ results << job_exposed_artifacts
+ end
+
+ break if results.size >= limit
+ end
+
+ results
+ end
+
+ def for_job(job)
+ return unless job.has_exposed_artifacts?
+
+ metadata_entries = first_2_metadata_entries_for_artifacts_paths(job)
+ return if metadata_entries.empty?
+
+ {
+ text: job.artifacts_expose_as,
+ url: path_for_entries(metadata_entries, job),
+ job_path: project_job_path(project, job),
+ job_name: job.name
+ }
+ end
+
+ private
+
+ # we don't need to fetch all artifacts entries for a job because
+ # it could contain many. We only need to know whether it has 1 or more
+ # artifacts, so fetching the first 2 would be sufficient.
+ def first_2_metadata_entries_for_artifacts_paths(job)
+ job.artifacts_paths
+ .lazy
+ .map { |path| job.artifacts_metadata_entry(path, recursive: true) }
+ .select { |entry| entry.exists? }
+ .first(2)
+ end
+
+ def path_for_entries(entries, job)
+ return if entries.empty?
+
+ if single_artifact?(entries)
+ file_project_job_artifacts_path(project, job, entries.first.path)
+ else
+ browse_project_job_artifacts_path(project, job)
+ end
+ end
+
+ def single_artifact?(entries)
+ entries.size == 1 && entries.first.file?
+ end
+ end
+end