gitlab_ci_yaml_processor.rb 8.6 KB
Newer Older
D
Douwe Maan 已提交
1 2
module Ci
  class GitlabCiYamlProcessor
3
    class ValidationError < StandardError; end
D
Douwe Maan 已提交
4

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

D
Douwe Maan 已提交
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]
K
Kamil Trzcinski 已提交
13
    ALLOWED_CACHE_KEYS = [:key, :untracked, :paths]
14
    ALLOWED_ARTIFACTS_KEYS = [:name, :untracked, :paths, :when, :expire_in]
D
Douwe Maan 已提交
15

16
    attr_reader :path, :cache, :stages
D
Douwe Maan 已提交
17

18
    def initialize(config, path = nil)
19
      @ci_config = Gitlab::Ci::Config.new(config)
20
      @config, @path = @ci_config.to_hash, path
D
Douwe Maan 已提交
21

22 23 24
      unless @ci_config.valid?
        raise ValidationError, @ci_config.errors.first
      end
D
Douwe Maan 已提交
25

26
      initial_parsing
27
    rescue Gitlab::Ci::Config::Loader::FormatError => e
28
      raise ValidationError, e.message
D
Douwe Maan 已提交
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
D
Douwe Maan 已提交
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)
G
Grzegorz Bizon 已提交
49 50 51
      job = @jobs[name.to_sym]
      return [] unless job

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

D
Douwe Maan 已提交
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
D
Douwe Maan 已提交
77 78 79 80
    end

    def build_job(name, job)
      {
81
        stage_idx: @stages.index(job[:stage]),
D
Douwe Maan 已提交
82
        stage: job[:stage],
83
        commands: [job[:before_script] || @before_script, job[:script]].flatten.compact.join("\n"),
K
Kamil Trzcinski 已提交
84
        tag_list: job[:tags] || [],
D
Douwe Maan 已提交
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],
D
Douwe Maan 已提交
91 92
        options: {
          image: job[:image] || @image,
K
Kamil Trzcinski 已提交
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,
D
Douwe Maan 已提交
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)
D
Douwe Maan 已提交
115
      job.keys.each do |key|
116
        unless ALLOWED_JOB_KEYS.include? key
K
Kamil Trzcinski 已提交
117
          raise ValidationError, "#{name} job: unknown parameter #{key}"
D
Douwe Maan 已提交
118 119
        end
      end
120
    end
D
Douwe Maan 已提交
121

122
    def validate_job_types!(name, job)
K
Kamil Trzcinski 已提交
123 124
      if job[:image] && !validate_string(job[:image])
        raise ValidationError, "#{name} job: image should be a string"
D
Douwe Maan 已提交
125 126 127
      end

      if job[:services] && !validate_array_of_strings(job[:services])
K
Kamil Trzcinski 已提交
128
        raise ValidationError, "#{name} job: services should be an array of strings"
D
Douwe Maan 已提交
129 130 131
      end

      if job[:tags] && !validate_array_of_strings(job[:tags])
K
Kamil Trzcinski 已提交
132
        raise ValidationError, "#{name} job: tags parameter should be an array of strings"
D
Douwe Maan 已提交
133 134
      end

135 136
      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"
D
Douwe Maan 已提交
137 138
      end

139 140
      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"
D
Douwe Maan 已提交
141 142
      end

143 144
      if job[:allow_failure] && !validate_boolean(job[:allow_failure])
        raise ValidationError, "#{name} job: allow_failure parameter should be an boolean"
145 146
      end

K
Kamil Trzcinski 已提交
147
      if job[:when] && !job[:when].in?(%w[on_success on_failure always])
148 149
        raise ValidationError, "#{name} job: when parameter should be on_success, on_failure or always"
      end
150

151 152
      if job[:environment] && !validate_environment(job[:environment])
        raise ValidationError, "#{name} job: environment parameter #{Gitlab::Regex.environment_name_regex_message}"
153
      end
154
    end
155

156
    def validate_job_variables!(name, job)
G
Grzegorz Bizon 已提交
157
      unless validate_variables(job[:variables])
158
        raise ValidationError,
G
Grzegorz Bizon 已提交
159
          "#{name} job: variables should be a map of key-value strings"
160 161 162
      end
    end

163
    def validate_job_cache!(name, job)
K
Kamil Trzcinski 已提交
164 165 166 167 168 169
      job[:cache].keys.each do |key|
        unless ALLOWED_CACHE_KEYS.include? key
          raise ValidationError, "#{name} job: cache unknown parameter #{key}"
        end
      end

170 171 172 173
      if job[:cache][:key] && !validate_string(job[:cache][:key])
        raise ValidationError, "#{name} job: cache:key parameter should be a string"
      end

174 175
      if job[:cache][:untracked] && !validate_boolean(job[:cache][:untracked])
        raise ValidationError, "#{name} job: cache:untracked parameter should be an boolean"
D
Douwe Maan 已提交
176
      end
177

178 179
      if job[:cache][:paths] && !validate_array_of_strings(job[:cache][:paths])
        raise ValidationError, "#{name} job: cache:paths parameter should be an array of strings"
180
      end
D
Douwe Maan 已提交
181 182
    end

183
    def validate_job_artifacts!(name, job)
K
Kamil Trzcinski 已提交
184 185 186 187 188 189
      job[:artifacts].keys.each do |key|
        unless ALLOWED_ARTIFACTS_KEYS.include? key
          raise ValidationError, "#{name} job: artifacts unknown parameter #{key}"
        end
      end

190 191 192 193
      if job[:artifacts][:name] && !validate_string(job[:artifacts][:name])
        raise ValidationError, "#{name} job: artifacts:name parameter should be a string"
      end

194 195 196 197 198 199 200
      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
K
Kamil Trzcinski 已提交
201

K
Kamil Trzcinski 已提交
202
      if job[:artifacts][:when] && !job[:artifacts][:when].in?(%w[on_success on_failure always])
K
Kamil Trzcinski 已提交
203 204
        raise ValidationError, "#{name} job: artifacts:when parameter should be on_success, on_failure or always"
      end
205 206 207 208

      if job[:artifacts][:expire_in] && !validate_duration(job[:artifacts][:expire_in])
        raise ValidationError, "#{name} job: artifacts:expire_in parameter should be a duration"
      end
209
    end
D
Douwe Maan 已提交
210

211
    def validate_job_dependencies!(name, job)
212
      unless validate_array_of_strings(job[:dependencies])
213 214 215
        raise ValidationError, "#{name} job: dependencies parameter should be an array of strings"
      end

216
      stage_index = @stages.index(job[:stage])
217 218

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

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

227
    def process?(only_params, except_params, ref, tag, trigger_request)
228
      if only_params.present?
229
        return false unless matching?(only_params, ref, tag, trigger_request)
230 231 232
      end

      if except_params.present?
233
        return false if matching?(except_params, ref, tag, trigger_request)
234 235 236 237 238
      end

      true
    end

239
    def matching?(patterns, ref, tag, trigger_request)
240
      patterns.any? do |pattern|
241
        match_ref?(pattern, ref, tag, trigger_request)
242 243 244
      end
    end

J
Jason Roehm 已提交
245
    def match_ref?(pattern, ref, tag, trigger_request)
246 247 248 249
      pattern, path = pattern.split('@', 2)
      return false if path && path != self.path
      return true if tag && pattern == 'tags'
      return true if !tag && pattern == 'branches'
J
Jason Roehm 已提交
250
      return true if trigger_request.present? && pattern == 'triggers'
251 252 253 254 255 256 257

      if pattern.first == "/" && pattern.last == "/"
        Regexp.new(pattern[1...-1]) =~ ref
      else
        pattern == ref
      end
    end
D
Douwe Maan 已提交
258
  end
259
end