Skip to content

Commit 19d1536

Browse files
authored
Implement new repository settings (#1216)
1 parent 39f89dc commit 19d1536

File tree

6 files changed

+85
-16
lines changed

6 files changed

+85
-16
lines changed

lib/travis/api/v3/models/user_settings.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ class Models::UserSettings < Models::JsonSlice
1010
attribute :auto_cancel_pull_requests, Boolean, default: lambda { |us, _| us.auto_cancel_default? }
1111
attribute :allow_config_imports, Boolean, default: false
1212
attribute :config_validation, Boolean, default: lambda { |us, _| us.config_validation? }
13+
attribute :share_encrypted_env_with_forks, Boolean, default: false
14+
attribute :share_ssh_keys_with_forks, Boolean, default: lambda { |us, _| us.share_ssh_keys_with_forks? }
1315

1416
attr_reader :repo
1517

@@ -34,6 +36,12 @@ def config_validation?
3436
new_repo? || old_repo?
3537
end
3638

39+
def share_ssh_keys_with_forks?
40+
return false unless ENV['IBM_REPO_SWITCHES_DATE']
41+
42+
repo.created_at <= Date.parse(ENV['IBM_REPO_SWITCHES_DATE'])
43+
end
44+
3745
def new_repo?
3846
repo.created_at >= NOV_15
3947
end

lib/travis/model/repository.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ def regenerate_key!
183183
def settings
184184
@settings ||= begin
185185
instance = Repository::Settings.load(super, repository_id: id)
186+
instance.handle_ssh_share(id)
186187
instance.on_save do
187188
self.settings = instance.to_json
188189
self.save!

lib/travis/model/repository/settings.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ def custom_timeouts?(settings)
9999
attribute :auto_cancel_pushes, Boolean, default: lambda { |s, _| s.auto_cancel_default? }
100100
attribute :auto_cancel_pull_requests, Boolean, default: lambda { |s, _| s.auto_cancel_default? }
101101
attribute :allow_config_imports, Boolean, default: false
102+
attribute :share_encrypted_env_with_forks, Boolean, default: false
103+
attribute :share_ssh_keys_with_forks, Boolean, default: nil
102104

103105
validates :maximum_number_of_builds, numericality: true
104106

@@ -147,6 +149,17 @@ def repository_id
147149
def repository
148150
Repository.find(repository_id)
149151
end
152+
153+
def handle_ssh_share(id)
154+
if self.share_ssh_keys_with_forks.nil?
155+
self.share_ssh_keys_with_forks = false
156+
return unless ENV['IBM_REPO_SWITCHES_DATE']
157+
158+
repo = Repository.find(id)
159+
self.share_ssh_keys_with_forks = repo.created_at <= Date.parse(ENV['IBM_REPO_SWITCHES_DATE']) if repo
160+
161+
end
162+
end
150163
end
151164

152165
class Repository::DefaultSettings < Repository::Settings

spec/lib/model/repository_spec.rb

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -358,21 +358,6 @@
358358
expect(repo.settings.build_pushes?).to be true
359359
end
360360

361-
it "allows to set nil for settings" do
362-
repo.settings = nil
363-
expect(repo.settings.to_hash).to eq(Repository::Settings.new.to_hash)
364-
end
365-
366-
it "allows to set settings as JSON string" do
367-
repo.settings = '{"maximum_number_of_builds": 44}'
368-
expect(repo.settings.to_hash).to eq(Repository::Settings.new(maximum_number_of_builds: 44).to_hash)
369-
end
370-
371-
it "allows to set settings as a Hash" do
372-
repo.settings = { maximum_number_of_builds: 44}
373-
expect(repo.settings.to_hash).to eq(Repository::Settings.new(maximum_number_of_builds: 44).to_hash)
374-
end
375-
376361
it 'updates settings in the DB' do
377362
repo.settings = {'build_pushes' => false}
378363
repo.save

spec/v3/services/user_setting/find_spec.rb

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,62 @@
9999
)
100100
end
101101
end
102+
103+
describe 'authenticated, existing repo, default share_encrypted_env_with_forks setting' do
104+
before do
105+
get("/v3/repo/#{repo.id}/setting/share_encrypted_env_with_forks", {}, auth_headers)
106+
end
107+
108+
example { expect(last_response.status).to eq(200) }
109+
example do
110+
expect(JSON.load(body)).to eq(
111+
'@type' => 'setting',
112+
'@representation' => 'standard',
113+
'@permissions' => { 'read' => true, 'write' => false },
114+
'@href' => "/v3/repo/#{repo.id}/setting/share_encrypted_env_with_forks",
115+
'name' => 'share_encrypted_env_with_forks',
116+
'value' => false
117+
)
118+
end
119+
end
120+
121+
describe 'authenticated, existing repo, default share_ssh_keys_with_forks setting' do
122+
let(:created_at) { Date.parse('2021-09-01') }
123+
124+
before do
125+
ENV['IBM_REPO_SWITCHES_DATE'] = '2021-10-01'
126+
repo.update(created_at: created_at)
127+
get("/v3/repo/#{repo.id}/setting/share_ssh_keys_with_forks", {}, auth_headers)
128+
end
129+
130+
after { ENV['IBM_REPO_SWITCHES_DATE'] = nil }
131+
132+
example { expect(last_response.status).to eq(200) }
133+
example do
134+
expect(JSON.load(body)).to eq(
135+
'@type' => 'setting',
136+
'@representation' => 'standard',
137+
'@permissions' => { 'read' => true, 'write' => false },
138+
'@href' => "/v3/repo/#{repo.id}/setting/share_ssh_keys_with_forks",
139+
'name' => 'share_ssh_keys_with_forks',
140+
'value' => true
141+
)
142+
end
143+
144+
context 'when repo is new' do
145+
let(:created_at) { Date.parse('2021-11-01') }
146+
147+
example { expect(last_response.status).to eq(200) }
148+
example do
149+
expect(JSON.load(body)).to eq(
150+
'@type' => 'setting',
151+
'@representation' => 'standard',
152+
'@permissions' => { 'read' => true, 'write' => false },
153+
'@href' => "/v3/repo/#{repo.id}/setting/share_ssh_keys_with_forks",
154+
'name' => 'share_ssh_keys_with_forks',
155+
'value' => false
156+
)
157+
end
158+
end
159+
end
102160
end

spec/v3/services/user_settings/for_repository_spec.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
{ '@type' => 'setting', '@href' => "/v3/repo/#{repo.id}/setting/auto_cancel_pushes", '@representation' => 'standard', '@permissions' => { 'read' => true, 'write' => true }, 'name' => 'auto_cancel_pushes', 'value' => false },
5050
{ '@type' => 'setting', '@href' => "/v3/repo/#{repo.id}/setting/auto_cancel_pull_requests", '@representation' => 'standard', '@permissions' => { 'read' => true, 'write' => true }, 'name' => 'auto_cancel_pull_requests', 'value' => false },
5151
{ '@type' => 'setting', '@href' => "/v3/repo/#{repo.id}/setting/config_validation", '@representation' => 'standard', '@permissions' => { 'read' => true, 'write' => true }, 'name' => 'config_validation', 'value' => false },
52+
{ '@type' => 'setting', '@href' => "/v3/repo/#{repo.id}/setting/share_encrypted_env_with_forks", '@representation' => 'standard', '@permissions' => { 'read' => true, 'write' => true }, 'name' => 'share_encrypted_env_with_forks', 'value' => false },
53+
{ '@type' => 'setting', '@href' => "/v3/repo/#{repo.id}/setting/share_ssh_keys_with_forks", '@representation' => 'standard', '@permissions' => { 'read' => true, 'write' => true }, 'name' => 'share_ssh_keys_with_forks', 'value' => false },
5254
]
5355
)
5456
end
@@ -86,6 +88,8 @@
8688
{ '@type' => 'setting', '@href' => "/v3/repo/#{repo.id}/setting/auto_cancel_pushes", '@representation' => 'standard', '@permissions' => { 'read' => true, 'write' => true }, 'name' => 'auto_cancel_pushes', 'value' => false },
8789
{ '@type' => 'setting', '@href' => "/v3/repo/#{repo.id}/setting/auto_cancel_pull_requests", '@representation' => 'standard', '@permissions' => { 'read' => true, 'write' => true }, 'name' => 'auto_cancel_pull_requests', 'value' => false },
8890
{ '@type' => 'setting', '@href' => "/v3/repo/#{repo.id}/setting/config_validation", '@representation' => 'standard', '@permissions' => { 'read' => true, 'write' => true }, 'name' => 'config_validation', 'value' => false },
91+
{ '@type' => 'setting', '@href' => "/v3/repo/#{repo.id}/setting/share_encrypted_env_with_forks", '@representation' => 'standard', '@permissions' => { 'read' => true, 'write' => true }, 'name' => 'share_encrypted_env_with_forks', 'value' => false },
92+
{ '@type' => 'setting', '@href' => "/v3/repo/#{repo.id}/setting/share_ssh_keys_with_forks", '@representation' => 'standard', '@permissions' => { 'read' => true, 'write' => true }, 'name' => 'share_ssh_keys_with_forks', 'value' => false },
8993
]
9094
)
9195
end
@@ -110,4 +114,4 @@
110114
)
111115
end
112116
end
113-
end
117+
end

0 commit comments

Comments
 (0)