diff --git a/app/models/ci/build.rb b/app/models/ci/build.rb index 5fe8ddf69d7168f8531848a6f45583e39107c29d..4691b33ee9bd020f511c9de3f8a7f2d611bc3fb0 100644 --- a/app/models/ci/build.rb +++ b/app/models/ci/build.rb @@ -276,8 +276,7 @@ module Ci def update_coverage return unless project - coverage_regex = project.build_coverage_regex - return unless coverage_regex + return unless coverage_regex = self.coverage_regex coverage = extract_coverage(trace, coverage_regex) if coverage.is_a? Numeric @@ -522,6 +521,10 @@ module Ci self.update(artifacts_expire_at: nil) end + def coverage_regex + read_attribute(:coverage_regex) || build_attributes_from_config[:coverage] || project.build_coverage_regex + end + def when read_attribute(:when) || build_attributes_from_config[:when] || 'on_success' end diff --git a/db/migrate/20161114024742_add_coverage_regex_to_builds.rb b/db/migrate/20161114024742_add_coverage_regex_to_builds.rb new file mode 100644 index 0000000000000000000000000000000000000000..88aa5d52b396ae2290a9f92f93ff00e6f2b06ab8 --- /dev/null +++ b/db/migrate/20161114024742_add_coverage_regex_to_builds.rb @@ -0,0 +1,13 @@ +# See http://doc.gitlab.com/ce/development/migration_style_guide.html +# for more information on how to write migrations for GitLab. + +class AddCoverageRegexToBuilds < ActiveRecord::Migration + include Gitlab::Database::MigrationHelpers + + # Set this constant to true if this migration requires downtime. + DOWNTIME = false + + def change + add_column :ci_builds, :coverage_regex, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 3c836db27fc2975bfabbb527dafdb94ec25b7602..1cc9e7eec5e23ecbdcba9eb31f05e01cbc02366b 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -215,6 +215,7 @@ ActiveRecord::Schema.define(version: 20170121130655) do t.datetime "queued_at" t.string "token" t.integer "lock_version" + t.string "coverage_regex" end add_index "ci_builds", ["commit_id", "stage_idx", "created_at"], name: "index_ci_builds_on_commit_id_and_stage_idx_and_created_at", using: :btree diff --git a/lib/ci/gitlab_ci_yaml_processor.rb b/lib/ci/gitlab_ci_yaml_processor.rb index 7463bd719d5bb14398d4f516d648ebde9b95593c..2f5ef4d36bd6c91c14873bef3ae419307a0d0f3d 100644 --- a/lib/ci/gitlab_ci_yaml_processor.rb +++ b/lib/ci/gitlab_ci_yaml_processor.rb @@ -61,6 +61,7 @@ module Ci allow_failure: job[:allow_failure] || false, when: job[:when] || 'on_success', environment: job[:environment_name], + coverage_regex: job[:coverage_regex], yaml_variables: yaml_variables(name), options: { image: job[:image], diff --git a/lib/gitlab/ci/config/entry/job.rb b/lib/gitlab/ci/config/entry/job.rb index a55362f0b6b76a8d36c71d2765fdb3215b6ffd94..3c7ef99cefc99ad2a6672e50cbd85182e31fa95f 100644 --- a/lib/gitlab/ci/config/entry/job.rb +++ b/lib/gitlab/ci/config/entry/job.rb @@ -11,7 +11,7 @@ module Gitlab ALLOWED_KEYS = %i[tags script only except type image services allow_failure type stage when artifacts cache dependencies before_script - after_script variables environment] + after_script variables environment coverage_regex] validations do validates :config, allowed_keys: ALLOWED_KEYS @@ -71,9 +71,12 @@ module Gitlab entry :environment, Entry::Environment, description: 'Environment configuration for this job.' + node :coverage_regex, Node::Regexp, + description: 'Coverage scanning regex configuration for this job.' + helpers :before_script, :script, :stage, :type, :after_script, :cache, :image, :services, :only, :except, :variables, - :artifacts, :commands, :environment + :artifacts, :commands, :environment, :coverage_regex attributes :script, :tags, :allow_failure, :when, :dependencies @@ -130,6 +133,7 @@ module Gitlab variables: variables_defined? ? variables_value : nil, environment: environment_defined? ? environment_value : nil, environment_name: environment_defined? ? environment_value[:name] : nil, + coverage_regex: coverage_regex_defined? ? coverage_regex_value : nil, artifacts: artifacts_value, after_script: after_script_value } end diff --git a/lib/gitlab/ci/config/entry/legacy_validation_helpers.rb b/lib/gitlab/ci/config/entry/legacy_validation_helpers.rb index f01975aab5c16858c2312aca11291e2f26f6e33f..34e7052befcd9619e5195ee354f809515ae06eda 100644 --- a/lib/gitlab/ci/config/entry/legacy_validation_helpers.rb +++ b/lib/gitlab/ci/config/entry/legacy_validation_helpers.rb @@ -28,17 +28,21 @@ module Gitlab value.is_a?(String) || value.is_a?(Symbol) end + def validate_regexp(value) + !!::Regexp.new(value) + rescue RegexpError + false + end + def validate_string_or_regexp(value) return true if value.is_a?(Symbol) return false unless value.is_a?(String) if value.first == '/' && value.last == '/' - Regexp.new(value[1...-1]) + validate_regexp(value[1...-1]) else true end - rescue RegexpError - false end def validate_boolean(value) diff --git a/lib/gitlab/ci/config/entry/validators.rb b/lib/gitlab/ci/config/entry/validators.rb index 8632dd0e2333105c8ef87b50afa0f2775e5f003e..03a8205b0815e9c82434d7b013e0027c71048190 100644 --- a/lib/gitlab/ci/config/entry/validators.rb +++ b/lib/gitlab/ci/config/entry/validators.rb @@ -54,6 +54,16 @@ module Gitlab end end + class RegexpValidator < ActiveModel::EachValidator + include LegacyValidationHelpers + + def validate_each(record, attribute, value) + unless validate_regexp(value) + record.errors.add(attribute, 'must be a regular expression') + end + end + end + class TypeValidator < ActiveModel::EachValidator def validate_each(record, attribute, value) type = options[:with] diff --git a/lib/gitlab/ci/config/node/regexp.rb b/lib/gitlab/ci/config/node/regexp.rb new file mode 100644 index 0000000000000000000000000000000000000000..7c5843eb8b6ac060109eacc0a1a191b3ce648fc8 --- /dev/null +++ b/lib/gitlab/ci/config/node/regexp.rb @@ -0,0 +1,26 @@ +module Gitlab + module Ci + class Config + module Node + ## + # Entry that represents a Regular Expression. + # + class Regexp < Entry + include Validatable + + validations do + validates :config, regexp: true + end + + def value + if @config.first == '/' && @config.last == '/' + @config[1...-1] + else + @config + end + end + end + end + end + end +end