Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
李少辉-开发者
gitlab-foss
提交
5ed8286e
G
gitlab-foss
项目概览
李少辉-开发者
/
gitlab-foss
通知
15
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
gitlab-foss
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
5ed8286e
编写于
3月 12, 2018
作者:
G
Grzegorz Bizon
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Extract variables collection item to a separate class
上级
9d4c9272
变更
6
显示空白变更内容
内联
并排
Showing
6 changed file
with
103 addition
and
36 deletion
+103
-36
app/models/ci/variable.rb
app/models/ci/variable.rb
+4
-0
app/models/project_services/kubernetes_service.rb
app/models/project_services/kubernetes_service.rb
+1
-3
lib/gitlab/ci/variables/collection.rb
lib/gitlab/ci/variables/collection.rb
+2
-31
lib/gitlab/ci/variables/collection/item.rb
lib/gitlab/ci/variables/collection/item.rb
+46
-0
spec/lib/gitlab/ci/variables/collection/item_spec.rb
spec/lib/gitlab/ci/variables/collection/item_spec.rb
+47
-0
spec/lib/gitlab/ci/variables/collection_spec.rb
spec/lib/gitlab/ci/variables/collection_spec.rb
+3
-2
未找到文件。
app/models/ci/variable.rb
浏览文件 @
5ed8286e
...
@@ -12,5 +12,9 @@ module Ci
...
@@ -12,5 +12,9 @@ module Ci
}
}
scope
:unprotected
,
->
{
where
(
protected:
false
)
}
scope
:unprotected
,
->
{
where
(
protected:
false
)
}
def
to_hash
{
key:
key
,
value:
value
,
public:
false
}
end
end
end
end
end
app/models/project_services/kubernetes_service.rb
浏览文件 @
5ed8286e
...
@@ -105,7 +105,7 @@ class KubernetesService < DeploymentService
...
@@ -105,7 +105,7 @@ class KubernetesService < DeploymentService
def
predefined_variables
def
predefined_variables
config
=
YAML
.
dump
(
kubeconfig
)
config
=
YAML
.
dump
(
kubeconfig
)
variables
=
Gitlab
::
Ci
::
Variables
::
Collection
.
new
.
tap
do
|
collection
|
Gitlab
::
Ci
::
Variables
::
Collection
.
new
.
tap
do
|
collection
|
collection
.
append
(
key:
'KUBE_URL'
,
value:
api_url
,
public:
true
)
collection
.
append
(
key:
'KUBE_URL'
,
value:
api_url
,
public:
true
)
collection
.
append
(
key:
'KUBE_TOKEN'
,
value:
token
,
public:
false
)
collection
.
append
(
key:
'KUBE_TOKEN'
,
value:
token
,
public:
false
)
collection
.
append
(
key:
'KUBE_NAMESPACE'
,
value:
actual_namespace
,
public:
true
)
collection
.
append
(
key:
'KUBE_NAMESPACE'
,
value:
actual_namespace
,
public:
true
)
...
@@ -114,8 +114,6 @@ class KubernetesService < DeploymentService
...
@@ -114,8 +114,6 @@ class KubernetesService < DeploymentService
collection
.
append
(
key:
'KUBE_CA_PEM'
,
value:
ca_pem
,
public:
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
)
collection
.
append
(
key:
'KUBE_CA_PEM_FILE'
,
value:
ca_pem
,
public:
true
,
file:
true
)
end
end
variables
.
to_runner_variables
end
end
# Constructs a list of terminals from the reactive cache
# Constructs a list of terminals from the reactive cache
...
...
lib/gitlab/ci/variables/collection.rb
浏览文件 @
5ed8286e
...
@@ -4,8 +4,6 @@ module Gitlab
...
@@ -4,8 +4,6 @@ module Gitlab
class
Collection
class
Collection
include
Enumerable
include
Enumerable
Variable
=
Struct
.
new
(
:key
,
:value
,
:public
,
:file
)
def
initialize
(
variables
=
[])
def
initialize
(
variables
=
[])
@variables
=
[]
@variables
=
[]
...
@@ -13,7 +11,7 @@ module Gitlab
...
@@ -13,7 +11,7 @@ module Gitlab
end
end
def
append
(
resource
)
def
append
(
resource
)
@variables
.
append
(
fabricate
(
resource
))
@variables
.
append
(
Collection
::
Item
.
fabricate
(
resource
))
end
end
def
each
def
each
...
@@ -27,35 +25,8 @@ module Gitlab
...
@@ -27,35 +25,8 @@ module Gitlab
end
end
end
end
##
# If `file: true` has been provided we expose it, otherwise we
# don't expose `file` attribute at all (stems from what the runner
# expects).
#
def
to_runner_variables
def
to_runner_variables
self
.
map
do
|
variable
|
self
.
map
(
&
:to_hash
)
variable
.
to_h
.
reject
do
|
component
,
value
|
component
==
:file
&&
value
==
false
end
end
end
private
def
fabricate
(
resource
)
case
resource
when
Hash
Collection
::
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
end
...
...
lib/gitlab/ci/variables/collection/item.rb
0 → 100644
浏览文件 @
5ed8286e
module
Gitlab
module
Ci
module
Variables
class
Collection
class
Item
def
initialize
(
**
options
)
@variable
=
{
key:
options
.
fetch
(
:key
),
value:
options
.
fetch
(
:value
),
public:
options
.
fetch
(
:public
,
false
),
file:
options
.
fetch
(
:files
,
false
)
}
end
def
==
(
other
)
to_hash
==
self
.
class
.
fabricate
(
other
).
to_hash
end
##
# If `file: true` has been provided we expose it, otherwise we
# don't expose `file` attribute at all (stems from what the runner
# expects).
#
def
to_hash
@variable
.
reject
do
|
hash_key
,
hash_value
|
hash_key
==
:file
&&
hash_value
==
false
end
end
def
self
.
fabricate
(
resource
)
case
resource
when
Hash
self
.
new
(
resource
)
when
::
Ci
::
Variable
self
.
new
(
resource
.
to_hash
)
when
self
resource
.
dup
else
raise
ArgumentError
,
'Unknown CI/CD variable resource!'
end
end
end
end
end
end
end
spec/lib/gitlab/ci/variables/collection/item_spec.rb
0 → 100644
浏览文件 @
5ed8286e
require
'spec_helper'
describe
Gitlab
::
Ci
::
Variables
::
Collection
::
Item
do
let
(
:variable
)
do
{
key:
'VAR'
,
value:
'something'
,
public:
true
}
end
describe
'.fabricate'
do
it
'supports using a hash'
do
resource
=
described_class
.
fabricate
(
variable
)
expect
(
resource
).
to
be_a
(
described_class
)
expect
(
resource
).
to
eq
variable
end
it
'supports using an active record resource'
do
resource
=
described_class
.
fabricate
(
create
(
:ci_variable
))
expect
(
resource
).
to
be_a
(
described_class
)
expect
(
resource
).
to
eq
(
key:
'VARIABLE_1'
,
value:
'VARIABLE_VALUE'
,
public:
false
)
end
it
'supports using another collection item'
do
item
=
described_class
.
new
(
**
variable
)
resource
=
described_class
.
fabricate
(
item
)
expect
(
resource
).
to
be_a
(
described_class
)
expect
(
resource
).
to
eq
variable
expect
(
resource
.
object_id
).
not_to
eq
item
.
object_id
end
end
describe
'#=='
do
it
'compares a hash representation of a variable'
do
expect
(
described_class
.
new
(
**
variable
)
==
variable
).
to
be
true
end
end
describe
'#to_hash'
do
it
'returns a hash representation of a collection item'
do
expect
(
described_class
.
new
(
**
variable
).
to_hash
).
to
eq
variable
end
end
end
spec/lib/gitlab/ci/variables/collection_spec.rb
浏览文件 @
5ed8286e
...
@@ -3,10 +3,11 @@ require 'spec_helper'
...
@@ -3,10 +3,11 @@ require 'spec_helper'
describe
Gitlab
::
Ci
::
Variables
::
Collection
do
describe
Gitlab
::
Ci
::
Variables
::
Collection
do
describe
'.new'
do
describe
'.new'
do
it
'can be initialized with an array'
do
it
'can be initialized with an array'
do
variable
=
{
key:
'SOME_VAR'
,
value:
'Some Value'
}
variable
=
{
key:
'VAR'
,
value:
'value'
,
public:
true
}
collection
=
described_class
.
new
([
variable
])
collection
=
described_class
.
new
([
variable
])
expect
(
collection
.
first
.
to_h
).
to
include
variable
expect
(
collection
.
first
.
to_h
ash
).
to
eq
variable
end
end
it
'can be initialized without an argument'
do
it
'can be initialized without an argument'
do
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录