gitlab_ci_yaml_processor.rb 8.68 KB
Newer Older
1 2
module Ci
  class GitlabCiYamlProcessor
3
    class ValidationError < StandardError; end
4

5
    include Gitlab::Ci::Config::Node::LegacyValidationHelpers
6

7
    DEFAULT_STAGE = 'test'
8
    ALLOWED_YAML_KEYS = [:before_script, :after_script, :image, :services, :types, :stages, :variables, :cache]
9 10
    ALLOWED_JOB_KEYS = [:tags, :script, :only, :except, :type, :image, :services,
                        :allow_failure, :type, :stage, :when, :artifacts, :cache,
11 12
                        :dependencies, :before_script, :after_script, :variables,
                        :environment]
Kamil Trzcinski's avatar
Kamil Trzcinski committed
13
    ALLOWED_CACHE_KEYS = [:key, :untracked, :paths]
14
    ALLOWED_ARTIFACTS_KEYS = [:name, :untracked, :paths, :when, :expire_in]
15

16
    attr_reader :path, :cache, :stages
17

18
    def initialize(config, path = nil)
19
      @ci_config = Gitlab::Ci::Config.new(config)
20
      @config, @path = @ci_config.to_hash, path
21

22 23 24
      unless @ci_config.valid?
        raise ValidationError, @ci_config.errors.first
      end
25

26
      initial_parsing
27
    rescue Gitlab::Ci::Config::Loader::FormatError => e
28
      raise ValidationError, e.message
29 30
    end

31
    def builds_for_stage_and_ref(stage, ref, tag = false, trigger_request = nil)
32 33 34 35
      builds.select do |build|
        build[:stage] == stage &&
          process?(build[:only], build[:except], ref, tag, trigger_request)
      end
36 37 38 39 40 41 42 43
    end

    def builds
      @jobs.map do |name, job|
        build_job(name, job)
      end
    end

44
    def global_variables
45 46 47 48
      @variables
    end

    def job_variables(name)
49 50 51
      job = @jobs[name.to_sym]
      return [] unless job

52
      job[:variables] || []
53 54
    end

55 56 57
    private

    def initial_parsing
58 59 60
      ##
      # Global config
      #
61 62
      @before_script = @ci_config.before_script
      @image = @ci_config.image
63
      @after_script = @ci_config.after_script
64
      @services = @ci_config.services
65
      @variables = @ci_config.variables
66
      @stages = @ci_config.stages
67
      @cache = @ci_config.cache
68

69 70 71 72
      ##
      # Jobs
      #
      @jobs = @ci_config.jobs
73 74 75 76

      @jobs.each do |name, job|
        validate_job!(name, job)
      end
77 78 79 80
    end

    def build_job(name, job)
      {
81
        stage_idx: @stages.index(job[:stage]),
82
        stage: job[:stage],
83
        commands: job[:commands],
Kamil Trzcinski's avatar
Kamil Trzcinski committed
84
        tag_list: job[:tags] || [],
85 86 87 88
        name: name,
        only: job[:only],
        except: job[:except],
        allow_failure: job[:allow_failure] || false,
89
        when: job[:when] || 'on_success',
90
        environment: job[:environment],
91 92
        options: {
          image: job[:image] || @image,
93
          services: job[:services] || @services,
94 95
          artifacts: job[:artifacts],
          cache: job[:cache] || @cache,
96
          dependencies: job[:dependencies],
97
          after_script: job[:after_script] || @after_script,
98 99 100 101 102
        }.compact
      }
    end

    def validate_job!(name, job)
103 104
      raise ValidationError, "Unknown parameter: #{name}" unless job.is_a?(Hash) && job.has_key?(:script)

105 106 107
      validate_job_keys!(name, job)
      validate_job_types!(name, job)

108
      validate_job_variables!(name, job) if job[:variables]
109 110
      validate_job_cache!(name, job) if job[:cache]
      validate_job_artifacts!(name, job) if job[:artifacts]
111
      validate_job_dependencies!(name, job) if job[:dependencies]
112 113 114
    end

    def validate_job_keys!(name, job)
115 116 117 118
      ##
      # TODO, remove refactoring keys
      #
      refactoring_keys = [:commands]
119
      job.keys.each do |key|
120
        unless (ALLOWED_JOB_KEYS + refactoring_keys).include? key
Kamil Trzcinski's avatar
Kamil Trzcinski committed
121
          raise ValidationError, "#{name} job: unknown parameter #{key}"
122 123
        end
      end
124
    end
125

126
    def validate_job_types!(name, job)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
127 128
      if job[:image] && !validate_string(job[:image])
        raise ValidationError, "#{name} job: image should be a string"
129 130 131
      end

      if job[:services] && !validate_array_of_strings(job[:services])
Kamil Trzcinski's avatar
Kamil Trzcinski committed
132
        raise ValidationError, "#{name} job: services should be an array of strings"
133 134 135
      end

      if job[:tags] && !validate_array_of_strings(job[:tags])
Kamil Trzcinski's avatar
Kamil Trzcinski committed
136
        raise ValidationError, "#{name} job: tags parameter should be an array of strings"
137 138
      end

139 140
      if job[:only] && !validate_array_of_strings_or_regexps(job[:only])
        raise ValidationError, "#{name} job: only parameter should be an array of strings or regexps"
141 142
      end

143 144
      if job[:except] && !validate_array_of_strings_or_regexps(job[:except])
        raise ValidationError, "#{name} job: except parameter should be an array of strings or regexps"
145 146
      end

147 148
      if job[:allow_failure] && !validate_boolean(job[:allow_failure])
        raise ValidationError, "#{name} job: allow_failure parameter should be an boolean"
149 150
      end

Kamil Trzcinski's avatar
Kamil Trzcinski committed
151
      if job[:when] && !job[:when].in?(%w[on_success on_failure always])
152 153
        raise ValidationError, "#{name} job: when parameter should be on_success, on_failure or always"
      end
154

155 156
      if job[:environment] && !validate_environment(job[:environment])
        raise ValidationError, "#{name} job: environment parameter #{Gitlab::Regex.environment_name_regex_message}"
157
      end
158
    end
159

160
    def validate_job_variables!(name, job)
161
      unless validate_variables(job[:variables])
162
        raise ValidationError,
163
          "#{name} job: variables should be a map of key-value strings"
164 165 166
      end
    end

167
    def validate_job_cache!(name, job)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
168 169 170 171 172 173
      job[:cache].keys.each do |key|
        unless ALLOWED_CACHE_KEYS.include? key
          raise ValidationError, "#{name} job: cache unknown parameter #{key}"
        end
      end

174 175 176 177
      if job[:cache][:key] && !validate_string(job[:cache][:key])
        raise ValidationError, "#{name} job: cache:key parameter should be a string"
      end

178 179
      if job[:cache][:untracked] && !validate_boolean(job[:cache][:untracked])
        raise ValidationError, "#{name} job: cache:untracked parameter should be an boolean"
180
      end
181

182 183
      if job[:cache][:paths] && !validate_array_of_strings(job[:cache][:paths])
        raise ValidationError, "#{name} job: cache:paths parameter should be an array of strings"
184
      end
185 186
    end

187
    def validate_job_artifacts!(name, job)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
188 189 190 191 192 193
      job[:artifacts].keys.each do |key|
        unless ALLOWED_ARTIFACTS_KEYS.include? key
          raise ValidationError, "#{name} job: artifacts unknown parameter #{key}"
        end
      end

194 195 196 197
      if job[:artifacts][:name] && !validate_string(job[:artifacts][:name])
        raise ValidationError, "#{name} job: artifacts:name parameter should be a string"
      end

198 199 200 201 202 203 204
      if job[:artifacts][:untracked] && !validate_boolean(job[:artifacts][:untracked])
        raise ValidationError, "#{name} job: artifacts:untracked parameter should be an boolean"
      end

      if job[:artifacts][:paths] && !validate_array_of_strings(job[:artifacts][:paths])
        raise ValidationError, "#{name} job: artifacts:paths parameter should be an array of strings"
      end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
205

Kamil Trzcinski's avatar
Kamil Trzcinski committed
206
      if job[:artifacts][:when] && !job[:artifacts][:when].in?(%w[on_success on_failure always])
Kamil Trzcinski's avatar
Kamil Trzcinski committed
207 208
        raise ValidationError, "#{name} job: artifacts:when parameter should be on_success, on_failure or always"
      end
209 210 211 212

      if job[:artifacts][:expire_in] && !validate_duration(job[:artifacts][:expire_in])
        raise ValidationError, "#{name} job: artifacts:expire_in parameter should be a duration"
      end
213
    end
214

215
    def validate_job_dependencies!(name, job)
216
      unless validate_array_of_strings(job[:dependencies])
217 218 219
        raise ValidationError, "#{name} job: dependencies parameter should be an array of strings"
      end

220
      stage_index = @stages.index(job[:stage])
221 222

      job[:dependencies].each do |dependency|
223
        raise ValidationError, "#{name} job: undefined dependency: #{dependency}" unless @jobs[dependency.to_sym]
224

225
        unless @stages.index(@jobs[dependency.to_sym][:stage]) < stage_index
226 227 228 229 230
          raise ValidationError, "#{name} job: dependency #{dependency} is not defined in prior stages"
        end
      end
    end

231
    def process?(only_params, except_params, ref, tag, trigger_request)
232
      if only_params.present?
233
        return false unless matching?(only_params, ref, tag, trigger_request)
234 235 236
      end

      if except_params.present?
237
        return false if matching?(except_params, ref, tag, trigger_request)
238 239 240 241 242
      end

      true
    end

243
    def matching?(patterns, ref, tag, trigger_request)
244
      patterns.any? do |pattern|
245
        match_ref?(pattern, ref, tag, trigger_request)
246 247 248
      end
    end

249
    def match_ref?(pattern, ref, tag, trigger_request)
250 251 252 253
      pattern, path = pattern.split('@', 2)
      return false if path && path != self.path
      return true if tag && pattern == 'tags'
      return true if !tag && pattern == 'branches'
254
      return true if trigger_request.present? && pattern == 'triggers'
255 256 257 258 259 260 261

      if pattern.first == "/" && pattern.last == "/"
        Regexp.new(pattern[1...-1]) =~ ref
      else
        pattern == ref
      end
    end
262
  end
263
end