summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorValery Sizov <vsv2711@gmail.com>2015-06-12 13:16:54 +0300
committerValery Sizov <vsv2711@gmail.com>2015-06-12 13:59:37 +0300
commitdc28749419635cf20b1a62a839186adf0640697c (patch)
tree702060acfb8a0953728f501bc4f12dce16f8a6d7 /lib
parente744d06515eb4195115d451b744009aa4dd24d1f (diff)
downloadgitlab-ci-dc28749419635cf20b1a62a839186adf0640697c.tar.gz
New syntax of gitlab-ci.yml
Diffstat (limited to 'lib')
-rw-r--r--lib/gitlab_ci_yaml_processor.rb122
1 files changed, 52 insertions, 70 deletions
diff --git a/lib/gitlab_ci_yaml_processor.rb b/lib/gitlab_ci_yaml_processor.rb
index 32f7938..274754f 100644
--- a/lib/gitlab_ci_yaml_processor.rb
+++ b/lib/gitlab_ci_yaml_processor.rb
@@ -3,104 +3,86 @@ class GitlabCiYamlProcessor
def initialize(config)
@config = YAML.load(config).deep_symbolize_keys
- @skip_refs = @config[:skip_refs] || ""
@before_script = @config[:before_script] || []
- @jobs = @config[:jobs] || []
- @deploy_jobs = @config[:deploy_jobs] || []
+
+ @config.delete(:before_script)
+
+ @jobs = @config.select{|key, value| value[:test]}
+
+ @deploy_jobs = @config.select{|key, value| value[:deploy]}
end
- def builds
- normalized_jobs.map do |job|
- {
- name: job[:name],
- commands: "#{normalized_script(@before_script)}\n#{job[:script]}",
- tag_list: job[:runner],
- branches: job[:branches],
- tags: job[:tag]
- }
- end
+ def deploy_builds_for_ref(ref, tag = false)
+ deploy_builds.select{|build| process?(build[:only], build[:except], ref, tag)}
end
- def deploy_builds
- normalized_deploy_jobs.map do |job|
- {
- name: job[:name],
- commands: "#{normalized_script(@before_script)}\n#{job[:script]}",
- deploy: true,
- refs: job[:refs],
- tag_list: job[:runner]
- }
- end
+ def builds_for_ref(ref, tag = false)
+ builds.select{|build| process?(build[:only], build[:except], ref, tag)}
end
- def create_commit_for_tag?(ref)
- normalized_jobs.any?{|job| job[:tags] == true} ||
- normalized_deploy_jobs.any?{|job| job[:refs].blank? || refs_matches?(job[:refs].split(",").map(&:strip), ref)}
+ def any_jobs?(ref, tag = false)
+ builds_for_ref(ref, tag).any? || deploy_builds_for_ref(ref, tag).any?
end
- def deploy_builds_for_ref(ref)
- deploy_builds.select do |build_attrs|
- refs = build_attrs.delete(:refs)
- refs.blank? || refs_matches?(refs.split(",").map(&:strip), ref)
+ def builds
+ @jobs.map do |name, job|
+ {
+ script: "#{@before_script.join("\n")}\n#{normilize_script(job[:test])}",
+ tags: job[:tags] || [],
+ name: name,
+ only: job[:only],
+ except: job[:except]
+ }
end
end
- def skip_ref?(ref_name)
- @skip_refs.split(",").each do |ref|
- return true if File.fnmatch(ref, ref_name)
+ def deploy_builds
+ @deploy_jobs.map do |name, job|
+ {
+ script: "#{@before_script.join("\n")}\n#{normilize_script(job[:deploy])}",
+ tags: job[:tags] || [],
+ name: name,
+ only: job[:only],
+ except: job[:except]
+ }
end
-
- false
end
private
- # refs - list of refs. Glob syntax is supported. Ex. ["feature*", "bug"]
- # ref - ref that should be checked
- def refs_matches?(refs, ref)
- refs.each do |ref_pattern|
- return true if File.fnmatch(ref_pattern, ref)
- end
+ def process?(only_params, except_params, ref, tag)
+ return true if only_params.nil? && except_params.nil?
- false
- end
+ if only_params
+ return true if tag && only_params.include?("tags")
+ return true if !tag && only_params.include?("branches")
+
+ only_params.each do |pattern|
+ return match_ref?(pattern, ref)
+ end
+ else
+ return false if tag && except_params.include?("tags")
+ return false if !tag && except_params.include?("branches")
- def normalized_jobs
- @jobs.map do |job|
- if job.is_a?(String)
- { script: job, runner: "", name: job[0..10], branches: true, tags: true }
- else
- {
- script: normalized_script(job[:script]),
- runner: job[:runner] || "",
- name: job[:name] || job[:script][0..10],
- branches: job[:branches].nil? ? true : job[:branches],
- tags: job[:tags].nil? ? true : job[:tags]
- }
+ except_params.each do |pattern|
+ return false if match_ref?(pattern, ref)
end
end
end
- def normalized_deploy_jobs
- @deploy_jobs.map do |job|
- if job.is_a?(String)
- { script: job, runner: "", refs: "", name: job[0..10].strip }
- else
- {
- script: normalized_script(job[:script]),
- refs: job[:refs] || "",
- name: job[:name] || job[:script][0..10].strip,
- runner: job[:runner] || "",
- }
- end
+ def match_ref?(pattern, ref)
+ if pattern.first == "/" && pattern.last == "/"
+ Regexp.new(pattern[1...-1]) =~ ref
+ else
+ pattern == ref
end
end
- def normalized_script(script)
+ def normilize_script(script)
if script.is_a? Array
- script.map(&:strip).join("\n")
+ script.join("\n")
else
- script.strip
+ script
end
end
end