From 1d57db0d8d9ed0515fb1839e963550a60d86cb70 Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Wed, 14 Mar 2018 11:04:42 +0100 Subject: Improve predefined variables collection methods --- app/models/project.rb | 21 ++++++++++----------- lib/gitlab/ci/variables/collection.rb | 4 ++-- spec/lib/gitlab/ci/variables/collection_spec.rb | 10 ++++++++++ 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/app/models/project.rb b/app/models/project.rb index 87d228997be..73f3794867b 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -1574,17 +1574,16 @@ class Project < ActiveRecord::Base def predefined_variables visibility = Gitlab::VisibilityLevel.string_level(visibility_level) - Gitlab::Ci::Variables::Collection.new.tap do |variables| - variables.append(key: 'CI_PROJECT_ID', value: id.to_s) - variables.append(key: 'CI_PROJECT_NAME', value: path) - variables.append(key: 'CI_PROJECT_PATH', value: full_path) - variables.append(key: 'CI_PROJECT_PATH_SLUG', value: full_path_slug) - variables.append(key: 'CI_PROJECT_NAMESPACE', value: namespace.full_path) - variables.append(key: 'CI_PROJECT_URL', value: web_url) - variables.append(key: 'CI_PROJECT_VISIBILITY', value: visibility) - variables.concat(container_registry_variables) - variables.concat(auto_devops_variables) - end + Gitlab::Ci::Variables::Collection.new + .append(key: 'CI_PROJECT_ID', value: id.to_s) + .append(key: 'CI_PROJECT_NAME', value: path) + .append(key: 'CI_PROJECT_PATH', value: full_path) + .append(key: 'CI_PROJECT_PATH_SLUG', value: full_path_slug) + .append(key: 'CI_PROJECT_NAMESPACE', value: namespace.full_path) + .append(key: 'CI_PROJECT_URL', value: web_url) + .append(key: 'CI_PROJECT_VISIBILITY', value: visibility) + .concat(container_registry_variables) + .concat(auto_devops_variables) end def container_registry_variables diff --git a/lib/gitlab/ci/variables/collection.rb b/lib/gitlab/ci/variables/collection.rb index ae7415fcdb1..0deca55fe8f 100644 --- a/lib/gitlab/ci/variables/collection.rb +++ b/lib/gitlab/ci/variables/collection.rb @@ -11,11 +11,11 @@ module Gitlab end def append(resource) - @variables.append(Collection::Item.fabricate(resource)) + tap { @variables.append(Collection::Item.fabricate(resource)) } end def concat(resources) - resources.each { |variable| self.append(variable) } + tap { resources.each { |variable| self.append(variable) } } end def each diff --git a/spec/lib/gitlab/ci/variables/collection_spec.rb b/spec/lib/gitlab/ci/variables/collection_spec.rb index 005e2bb17b4..90b6e178242 100644 --- a/spec/lib/gitlab/ci/variables/collection_spec.rb +++ b/spec/lib/gitlab/ci/variables/collection_spec.rb @@ -35,6 +35,11 @@ describe Gitlab::Ci::Variables::Collection do expect(subject).to be_one end + + it 'returns self' do + expect(subject.append(key: 'VAR', value: 'test')) + .to eq subject + end end describe '#concat' do @@ -60,6 +65,11 @@ describe Gitlab::Ci::Variables::Collection do expect(collection).to include(key: 'VAR_2', value: '2', public: true) expect(collection).to include(key: 'VAR_3', value: '3', public: true) end + + it 'returns self' do + expect(subject.concat([key: 'VAR', value: 'test'])) + .to eq subject + end end describe '#+' do -- cgit v1.2.1