summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorValery Sizov <vsv2711@gmail.com>2015-05-29 16:25:04 +0300
committerValery Sizov <vsv2711@gmail.com>2015-06-03 16:36:54 +0300
commit85cd3463d6c023aea4441ecbd086c53e93a600cb (patch)
treefb8f7b3dc1e382c16278156c4cf3a390576aa8c2 /lib
parent2999c8b41f8272b239e4364867012c9e3d4a997a (diff)
downloadgitlab-ci-85cd3463d6c023aea4441ecbd086c53e93a600cb.tar.gz
Implementation of configuration CI with gitlab-ci.yml
Diffstat (limited to 'lib')
-rw-r--r--lib/gitlab_ci_yaml_parser.rb31
-rw-r--r--lib/gitlab_ci_yaml_processor.rb96
2 files changed, 96 insertions, 31 deletions
diff --git a/lib/gitlab_ci_yaml_parser.rb b/lib/gitlab_ci_yaml_parser.rb
deleted file mode 100644
index 51458ad..0000000
--- a/lib/gitlab_ci_yaml_parser.rb
+++ /dev/null
@@ -1,31 +0,0 @@
-# Prototype of parser
-
-class GitlabCiYamlParser
- attr_reader :before_script, :jobs, :on_success
-
- def initialize(config)
- @before_script = ["pwd"]
- @jobs = [{script: "ruby -v", runner: "", name: "Rspec"}]
- @on_success = [script: "cap deploy production", refs: [], name: "Deploy"]
- end
-
- def builds
- @jobs.map do |job|
- {
- name: job[:name],
- commands: "#{@before_script.join("\n")}\n#{job[:script]}"
- }
- end
- end
-
- def deploy_builds
- @on_success.map do |job|
- {
- name: job[:name],
- commands: "#{@before_script.join("\n")}\n#{job[:script]}",
- deploy: true,
- refs: job[:refs]
- }
- end
- end
-end \ No newline at end of file
diff --git a/lib/gitlab_ci_yaml_processor.rb b/lib/gitlab_ci_yaml_processor.rb
new file mode 100644
index 0000000..3726a09
--- /dev/null
+++ b/lib/gitlab_ci_yaml_processor.rb
@@ -0,0 +1,96 @@
+class GitlabCiYamlProcessor
+ attr_reader :before_script, :skip_refs
+
+ 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]
+ end
+
+ 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: job[:script],
+ runner: job[:runner] || "",
+ name: job[:name] || job[:script][0..10],
+ branches: job[:branches] || true,
+ tags: job[:tags] || true
+ }
+ end
+ end
+ end
+
+ def normalized_deploy_jobs
+ @deploy_jobs.map do |job|
+ if job.is_a?(String)
+ {script: job, refs: [], name: job[0..10].strip}
+ else
+ {
+ script: job[:script],
+ refs: job[:refs] || [],
+ name: job[:name] || job[:script][0..10]
+ }
+ end
+ end
+ end
+
+ def builds
+ normalized_jobs.map do |job|
+ {
+ name: job[:name],
+ commands: "#{@before_script.join("\n")}\n#{job[:script]}",
+ tag_list: job[:runner],
+ branches: job[:branches],
+ tags: job[:tag]
+ }
+ end
+ end
+
+ def deploy_builds
+ normalized_deploy_jobs.map do |job|
+ {
+ name: job[:name],
+ commands: "#{@before_script.join("\n")}\n#{job[:script]}",
+ deploy: true,
+ refs: job[:refs]
+ }
+ end
+ end
+
+ def create_commit_for_tag?(ref)
+ normalized_jobs.any?{|job| job[:tags] == true} ||
+ normalized_deploy_jobs.any?{|job| job[:refs].empty? || refs_matches?(job[:refs], ref)}
+ end
+
+ def deploy_builds_for_ref(ref)
+ deploy_builds.select do |build_attrs|
+ refs = build_attrs.delete(:refs)
+ refs.empty? || refs_matches?(refs, ref)
+ end
+ end
+
+ def skip_ref?(ref_name)
+ @skip_refs.split(",").each do |ref|
+ return true if File.fnmatch(ref, ref_name)
+ 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.map(&:strip).each do |ref_pattern|
+ return true if File.fnmatch(ref_pattern, ref)
+ end
+
+ false
+ end
+end \ No newline at end of file