diff options
author | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2017-09-22 12:17:01 +0200 |
---|---|---|
committer | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2017-09-22 12:29:06 +0200 |
commit | 87637693acbd443c674bcbf26d87281156a70761 (patch) | |
tree | 5e9acb924d93b27c1485bb1e5fdcdc1fcda180d1 | |
parent | 26607a1690631916baf5a39d198ff7096cb5bde6 (diff) | |
download | gitlab-ce-87637693acbd443c674bcbf26d87281156a70761.tar.gz |
Introduce CI/CD variables collection class
-rw-r--r-- | app/models/project_services/kubernetes_service.rb | 20 | ||||
-rw-r--r-- | lib/gitlab/ci/variables/collection.rb | 60 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/variables/collection_spec.rb | 63 |
3 files changed, 132 insertions, 11 deletions
diff --git a/app/models/project_services/kubernetes_service.rb b/app/models/project_services/kubernetes_service.rb index 8ba07173c74..5c1a1063baa 100644 --- a/app/models/project_services/kubernetes_service.rb +++ b/app/models/project_services/kubernetes_service.rb @@ -100,19 +100,17 @@ class KubernetesService < DeploymentService def predefined_variables config = YAML.dump(kubeconfig) - variables = [ - { key: 'KUBE_URL', value: api_url, public: true }, - { key: 'KUBE_TOKEN', value: token, public: false }, - { key: 'KUBE_NAMESPACE', value: actual_namespace, public: true }, - { key: 'KUBECONFIG', value: config, public: false, file: true } - ] - - if ca_pem.present? - variables << { key: 'KUBE_CA_PEM', value: ca_pem, public: true } - variables << { key: 'KUBE_CA_PEM_FILE', value: ca_pem, public: true, file: true } + variables = Gitlab::Ci::Variables::Collection.new.tap do |collection| + collection.append(key: 'KUBE_URL', value: api_url, public: true) + collection.append(key: 'KUBE_TOKEN', value: token, public: false) + collection.append(key: 'KUBE_NAMESPACE', value: actual_namespace, public: true) + collection.append(key: 'KUBECONFIG', value: config, public: false, file: true) + + collection.append(key: 'KUBE_CA_PEM', value: ca_pem, public: true) + collection.append(key: 'KUBE_CA_PEM_FILE', value: ca_pem, public: true, file: true) end - variables + variables.to_hash end # Constructs a list of terminals from the reactive cache diff --git a/lib/gitlab/ci/variables/collection.rb b/lib/gitlab/ci/variables/collection.rb new file mode 100644 index 00000000000..0d830293c3c --- /dev/null +++ b/lib/gitlab/ci/variables/collection.rb @@ -0,0 +1,60 @@ +module Gitlab + module Ci + module Variables + class Collection + include Enumerable + + Variable = Struct.new(:key, :value, :public, :file) + + def initialize(variables = []) + @variables = [] + + variables.each { |variable| append(variable) } + end + + def append(resource) + @variables.append(fabricate(resource)) + end + + def each + @variables.each { |variable| yield variable } + end + + def +(other) + self.class.new.tap do |collection| + self.each { |variable| collection.append(variable) } + other.each { |variable| collection.append(variable) } + end + end + + def to_h + self.map do |variable| + variable.to_h.reject do |key, value| + key == :file && value == false + end + end + end + + alias_method :to_hash, :to_h + + private + + def fabricate(resource) + case resource + when Hash + Variable.new(resource.fetch(:key), + resource.fetch(:value), + resource.fetch(:public, false), + resource.fetch(:file, false)) + when ::Ci::Variable + Variable.new(resource.key, resource.value, false, false) + when Collection::Variable + resource.dup + else + raise ArgumentError, 'Unknown CI/CD variable resource!' + end + end + end + end + end +end diff --git a/spec/lib/gitlab/ci/variables/collection_spec.rb b/spec/lib/gitlab/ci/variables/collection_spec.rb new file mode 100644 index 00000000000..0ded8581a86 --- /dev/null +++ b/spec/lib/gitlab/ci/variables/collection_spec.rb @@ -0,0 +1,63 @@ +require 'spec_helper' + +describe Gitlab::Ci::Variables::Collection do + describe '.new' do + it 'can be initialized with an array' do + variable = { key: 'SOME_VAR', value: 'Some Value'} + collection = described_class.new([variable]) + + expect(collection.first.to_h).to include variable + end + + it 'can be initialized without an argument' do + expect(subject).to be_none + end + end + + describe '#append' do + it 'appends a hash' do + subject.append(key: 'VARIABLE', value: 'something') + + expect(subject).to be_one + end + + it 'appends a Ci::Variable' do + subject.append(build(:ci_variable)) + + expect(subject).to be_one + end + + it 'appends an internal resource' do + collection = described_class.new([{ key: 'TEST', value: 1 }]) + + subject.append(collection.first) + + expect(subject).to be_one + end + end + + describe '#+' do + it 'makes it possible to combine with an array' do + collection = described_class.new([{ key: 'TEST', value: 1 }]) + variables = [{ key: 'TEST', value: 'something'}] + + expect((collection + variables).count).to eq 2 + end + + it 'makes it possible to combine with another collection' do + collection = described_class.new([{ key: 'TEST', value: 1 }]) + other = described_class.new([{ key: 'TEST', value: 2 }]) + + expect((collection + other).count).to eq 2 + end + end + + describe '#to_hash' do + it 'creates a hash / value mapping' do + collection = described_class.new([{ key: 'TEST', value: 1 }]) + + expect(collection.to_hash) + .to eq [{ key: 'TEST', value: 1, public: false }] + end + end +end |