Commit 470e526a authored by 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
parents 3bd8a783 20076605
---
title: Fix accessing imported pipeline builds
merge_request: 20713
author:
type: fixed
...@@ -34,7 +34,7 @@ module Gitlab ...@@ -34,7 +34,7 @@ module Gitlab
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 @@ module Gitlab ...@@ -13,8 +13,9 @@ module Gitlab
object = YAML.safe_load(string, [Symbol]) object = YAML.safe_load(string, [Symbol])
object.map do |variable| object.map do |variable|
variable.symbolize_keys.tap do |variable|
variable[:key] = variable[:key].to_s variable[:key] = variable[:key].to_s
variable end
end end
end end
......
...@@ -75,6 +75,14 @@ describe Gitlab::Ci::Variables::Collection::Item do ...@@ -75,6 +75,14 @@ describe Gitlab::Ci::Variables::Collection::Item do
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 @@ describe Gitlab::Serializer::Ci::Variables do ...@@ -6,11 +6,11 @@ describe Gitlab::Serializer::Ci::Variables do
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 @@ describe Ci::Build do ...@@ -2269,6 +2269,34 @@ describe Ci::Build do
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) }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment