From 646b9c54d043edf17924e82d8e80a56e18d14ce4 Mon Sep 17 00:00:00 2001
From: Leandro Camargo <leandroico@gmail.com>
Date: Fri, 18 Nov 2016 01:42:35 -0200
Subject: [PATCH] Comply to requests made in the review and adjust to the
 Entry/Node changes

This commit:
* Turns `coverage_regex` into `coverage` entry in yml file
* Fixes smaller requests from code reviewers for the previous commit
* This commit is temporary (will be squashed afterwards)

This commit does not (further commits will do though):
* Add global `coverage` entry handling in yml file as suggested by Grzegorz
* Add specs
* Create changelog
* Create docs
---
 app/models/ci/build.rb                                    | 6 +++---
 lib/ci/gitlab_ci_yaml_processor.rb                        | 2 +-
 .../ci/config/{node/regexp.rb => entry/coverage.rb}       | 4 ++--
 lib/gitlab/ci/config/entry/job.rb                         | 8 ++++----
 lib/gitlab/ci/config/entry/legacy_validation_helpers.rb   | 3 ++-
 5 files changed, 12 insertions(+), 11 deletions(-)
 rename lib/gitlab/ci/config/{node/regexp.rb => entry/coverage.rb} (90%)

diff --git a/app/models/ci/build.rb b/app/models/ci/build.rb
index 4691b33ee9b..46a6b4c724a 100644
--- a/app/models/ci/build.rb
+++ b/app/models/ci/build.rb
@@ -276,8 +276,8 @@ module Ci
 
     def update_coverage
       return unless project
-      return unless coverage_regex = self.coverage_regex
-      coverage = extract_coverage(trace, coverage_regex)
+      return unless regex = self.coverage_regex
+      coverage = extract_coverage(trace, regex)
 
       if coverage.is_a? Numeric
         update_attributes(coverage: coverage)
@@ -522,7 +522,7 @@ module Ci
     end
 
     def coverage_regex
-      read_attribute(:coverage_regex) || build_attributes_from_config[:coverage] || project.build_coverage_regex
+      read_attribute(:coverage_regex) || project.build_coverage_regex
     end
 
     def when
diff --git a/lib/ci/gitlab_ci_yaml_processor.rb b/lib/ci/gitlab_ci_yaml_processor.rb
index 2f5ef4d36bd..649ee4d018b 100644
--- a/lib/ci/gitlab_ci_yaml_processor.rb
+++ b/lib/ci/gitlab_ci_yaml_processor.rb
@@ -61,7 +61,7 @@ module Ci
         allow_failure: job[:allow_failure] || false,
         when: job[:when] || 'on_success',
         environment: job[:environment_name],
-        coverage_regex: job[:coverage_regex],
+        coverage_regex: job[:coverage],
         yaml_variables: yaml_variables(name),
         options: {
           image: job[:image],
diff --git a/lib/gitlab/ci/config/node/regexp.rb b/lib/gitlab/ci/config/entry/coverage.rb
similarity index 90%
rename from lib/gitlab/ci/config/node/regexp.rb
rename to lib/gitlab/ci/config/entry/coverage.rb
index 7c5843eb8b6..88fc03db2d9 100644
--- a/lib/gitlab/ci/config/node/regexp.rb
+++ b/lib/gitlab/ci/config/entry/coverage.rb
@@ -1,11 +1,11 @@
 module Gitlab
   module Ci
     class Config
-      module Node
+      module Entry
         ##
         # Entry that represents a Regular Expression.
         #
-        class Regexp < Entry
+        class Coverage < Node
           include Validatable
 
           validations do
diff --git a/lib/gitlab/ci/config/entry/job.rb b/lib/gitlab/ci/config/entry/job.rb
index 3c7ef99cefc..bde6663344a 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 coverage_regex]
+                            after_script variables environment coverage]
 
           validations do
             validates :config, allowed_keys: ALLOWED_KEYS
@@ -71,12 +71,12 @@ module Gitlab
           entry :environment, Entry::Environment,
                description: 'Environment configuration for this job.'
 
-          node :coverage_regex, Node::Regexp,
+          entry :coverage, Entry::Coverage,
                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, :coverage_regex
+                  :artifacts, :commands, :environment, :coverage
 
           attributes :script, :tags, :allow_failure, :when, :dependencies
 
@@ -133,7 +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,
+              coverage: coverage_defined? ? coverage_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 34e7052befc..98db4632dad 100644
--- a/lib/gitlab/ci/config/entry/legacy_validation_helpers.rb
+++ b/lib/gitlab/ci/config/entry/legacy_validation_helpers.rb
@@ -29,7 +29,8 @@ module Gitlab
           end
 
           def validate_regexp(value)
-            !!::Regexp.new(value)
+            Regexp.new(value)
+            true
           rescue RegexpError
             false
           end
-- 
GitLab