From 6609589b935147886fbaba187231af7ada846d43 Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Mon, 6 Jun 2016 09:05:00 +0200 Subject: Add ci config global and before_script entries --- lib/gitlab/ci/config/entry/base_entry.rb | 15 +++++++++++++++ lib/gitlab/ci/config/entry/before_script.rb | 13 +++++++++++++ lib/gitlab/ci/config/entry/global.rb | 13 +++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 lib/gitlab/ci/config/entry/base_entry.rb create mode 100644 lib/gitlab/ci/config/entry/before_script.rb create mode 100644 lib/gitlab/ci/config/entry/global.rb (limited to 'lib') diff --git a/lib/gitlab/ci/config/entry/base_entry.rb b/lib/gitlab/ci/config/entry/base_entry.rb new file mode 100644 index 00000000000..3a41487d897 --- /dev/null +++ b/lib/gitlab/ci/config/entry/base_entry.rb @@ -0,0 +1,15 @@ +module Gitlab + module Ci + class Config + module Entry + class BaseEntry + def initialize(hash, config, parent = nil) + @hash = hash + @config = config + @parent = parent + end + end + end + end + end +end diff --git a/lib/gitlab/ci/config/entry/before_script.rb b/lib/gitlab/ci/config/entry/before_script.rb new file mode 100644 index 00000000000..b7f15355a51 --- /dev/null +++ b/lib/gitlab/ci/config/entry/before_script.rb @@ -0,0 +1,13 @@ +module Gitlab + module Ci + class Config + module Entry + class BeforeScript < BaseEntry + def leaf? + true + end + end + end + end + end +end diff --git a/lib/gitlab/ci/config/entry/global.rb b/lib/gitlab/ci/config/entry/global.rb new file mode 100644 index 00000000000..e333ecb9495 --- /dev/null +++ b/lib/gitlab/ci/config/entry/global.rb @@ -0,0 +1,13 @@ +module Gitlab + module Ci + class Config + module Entry + class Global < BaseEntry + def allowed_keys + [] + end + end + end + end + end +end -- cgit v1.2.1 From 7f2f683eeb2c3b443f519e2e83dbb3d789a00cf8 Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Mon, 6 Jun 2016 09:24:16 +0200 Subject: Rename ci config module that holds nodes to Node --- lib/gitlab/ci/config/entry/base_entry.rb | 15 --------------- lib/gitlab/ci/config/entry/before_script.rb | 13 ------------- lib/gitlab/ci/config/entry/global.rb | 13 ------------- lib/gitlab/ci/config/node/before_script.rb | 13 +++++++++++++ lib/gitlab/ci/config/node/entry.rb | 19 +++++++++++++++++++ lib/gitlab/ci/config/node/global.rb | 10 ++++++++++ 6 files changed, 42 insertions(+), 41 deletions(-) delete mode 100644 lib/gitlab/ci/config/entry/base_entry.rb delete mode 100644 lib/gitlab/ci/config/entry/before_script.rb delete mode 100644 lib/gitlab/ci/config/entry/global.rb create mode 100644 lib/gitlab/ci/config/node/before_script.rb create mode 100644 lib/gitlab/ci/config/node/entry.rb create mode 100644 lib/gitlab/ci/config/node/global.rb (limited to 'lib') diff --git a/lib/gitlab/ci/config/entry/base_entry.rb b/lib/gitlab/ci/config/entry/base_entry.rb deleted file mode 100644 index 3a41487d897..00000000000 --- a/lib/gitlab/ci/config/entry/base_entry.rb +++ /dev/null @@ -1,15 +0,0 @@ -module Gitlab - module Ci - class Config - module Entry - class BaseEntry - def initialize(hash, config, parent = nil) - @hash = hash - @config = config - @parent = parent - end - end - end - end - end -end diff --git a/lib/gitlab/ci/config/entry/before_script.rb b/lib/gitlab/ci/config/entry/before_script.rb deleted file mode 100644 index b7f15355a51..00000000000 --- a/lib/gitlab/ci/config/entry/before_script.rb +++ /dev/null @@ -1,13 +0,0 @@ -module Gitlab - module Ci - class Config - module Entry - class BeforeScript < BaseEntry - def leaf? - true - end - end - end - end - end -end diff --git a/lib/gitlab/ci/config/entry/global.rb b/lib/gitlab/ci/config/entry/global.rb deleted file mode 100644 index e333ecb9495..00000000000 --- a/lib/gitlab/ci/config/entry/global.rb +++ /dev/null @@ -1,13 +0,0 @@ -module Gitlab - module Ci - class Config - module Entry - class Global < BaseEntry - def allowed_keys - [] - end - end - end - end - end -end diff --git a/lib/gitlab/ci/config/node/before_script.rb b/lib/gitlab/ci/config/node/before_script.rb new file mode 100644 index 00000000000..bf73c01efbc --- /dev/null +++ b/lib/gitlab/ci/config/node/before_script.rb @@ -0,0 +1,13 @@ +module Gitlab + module Ci + class Config + module Node + class BeforeScript < Entry + def leaf? + true + end + end + end + end + end +end diff --git a/lib/gitlab/ci/config/node/entry.rb b/lib/gitlab/ci/config/node/entry.rb new file mode 100644 index 00000000000..eb1b52a3e5d --- /dev/null +++ b/lib/gitlab/ci/config/node/entry.rb @@ -0,0 +1,19 @@ +module Gitlab + module Ci + class Config + module Node + class Entry + def initialize(hash, config, parent = nil) + @hash = hash + @config = config + @parent = parent + end + + def allowed_keys + [] + end + end + end + end + end +end diff --git a/lib/gitlab/ci/config/node/global.rb b/lib/gitlab/ci/config/node/global.rb new file mode 100644 index 00000000000..b3dd6df0a44 --- /dev/null +++ b/lib/gitlab/ci/config/node/global.rb @@ -0,0 +1,10 @@ +module Gitlab + module Ci + class Config + module Node + class Global < Entry + end + end + end + end +end -- cgit v1.2.1 From 8048dcc8e693d713a94a7b9361672692f4e5932f Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Mon, 6 Jun 2016 10:43:11 +0200 Subject: Implement CI configuration nodes tree processing --- lib/gitlab/ci/config/node/before_script.rb | 7 +++++-- lib/gitlab/ci/config/node/entry.rb | 22 ++++++++++++++++++++-- lib/gitlab/ci/config/node/global.rb | 3 +++ 3 files changed, 28 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/ci/config/node/before_script.rb b/lib/gitlab/ci/config/node/before_script.rb index bf73c01efbc..88ebd6bb304 100644 --- a/lib/gitlab/ci/config/node/before_script.rb +++ b/lib/gitlab/ci/config/node/before_script.rb @@ -3,8 +3,11 @@ module Gitlab class Config module Node class BeforeScript < Entry - def leaf? - true + def keys + {} + end + + def validate! end end end diff --git a/lib/gitlab/ci/config/node/entry.rb b/lib/gitlab/ci/config/node/entry.rb index eb1b52a3e5d..6336535bc03 100644 --- a/lib/gitlab/ci/config/node/entry.rb +++ b/lib/gitlab/ci/config/node/entry.rb @@ -3,14 +3,32 @@ module Gitlab class Config module Node class Entry + attr_reader :hash, :config, :parent, :nodes, :errors + def initialize(hash, config, parent = nil) @hash = hash @config = config @parent = parent + @nodes = {} + @errors = [] + end + + def process! + keys.each_pair do |key, entry| + next unless hash.include?(key) + @nodes[key] = entry.new(hash[key], config, self) + end + + @nodes.values.each(&:process!) + @nodes.values.each(&:validate!) + end + + def keys + raise NotImplementedError end - def allowed_keys - [] + def validate! + raise NotImplementedError end end end diff --git a/lib/gitlab/ci/config/node/global.rb b/lib/gitlab/ci/config/node/global.rb index b3dd6df0a44..81a9d0667be 100644 --- a/lib/gitlab/ci/config/node/global.rb +++ b/lib/gitlab/ci/config/node/global.rb @@ -3,6 +3,9 @@ module Gitlab class Config module Node class Global < Entry + def keys + { before_script: BeforeScript } + end end end end -- cgit v1.2.1 From 251dd571dfc3e6261ed075ecf725dd98ee176b69 Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Mon, 6 Jun 2016 11:05:15 +0200 Subject: Extract CI config validation helpers to mixin --- lib/ci/gitlab_ci_yaml_processor.rb | 18 ++---------------- lib/gitlab/ci/config/validation_helpers.rb | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 16 deletions(-) create mode 100644 lib/gitlab/ci/config/validation_helpers.rb (limited to 'lib') diff --git a/lib/ci/gitlab_ci_yaml_processor.rb b/lib/ci/gitlab_ci_yaml_processor.rb index 46a923161c8..e470ec56b79 100644 --- a/lib/ci/gitlab_ci_yaml_processor.rb +++ b/lib/ci/gitlab_ci_yaml_processor.rb @@ -2,6 +2,8 @@ module Ci class GitlabCiYamlProcessor class ValidationError < StandardError; end + include Gitlab::Ci::Config::ValidationHelpers + DEFAULT_STAGES = %w(build test deploy) DEFAULT_STAGE = 'test' ALLOWED_YAML_KEYS = [:before_script, :after_script, :image, :services, :types, :stages, :variables, :cache] @@ -276,22 +278,6 @@ module Ci end end - def validate_array_of_strings(values) - values.is_a?(Array) && values.all? { |value| validate_string(value) } - end - - def validate_variables(variables) - variables.is_a?(Hash) && variables.all? { |key, value| validate_string(key) && validate_string(value) } - end - - def validate_string(value) - value.is_a?(String) || value.is_a?(Symbol) - end - - def validate_boolean(value) - value.in?([true, false]) - end - def process?(only_params, except_params, ref, tag, trigger_request) if only_params.present? return false unless matching?(only_params, ref, tag, trigger_request) diff --git a/lib/gitlab/ci/config/validation_helpers.rb b/lib/gitlab/ci/config/validation_helpers.rb new file mode 100644 index 00000000000..9e4e9a83323 --- /dev/null +++ b/lib/gitlab/ci/config/validation_helpers.rb @@ -0,0 +1,26 @@ +module Gitlab + module Ci + class Config + module ValidationHelpers + private + + def validate_array_of_strings(values) + values.is_a?(Array) && values.all? { |value| validate_string(value) } + end + + def validate_variables(variables) + variables.is_a?(Hash) && + variables.all? { |key, value| validate_string(key) && validate_string(value) } + end + + def validate_string(value) + value.is_a?(String) || value.is_a?(Symbol) + end + + def validate_boolean(value) + value.in?([true, false]) + end + end + end + end +end -- cgit v1.2.1 From 6dbd1c86a82156dd5ad39b0e2ad119a493dadeae Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Mon, 6 Jun 2016 11:20:47 +0200 Subject: Validate new before script CI configuration entry --- lib/gitlab/ci/config/node/before_script.rb | 3 +++ lib/gitlab/ci/config/node/entry.rb | 12 +++++++----- 2 files changed, 10 insertions(+), 5 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/ci/config/node/before_script.rb b/lib/gitlab/ci/config/node/before_script.rb index 88ebd6bb304..204e0970a9e 100644 --- a/lib/gitlab/ci/config/node/before_script.rb +++ b/lib/gitlab/ci/config/node/before_script.rb @@ -8,6 +8,9 @@ module Gitlab end def validate! + unless validate_array_of_strings(@value) + @errors << 'before_script should be an array of strings' + end end end end diff --git a/lib/gitlab/ci/config/node/entry.rb b/lib/gitlab/ci/config/node/entry.rb index 6336535bc03..3220b01ca1a 100644 --- a/lib/gitlab/ci/config/node/entry.rb +++ b/lib/gitlab/ci/config/node/entry.rb @@ -3,10 +3,12 @@ module Gitlab class Config module Node class Entry - attr_reader :hash, :config, :parent, :nodes, :errors + include Config::ValidationHelpers - def initialize(hash, config, parent = nil) - @hash = hash + attr_reader :value, :config, :parent, :nodes, :errors + + def initialize(value, config, parent = nil) + @value = value @config = config @parent = parent @nodes = {} @@ -15,8 +17,8 @@ module Gitlab def process! keys.each_pair do |key, entry| - next unless hash.include?(key) - @nodes[key] = entry.new(hash[key], config, self) + next unless @value.include?(key) + @nodes[key] = entry.new(@value[key], config, self) end @nodes.values.each(&:process!) -- cgit v1.2.1 From a3c0745514ad98df1fbb8a6142f6cc50df76edae Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Mon, 6 Jun 2016 11:54:54 +0200 Subject: Collect errors from all nodes in new CI config --- lib/gitlab/ci/config/node/entry.rb | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/ci/config/node/entry.rb b/lib/gitlab/ci/config/node/entry.rb index 3220b01ca1a..45475316539 100644 --- a/lib/gitlab/ci/config/node/entry.rb +++ b/lib/gitlab/ci/config/node/entry.rb @@ -5,7 +5,7 @@ module Gitlab class Entry include Config::ValidationHelpers - attr_reader :value, :config, :parent, :nodes, :errors + attr_reader :value, :parent def initialize(value, config, parent = nil) @value = value @@ -21,8 +21,20 @@ module Gitlab @nodes[key] = entry.new(@value[key], config, self) end - @nodes.values.each(&:process!) - @nodes.values.each(&:validate!) + nodes.each(&:process!) + nodes.each(&:validate!) + end + + def errors + @errors + nodes.map(&:errors).flatten + end + + def valid? + errors.none? + end + + def nodes + @nodes.values end def keys -- cgit v1.2.1 From 940763e0e72a7f71c6e60f2a1a848f8fe4afaf33 Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Mon, 6 Jun 2016 12:23:27 +0200 Subject: Use CI config errors from new processor in legacy one --- lib/ci/gitlab_ci_yaml_processor.rb | 12 +++++++----- lib/gitlab/ci/config.rb | 4 ++++ lib/gitlab/ci/config/node/entry.rb | 2 +- 3 files changed, 12 insertions(+), 6 deletions(-) (limited to 'lib') diff --git a/lib/ci/gitlab_ci_yaml_processor.rb b/lib/ci/gitlab_ci_yaml_processor.rb index e470ec56b79..4bd2ac4f2db 100644 --- a/lib/ci/gitlab_ci_yaml_processor.rb +++ b/lib/ci/gitlab_ci_yaml_processor.rb @@ -14,7 +14,9 @@ module Ci attr_reader :before_script, :after_script, :image, :services, :path, :cache def initialize(config, path = nil) - @config = Gitlab::Ci::Config.new(config).to_hash + @ci_config = Gitlab::Ci::Config.new(config) + @config = @ci_config.to_hash + @path = path initial_parsing @@ -99,6 +101,10 @@ module Ci end def validate! + unless @ci_config.valid? + raise ValidationError, @ci_config.errors.first + end + validate_global! @jobs.each do |name, job| @@ -109,10 +115,6 @@ module Ci end def validate_global! - unless validate_array_of_strings(@before_script) - raise ValidationError, "before_script should be an array of strings" - end - unless @after_script.nil? || validate_array_of_strings(@after_script) raise ValidationError, "after_script should be an array of strings" end diff --git a/lib/gitlab/ci/config.rb b/lib/gitlab/ci/config.rb index 5fc4894311f..a042c49add7 100644 --- a/lib/gitlab/ci/config.rb +++ b/lib/gitlab/ci/config.rb @@ -3,6 +3,8 @@ module Gitlab class Config class LoaderError < StandardError; end + delegate :valid?, :errors, to: :@global + def initialize(config) loader = Loader.new(config) @@ -11,6 +13,8 @@ module Gitlab end @config = loader.load + @global = Node::Global.new(@config, self) + @global.process! end def to_hash diff --git a/lib/gitlab/ci/config/node/entry.rb b/lib/gitlab/ci/config/node/entry.rb index 45475316539..e8ed5f54c5b 100644 --- a/lib/gitlab/ci/config/node/entry.rb +++ b/lib/gitlab/ci/config/node/entry.rb @@ -18,7 +18,7 @@ module Gitlab def process! keys.each_pair do |key, entry| next unless @value.include?(key) - @nodes[key] = entry.new(@value[key], config, self) + @nodes[key] = entry.new(@value[key], @config, self) end nodes.each(&:process!) -- cgit v1.2.1 From b95c60a0715b5639e70b64e04fd4923e8bdd1923 Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Tue, 7 Jun 2016 11:26:39 +0200 Subject: Do not process Ci config node when node is a leaf --- lib/gitlab/ci/config/node/entry.rb | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/ci/config/node/entry.rb b/lib/gitlab/ci/config/node/entry.rb index e8ed5f54c5b..007585d4019 100644 --- a/lib/gitlab/ci/config/node/entry.rb +++ b/lib/gitlab/ci/config/node/entry.rb @@ -16,6 +16,8 @@ module Gitlab end def process! + return if leaf? + keys.each_pair do |key, entry| next unless @value.include?(key) @nodes[key] = entry.new(@value[key], @config, self) @@ -29,12 +31,16 @@ module Gitlab @errors + nodes.map(&:errors).flatten end + def nodes + @nodes.values + end + def valid? errors.none? end - def nodes - @nodes.values + def leaf? + keys.none? end def keys -- cgit v1.2.1 From 69a3755c5a93395fd2fdfd5bee00e6064d1670f8 Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Tue, 7 Jun 2016 11:58:02 +0200 Subject: Add Ci config entry that implements Null Object --- lib/gitlab/ci/config/node/entry.rb | 22 ++++++++++++---------- lib/gitlab/ci/config/node/null.rb | 17 +++++++++++++++++ 2 files changed, 29 insertions(+), 10 deletions(-) create mode 100644 lib/gitlab/ci/config/node/null.rb (limited to 'lib') diff --git a/lib/gitlab/ci/config/node/entry.rb b/lib/gitlab/ci/config/node/entry.rb index 007585d4019..af92899af40 100644 --- a/lib/gitlab/ci/config/node/entry.rb +++ b/lib/gitlab/ci/config/node/entry.rb @@ -5,22 +5,28 @@ module Gitlab class Entry include Config::ValidationHelpers - attr_reader :value, :parent + attr_reader :value, :nodes, :parent def initialize(value, config, parent = nil) @value = value @config = config @parent = parent - @nodes = {} - @errors = [] + @nodes, @errors = [], [] + + keys.each_key do |key| + instance_variable_set("@#{key}", Null.new(nil, config, self)) + end end def process! return if leaf? - keys.each_pair do |key, entry| - next unless @value.include?(key) - @nodes[key] = entry.new(@value[key], @config, self) + keys.each do |key, entry_class| + next unless @value.has_key?(key) + + entry = entry_class.new(@value[key], @config, self) + instance_variable_set("@#{key}", entry) + @nodes.append(entry) end nodes.each(&:process!) @@ -31,10 +37,6 @@ module Gitlab @errors + nodes.map(&:errors).flatten end - def nodes - @nodes.values - end - def valid? errors.none? end diff --git a/lib/gitlab/ci/config/node/null.rb b/lib/gitlab/ci/config/node/null.rb new file mode 100644 index 00000000000..6147b0d882f --- /dev/null +++ b/lib/gitlab/ci/config/node/null.rb @@ -0,0 +1,17 @@ +module Gitlab + module Ci + class Config + module Node + class Null < Entry + def keys + {} + end + + def method_missing(*) + nil + end + end + end + end + end +end -- cgit v1.2.1 From e8f995ef2631983ffe960464d0fd13a4c5ed8e09 Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Tue, 7 Jun 2016 12:13:22 +0200 Subject: Pass root Ci config entry to each subsequent entry --- lib/gitlab/ci/config.rb | 2 +- lib/gitlab/ci/config/node/entry.rb | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/ci/config.rb b/lib/gitlab/ci/config.rb index a042c49add7..62cd514a72d 100644 --- a/lib/gitlab/ci/config.rb +++ b/lib/gitlab/ci/config.rb @@ -13,7 +13,7 @@ module Gitlab end @config = loader.load - @global = Node::Global.new(@config, self) + @global = Node::Global.new(@config) @global.process! end diff --git a/lib/gitlab/ci/config/node/entry.rb b/lib/gitlab/ci/config/node/entry.rb index af92899af40..e2afeb1b3cf 100644 --- a/lib/gitlab/ci/config/node/entry.rb +++ b/lib/gitlab/ci/config/node/entry.rb @@ -7,14 +7,14 @@ module Gitlab attr_reader :value, :nodes, :parent - def initialize(value, config, parent = nil) + def initialize(value, root = nil, parent = nil) @value = value - @config = config + @root = root @parent = parent @nodes, @errors = [], [] keys.each_key do |key| - instance_variable_set("@#{key}", Null.new(nil, config, self)) + instance_variable_set("@#{key}", Null.new(nil, root, self)) end end @@ -24,7 +24,7 @@ module Gitlab keys.each do |key, entry_class| next unless @value.has_key?(key) - entry = entry_class.new(@value[key], @config, self) + entry = entry_class.new(@value[key], @root, self) instance_variable_set("@#{key}", entry) @nodes.append(entry) end @@ -42,7 +42,7 @@ module Gitlab end def leaf? - keys.none? + keys.none? # TODO || !@value.is_a?(Hash) end def keys -- cgit v1.2.1 From 6bd67f5212de739b3016b0941853ce42f523a0f1 Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Tue, 7 Jun 2016 12:48:26 +0200 Subject: Do not process new Ci config entry when invalid --- lib/gitlab/ci/config/node/entry.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/ci/config/node/entry.rb b/lib/gitlab/ci/config/node/entry.rb index e2afeb1b3cf..c07e7cf652e 100644 --- a/lib/gitlab/ci/config/node/entry.rb +++ b/lib/gitlab/ci/config/node/entry.rb @@ -16,10 +16,14 @@ module Gitlab keys.each_key do |key| instance_variable_set("@#{key}", Null.new(nil, root, self)) end + + unless leaf? || value.is_a?(Hash) + @errors << 'should be a configuration entry with hash value' + end end def process! - return if leaf? + return if leaf? || !valid? keys.each do |key, entry_class| next unless @value.has_key?(key) @@ -42,7 +46,7 @@ module Gitlab end def leaf? - keys.none? # TODO || !@value.is_a?(Hash) + keys.none? end def keys -- cgit v1.2.1 From df25c19699ba35682fd92da2b9c451bb4ba1c775 Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Tue, 7 Jun 2016 12:58:32 +0200 Subject: Use Ci config validation helpers only where needed --- lib/ci/gitlab_ci_yaml_processor.rb | 2 +- lib/gitlab/ci/config/node/before_script.rb | 2 ++ lib/gitlab/ci/config/node/entry.rb | 2 -- lib/gitlab/ci/config/node/validation_helpers.rb | 28 +++++++++++++++++++++++++ lib/gitlab/ci/config/validation_helpers.rb | 26 ----------------------- 5 files changed, 31 insertions(+), 29 deletions(-) create mode 100644 lib/gitlab/ci/config/node/validation_helpers.rb delete mode 100644 lib/gitlab/ci/config/validation_helpers.rb (limited to 'lib') diff --git a/lib/ci/gitlab_ci_yaml_processor.rb b/lib/ci/gitlab_ci_yaml_processor.rb index 4bd2ac4f2db..c2b941a270a 100644 --- a/lib/ci/gitlab_ci_yaml_processor.rb +++ b/lib/ci/gitlab_ci_yaml_processor.rb @@ -2,7 +2,7 @@ module Ci class GitlabCiYamlProcessor class ValidationError < StandardError; end - include Gitlab::Ci::Config::ValidationHelpers + include Gitlab::Ci::Config::Node::ValidationHelpers DEFAULT_STAGES = %w(build test deploy) DEFAULT_STAGE = 'test' diff --git a/lib/gitlab/ci/config/node/before_script.rb b/lib/gitlab/ci/config/node/before_script.rb index 204e0970a9e..586eab12a08 100644 --- a/lib/gitlab/ci/config/node/before_script.rb +++ b/lib/gitlab/ci/config/node/before_script.rb @@ -3,6 +3,8 @@ module Gitlab class Config module Node class BeforeScript < Entry + include ValidationHelpers + def keys {} end diff --git a/lib/gitlab/ci/config/node/entry.rb b/lib/gitlab/ci/config/node/entry.rb index c07e7cf652e..e95bc7bad4b 100644 --- a/lib/gitlab/ci/config/node/entry.rb +++ b/lib/gitlab/ci/config/node/entry.rb @@ -3,8 +3,6 @@ module Gitlab class Config module Node class Entry - include Config::ValidationHelpers - attr_reader :value, :nodes, :parent def initialize(value, root = nil, parent = nil) diff --git a/lib/gitlab/ci/config/node/validation_helpers.rb b/lib/gitlab/ci/config/node/validation_helpers.rb new file mode 100644 index 00000000000..4ea26492b6a --- /dev/null +++ b/lib/gitlab/ci/config/node/validation_helpers.rb @@ -0,0 +1,28 @@ +module Gitlab + module Ci + class Config + module Node + module ValidationHelpers + private + + def validate_array_of_strings(values) + values.is_a?(Array) && values.all? { |value| validate_string(value) } + end + + def validate_variables(variables) + variables.is_a?(Hash) && + variables.all? { |key, value| validate_string(key) && validate_string(value) } + end + + def validate_string(value) + value.is_a?(String) || value.is_a?(Symbol) + end + + def validate_boolean(value) + value.in?([true, false]) + end + end + end + end + end +end diff --git a/lib/gitlab/ci/config/validation_helpers.rb b/lib/gitlab/ci/config/validation_helpers.rb deleted file mode 100644 index 9e4e9a83323..00000000000 --- a/lib/gitlab/ci/config/validation_helpers.rb +++ /dev/null @@ -1,26 +0,0 @@ -module Gitlab - module Ci - class Config - module ValidationHelpers - private - - def validate_array_of_strings(values) - values.is_a?(Array) && values.all? { |value| validate_string(value) } - end - - def validate_variables(variables) - variables.is_a?(Hash) && - variables.all? { |key, value| validate_string(key) && validate_string(value) } - end - - def validate_string(value) - value.is_a?(String) || value.is_a?(Symbol) - end - - def validate_boolean(value) - value.in?([true, false]) - end - end - end - end -end -- cgit v1.2.1 From c2d6d61dac2bf04b649c84ab0f4fe98da906c2c4 Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Tue, 7 Jun 2016 13:19:22 +0200 Subject: Add DSL for adding nodes in Ci config interface --- lib/gitlab/ci/config/node/before_script.rb | 4 ---- lib/gitlab/ci/config/node/entry.rb | 12 +++++++++++- lib/gitlab/ci/config/node/global.rb | 4 +--- lib/gitlab/ci/config/node/null.rb | 4 ---- 4 files changed, 12 insertions(+), 12 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/ci/config/node/before_script.rb b/lib/gitlab/ci/config/node/before_script.rb index 586eab12a08..a8c350f3c7d 100644 --- a/lib/gitlab/ci/config/node/before_script.rb +++ b/lib/gitlab/ci/config/node/before_script.rb @@ -5,10 +5,6 @@ module Gitlab class BeforeScript < Entry include ValidationHelpers - def keys - {} - end - def validate! unless validate_array_of_strings(@value) @errors << 'before_script should be an array of strings' diff --git a/lib/gitlab/ci/config/node/entry.rb b/lib/gitlab/ci/config/node/entry.rb index e95bc7bad4b..3043dc4c61f 100644 --- a/lib/gitlab/ci/config/node/entry.rb +++ b/lib/gitlab/ci/config/node/entry.rb @@ -48,12 +48,22 @@ module Gitlab end def keys - raise NotImplementedError + self.class.nodes || {} end def validate! raise NotImplementedError end + + class << self + attr_reader :nodes + + private + + def add_node(symbol, entry_class) + (@nodes ||= {}).merge!(symbol.to_sym => entry_class) + end + end end end end diff --git a/lib/gitlab/ci/config/node/global.rb b/lib/gitlab/ci/config/node/global.rb index 81a9d0667be..cfa506c28b7 100644 --- a/lib/gitlab/ci/config/node/global.rb +++ b/lib/gitlab/ci/config/node/global.rb @@ -3,9 +3,7 @@ module Gitlab class Config module Node class Global < Entry - def keys - { before_script: BeforeScript } - end + add_node :before_script, BeforeScript end end end diff --git a/lib/gitlab/ci/config/node/null.rb b/lib/gitlab/ci/config/node/null.rb index 6147b0d882f..fc240e16f55 100644 --- a/lib/gitlab/ci/config/node/null.rb +++ b/lib/gitlab/ci/config/node/null.rb @@ -3,10 +3,6 @@ module Gitlab class Config module Node class Null < Entry - def keys - {} - end - def method_missing(*) nil end -- cgit v1.2.1 From 70bda3e89bc3828fc8771496ec6d61e41ac3d3ed Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Tue, 7 Jun 2016 14:23:47 +0200 Subject: Implement script in Ci config and use in legacy one --- lib/ci/gitlab_ci_yaml_processor.rb | 2 +- lib/gitlab/ci/config.rb | 5 +++++ lib/gitlab/ci/config/node/before_script.rb | 10 ++++++++++ lib/gitlab/ci/config/node/entry.rb | 4 ++++ lib/gitlab/ci/config/node/global.rb | 4 ++++ 5 files changed, 24 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/ci/gitlab_ci_yaml_processor.rb b/lib/ci/gitlab_ci_yaml_processor.rb index c2b941a270a..0483e13b098 100644 --- a/lib/ci/gitlab_ci_yaml_processor.rb +++ b/lib/ci/gitlab_ci_yaml_processor.rb @@ -82,7 +82,7 @@ module Ci { stage_idx: stages.index(job[:stage]), stage: job[:stage], - commands: [job[:before_script] || @before_script, job[:script]].flatten.join("\n"), + commands: [job[:before_script] || [@ci_config.before_script], job[:script]].flatten.compact.join("\n"), tag_list: job[:tags] || [], name: name, only: job[:only], diff --git a/lib/gitlab/ci/config.rb b/lib/gitlab/ci/config.rb index 62cd514a72d..6e3fd2aa604 100644 --- a/lib/gitlab/ci/config.rb +++ b/lib/gitlab/ci/config.rb @@ -5,6 +5,11 @@ module Gitlab delegate :valid?, :errors, to: :@global + ## + # Temporary delegations that should be removed after refactoring + # + delegate :before_script, to: :@global + def initialize(config) loader = Loader.new(config) diff --git a/lib/gitlab/ci/config/node/before_script.rb b/lib/gitlab/ci/config/node/before_script.rb index a8c350f3c7d..271cb7b5da1 100644 --- a/lib/gitlab/ci/config/node/before_script.rb +++ b/lib/gitlab/ci/config/node/before_script.rb @@ -5,6 +5,16 @@ module Gitlab class BeforeScript < Entry include ValidationHelpers + def description + 'Script that is executed before the one defined in a job.' + end + + def script + raise unless valid? + + @value.join("\n") + end + def validate! unless validate_array_of_strings(@value) @errors << 'before_script should be an array of strings' diff --git a/lib/gitlab/ci/config/node/entry.rb b/lib/gitlab/ci/config/node/entry.rb index 3043dc4c61f..f8f2d0be23a 100644 --- a/lib/gitlab/ci/config/node/entry.rb +++ b/lib/gitlab/ci/config/node/entry.rb @@ -55,6 +55,10 @@ module Gitlab raise NotImplementedError end + def description + raise NotImplementedError + end + class << self attr_reader :nodes diff --git a/lib/gitlab/ci/config/node/global.rb b/lib/gitlab/ci/config/node/global.rb index cfa506c28b7..5912ead21c6 100644 --- a/lib/gitlab/ci/config/node/global.rb +++ b/lib/gitlab/ci/config/node/global.rb @@ -4,6 +4,10 @@ module Gitlab module Node class Global < Entry add_node :before_script, BeforeScript + + def before_script + @before_script.script + end end end end -- cgit v1.2.1 From cba266aabc60aeee64ac2eb7e76b3e9e7012bad4 Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Wed, 8 Jun 2016 11:44:07 +0200 Subject: Remove old before_script from legacy Ci config --- lib/ci/gitlab_ci_yaml_processor.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/ci/gitlab_ci_yaml_processor.rb b/lib/ci/gitlab_ci_yaml_processor.rb index b37d231e893..c5a820563f0 100644 --- a/lib/ci/gitlab_ci_yaml_processor.rb +++ b/lib/ci/gitlab_ci_yaml_processor.rb @@ -11,7 +11,7 @@ module Ci :allow_failure, :type, :stage, :when, :artifacts, :cache, :dependencies, :before_script, :after_script, :variables] - attr_reader :before_script, :after_script, :image, :services, :path, :cache + attr_reader :after_script, :image, :services, :path, :cache def initialize(config, path = nil) @ci_config = Gitlab::Ci::Config.new(config) @@ -54,7 +54,6 @@ module Ci private def initial_parsing - @before_script = @config[:before_script] || [] @after_script = @config[:after_script] @image = @config[:image] @services = @config[:services] -- cgit v1.2.1 From 87fe50f2a0facd5bfdf287195a21932ff2340e1b Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Wed, 8 Jun 2016 12:32:56 +0200 Subject: Delegate Ci config entry value to single method --- lib/gitlab/ci/config/node/before_script.rb | 4 +--- lib/gitlab/ci/config/node/entry.rb | 34 +++++++++++++++++++++--------- lib/gitlab/ci/config/node/global.rb | 4 ---- lib/gitlab/ci/config/node/null.rb | 7 ++++++ 4 files changed, 32 insertions(+), 17 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/ci/config/node/before_script.rb b/lib/gitlab/ci/config/node/before_script.rb index 271cb7b5da1..be2ceebf3f9 100644 --- a/lib/gitlab/ci/config/node/before_script.rb +++ b/lib/gitlab/ci/config/node/before_script.rb @@ -9,9 +9,7 @@ module Gitlab 'Script that is executed before the one defined in a job.' end - def script - raise unless valid? - + def value @value.join("\n") end diff --git a/lib/gitlab/ci/config/node/entry.rb b/lib/gitlab/ci/config/node/entry.rb index f8f2d0be23a..0767fadcb9a 100644 --- a/lib/gitlab/ci/config/node/entry.rb +++ b/lib/gitlab/ci/config/node/entry.rb @@ -3,17 +3,14 @@ module Gitlab class Config module Node class Entry - attr_reader :value, :nodes, :parent + class InvalidError < StandardError; end def initialize(value, root = nil, parent = nil) @value = value @root = root @parent = parent - @nodes, @errors = [], [] - - keys.each_key do |key| - instance_variable_set("@#{key}", Null.new(nil, root, self)) - end + @nodes = {} + @errors = [] unless leaf? || value.is_a?(Hash) @errors << 'should be a configuration entry with hash value' @@ -24,17 +21,23 @@ module Gitlab return if leaf? || !valid? keys.each do |key, entry_class| - next unless @value.has_key?(key) + if @value.has_key?(key) + entry = entry_class.new(@value[key], @root, self) + else + entry = Node::Null.new(nil, @root, self) + end - entry = entry_class.new(@value[key], @root, self) - instance_variable_set("@#{key}", entry) - @nodes.append(entry) + @nodes[key] = entry end nodes.each(&:process!) nodes.each(&:validate!) end + def nodes + @nodes.values + end + def errors @errors + nodes.map(&:errors).flatten end @@ -51,6 +54,17 @@ module Gitlab self.class.nodes || {} end + def method_missing(name, *args) + super unless keys.has_key?(name) + raise InvalidError unless valid? + + @nodes[name].value + end + + def value + raise NotImplementedError + end + def validate! raise NotImplementedError end diff --git a/lib/gitlab/ci/config/node/global.rb b/lib/gitlab/ci/config/node/global.rb index 5912ead21c6..cfa506c28b7 100644 --- a/lib/gitlab/ci/config/node/global.rb +++ b/lib/gitlab/ci/config/node/global.rb @@ -4,10 +4,6 @@ module Gitlab module Node class Global < Entry add_node :before_script, BeforeScript - - def before_script - @before_script.script - end end end end diff --git a/lib/gitlab/ci/config/node/null.rb b/lib/gitlab/ci/config/node/null.rb index fc240e16f55..db3fa05c328 100644 --- a/lib/gitlab/ci/config/node/null.rb +++ b/lib/gitlab/ci/config/node/null.rb @@ -3,6 +3,13 @@ module Gitlab class Config module Node class Null < Entry + def value + nil + end + + def validate! + end + def method_missing(*) nil end -- cgit v1.2.1 From 5065612a0a1a5dd68c075e54f5f5f89c5c025a6b Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Wed, 8 Jun 2016 13:01:44 +0200 Subject: Add minor improvements in new Ci config design --- lib/gitlab/ci/config/node/entry.rb | 40 +++++++++++++++++++++++++------------- lib/gitlab/ci/config/node/null.rb | 1 + 2 files changed, 28 insertions(+), 13 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/ci/config/node/entry.rb b/lib/gitlab/ci/config/node/entry.rb index 0767fadcb9a..302cded664f 100644 --- a/lib/gitlab/ci/config/node/entry.rb +++ b/lib/gitlab/ci/config/node/entry.rb @@ -12,22 +12,16 @@ module Gitlab @nodes = {} @errors = [] - unless leaf? || value.is_a?(Hash) + unless leaf? || has_config? @errors << 'should be a configuration entry with hash value' end end def process! - return if leaf? || !valid? + return if leaf? || invalid? keys.each do |key, entry_class| - if @value.has_key?(key) - entry = entry_class.new(@value[key], @root, self) - else - entry = Node::Null.new(nil, @root, self) - end - - @nodes[key] = entry + add_node(key, entry_class) end nodes.each(&:process!) @@ -38,22 +32,30 @@ module Gitlab @nodes.values end - def errors - @errors + nodes.map(&:errors).flatten - end - def valid? errors.none? end + def invalid? + !valid? + end + def leaf? keys.none? end + def has_config? + @value.is_a?(Hash) + end + def keys self.class.nodes || {} end + def errors + @errors + nodes.map(&:errors).flatten + end + def method_missing(name, *args) super unless keys.has_key?(name) raise InvalidError unless valid? @@ -73,6 +75,18 @@ module Gitlab raise NotImplementedError end + private + + def add_node(key, entry_class) + if @value.has_key?(key) + entry = entry_class.new(@value[key], @root, self) + else + entry = Node::Null.new(nil, @root, self) + end + + @nodes[key] = entry + end + class << self attr_reader :nodes diff --git a/lib/gitlab/ci/config/node/null.rb b/lib/gitlab/ci/config/node/null.rb index db3fa05c328..bf8bc62dc91 100644 --- a/lib/gitlab/ci/config/node/null.rb +++ b/lib/gitlab/ci/config/node/null.rb @@ -8,6 +8,7 @@ module Gitlab end def validate! + nil end def method_missing(*) -- cgit v1.2.1 From 48a59c1a8baf3921f26c8503a9fdd63bf7398f0f Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Wed, 8 Jun 2016 13:22:39 +0200 Subject: Rename BeforeScript to Script in new Ci config --- lib/gitlab/ci/config/node/before_script.rb | 25 ------------------------- lib/gitlab/ci/config/node/global.rb | 2 +- lib/gitlab/ci/config/node/script.rb | 25 +++++++++++++++++++++++++ 3 files changed, 26 insertions(+), 26 deletions(-) delete mode 100644 lib/gitlab/ci/config/node/before_script.rb create mode 100644 lib/gitlab/ci/config/node/script.rb (limited to 'lib') diff --git a/lib/gitlab/ci/config/node/before_script.rb b/lib/gitlab/ci/config/node/before_script.rb deleted file mode 100644 index be2ceebf3f9..00000000000 --- a/lib/gitlab/ci/config/node/before_script.rb +++ /dev/null @@ -1,25 +0,0 @@ -module Gitlab - module Ci - class Config - module Node - class BeforeScript < Entry - include ValidationHelpers - - def description - 'Script that is executed before the one defined in a job.' - end - - def value - @value.join("\n") - end - - def validate! - unless validate_array_of_strings(@value) - @errors << 'before_script should be an array of strings' - end - end - end - end - end - end -end diff --git a/lib/gitlab/ci/config/node/global.rb b/lib/gitlab/ci/config/node/global.rb index cfa506c28b7..2e899b0b2a3 100644 --- a/lib/gitlab/ci/config/node/global.rb +++ b/lib/gitlab/ci/config/node/global.rb @@ -3,7 +3,7 @@ module Gitlab class Config module Node class Global < Entry - add_node :before_script, BeforeScript + add_node :before_script, Script end end end diff --git a/lib/gitlab/ci/config/node/script.rb b/lib/gitlab/ci/config/node/script.rb new file mode 100644 index 00000000000..db635f6541f --- /dev/null +++ b/lib/gitlab/ci/config/node/script.rb @@ -0,0 +1,25 @@ +module Gitlab + module Ci + class Config + module Node + class Script < Entry + include ValidationHelpers + + def description + 'Script that is executed before the one defined in a job.' + end + + def value + @value.join("\n") + end + + def validate! + unless validate_array_of_strings(@value) + @errors << 'before_script should be an array of strings' + end + end + end + end + end + end +end -- cgit v1.2.1 From 33cd090b93714e147e59195d24918e8b7c6d4614 Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Thu, 9 Jun 2016 10:08:49 +0200 Subject: Move new Ci config configurable DSL to concern --- lib/gitlab/ci/config/node/configurable.rb | 39 +++++++++++++++++++++++++++++++ lib/gitlab/ci/config/node/entry.rb | 34 +++++++-------------------- lib/gitlab/ci/config/node/global.rb | 2 ++ 3 files changed, 49 insertions(+), 26 deletions(-) create mode 100644 lib/gitlab/ci/config/node/configurable.rb (limited to 'lib') diff --git a/lib/gitlab/ci/config/node/configurable.rb b/lib/gitlab/ci/config/node/configurable.rb new file mode 100644 index 00000000000..9c04a1cdc08 --- /dev/null +++ b/lib/gitlab/ci/config/node/configurable.rb @@ -0,0 +1,39 @@ +module Gitlab + module Ci + class Config + module Node + module Configurable + extend ActiveSupport::Concern + + def keys + self.class.nodes || {} + end + + private + + def add_node(key, entry_class) + if @value.has_key?(key) + entry = entry_class.new(@value[key], @root, self) + else + entry = Node::Null.new(nil, @root, self) + end + + @nodes[key] = entry + end + + class_methods do + attr_reader :nodes + + private + + def add_node(symbol, entry_class) + node = { symbol.to_sym => entry_class } + + (@nodes ||= {}).merge!(node) + end + end + end + end + end + end +end diff --git a/lib/gitlab/ci/config/node/entry.rb b/lib/gitlab/ci/config/node/entry.rb index 302cded664f..c45744efdf5 100644 --- a/lib/gitlab/ci/config/node/entry.rb +++ b/lib/gitlab/ci/config/node/entry.rb @@ -20,8 +20,8 @@ module Gitlab def process! return if leaf? || invalid? - keys.each do |key, entry_class| - add_node(key, entry_class) + keys.each do |key, entry| + add_node(key, entry) end nodes.each(&:process!) @@ -49,7 +49,7 @@ module Gitlab end def keys - self.class.nodes || {} + {} end def errors @@ -60,7 +60,11 @@ module Gitlab super unless keys.has_key?(name) raise InvalidError unless valid? - @nodes[name].value + @nodes[name].try(:value) + end + + def add_node(key, entry) + raise NotImplementedError end def value @@ -74,28 +78,6 @@ module Gitlab def description raise NotImplementedError end - - private - - def add_node(key, entry_class) - if @value.has_key?(key) - entry = entry_class.new(@value[key], @root, self) - else - entry = Node::Null.new(nil, @root, self) - end - - @nodes[key] = entry - end - - class << self - attr_reader :nodes - - private - - def add_node(symbol, entry_class) - (@nodes ||= {}).merge!(symbol.to_sym => entry_class) - end - end end end end diff --git a/lib/gitlab/ci/config/node/global.rb b/lib/gitlab/ci/config/node/global.rb index 2e899b0b2a3..5a176ab5eaf 100644 --- a/lib/gitlab/ci/config/node/global.rb +++ b/lib/gitlab/ci/config/node/global.rb @@ -3,6 +3,8 @@ module Gitlab class Config module Node class Global < Entry + include Configurable + add_node :before_script, Script end end -- cgit v1.2.1 From d9d5042fd9edf2abd662566ddc4c65b6a9bdbb08 Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Thu, 9 Jun 2016 10:28:44 +0200 Subject: Extract method that composes new Ci config entry --- lib/gitlab/ci/config/node/entry.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/ci/config/node/entry.rb b/lib/gitlab/ci/config/node/entry.rb index c45744efdf5..bdef2af9ae1 100644 --- a/lib/gitlab/ci/config/node/entry.rb +++ b/lib/gitlab/ci/config/node/entry.rb @@ -20,14 +20,18 @@ module Gitlab def process! return if leaf? || invalid? - keys.each do |key, entry| - add_node(key, entry) - end + compose! nodes.each(&:process!) nodes.each(&:validate!) end + def compose! + keys.each do |key, entry| + add_node(key, entry) + end + end + def nodes @nodes.values end -- cgit v1.2.1 From 6a319fd28790228295de19d8c786d1a807f73376 Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Thu, 9 Jun 2016 10:53:56 +0200 Subject: Make it possible configure Ci entry description --- lib/gitlab/ci/config/node/configurable.rb | 21 ++++++++++++++------- lib/gitlab/ci/config/node/entry.rb | 6 ++---- lib/gitlab/ci/config/node/global.rb | 3 ++- lib/gitlab/ci/config/node/script.rb | 4 ---- 4 files changed, 18 insertions(+), 16 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/ci/config/node/configurable.rb b/lib/gitlab/ci/config/node/configurable.rb index 9c04a1cdc08..4b33fe025bb 100644 --- a/lib/gitlab/ci/config/node/configurable.rb +++ b/lib/gitlab/ci/config/node/configurable.rb @@ -11,14 +11,19 @@ module Gitlab private - def add_node(key, entry_class) + def add_node(key, metadata) + entry = create_entry(key, metadata[:class]) + entry.description = metadata[:description] + + @nodes[key] = entry + end + + def create_entry(key, entry_class) if @value.has_key?(key) - entry = entry_class.new(@value[key], @root, self) + entry_class.new(@value[key], @root, self) else - entry = Node::Null.new(nil, @root, self) + Node::Null.new(nil, @root, self) end - - @nodes[key] = entry end class_methods do @@ -26,8 +31,10 @@ module Gitlab private - def add_node(symbol, entry_class) - node = { symbol.to_sym => entry_class } + def add_node(symbol, entry_class, metadata) + node = { symbol.to_sym => + { class: entry_class, + description: metadata[:description] } } (@nodes ||= {}).merge!(node) end diff --git a/lib/gitlab/ci/config/node/entry.rb b/lib/gitlab/ci/config/node/entry.rb index bdef2af9ae1..bbe07d68b36 100644 --- a/lib/gitlab/ci/config/node/entry.rb +++ b/lib/gitlab/ci/config/node/entry.rb @@ -5,6 +5,8 @@ module Gitlab class Entry class InvalidError < StandardError; end + attr_accessor :description + def initialize(value, root = nil, parent = nil) @value = value @root = root @@ -78,10 +80,6 @@ module Gitlab def validate! raise NotImplementedError end - - def description - raise NotImplementedError - end end end end diff --git a/lib/gitlab/ci/config/node/global.rb b/lib/gitlab/ci/config/node/global.rb index 5a176ab5eaf..7411f8c863e 100644 --- a/lib/gitlab/ci/config/node/global.rb +++ b/lib/gitlab/ci/config/node/global.rb @@ -5,7 +5,8 @@ module Gitlab class Global < Entry include Configurable - add_node :before_script, Script + add_node :before_script, Script, + description: 'Script that will be executed before each job.' end end end diff --git a/lib/gitlab/ci/config/node/script.rb b/lib/gitlab/ci/config/node/script.rb index db635f6541f..34d18ad2781 100644 --- a/lib/gitlab/ci/config/node/script.rb +++ b/lib/gitlab/ci/config/node/script.rb @@ -5,10 +5,6 @@ module Gitlab class Script < Entry include ValidationHelpers - def description - 'Script that is executed before the one defined in a job.' - end - def value @value.join("\n") end -- cgit v1.2.1 From 20ccd4465b0fbba45839256af93cf36c7b45d4e9 Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Thu, 9 Jun 2016 12:35:24 +0200 Subject: Do not require Ci config node to have a hash value --- lib/gitlab/ci/config/node/configurable.rb | 8 ++++++++ lib/gitlab/ci/config/node/entry.rb | 4 ---- 2 files changed, 8 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/ci/config/node/configurable.rb b/lib/gitlab/ci/config/node/configurable.rb index 4b33fe025bb..e0a0b40fc60 100644 --- a/lib/gitlab/ci/config/node/configurable.rb +++ b/lib/gitlab/ci/config/node/configurable.rb @@ -5,6 +5,14 @@ module Gitlab module Configurable extend ActiveSupport::Concern + def initialize(*) + super + + unless leaf? || has_config? + @errors << 'should be a configuration entry with hash value' + end + end + def keys self.class.nodes || {} end diff --git a/lib/gitlab/ci/config/node/entry.rb b/lib/gitlab/ci/config/node/entry.rb index bbe07d68b36..6b59461a585 100644 --- a/lib/gitlab/ci/config/node/entry.rb +++ b/lib/gitlab/ci/config/node/entry.rb @@ -13,10 +13,6 @@ module Gitlab @parent = parent @nodes = {} @errors = [] - - unless leaf? || has_config? - @errors << 'should be a configuration entry with hash value' - end end def process! -- cgit v1.2.1 From 99ee39bf6c21eef8cebc431fb79286d5347d1d21 Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Thu, 9 Jun 2016 13:01:19 +0200 Subject: Add comments to new CI config classes and modules --- lib/gitlab/ci/config.rb | 5 +++-- lib/gitlab/ci/config/node/configurable.rb | 11 +++++++++++ lib/gitlab/ci/config/node/entry.rb | 3 +++ lib/gitlab/ci/config/node/global.rb | 4 ++++ lib/gitlab/ci/config/node/null.rb | 6 ++++++ lib/gitlab/ci/config/node/script.rb | 8 ++++++++ 6 files changed, 35 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/ci/config.rb b/lib/gitlab/ci/config.rb index 2d02036af11..b48d3592f16 100644 --- a/lib/gitlab/ci/config.rb +++ b/lib/gitlab/ci/config.rb @@ -1,8 +1,9 @@ module Gitlab module Ci + ## + # Base GitLab CI Configuration facade + # class Config - class LoaderError < StandardError; end - delegate :valid?, :errors, to: :@global ## diff --git a/lib/gitlab/ci/config/node/configurable.rb b/lib/gitlab/ci/config/node/configurable.rb index e0a0b40fc60..d3ed72649bc 100644 --- a/lib/gitlab/ci/config/node/configurable.rb +++ b/lib/gitlab/ci/config/node/configurable.rb @@ -2,6 +2,17 @@ module Gitlab module Ci class Config module Node + ## + # This mixin is responsible for adding DSL, which purpose is to + # simplifly process of adding child nodes. + # + # This can be used only if parent node is a configuration entry that + # holds a hash as a configuration value, for example: + # + # job: + # script: ... + # artifacts: ... + # module Configurable extend ActiveSupport::Concern diff --git a/lib/gitlab/ci/config/node/entry.rb b/lib/gitlab/ci/config/node/entry.rb index 6b59461a585..7d7e6f26cbd 100644 --- a/lib/gitlab/ci/config/node/entry.rb +++ b/lib/gitlab/ci/config/node/entry.rb @@ -2,6 +2,9 @@ module Gitlab module Ci class Config module Node + ## + # Base abstract class for each configuration entry node. + # class Entry class InvalidError < StandardError; end diff --git a/lib/gitlab/ci/config/node/global.rb b/lib/gitlab/ci/config/node/global.rb index 7411f8c863e..911dc51da48 100644 --- a/lib/gitlab/ci/config/node/global.rb +++ b/lib/gitlab/ci/config/node/global.rb @@ -2,6 +2,10 @@ module Gitlab module Ci class Config module Node + ## + # This class represents a global entry - root node for entire + # GitLab CI Configuration file. + # class Global < Entry include Configurable diff --git a/lib/gitlab/ci/config/node/null.rb b/lib/gitlab/ci/config/node/null.rb index bf8bc62dc91..ab7b0abaf23 100644 --- a/lib/gitlab/ci/config/node/null.rb +++ b/lib/gitlab/ci/config/node/null.rb @@ -1,6 +1,12 @@ module Gitlab module Ci class Config + ## + # This class represents a configuration entry that is not being used + # in configuration file. + # + # This implements Null Object pattern. + # module Node class Null < Entry def value diff --git a/lib/gitlab/ci/config/node/script.rb b/lib/gitlab/ci/config/node/script.rb index 34d18ad2781..84f9ec0eb04 100644 --- a/lib/gitlab/ci/config/node/script.rb +++ b/lib/gitlab/ci/config/node/script.rb @@ -2,6 +2,14 @@ module Gitlab module Ci class Config module Node + ## + # Entry that represents a script. + # + # Each element in the value array is a command that will be executed + # by GitLab Runner. Currently we concatenate this commands with + # new line character as a separator what is compatbile with + # implementation in Runner. + # class Script < Entry include ValidationHelpers -- cgit v1.2.1 From d7e125116124b9c08c27b4a02f4738619db1d2f5 Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Thu, 9 Jun 2016 14:59:59 +0200 Subject: Rename method that returns allowed nodes in Ci config --- lib/gitlab/ci/config/node/configurable.rb | 2 +- lib/gitlab/ci/config/node/entry.rb | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/ci/config/node/configurable.rb b/lib/gitlab/ci/config/node/configurable.rb index d3ed72649bc..cf065c7f6fe 100644 --- a/lib/gitlab/ci/config/node/configurable.rb +++ b/lib/gitlab/ci/config/node/configurable.rb @@ -24,7 +24,7 @@ module Gitlab end end - def keys + def allowed_nodes self.class.nodes || {} end diff --git a/lib/gitlab/ci/config/node/entry.rb b/lib/gitlab/ci/config/node/entry.rb index 7d7e6f26cbd..19fc997297a 100644 --- a/lib/gitlab/ci/config/node/entry.rb +++ b/lib/gitlab/ci/config/node/entry.rb @@ -28,7 +28,7 @@ module Gitlab end def compose! - keys.each do |key, entry| + allowed_nodes.each do |key, entry| add_node(key, entry) end end @@ -46,23 +46,23 @@ module Gitlab end def leaf? - keys.none? + allowed_nodes.none? end def has_config? @value.is_a?(Hash) end - def keys - {} - end - def errors @errors + nodes.map(&:errors).flatten end + def allowed_nodes + {} + end + def method_missing(name, *args) - super unless keys.has_key?(name) + super unless allowed_nodes.has_key?(name) raise InvalidError unless valid? @nodes[name].try(:value) -- cgit v1.2.1 From 828a15bccd5a6fe0471e97ebd5c0c0f6f674b9b7 Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Fri, 10 Jun 2016 10:49:47 +0200 Subject: Rename method used to allow node in Ci config --- lib/gitlab/ci/config/node/configurable.rb | 8 ++++---- lib/gitlab/ci/config/node/global.rb | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/ci/config/node/configurable.rb b/lib/gitlab/ci/config/node/configurable.rb index cf065c7f6fe..c8c917f229f 100644 --- a/lib/gitlab/ci/config/node/configurable.rb +++ b/lib/gitlab/ci/config/node/configurable.rb @@ -25,7 +25,7 @@ module Gitlab end def allowed_nodes - self.class.nodes || {} + self.class.allowed_nodes || {} end private @@ -46,16 +46,16 @@ module Gitlab end class_methods do - attr_reader :nodes + attr_reader :allowed_nodes private - def add_node(symbol, entry_class, metadata) + def allow_node(symbol, entry_class, metadata) node = { symbol.to_sym => { class: entry_class, description: metadata[:description] } } - (@nodes ||= {}).merge!(node) + (@allowed_nodes ||= {}).merge!(node) end end end diff --git a/lib/gitlab/ci/config/node/global.rb b/lib/gitlab/ci/config/node/global.rb index 911dc51da48..044603423d5 100644 --- a/lib/gitlab/ci/config/node/global.rb +++ b/lib/gitlab/ci/config/node/global.rb @@ -9,7 +9,7 @@ module Gitlab class Global < Entry include Configurable - add_node :before_script, Script, + allow_node :before_script, Script, description: 'Script that will be executed before each job.' end end -- cgit v1.2.1 From 12080ba150328963987674d282f435fc0e88b9d6 Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Fri, 10 Jun 2016 11:20:46 +0200 Subject: Simplify new ci config entry class interface --- lib/gitlab/ci/config/node/configurable.rb | 6 +++--- lib/gitlab/ci/config/node/entry.rb | 15 +++------------ lib/gitlab/ci/config/node/null.rb | 12 ++++++------ lib/gitlab/ci/config/node/script.rb | 4 ++-- 4 files changed, 14 insertions(+), 23 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/ci/config/node/configurable.rb b/lib/gitlab/ci/config/node/configurable.rb index c8c917f229f..120457690d8 100644 --- a/lib/gitlab/ci/config/node/configurable.rb +++ b/lib/gitlab/ci/config/node/configurable.rb @@ -19,7 +19,7 @@ module Gitlab def initialize(*) super - unless leaf? || has_config? + unless @value.is_a?(Hash) @errors << 'should be a configuration entry with hash value' end end @@ -39,9 +39,9 @@ module Gitlab def create_entry(key, entry_class) if @value.has_key?(key) - entry_class.new(@value[key], @root, self) + entry_class.new(@value[key]) else - Node::Null.new(nil, @root, self) + Node::Null.new(nil) end end diff --git a/lib/gitlab/ci/config/node/entry.rb b/lib/gitlab/ci/config/node/entry.rb index 19fc997297a..ed1cdd6f15d 100644 --- a/lib/gitlab/ci/config/node/entry.rb +++ b/lib/gitlab/ci/config/node/entry.rb @@ -10,16 +10,15 @@ module Gitlab attr_accessor :description - def initialize(value, root = nil, parent = nil) + def initialize(value) @value = value - @root = root - @parent = parent @nodes = {} @errors = [] end def process! - return if leaf? || invalid? + return if leaf? + return unless valid? compose! @@ -41,18 +40,10 @@ module Gitlab errors.none? end - def invalid? - !valid? - end - def leaf? allowed_nodes.none? end - def has_config? - @value.is_a?(Hash) - end - def errors @errors + nodes.map(&:errors).flatten end diff --git a/lib/gitlab/ci/config/node/null.rb b/lib/gitlab/ci/config/node/null.rb index ab7b0abaf23..4f590f6bec8 100644 --- a/lib/gitlab/ci/config/node/null.rb +++ b/lib/gitlab/ci/config/node/null.rb @@ -1,13 +1,13 @@ module Gitlab module Ci class Config - ## - # This class represents a configuration entry that is not being used - # in configuration file. - # - # This implements Null Object pattern. - # module Node + ## + # This class represents a configuration entry that is not being used + # in configuration file. + # + # This implements Null Object pattern. + # class Null < Entry def value nil diff --git a/lib/gitlab/ci/config/node/script.rb b/lib/gitlab/ci/config/node/script.rb index 84f9ec0eb04..5072bf0db7d 100644 --- a/lib/gitlab/ci/config/node/script.rb +++ b/lib/gitlab/ci/config/node/script.rb @@ -6,8 +6,8 @@ module Gitlab # Entry that represents a script. # # Each element in the value array is a command that will be executed - # by GitLab Runner. Currently we concatenate this commands with - # new line character as a separator what is compatbile with + # by GitLab Runner. Currently we concatenate these commands with + # new line character as a separator, what is compatible with # implementation in Runner. # class Script < Entry -- cgit v1.2.1 From 5abfc7fa7157e876299d1675f1cc96b78a3feadc Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Fri, 10 Jun 2016 11:32:49 +0200 Subject: Define ci entry accessor instead of method_missing --- lib/gitlab/ci/config/node/configurable.rb | 6 ++++++ lib/gitlab/ci/config/node/entry.rb | 7 ------- 2 files changed, 6 insertions(+), 7 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/ci/config/node/configurable.rb b/lib/gitlab/ci/config/node/configurable.rb index 120457690d8..b72bc0d592a 100644 --- a/lib/gitlab/ci/config/node/configurable.rb +++ b/lib/gitlab/ci/config/node/configurable.rb @@ -55,6 +55,12 @@ module Gitlab { class: entry_class, description: metadata[:description] } } + define_method(symbol) do + raise Entry::InvalidError unless valid? + + @nodes[symbol].try(:value) + end + (@allowed_nodes ||= {}).merge!(node) end end diff --git a/lib/gitlab/ci/config/node/entry.rb b/lib/gitlab/ci/config/node/entry.rb index ed1cdd6f15d..f7649784c28 100644 --- a/lib/gitlab/ci/config/node/entry.rb +++ b/lib/gitlab/ci/config/node/entry.rb @@ -52,13 +52,6 @@ module Gitlab {} end - def method_missing(name, *args) - super unless allowed_nodes.has_key?(name) - raise InvalidError unless valid? - - @nodes[name].try(:value) - end - def add_node(key, entry) raise NotImplementedError end -- cgit v1.2.1 From cc373a35504bc1f92f1a040c87a712a6480757ec Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Fri, 10 Jun 2016 14:01:07 +0200 Subject: Add factory for fabricating new ci config nodes --- lib/gitlab/ci/config/node/configurable.rb | 24 +++++------------ lib/gitlab/ci/config/node/entry.rb | 10 ++++--- lib/gitlab/ci/config/node/factory.rb | 44 +++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 21 deletions(-) create mode 100644 lib/gitlab/ci/config/node/factory.rb (limited to 'lib') diff --git a/lib/gitlab/ci/config/node/configurable.rb b/lib/gitlab/ci/config/node/configurable.rb index b72bc0d592a..650c6efba63 100644 --- a/lib/gitlab/ci/config/node/configurable.rb +++ b/lib/gitlab/ci/config/node/configurable.rb @@ -30,19 +30,10 @@ module Gitlab private - def add_node(key, metadata) - entry = create_entry(key, metadata[:class]) - entry.description = metadata[:description] - - @nodes[key] = entry - end - - def create_entry(key, entry_class) - if @value.has_key?(key) - entry_class.new(@value[key]) - else - Node::Null.new(nil) - end + def create_node(key, factory) + factory.with_value(@value[key]) + factory.null_node unless @value.has_key?(key) + factory.create! end class_methods do @@ -51,9 +42,8 @@ module Gitlab private def allow_node(symbol, entry_class, metadata) - node = { symbol.to_sym => - { class: entry_class, - description: metadata[:description] } } + factory = Node::Factory.new(entry_class) + .with_description(metadata[:description]) define_method(symbol) do raise Entry::InvalidError unless valid? @@ -61,7 +51,7 @@ module Gitlab @nodes[symbol].try(:value) end - (@allowed_nodes ||= {}).merge!(node) + (@allowed_nodes ||= {}).merge!(symbol => factory) end end end diff --git a/lib/gitlab/ci/config/node/entry.rb b/lib/gitlab/ci/config/node/entry.rb index f7649784c28..2f327fa9bf3 100644 --- a/lib/gitlab/ci/config/node/entry.rb +++ b/lib/gitlab/ci/config/node/entry.rb @@ -27,8 +27,8 @@ module Gitlab end def compose! - allowed_nodes.each do |key, entry| - add_node(key, entry) + allowed_nodes.each do |key, factory| + @nodes[key] = create_node(key, factory.dup) end end @@ -52,7 +52,7 @@ module Gitlab {} end - def add_node(key, entry) + def validate! raise NotImplementedError end @@ -60,7 +60,9 @@ module Gitlab raise NotImplementedError end - def validate! + private + + def create_node(key, factory) raise NotImplementedError end end diff --git a/lib/gitlab/ci/config/node/factory.rb b/lib/gitlab/ci/config/node/factory.rb new file mode 100644 index 00000000000..969af45272e --- /dev/null +++ b/lib/gitlab/ci/config/node/factory.rb @@ -0,0 +1,44 @@ +module Gitlab + module Ci + class Config + module Node + ## + # Factory class responsible for fabricating node entry objects. + # + # It uses Fluent Interface pattern to set all necessary attributes. + # + class Factory + class InvalidFactory < StandardError; end + + def initialize(entry_class) + @entry_class = entry_class + @attributes = {} + end + + def with_value(value) + @attributes[:value] = value + self + end + + def with_description(description) + @attributes[:description] = description + self + end + + def null_node + @entry_class = Node::Null + self + end + + def create! + raise InvalidFactory unless @attributes.has_key?(:value) + + @entry_class.new(@attributes[:value]).tap do |entry| + entry.description = @attributes[:description] + end + end + end + end + end + end +end -- cgit v1.2.1 From 907c0e6796b69f9577c147dd489cf55748c749ac Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Fri, 10 Jun 2016 23:36:54 +0200 Subject: Added initial version of deployments --- lib/api/builds.rb | 2 +- lib/ci/gitlab_ci_yaml_processor.rb | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/api/builds.rb b/lib/api/builds.rb index 0ff8fa74a84..6bf59afab53 100644 --- a/lib/api/builds.rb +++ b/lib/api/builds.rb @@ -142,7 +142,7 @@ module API return not_found!(build) unless build return forbidden!('Build is not retryable') unless build.retryable? - build = Ci::Build.retry(build) + build = Ci::Build.retry(build, current_user) present build, with: Entities::Build, user_can_download_artifacts: can?(current_user, :read_build, user_project) diff --git a/lib/ci/gitlab_ci_yaml_processor.rb b/lib/ci/gitlab_ci_yaml_processor.rb index 130f5b0892e..5aacb59dc5c 100644 --- a/lib/ci/gitlab_ci_yaml_processor.rb +++ b/lib/ci/gitlab_ci_yaml_processor.rb @@ -7,7 +7,8 @@ module Ci ALLOWED_YAML_KEYS = [:before_script, :after_script, :image, :services, :types, :stages, :variables, :cache] ALLOWED_JOB_KEYS = [:tags, :script, :only, :except, :type, :image, :services, :allow_failure, :type, :stage, :when, :artifacts, :cache, - :dependencies, :before_script, :after_script, :variables] + :dependencies, :before_script, :after_script, :variables, + :environment] attr_reader :before_script, :after_script, :image, :services, :path, :cache @@ -85,6 +86,7 @@ module Ci except: job[:except], allow_failure: job[:allow_failure] || false, when: job[:when] || 'on_success', + environment: job[:environment], options: { image: job[:image] || @image, services: job[:services] || @services, @@ -203,6 +205,10 @@ module Ci if job[:when] && !job[:when].in?(%w(on_success on_failure always)) raise ValidationError, "#{name} job: when parameter should be on_success, on_failure or always" end + + if job[:environment] && !validate_string(job[:environment]) + raise ValidationError, "#{name} job: environment should be a string" + end end def validate_job_script!(name, job) -- cgit v1.2.1 From 7c8f3b0cfc38838755a21641e402b3ef7a1f9d0b Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Mon, 13 Jun 2016 08:50:12 +0200 Subject: Duplicate CI config node factory on class level --- lib/gitlab/ci/config/node/configurable.rb | 4 +++- lib/gitlab/ci/config/node/entry.rb | 6 +++--- 2 files changed, 6 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/ci/config/node/configurable.rb b/lib/gitlab/ci/config/node/configurable.rb index 650c6efba63..f2383e07aa7 100644 --- a/lib/gitlab/ci/config/node/configurable.rb +++ b/lib/gitlab/ci/config/node/configurable.rb @@ -37,7 +37,9 @@ module Gitlab end class_methods do - attr_reader :allowed_nodes + def allowed_nodes + Hash[@allowed_nodes.map { |key, factory| [key, factory.dup] } ] + end private diff --git a/lib/gitlab/ci/config/node/entry.rb b/lib/gitlab/ci/config/node/entry.rb index 2f327fa9bf3..e5692e72947 100644 --- a/lib/gitlab/ci/config/node/entry.rb +++ b/lib/gitlab/ci/config/node/entry.rb @@ -27,8 +27,8 @@ module Gitlab end def compose! - allowed_nodes.each do |key, factory| - @nodes[key] = create_node(key, factory.dup) + allowed_nodes.each do |key, essence| + @nodes[key] = create_node(key, essence) end end @@ -62,7 +62,7 @@ module Gitlab private - def create_node(key, factory) + def create_node(key, essence) raise NotImplementedError end end -- cgit v1.2.1 From 11c0d022835cafc1d52e18580d0e1523a83bbdd2 Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Mon, 13 Jun 2016 09:14:23 +0200 Subject: Simplify ci config node factory --- lib/gitlab/ci/config/node/configurable.rb | 6 +++--- lib/gitlab/ci/config/node/factory.rb | 11 +++-------- 2 files changed, 6 insertions(+), 11 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/ci/config/node/configurable.rb b/lib/gitlab/ci/config/node/configurable.rb index f2383e07aa7..86cc33e11be 100644 --- a/lib/gitlab/ci/config/node/configurable.rb +++ b/lib/gitlab/ci/config/node/configurable.rb @@ -31,8 +31,8 @@ module Gitlab private def create_node(key, factory) - factory.with_value(@value[key]) - factory.null_node unless @value.has_key?(key) + factory.with(value: @value[key]) + factory.nullify! unless @value.has_key?(key) factory.create! end @@ -45,7 +45,7 @@ module Gitlab def allow_node(symbol, entry_class, metadata) factory = Node::Factory.new(entry_class) - .with_description(metadata[:description]) + .with(description: metadata[:description]) define_method(symbol) do raise Entry::InvalidError unless valid? diff --git a/lib/gitlab/ci/config/node/factory.rb b/lib/gitlab/ci/config/node/factory.rb index 969af45272e..787ca006f5a 100644 --- a/lib/gitlab/ci/config/node/factory.rb +++ b/lib/gitlab/ci/config/node/factory.rb @@ -15,17 +15,12 @@ module Gitlab @attributes = {} end - def with_value(value) - @attributes[:value] = value + def with(attributes) + @attributes.merge!(attributes) self end - def with_description(description) - @attributes[:description] = description - self - end - - def null_node + def nullify! @entry_class = Node::Null self end -- cgit v1.2.1 From 7a1b2e4f94e3e651d3264aa566a9056fe0f554e9 Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Wed, 18 May 2016 15:28:46 -0500 Subject: Added when to artifacts --- lib/ci/gitlab_ci_yaml_processor.rb | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'lib') diff --git a/lib/ci/gitlab_ci_yaml_processor.rb b/lib/ci/gitlab_ci_yaml_processor.rb index 130f5b0892e..15d57a46eb0 100644 --- a/lib/ci/gitlab_ci_yaml_processor.rb +++ b/lib/ci/gitlab_ci_yaml_processor.rb @@ -8,6 +8,8 @@ module Ci ALLOWED_JOB_KEYS = [:tags, :script, :only, :except, :type, :image, :services, :allow_failure, :type, :stage, :when, :artifacts, :cache, :dependencies, :before_script, :after_script, :variables] + ALLOWED_CACHE_KEYS = [:key, :untracked, :paths] + ALLOWED_ARTIFACTS_KEYS = [:name, :untracked, :paths, :when] attr_reader :before_script, :after_script, :image, :services, :path, :cache @@ -135,6 +137,12 @@ module Ci end def validate_global_cache! + @cache.keys.each do |key| + unless ALLOWED_CACHE_KEYS.include? key + raise ValidationError, "#{name} cache unknown parameter #{key}" + end + end + if @cache[:key] && !validate_string(@cache[:key]) raise ValidationError, "cache:key parameter should be a string" end @@ -233,6 +241,12 @@ module Ci end def validate_job_cache!(name, job) + job[:cache].keys.each do |key| + unless ALLOWED_CACHE_KEYS.include? key + raise ValidationError, "#{name} job: cache unknown parameter #{key}" + end + end + if job[:cache][:key] && !validate_string(job[:cache][:key]) raise ValidationError, "#{name} job: cache:key parameter should be a string" end @@ -247,6 +261,12 @@ module Ci end def validate_job_artifacts!(name, job) + job[:artifacts].keys.each do |key| + unless ALLOWED_ARTIFACTS_KEYS.include? key + raise ValidationError, "#{name} job: artifacts unknown parameter #{key}" + end + end + if job[:artifacts][:name] && !validate_string(job[:artifacts][:name]) raise ValidationError, "#{name} job: artifacts:name parameter should be a string" end @@ -258,6 +278,10 @@ module Ci 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 + + if job[:artifacts][:when] && !job[:artifacts][:when].in?(%w(on_success on_failure always)) + raise ValidationError, "#{name} job: artifacts:when parameter should be on_success, on_failure or always" + end end def validate_job_dependencies!(name, job) -- cgit v1.2.1 From aea4041ce96f18afea70da15af3cbe1be4fa1f94 Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Wed, 18 May 2016 15:21:51 -0500 Subject: Allow to expire build artifacts --- lib/ci/api/builds.rb | 2 ++ lib/ci/gitlab_ci_yaml_processor.rb | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/ci/api/builds.rb b/lib/ci/api/builds.rb index 607359769d1..54f5626c7d7 100644 --- a/lib/ci/api/builds.rb +++ b/lib/ci/api/builds.rb @@ -114,6 +114,7 @@ module Ci # id (required) - The ID of a build # token (required) - The build authorization token # file (required) - Artifacts file + # expire_in (optional) - Specify when artifacts should expire (ex. 7d) # Parameters (accelerated by GitLab Workhorse): # file.path - path to locally stored body (generated by Workhorse) # file.name - real filename as send in Content-Disposition @@ -145,6 +146,7 @@ module Ci build.artifacts_file = artifacts build.artifacts_metadata = metadata + build.artifacts_expire_at = Time.now + ChronicDuration.parse(params['expire_in']) if build.save present(build, with: Entities::BuildDetails) diff --git a/lib/ci/gitlab_ci_yaml_processor.rb b/lib/ci/gitlab_ci_yaml_processor.rb index 15d57a46eb0..b1297565ebe 100644 --- a/lib/ci/gitlab_ci_yaml_processor.rb +++ b/lib/ci/gitlab_ci_yaml_processor.rb @@ -9,7 +9,7 @@ module Ci :allow_failure, :type, :stage, :when, :artifacts, :cache, :dependencies, :before_script, :after_script, :variables] ALLOWED_CACHE_KEYS = [:key, :untracked, :paths] - ALLOWED_ARTIFACTS_KEYS = [:name, :untracked, :paths, :when] + ALLOWED_ARTIFACTS_KEYS = [:name, :untracked, :paths, :when, :expire_in] attr_reader :before_script, :after_script, :image, :services, :path, :cache -- cgit v1.2.1 From 1501940ee0452f01acc5a228df17928e2f91cf39 Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Fri, 10 Jun 2016 14:26:12 +0200 Subject: Validate artifacts:expire_in in yaml processor --- lib/ci/gitlab_ci_yaml_processor.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'lib') diff --git a/lib/ci/gitlab_ci_yaml_processor.rb b/lib/ci/gitlab_ci_yaml_processor.rb index b1297565ebe..88fa079f30d 100644 --- a/lib/ci/gitlab_ci_yaml_processor.rb +++ b/lib/ci/gitlab_ci_yaml_processor.rb @@ -282,6 +282,10 @@ module Ci if job[:artifacts][:when] && !job[:artifacts][:when].in?(%w(on_success on_failure always)) raise ValidationError, "#{name} job: artifacts:when parameter should be on_success, on_failure or always" end + + if job[:artifacts][:expire_in] && !validate_duration(job[:artifacts][:expire_in]) + raise ValidationError, "#{name} job: artifacts:expire_in parameter should be a duration" + end end def validate_job_dependencies!(name, job) @@ -300,6 +304,12 @@ module Ci end end + def validate_duration(value) + value.is_a?(String) && ChronicDuration.parse(value) + rescue ChronicDuration::DurationParseError + false + end + def validate_array_of_strings(values) values.is_a?(Array) && values.all? { |value| validate_string(value) } end -- cgit v1.2.1 From 86800bf51aec25eef970eac82838bcba087703f8 Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Fri, 10 Jun 2016 14:26:31 +0200 Subject: Support expiration date in CI API when uploading artifacts --- lib/ci/api/builds.rb | 2 +- lib/ci/api/entities.rb | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/ci/api/builds.rb b/lib/ci/api/builds.rb index 54f5626c7d7..9f270f7b387 100644 --- a/lib/ci/api/builds.rb +++ b/lib/ci/api/builds.rb @@ -146,7 +146,7 @@ module Ci build.artifacts_file = artifacts build.artifacts_metadata = metadata - build.artifacts_expire_at = Time.now + ChronicDuration.parse(params['expire_in']) + build.artifacts_expire_in = params['expire_in'] if build.save present(build, with: Entities::BuildDetails) diff --git a/lib/ci/api/entities.rb b/lib/ci/api/entities.rb index a902ced35d7..352d92e7cc0 100644 --- a/lib/ci/api/entities.rb +++ b/lib/ci/api/entities.rb @@ -29,6 +29,7 @@ module Ci expose :before_sha expose :allow_git_fetch expose :token + expose :artifacts_expire_at, if: lambda { |build, opts| build.artifacts? } expose :options do |model| model.options -- cgit v1.2.1 From 6013768fec33e3bf084019d97dbfb7cca78f8e82 Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Fri, 10 Jun 2016 17:11:27 +0200 Subject: Added keep artifacts API endpoint --- lib/api/builds.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'lib') diff --git a/lib/api/builds.rb b/lib/api/builds.rb index 0ff8fa74a84..704654e9e8c 100644 --- a/lib/api/builds.rb +++ b/lib/api/builds.rb @@ -166,6 +166,25 @@ module API present build, with: Entities::Build, user_can_download_artifacts: can?(current_user, :download_build_artifacts, user_project) end + + # Keep the artifacts to prevent them to be deleted + # + # Parameters: + # id (required) - The ID of a build + # Example Request: + # POST /projects/:id/builds/:build_id/artifacts/keep + post ':id/builds/:build_id/artifacts/keep' do + authorize_update_builds! + + build = get_build(params[:build_id]) + return not_found!(build) unless build && build.artifacts? + + build.keep_artifacts! + + status 200 + present build, with: Entities::Build, + user_can_download_artifacts: can?(current_user, :read_build, user_project) + end end helpers do -- cgit v1.2.1 From d23b91b0d9b8db16801872c49a1fb1d3be3a7144 Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Fri, 10 Jun 2016 21:25:48 +0200 Subject: Improve after review --- lib/ci/gitlab_ci_yaml_processor.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/ci/gitlab_ci_yaml_processor.rb b/lib/ci/gitlab_ci_yaml_processor.rb index 88fa079f30d..76d84433cbe 100644 --- a/lib/ci/gitlab_ci_yaml_processor.rb +++ b/lib/ci/gitlab_ci_yaml_processor.rb @@ -208,7 +208,7 @@ module Ci raise ValidationError, "#{name} job: allow_failure parameter should be an boolean" end - if job[:when] && !job[:when].in?(%w(on_success on_failure always)) + if job[:when] && !job[:when].in?(%w[on_success on_failure always]) raise ValidationError, "#{name} job: when parameter should be on_success, on_failure or always" end end @@ -279,7 +279,7 @@ module Ci raise ValidationError, "#{name} job: artifacts:paths parameter should be an array of strings" end - if job[:artifacts][:when] && !job[:artifacts][:when].in?(%w(on_success on_failure always)) + if job[:artifacts][:when] && !job[:artifacts][:when].in?(%w[on_success on_failure always]) raise ValidationError, "#{name} job: artifacts:when parameter should be on_success, on_failure or always" end -- cgit v1.2.1 From 421be01dabb13cd1f45d0118b4e1be9d33baef61 Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Fri, 10 Jun 2016 21:45:06 +0200 Subject: Improve design based on review --- lib/api/builds.rb | 5 +++-- lib/ci/api/entities.rb | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/api/builds.rb b/lib/api/builds.rb index 704654e9e8c..644e5a2a99d 100644 --- a/lib/api/builds.rb +++ b/lib/api/builds.rb @@ -167,10 +167,11 @@ module API user_can_download_artifacts: can?(current_user, :download_build_artifacts, user_project) end - # Keep the artifacts to prevent them to be deleted + # Keep the artifacts to prevent them from being deleted # # Parameters: - # id (required) - The ID of a build + # id (required) - the id of a project + # build_id (required) - The ID of a build # Example Request: # POST /projects/:id/builds/:build_id/artifacts/keep post ':id/builds/:build_id/artifacts/keep' do diff --git a/lib/ci/api/entities.rb b/lib/ci/api/entities.rb index 352d92e7cc0..3f5bdaba3f5 100644 --- a/lib/ci/api/entities.rb +++ b/lib/ci/api/entities.rb @@ -20,7 +20,7 @@ module Ci expose :name, :token, :stage expose :project_id expose :project_name - expose :artifacts_file, using: ArtifactFile, if: lambda { |build, opts| build.artifacts? } + expose :artifacts_file, using: ArtifactFile, if: ->(build, _) { build.artifacts? } end class BuildDetails < Build @@ -29,7 +29,7 @@ module Ci expose :before_sha expose :allow_git_fetch expose :token - expose :artifacts_expire_at, if: lambda { |build, opts| build.artifacts? } + expose :artifacts_expire_at, if: ->(build, _) { build.artifacts? } expose :options do |model| model.options -- cgit v1.2.1 From 59eeec3ff87ce175e34ac96e86c9690c5290502b Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Mon, 13 Jun 2016 14:03:11 +0200 Subject: Make method that composes ci config entry private --- lib/gitlab/ci/config/node/configurable.rb | 2 +- lib/gitlab/ci/config/node/entry.rb | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/ci/config/node/configurable.rb b/lib/gitlab/ci/config/node/configurable.rb index 86cc33e11be..7587c8c34c9 100644 --- a/lib/gitlab/ci/config/node/configurable.rb +++ b/lib/gitlab/ci/config/node/configurable.rb @@ -38,7 +38,7 @@ module Gitlab class_methods do def allowed_nodes - Hash[@allowed_nodes.map { |key, factory| [key, factory.dup] } ] + Hash[@allowed_nodes.map { |key, factory| [key, factory.dup] }] end private diff --git a/lib/gitlab/ci/config/node/entry.rb b/lib/gitlab/ci/config/node/entry.rb index e5692e72947..507312e0c09 100644 --- a/lib/gitlab/ci/config/node/entry.rb +++ b/lib/gitlab/ci/config/node/entry.rb @@ -26,12 +26,6 @@ module Gitlab nodes.each(&:validate!) end - def compose! - allowed_nodes.each do |key, essence| - @nodes[key] = create_node(key, essence) - end - end - def nodes @nodes.values end @@ -62,6 +56,12 @@ module Gitlab private + def compose! + allowed_nodes.each do |key, essence| + @nodes[key] = create_node(key, essence) + end + end + def create_node(key, essence) raise NotImplementedError end -- cgit v1.2.1 From 30e946ce8a9272b3de1a64498965933804b7bb6d Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Tue, 14 Jun 2016 11:28:20 +0200 Subject: Validate ci config entry value before processing nodes --- lib/gitlab/ci/config/node/configurable.rb | 14 ++++++-------- lib/gitlab/ci/config/node/entry.rb | 5 +++++ 2 files changed, 11 insertions(+), 8 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/ci/config/node/configurable.rb b/lib/gitlab/ci/config/node/configurable.rb index 7587c8c34c9..d60f87f3f94 100644 --- a/lib/gitlab/ci/config/node/configurable.rb +++ b/lib/gitlab/ci/config/node/configurable.rb @@ -16,20 +16,18 @@ module Gitlab module Configurable extend ActiveSupport::Concern - def initialize(*) - super - - unless @value.is_a?(Hash) - @errors << 'should be a configuration entry with hash value' - end - end - def allowed_nodes self.class.allowed_nodes || {} end private + def prevalidate! + unless @value.is_a?(Hash) + @errors << 'should be a configuration entry with hash value' + end + end + def create_node(key, factory) factory.with(value: @value[key]) factory.nullify! unless @value.has_key?(key) diff --git a/lib/gitlab/ci/config/node/entry.rb b/lib/gitlab/ci/config/node/entry.rb index 507312e0c09..52758a962f3 100644 --- a/lib/gitlab/ci/config/node/entry.rb +++ b/lib/gitlab/ci/config/node/entry.rb @@ -14,6 +14,8 @@ module Gitlab @value = value @nodes = {} @errors = [] + + prevalidate! end def process! @@ -56,6 +58,9 @@ module Gitlab private + def prevalidate! + end + def compose! allowed_nodes.each do |key, essence| @nodes[key] = create_node(key, essence) -- cgit v1.2.1 From 60e0137c864e26fee0120dc4447bb95acc46ce51 Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Tue, 14 Jun 2016 11:38:34 +0200 Subject: Fix specs --- lib/api/builds.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/api/builds.rb b/lib/api/builds.rb index 644e5a2a99d..645e2dda0b7 100644 --- a/lib/api/builds.rb +++ b/lib/api/builds.rb @@ -184,7 +184,7 @@ module API status 200 present build, with: Entities::Build, - user_can_download_artifacts: can?(current_user, :read_build, user_project) + user_can_download_artifacts: can?(current_user, :read_build, user_project) end end -- cgit v1.2.1 From 120fbbd4875f340b5c863b7e0e3eabcb2796e15d Mon Sep 17 00:00:00 2001 From: Paco Guzman Date: Mon, 13 Jun 2016 18:41:37 +0200 Subject: Measure CPU time for instrumented methods --- lib/gitlab/metrics/instrumentation.rb | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/metrics/instrumentation.rb b/lib/gitlab/metrics/instrumentation.rb index 0f115893a15..ad9ce3fa442 100644 --- a/lib/gitlab/metrics/instrumentation.rb +++ b/lib/gitlab/metrics/instrumentation.rb @@ -149,13 +149,16 @@ module Gitlab trans = Gitlab::Metrics::Instrumentation.transaction if trans - start = Time.now - retval = super - duration = (Time.now - start) * 1000.0 + start = Time.now + cpu_start = Gitlab::Metrics::System.cpu_time + retval = super + duration = (Time.now - start) * 1000.0 if duration >= Gitlab::Metrics.method_call_threshold + cpu_duration = Gitlab::Metrics::System.cpu_time - cpu_start + trans.add_metric(Gitlab::Metrics::Instrumentation::SERIES, - { duration: duration }, + { duration: duration, cpu_duration: cpu_duration }, method: #{label.inspect}) end -- cgit v1.2.1 From e8f09f02bf8b0053f276a8e5ce0bdd18c621a1a3 Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Tue, 14 Jun 2016 13:04:21 +0200 Subject: Validate environment name with regex --- lib/ci/gitlab_ci_yaml_processor.rb | 8 ++++++-- lib/gitlab/regex.rb | 8 ++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/ci/gitlab_ci_yaml_processor.rb b/lib/ci/gitlab_ci_yaml_processor.rb index 66f1bcea4ff..b19ce4aaff9 100644 --- a/lib/ci/gitlab_ci_yaml_processor.rb +++ b/lib/ci/gitlab_ci_yaml_processor.rb @@ -214,8 +214,8 @@ module Ci raise ValidationError, "#{name} job: when parameter should be on_success, on_failure or always" end - if job[:environment] && !validate_string(job[:environment]) - raise ValidationError, "#{name} job: environment should be a string" + if job[:environment] && !validate_environment(job[:environment]) + raise ValidationError, "#{name} job: environment parameter #{Gitlab::Regex.environment_name_regex_message}" end end @@ -322,6 +322,10 @@ module Ci value.in?([true, false]) end + def validate_environment(value) + value.is_a?(String) && value =~ Gitlab::Regex.environment_name_regex + end + def process?(only_params, except_params, ref, tag, trigger_request) if only_params.present? return false unless matching?(only_params, ref, tag, trigger_request) diff --git a/lib/gitlab/regex.rb b/lib/gitlab/regex.rb index 1cbd6d945a0..c84c68f96f6 100644 --- a/lib/gitlab/regex.rb +++ b/lib/gitlab/regex.rb @@ -100,5 +100,13 @@ module Gitlab def container_registry_reference_regex git_reference_regex end + + def environment_name_regex + @environment_name_regex ||= /\A[a-zA-Z0-9_-]+\z/.freeze + end + + def environment_name_regex_message + "can contain only letters, digits, '-' and '_'." + end end end -- cgit v1.2.1 From 509082bafb01e39f4dac6f45b4ea98129ed5109c Mon Sep 17 00:00:00 2001 From: Paco Guzman Date: Mon, 13 Jun 2016 16:23:17 +0200 Subject: Instrument Grape Endpoint with Metrics::RackMiddleware MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Generating the following tags Grape#GET /projects/:id/archive from Grape::Route objects like { :path => /:version/projects/:id/archive(.:format) :version => “v3”, :method => “GET” } Use an instance variable to cache raw_path transformations. This variable is only going to growth to the number of endpoints of the API, not with exact different requests We can store this cache as an instance variable because middleware are initialised only once --- lib/gitlab/metrics/rack_middleware.rb | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/gitlab/metrics/rack_middleware.rb b/lib/gitlab/metrics/rack_middleware.rb index 6f179789d3e..3fe27779d03 100644 --- a/lib/gitlab/metrics/rack_middleware.rb +++ b/lib/gitlab/metrics/rack_middleware.rb @@ -1,8 +1,9 @@ module Gitlab module Metrics - # Rack middleware for tracking Rails requests. + # Rack middleware for tracking Rails and Grape requests. class RackMiddleware CONTROLLER_KEY = 'action_controller.instance' + ENDPOINT_KEY = 'api.endpoint' def initialize(app) @app = app @@ -21,6 +22,8 @@ module Gitlab ensure if env[CONTROLLER_KEY] tag_controller(trans, env) + elsif env[ENDPOINT_KEY] + tag_endpoint(trans, env) end trans.finish @@ -42,6 +45,26 @@ module Gitlab controller = env[CONTROLLER_KEY] trans.action = "#{controller.class.name}##{controller.action_name}" end + + def tag_endpoint(trans, env) + endpoint = env[ENDPOINT_KEY] + path = endpoint_paths_cache[endpoint.route.route_method][endpoint.route.route_path] + trans.action = "Grape##{endpoint.route.route_method} #{path}" + end + + private + + def endpoint_paths_cache + @endpoint_paths_cache ||= Hash.new do |hash, http_method| + hash[http_method] = Hash.new do |inner_hash, raw_path| + inner_hash[raw_path] = endpoint_instrumentable_path(raw_path) + end + end + end + + def endpoint_instrumentable_path(raw_path) + raw_path.sub('(.:format)', '').sub('/:version', '') + end end end end -- cgit v1.2.1 From d26f81239a33b80694783ee35f0da0e2ed082c9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Coutable?= Date: Mon, 18 Apr 2016 18:53:32 +0200 Subject: Add request access for groups MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rémy Coutable --- lib/api/project_members.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/api/project_members.rb b/lib/api/project_members.rb index 4aefdf319c6..b703da0557a 100644 --- a/lib/api/project_members.rb +++ b/lib/api/project_members.rb @@ -46,7 +46,7 @@ module API required_attributes! [:user_id, :access_level] # either the user is already a team member or a new one - project_member = user_project.project_member_by_id(params[:user_id]) + project_member = user_project.project_member(params[:user_id]) if project_member.nil? project_member = user_project.project_members.new( user_id: params[:user_id], -- cgit v1.2.1 From 515205d3c1c6655302ed0ae44cc5954dead7ae79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Coutable?= Date: Thu, 2 Jun 2016 18:05:06 +0200 Subject: UI and copywriting improvements MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit + Move 'Edit Project/Group' out of membership-related partial + Show the access request buttons only to logged-in users + Put the request access buttons out of in a more visible button + Improve the copy in the #remove_member_message helper Signed-off-by: Rémy Coutable --- lib/api/entities.rb | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/api/entities.rb b/lib/api/entities.rb index 14370ac218d..cc29c7ef428 100644 --- a/lib/api/entities.rb +++ b/lib/api/entities.rb @@ -88,10 +88,7 @@ module API class Group < Grape::Entity expose :id, :name, :path, :description, :visibility_level expose :avatar_url - - expose :web_url do |group, options| - Gitlab::Routing.url_helpers.group_url(group) - end + expose :web_url end class GroupDetail < Group -- cgit v1.2.1 From dadc531353bdf0e384d05d173d19756b0d9fba13 Mon Sep 17 00:00:00 2001 From: Paco Guzman Date: Mon, 13 Jun 2016 18:49:21 +0200 Subject: Instrument private/protected methods MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit By default instrumentation will instrument public, protected and private methods, because usually heavy work is done on private method or at least that’s what facts is showing --- lib/gitlab/metrics/instrumentation.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/metrics/instrumentation.rb b/lib/gitlab/metrics/instrumentation.rb index ad9ce3fa442..d81d26754fe 100644 --- a/lib/gitlab/metrics/instrumentation.rb +++ b/lib/gitlab/metrics/instrumentation.rb @@ -56,7 +56,7 @@ module Gitlab end end - # Instruments all public methods of a module. + # Instruments all public and private methods of a module. # # This method optionally takes a block that can be used to determine if a # method should be instrumented or not. The block is passed the receiving @@ -65,7 +65,8 @@ module Gitlab # # mod - The module to instrument. def self.instrument_methods(mod) - mod.public_methods(false).each do |name| + methods = mod.methods(false) + mod.private_methods(false) + methods.each do |name| method = mod.method(name) if method.owner == mod.singleton_class @@ -76,13 +77,14 @@ module Gitlab end end - # Instruments all public instance methods of a module. + # Instruments all public and private instance methods of a module. # # See `instrument_methods` for more information. # # mod - The module to instrument. def self.instrument_instance_methods(mod) - mod.public_instance_methods(false).each do |name| + methods = mod.instance_methods(false) + mod.private_instance_methods(false) + methods.each do |name| method = mod.instance_method(name) if method.owner == mod -- cgit v1.2.1 From ab91f1226f9dc99725e10323c0ea319f335204b3 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Tue, 14 Jun 2016 14:35:25 +0200 Subject: Filter out classes without names in the sampler We can't do a lot with classes without names as we can't filter by them, have no idea where they come from, etc. As such it's best to just ignore these. --- lib/gitlab/metrics/sampler.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/gitlab/metrics/sampler.rb b/lib/gitlab/metrics/sampler.rb index fc709222a9b..0000450d9bb 100644 --- a/lib/gitlab/metrics/sampler.rb +++ b/lib/gitlab/metrics/sampler.rb @@ -66,7 +66,11 @@ module Gitlab def sample_objects sample = Allocations.to_hash counts = sample.each_with_object({}) do |(klass, count), hash| - hash[klass.name] = count + name = klass.name + + next unless name + + hash[name] = count end # Symbols aren't allocated so we'll need to add those manually. -- cgit v1.2.1 From b2df11856144e91c84f51e8934e10e21f4f3fa70 Mon Sep 17 00:00:00 2001 From: "Z.J. van de Weg" Date: Wed, 15 Jun 2016 12:10:41 +0200 Subject: Random selection now also works for MySQL --- lib/gitlab/database.rb | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'lib') diff --git a/lib/gitlab/database.rb b/lib/gitlab/database.rb index 04fa6a3a5de..d76ecb54017 100644 --- a/lib/gitlab/database.rb +++ b/lib/gitlab/database.rb @@ -30,6 +30,10 @@ module Gitlab order end + def self.random + Gitlab::Database.postgresql? ? "RANDOM()" : "RAND()" + end + def true_value if Gitlab::Database.postgresql? "'t'" -- cgit v1.2.1 From f30d1fdf94a373649b2b570bbd6d77cbe817ebe0 Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Wed, 15 Jun 2016 12:53:10 +0200 Subject: Add support for Docker Registry manifest v1 --- lib/container_registry/blob.rb | 2 +- lib/container_registry/client.rb | 4 +++- lib/container_registry/tag.rb | 14 ++++++++++++-- 3 files changed, 16 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/container_registry/blob.rb b/lib/container_registry/blob.rb index 4e20dc4f875..eb5a2596177 100644 --- a/lib/container_registry/blob.rb +++ b/lib/container_registry/blob.rb @@ -18,7 +18,7 @@ module ContainerRegistry end def digest - config['digest'] + config['digest'] || config['blobSum'] end def type diff --git a/lib/container_registry/client.rb b/lib/container_registry/client.rb index 4d726692f45..e0b3f14d384 100644 --- a/lib/container_registry/client.rb +++ b/lib/container_registry/client.rb @@ -47,7 +47,9 @@ module ContainerRegistry conn.request :json conn.headers['Accept'] = MANIFEST_VERSION - conn.response :json, content_type: /\bjson$/ + conn.response :json, content_type: 'application/vnd.docker.distribution.manifest.v1+prettyjws' + conn.response :json, content_type: 'application/vnd.docker.distribution.manifest.v1+json' + conn.response :json, content_type: 'application/vnd.docker.distribution.manifest.v2+json' if options[:user] && options[:password] conn.request(:basic_auth, options[:user].to_s, options[:password].to_s) diff --git a/lib/container_registry/tag.rb b/lib/container_registry/tag.rb index 43f8d6dc8c2..7a0929d774e 100644 --- a/lib/container_registry/tag.rb +++ b/lib/container_registry/tag.rb @@ -12,6 +12,14 @@ module ContainerRegistry manifest.present? end + def v1? + manifest && manifest['schemaVersion'] == 1 + end + + def v2? + manifest && manifest['schemaVersion'] == 2 + end + def manifest return @manifest if defined?(@manifest) @@ -57,7 +65,9 @@ module ContainerRegistry return @layers if defined?(@layers) return unless manifest - @layers = manifest['layers'].map do |layer| + layers = manifest['layers'] || manifest['fsLayers'] + + @layers = layers.map do |layer| repository.blob(layer) end end @@ -65,7 +75,7 @@ module ContainerRegistry def total_size return unless layers - layers.map(&:size).sum + layers.map(&:size).sum if v2? end def delete -- cgit v1.2.1 From c32e61251e5afa9131f4c5d08f762a6e9f7de110 Mon Sep 17 00:00:00 2001 From: Jacob Vosmaer Date: Wed, 15 Jun 2016 14:59:37 +0200 Subject: Get rid of Gitlab::ShellEnv --- lib/gitlab/backend/grack_auth.rb | 7 ------- lib/gitlab/backend/shell_env.rb | 28 ---------------------------- lib/gitlab/gl_id.rb | 11 +++++++++++ lib/gitlab/workhorse.rb | 2 +- 4 files changed, 12 insertions(+), 36 deletions(-) delete mode 100644 lib/gitlab/backend/shell_env.rb create mode 100644 lib/gitlab/gl_id.rb (limited to 'lib') diff --git a/lib/gitlab/backend/grack_auth.rb b/lib/gitlab/backend/grack_auth.rb index adbf5941a96..7e3f5abba62 100644 --- a/lib/gitlab/backend/grack_auth.rb +++ b/lib/gitlab/backend/grack_auth.rb @@ -1,5 +1,3 @@ -require_relative 'shell_env' - module Grack class AuthSpawner def self.call(env) @@ -61,11 +59,6 @@ module Grack end @user = authenticate_user(login, password) - - if @user - Gitlab::ShellEnv.set_env(@user) - @env['REMOTE_USER'] = @auth.username - end end def ci_request?(login, password) diff --git a/lib/gitlab/backend/shell_env.rb b/lib/gitlab/backend/shell_env.rb deleted file mode 100644 index 9f5adee594a..00000000000 --- a/lib/gitlab/backend/shell_env.rb +++ /dev/null @@ -1,28 +0,0 @@ -module Gitlab - # This module provide 2 methods - # to set specific ENV variables for GitLab Shell - module ShellEnv - extend self - - def set_env(user) - # Set GL_ID env variable - if user - ENV['GL_ID'] = gl_id(user) - end - end - - def reset_env - # Reset GL_ID env variable - ENV['GL_ID'] = nil - end - - def gl_id(user) - if user.present? - "user-#{user.id}" - else - # This empty string is used in the render_grack_auth_ok method - "" - end - end - end -end diff --git a/lib/gitlab/gl_id.rb b/lib/gitlab/gl_id.rb new file mode 100644 index 00000000000..624fd00367e --- /dev/null +++ b/lib/gitlab/gl_id.rb @@ -0,0 +1,11 @@ +module Gitlab + module GlId + def self.gl_id(user) + if user.present? + "user-#{user.id}" + else + "" + end + end + end +end diff --git a/lib/gitlab/workhorse.rb b/lib/gitlab/workhorse.rb index 388f84dbe0e..40e8299c36b 100644 --- a/lib/gitlab/workhorse.rb +++ b/lib/gitlab/workhorse.rb @@ -8,7 +8,7 @@ module Gitlab class << self def git_http_ok(repository, user) { - 'GL_ID' => Gitlab::ShellEnv.gl_id(user), + 'GL_ID' => Gitlab::GlId.gl_id(user), 'RepoPath' => repository.path_to_repo, } end -- cgit v1.2.1 From fce675d7fc7e408b3ec01a017a719c8cd036fa0d Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Wed, 8 Jun 2016 18:13:52 +0200 Subject: Eager load project relations in IssueParser By eager loading these associations we can greatly cut down the number of SQL queries executed when processing documents with lots of references, especially in cases where there are references belonging to the same project. Since these associations are so specific to the reference parsing process and the permissions checking process that follows it I opted to include them directly in IssueParser instead of using something like a scope. Once we have a need for it we can move this code to a scope or method. --- lib/banzai/reference_parser/issue_parser.rb | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/banzai/reference_parser/issue_parser.rb b/lib/banzai/reference_parser/issue_parser.rb index 24076e3d9ec..f306079d833 100644 --- a/lib/banzai/reference_parser/issue_parser.rb +++ b/lib/banzai/reference_parser/issue_parser.rb @@ -25,7 +25,21 @@ module Banzai def issues_for_nodes(nodes) @issues_for_nodes ||= grouped_objects_for_nodes( nodes, - Issue.all.includes(:author, :assignee, :project), + Issue.all.includes( + :author, + :assignee, + { + # These associations are primarily used for checking permissions. + # Eager loading these ensures we don't end up running dozens of + # queries in this process. + project: [ + { namespace: :owner }, + { group: [:owners, :group_members] }, + :invited_groups, + :project_members + ] + } + ), self.class.data_attribute ) end -- cgit v1.2.1