Skip to content

Commit 728a29e

Browse files
committed
Change ssh_key to be a model, not a single value
It seems like a good idea to add a description for an ssh key, so that it's easier to know which key is it. I changed ssh key to be a model with description and value.
1 parent c6e13da commit 728a29e

File tree

6 files changed

+26
-18
lines changed

6 files changed

+26
-18
lines changed

lib/travis/api/v0/worker/job/test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ def repository_data
6464
end
6565

6666
def ssh_key
67-
if ssh_key = repository.settings.ssh_key.decrypt
68-
{ 'source' => 'repo_settings', 'value' => ssh_key }
67+
if ssh_key = repository.settings.ssh_key
68+
{ 'source' => 'repo_settings', 'value' => ssh_key.value.decrypt }
6969
elsif ssh_key = job.ssh_key
7070
{ 'source' => 'config', 'value' => ssh_key }
7171
end

lib/travis/model/repository/settings.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ class EnvVar < Travis::Settings::Model
1414
validates :name, presence: true
1515
end
1616

17+
class SshKey < Travis::Settings::Model
18+
attribute :description, String
19+
attribute :value, Travis::Settings::EncryptedValue
20+
21+
validates :value, presence: true
22+
end
23+
1724
class EnvVars < Collection
1825
model EnvVar
1926
end
@@ -24,7 +31,7 @@ class EnvVars < Collection
2431
attribute :build_pushes, Boolean, default: true
2532
attribute :build_pull_requests, Boolean, default: true
2633
attribute :maximum_number_of_builds, Integer
27-
attribute :ssh_key, Travis::Settings::EncryptedValue
34+
attribute :ssh_key, SshKey
2835

2936
def maximum_number_of_builds
3037
super.to_i

lib/travis/settings/model_extensions.rb

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,7 @@ def set(instance, value)
1010
end
1111

1212
def get(instance)
13-
if type.primitive <= Travis::Settings::Model
14-
unless instance.instance_variable_get(instance_variable_name)
15-
value = Travis::Settings::Model.new
16-
if instance.frozen?
17-
return value
18-
else
19-
set(instance, value)
20-
end
21-
end
22-
elsif type.primitive <= Travis::Settings::EncryptedValue
13+
if type.primitive <= Travis::Settings::EncryptedValue
2314
unless instance.instance_variable_get(instance_variable_name)
2415
value = Travis::Settings::EncryptedValue.new(nil)
2516
if instance.frozen?
@@ -92,6 +83,10 @@ def model?(name)
9283
end
9384
end
9485

86+
def primitive(name)
87+
attribute_set[name.to_sym].type.primitive
88+
end
89+
9590
def get(key)
9691
if attribute?(key)
9792
self.send(key)
@@ -107,9 +102,13 @@ def set(key, value)
107102
private :set
108103

109104
def load(hash)
105+
return unless hash
106+
110107
hash.each do |key, value|
111108
if collection?(key) || encrypted?(key) || model?(key)
112-
get(key).load(value)
109+
thing = get(key)
110+
thing = set(key, primitive(key).new) if !thing && value
111+
thing.load(value) if thing
113112
elsif attribute?(key)
114113
set(key, value)
115114
end

lib/travis/testing/stubs.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,8 @@ def stub_test(attributes = {})
190190
finished_at: Time.now.utc,
191191
worker: 'ruby3.worker.travis-ci.org:travis-ruby-4',
192192
tags: 'tag-a,tag-b',
193-
log_content: log.content
193+
log_content: log.content,
194+
ssh_key: nil
194195
)
195196

196197
source = stub_build(:matrix => [test])

spec/travis/api/v0/worker/job/test_spec.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
let(:test) do
77
test = stub_test
88
settings = Repository::Settings.load({
9-
'ssh_key' => Travis::Model::EncryptedColumn.new(use_prefix: false).dump('an ssh key'),
9+
'ssh_key' => {
10+
'value' => Travis::Model::EncryptedColumn.new(use_prefix: false).dump('an ssh key')
11+
},
1012
'env_vars' => [{
1113
'name' => 'FOO',
1214
'value' => Travis::Model::EncryptedColumn.new(use_prefix: false).dump('bar')

spec/travis/enqueue/services/enqueue_jobs_spec.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@
4444
before :each do
4545
settings = OpenStruct.new(
4646
restricts_number_of_builds?: false,
47-
env_vars: [],
48-
ssh_key: Travis::Settings::EncryptedValue.new('an ssh key')
47+
env_vars: []
4948
)
5049
test.repository.stubs(:settings).returns(settings)
5150
scope = stub('scope')

0 commit comments

Comments
 (0)