Commit 6a3d29c7 authored by Leandro Camargo's avatar Leandro Camargo

Add ability to define a coverage regex in the .gitlab-ci.yml

* Instead of using the proposed `coverage` key, this expects `coverage_regex`
parent d1d90b57
...@@ -276,8 +276,7 @@ module Ci ...@@ -276,8 +276,7 @@ module Ci
def update_coverage def update_coverage
return unless project return unless project
coverage_regex = project.build_coverage_regex return unless coverage_regex = self.coverage_regex
return unless coverage_regex
coverage = extract_coverage(trace, coverage_regex) coverage = extract_coverage(trace, coverage_regex)
if coverage.is_a? Numeric if coverage.is_a? Numeric
...@@ -522,6 +521,10 @@ module Ci ...@@ -522,6 +521,10 @@ module Ci
self.update(artifacts_expire_at: nil) self.update(artifacts_expire_at: nil)
end end
def coverage_regex
read_attribute(:coverage_regex) || build_attributes_from_config[:coverage] || project.build_coverage_regex
end
def when def when
read_attribute(:when) || build_attributes_from_config[:when] || 'on_success' read_attribute(:when) || build_attributes_from_config[:when] || 'on_success'
end end
......
# 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
...@@ -215,6 +215,7 @@ ActiveRecord::Schema.define(version: 20170121130655) do ...@@ -215,6 +215,7 @@ ActiveRecord::Schema.define(version: 20170121130655) do
t.datetime "queued_at" t.datetime "queued_at"
t.string "token" t.string "token"
t.integer "lock_version" t.integer "lock_version"
t.string "coverage_regex"
end 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 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
......
...@@ -61,6 +61,7 @@ module Ci ...@@ -61,6 +61,7 @@ module Ci
allow_failure: job[:allow_failure] || false, allow_failure: job[:allow_failure] || false,
when: job[:when] || 'on_success', when: job[:when] || 'on_success',
environment: job[:environment_name], environment: job[:environment_name],
coverage_regex: job[:coverage_regex],
yaml_variables: yaml_variables(name), yaml_variables: yaml_variables(name),
options: { options: {
image: job[:image], image: job[:image],
......
...@@ -11,7 +11,7 @@ module Gitlab ...@@ -11,7 +11,7 @@ module Gitlab
ALLOWED_KEYS = %i[tags script only except type image services allow_failure ALLOWED_KEYS = %i[tags script only except type image services allow_failure
type stage when artifacts cache dependencies before_script type stage when artifacts cache dependencies before_script
after_script variables environment] after_script variables environment coverage_regex]
validations do validations do
validates :config, allowed_keys: ALLOWED_KEYS validates :config, allowed_keys: ALLOWED_KEYS
...@@ -71,9 +71,12 @@ module Gitlab ...@@ -71,9 +71,12 @@ module Gitlab
entry :environment, Entry::Environment, entry :environment, Entry::Environment,
description: 'Environment configuration for this job.' 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, helpers :before_script, :script, :stage, :type, :after_script,
:cache, :image, :services, :only, :except, :variables, :cache, :image, :services, :only, :except, :variables,
:artifacts, :commands, :environment :artifacts, :commands, :environment, :coverage_regex
attributes :script, :tags, :allow_failure, :when, :dependencies attributes :script, :tags, :allow_failure, :when, :dependencies
...@@ -130,6 +133,7 @@ module Gitlab ...@@ -130,6 +133,7 @@ module Gitlab
variables: variables_defined? ? variables_value : nil, variables: variables_defined? ? variables_value : nil,
environment: environment_defined? ? environment_value : nil, environment: environment_defined? ? environment_value : nil,
environment_name: environment_defined? ? environment_value[:name] : nil, environment_name: environment_defined? ? environment_value[:name] : nil,
coverage_regex: coverage_regex_defined? ? coverage_regex_value : nil,
artifacts: artifacts_value, artifacts: artifacts_value,
after_script: after_script_value } after_script: after_script_value }
end end
......
...@@ -28,17 +28,21 @@ module Gitlab ...@@ -28,17 +28,21 @@ module Gitlab
value.is_a?(String) || value.is_a?(Symbol) value.is_a?(String) || value.is_a?(Symbol)
end end
def validate_regexp(value)
!!::Regexp.new(value)
rescue RegexpError
false
end
def validate_string_or_regexp(value) def validate_string_or_regexp(value)
return true if value.is_a?(Symbol) return true if value.is_a?(Symbol)
return false unless value.is_a?(String) return false unless value.is_a?(String)
if value.first == '/' && value.last == '/' if value.first == '/' && value.last == '/'
Regexp.new(value[1...-1]) validate_regexp(value[1...-1])
else else
true true
end end
rescue RegexpError
false
end end
def validate_boolean(value) def validate_boolean(value)
......
...@@ -54,6 +54,16 @@ module Gitlab ...@@ -54,6 +54,16 @@ module Gitlab
end end
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 class TypeValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value) def validate_each(record, attribute, value)
type = options[:with] type = options[:with]
......
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
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment