summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShinya Maeda <shinya@gitlab.com>2018-07-20 15:42:16 +0900
committerShinya Maeda <shinya@gitlab.com>2018-07-20 15:42:16 +0900
commit75f75b3f5988398fff0660ca5f04aec756ab03bb (patch)
tree8021dbdbdb4c91f30a6fb4577e6e0f8b48002c68
parent9ecaee914defba5f12a7a06375ea2876b4328d7f (diff)
downloadgitlab-ce-75f75b3f5988398fff0660ca5f04aec756ab03bb.tar.gz
Implement config artifact presenter
-rw-r--r--app/models/ci/job_artifact.rb2
-rw-r--r--app/presenters/ci/build_presenter.rb41
-rw-r--r--lib/api/entities.rb4
-rw-r--r--lib/gitlab/ci/config/entry/artifacts.rb46
-rw-r--r--lib/gitlab/ci/config/entry/reports.rb12
5 files changed, 81 insertions, 24 deletions
diff --git a/app/models/ci/job_artifact.rb b/app/models/ci/job_artifact.rb
index e31662efae3..c4f1e900615 100644
--- a/app/models/ci/job_artifact.rb
+++ b/app/models/ci/job_artifact.rb
@@ -33,7 +33,9 @@ module Ci
junit: 4
}
+ GENERAL_FILE_TYPES = %w[archive].freeze
TEST_REPORTS_FILE_TYPES = %w[junit].freeze
+ DEFAULT_FILE_NAMES = { junit: 'junit.xml' }.freeze
enum file_format: {
raw: 1,
diff --git a/app/presenters/ci/build_presenter.rb b/app/presenters/ci/build_presenter.rb
index 6b52c91749f..cca05771a08 100644
--- a/app/presenters/ci/build_presenter.rb
+++ b/app/presenters/ci/build_presenter.rb
@@ -34,14 +34,27 @@ module Ci
end
def config_artifacts
- list = []
+ [].tap do |list|
+ next unless options[:artifacts]
- options.dig(:artifacts).try do |artifacts|
- list << config_artifacts_reports(artifacts.delete(:reports)) if artifacts.dig(:reports)
- list << config_artifacts_archive(artifacts) if artifacts.dig(:paths)
- end
+ options[:artifacts].except(:reports).try do |archive|
+ ::Gitlab::Ci::Config::Entry::Artifacts.new(archive).present_for_runner.tap do |runner_config|
+ raise ArgumentError, 'General artifact config for runner is not valid' unless ::Gitlab::Ci::Config::Entry::ArtifactsForRunner.new(runner_config).valid?
+
+ list << runner_config
+ end
+ end
+
+ options[:artifacts].dig(:reports).try do |reports|
+ ::Gitlab::Ci::Config::Entry::Reports.new(reports).present_for_runner.tap do |runner_configs|
+ list << runner_configs.map do |runner_config|
+ raise ArgumentError, 'Report-type artifact config for runner is not valid' unless ::Gitlab::Ci::Config::Entry::ArtifactsForRunner.new(runner_config).valid?
- list.flatten
+ runner_config
+ end
+ end
+ end
+ end.flatten
end
private
@@ -53,21 +66,5 @@ module Ci
def detailed_status
@detailed_status ||= subject.detailed_status(user)
end
-
- def config_artifacts_archive(artifacts)
- artifacts.merge(type: :archive, format: :zip)
- end
-
- def config_artifacts_reports(reports)
- list = []
-
- list << config_artifacts_reports_junit(reports.dig(:junit)) if reports.dig(:junit)
-
- list
- end
-
- def config_artifacts_reports_junit(junit)
- { name: 'junit.xml', paths: junit, type: :junit, format: :gzip, when: :always }
- end
end
end
diff --git a/lib/api/entities.rb b/lib/api/entities.rb
index 84453666606..f5db70a9466 100644
--- a/lib/api/entities.rb
+++ b/lib/api/entities.rb
@@ -1228,8 +1228,8 @@ module API
class Artifacts < Grape::Entity
expose :name, :untracked, :paths, :when, :expire_in
- expose :type, as: :artifact_type
- expose :format, as: :artifact_format
+ expose :artifact_type
+ expose :artifact_format
end
class Cache < Grape::Entity
diff --git a/lib/gitlab/ci/config/entry/artifacts.rb b/lib/gitlab/ci/config/entry/artifacts.rb
index 05788f62c72..bc5013dba6a 100644
--- a/lib/gitlab/ci/config/entry/artifacts.rb
+++ b/lib/gitlab/ci/config/entry/artifacts.rb
@@ -40,6 +40,52 @@ module Gitlab
@config[:reports] = reports_value if @config.key?(:reports)
@config
end
+
+ def present_for_runner
+ @config.merge(artifact_type: :archive, artifact_format: :zip)
+ end
+ end
+
+ class ArtifactsForRunner < Node
+ include Validatable
+ include Attributable
+
+ ALLOWED_KEYS = %i[name untracked paths when expire_in artifact_type artifact_format].freeze
+
+ attributes ALLOWED_KEYS
+
+ validations do
+ validates :config, type: Hash
+ validates :config, allowed_keys: ALLOWED_KEYS
+ validates :artifact_type, type: Symbol
+ validates :artifact_format, type: Symbol
+
+ with_options if: :general_artifact? do
+ with_options allow_nil: true do
+ validates :name, type: String
+ validates :untracked, boolean: true
+ validates :paths, array_of_strings: true
+ validates :when, inclusion: { in: %w[on_success on_failure always] }
+ validates :expire_in, duration: true
+ end
+ end
+
+ with_options if: :report_artifact? do
+ validates :name, type: String
+ validates :untracked, presence: false
+ validates :paths, array_of_strings: true
+ validates :when, inclusion: { in: %w[always] }
+ validates :expire_in, presence: false
+ end
+ end
+
+ def general_artifact?
+ ::Ci::JobArtifact::GENERAL_FILE_TYPES.include?(@config[:artifact_type])
+ end
+
+ def report_artifact?
+ ::Ci::JobArtifact::TEST_REPORTS_FILE_TYPES.include?(@config[:artifact_type])
+ end
end
end
end
diff --git a/lib/gitlab/ci/config/entry/reports.rb b/lib/gitlab/ci/config/entry/reports.rb
index 5963f3eb90c..fcc28a42521 100644
--- a/lib/gitlab/ci/config/entry/reports.rb
+++ b/lib/gitlab/ci/config/entry/reports.rb
@@ -25,6 +25,18 @@ module Gitlab
def value
@config.transform_values { |v| Array(v) }
end
+
+ def present_for_runner
+ @config.map do |k, v|
+ {
+ name: ::Ci::JobArtifact::DEFAULT_FILE_NAMES[k.to_sym],
+ paths: v,
+ artifact_type: k.to_sym,
+ artifact_format: :gzip,
+ when: 'always'
+ }
+ end
+ end
end
end
end