Skip to content
代码片段 群组 项目
提交 470e526a 编辑于 作者: Kamil Trzciński's avatar Kamil Trzciński
浏览文件

Merge branch 'fix/gb/fix-deserializing-ci-yaml-variables' into 'master'

Fix deserializing YAML variables when a build has been imported

Closes #49406

See merge request gitlab-org/gitlab-ce!20713
No related branches found
No related tags found
无相关合并请求
---
title: Fix accessing imported pipeline builds
merge_request: 20713
author:
type: fixed
...@@ -34,7 +34,7 @@ def to_runner_variable ...@@ -34,7 +34,7 @@ def to_runner_variable
def self.fabricate(resource) def self.fabricate(resource)
case resource case resource
when Hash when Hash
self.new(resource) self.new(resource.symbolize_keys)
when ::HasVariable when ::HasVariable
self.new(resource.to_runner_variable) self.new(resource.to_runner_variable)
when self when self
......
...@@ -13,8 +13,9 @@ def load(string) ...@@ -13,8 +13,9 @@ def load(string)
object = YAML.safe_load(string, [Symbol]) object = YAML.safe_load(string, [Symbol])
object.map do |variable| object.map do |variable|
variable[:key] = variable[:key].to_s variable.symbolize_keys.tap do |variable|
variable variable[:key] = variable[:key].to_s
end
end end
end end
......
...@@ -75,6 +75,14 @@ ...@@ -75,6 +75,14 @@
expect(resource).to eq variable expect(resource).to eq variable
end end
it 'supports using a hash with stringified values' do
variable = { 'key' => 'VARIABLE', 'value' => 'my value' }
resource = described_class.fabricate(variable)
expect(resource).to eq(key: 'VARIABLE', value: 'my value')
end
it 'supports using an active record resource' do it 'supports using an active record resource' do
variable = create(:ci_variable, key: 'CI_VAR', value: '123') variable = create(:ci_variable, key: 'CI_VAR', value: '123')
resource = described_class.fabricate(variable) resource = described_class.fabricate(variable)
......
require 'spec_helper' require 'fast_spec_helper'
describe Gitlab::Serializer::Ci::Variables do describe Gitlab::Serializer::Ci::Variables do
subject do subject do
...@@ -6,11 +6,11 @@ ...@@ -6,11 +6,11 @@
end end
let(:object) do let(:object) do
[{ key: :key, value: 'value', public: true }, [{ 'key' => :key, 'value' => 'value', 'public' => true },
{ key: 'wee', value: 1, public: false }] { key: 'wee', value: 1, public: false }]
end end
it 'converts keys into strings' do it 'converts keys into strings and symbolizes hash' do
is_expected.to eq([ is_expected.to eq([
{ key: 'key', value: 'value', public: true }, { key: 'key', value: 'value', public: true },
{ key: 'wee', value: 1, public: false } { key: 'wee', value: 1, public: false }
......
...@@ -2269,6 +2269,34 @@ def create_mr(build, pipeline, factory: :merge_request, created_at: Time.now) ...@@ -2269,6 +2269,34 @@ def create_mr(build, pipeline, factory: :merge_request, created_at: Time.now)
end end
end end
describe '#yaml_variables' do
before do
build.update_attribute(:yaml_variables, variables)
end
context 'when serialized valu is a symbolized hash' do
let(:variables) do
[{ key: :VARIABLE, value: 'my value 1' }]
end
it 'keeps symbolizes keys and stringifies variables names' do
expect(build.yaml_variables)
.to eq [{ key: 'VARIABLE', value: 'my value 1' }]
end
end
context 'when serialized value is a hash with string keys' do
let(:variables) do
[{ 'key' => :VARIABLE, 'value' => 'my value 2' }]
end
it 'symblizes variables hash' do
expect(build.yaml_variables)
.to eq [{ key: 'VARIABLE', value: 'my value 2' }]
end
end
end
describe 'state transition: any => [:pending]' do describe 'state transition: any => [:pending]' do
let(:build) { create(:ci_build, :created) } let(:build) { create(:ci_build, :created) }
......
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册