diff options
author | Valery Sizov <vsv2711@gmail.com> | 2015-06-19 17:49:40 +0300 |
---|---|---|
committer | Valery Sizov <vsv2711@gmail.com> | 2015-06-19 17:51:53 +0300 |
commit | c61492a1f44abe3236ef0d43118b7e3da9fb4f4d (patch) | |
tree | bc4c307fe361d0caa380a64bbfe4ede9c47f5a18 /lib | |
parent | 274f5302edbb77be3034c56410bf3021ffe053cc (diff) | |
download | gitlab-ci-c61492a1f44abe3236ef0d43118b7e3da9fb4f4d.tar.gz |
yaml refactoring
Diffstat (limited to 'lib')
-rw-r--r-- | lib/gitlab_ci_yaml_processor.rb | 53 |
1 files changed, 22 insertions, 31 deletions
diff --git a/lib/gitlab_ci_yaml_processor.rb b/lib/gitlab_ci_yaml_processor.rb index 51f412a..6bb1c4b 100644 --- a/lib/gitlab_ci_yaml_processor.rb +++ b/lib/gitlab_ci_yaml_processor.rb @@ -1,25 +1,20 @@ class GitlabCiYamlProcessor - attr_reader :before_script, :skip_refs, :errors + class ValidationError < StandardError;end - def initialize(config) - @errors = [] - - @config = YAML.load(config).deep_symbolize_keys - @before_script = @config[:before_script] || [] + attr_reader :before_script - @config.delete(:before_script) + def initialize(config) + @config = YAML.load(config) - @jobs = @config.select{|key, value| value[:type] != "deploy"} + unless @config.is_a? Hash + raise ValidationError, "YAML should be a hash" + end - @deploy_jobs = @config.select{|key, value| value[:type] == "deploy"} + @config = @config.deep_symbolize_keys - rescue Exception => e - @errors << "Yaml file is invalid" - end + initial_parsing - def valid? validate! - !@errors.any? end def deploy_builds_for_ref(ref, tag = false) @@ -30,10 +25,6 @@ class GitlabCiYamlProcessor builds.select{|build| process?(build[:only], build[:except], ref, tag)} end - def any_jobs?(ref, tag = false) - builds_for_ref(ref, tag).any? || deploy_builds_for_ref(ref, tag).any? - end - def builds @jobs.map do |name, job| { @@ -60,6 +51,13 @@ class GitlabCiYamlProcessor private + def initial_parsing + @before_script = @config[:before_script] || [] + @config.delete(:before_script) + @jobs = @config.select{|key, value| value[:type] != "deploy"} + @deploy_jobs = @config.select{|key, value| value[:type] == "deploy"} + end + def process?(only_params, except_params, ref, tag) return true if only_params.nil? && except_params.nil? @@ -97,41 +95,34 @@ class GitlabCiYamlProcessor end def validate! - unless @config.is_a? Hash - @errors << "should be a hash" - return false - end - @jobs.each do |name, job| if job[:tags] && !job[:tags].is_a?(Array) - @errors << "#{name} job: tags parameter should be an array" + raise ValidationError, "#{name} job: tags parameter should be an array" end if job[:only] && !job[:only].is_a?(Array) - @errors << "#{name} job: only parameter should be an array" + raise ValidationError, "#{name} job: only parameter should be an array" end if job[:except] && !job[:except].is_a?(Array) - @errors << "#{name} job: except parameter should be an array" + raise ValidationError, "#{name} job: except parameter should be an array" end end @deploy_jobs.each do |name, job| if job[:tags] && !job[:tags].is_a?(Array) - @errors << "#{name} deploy job: tags parameter should be an array" + raise ValidationError, "#{name} deploy job: tags parameter should be an array" end if job[:only] && !job[:only].is_a?(Array) - @errors << "#{name} deploy job: only parameter should be an array" + raise ValidationError, "#{name} deploy job: only parameter should be an array" end if job[:except] && !job[:except].is_a?(Array) - @errors << "#{name} deploy job: except parameter should be an array" + raise ValidationError, "#{name} deploy job: except parameter should be an array" end end true - rescue - @errors << "Undefined error" end end |