Skip to content

Commit 1c105b9

Browse files
Merge pull request #1363 from travis-ci/Release_250319
Release 2025 03 19
2 parents 72140d5 + db95714 commit 1c105b9

File tree

24 files changed

+272
-11
lines changed

24 files changed

+272
-11
lines changed

lib/travis/api/serialize/v2/http.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@
2121
require 'travis/api/serialize/v2/http/user'
2222
require 'travis/api/serialize/v2/http/validation_error'
2323
require 'travis/api/serialize/v2/http/ssh_key'
24+
require 'travis/api/serialize/v2/http/account_env_var'
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module Travis
2+
module Api
3+
module Serialize
4+
module V2
5+
module Http
6+
class AccountEnvVar < Travis::Api::Serialize::ObjectSerializer
7+
attributes :id, :owner_id, :owner_type, :name, :value, :public, :created_at, :updated_at
8+
9+
def serializable_hash(adapter_options)
10+
hash = super(adapter_options)
11+
hash.delete :value unless object.public?
12+
hash
13+
end
14+
end
15+
end
16+
end
17+
end
18+
end
19+
end
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
module Travis::API::V3
2+
class Models::AccountEnvVar < Model
3+
belongs_to :owner, polymorphic: true
4+
5+
serialize :value, Travis::Model::EncryptedColumn.new
6+
define_callbacks :after_change
7+
set_callback :after_change, :after, :save_audit
8+
9+
def save_account_env_var!(account_env_var)
10+
account_env_var.save if account_env_var.valid?
11+
@changes = {
12+
created:
13+
"name: #{account_env_var.name}, public: #{account_env_var.public}"
14+
}
15+
run_callbacks :after_change
16+
account_env_var
17+
end
18+
19+
def delete(account_env_var)
20+
@changes = {
21+
deleted:
22+
"name: #{account_env_var.name}, public: #{account_env_var.public}"
23+
}
24+
account_env_var.destroy
25+
run_callbacks :after_change
26+
end
27+
28+
def changes
29+
@changes
30+
end
31+
32+
private
33+
34+
def save_audit
35+
Travis::API::V3::Models::Audit.create!(
36+
owner: self.owner,
37+
change_source: 'travis-api',
38+
source: self,
39+
source_changes: {
40+
account_env_var: self.changes
41+
}
42+
)
43+
@changes = {}
44+
end
45+
end
46+
end

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class Models::Organization < Model
55
has_many :memberships
66
has_many :users, through: :memberships
77
has_one :beta_migration_request
8+
has_many :account_env_vars, as: :owner
89

910
has_preferences Models::OrganizationPreferences
1011

@@ -43,8 +44,11 @@ def ensure_preferences
4344
end
4445

4546
def custom_keys
46-
return @custom_keys if defined? @custom_keys
47-
@custom_keys = Models::CustomKey.where(owner_type: 'Organization', owner_id: id)
47+
@custom_keys ||= Models::CustomKey.where(owner_type: 'Organization', owner_id: id)
48+
end
49+
50+
def account_env_vars
51+
@account_env_vars ||= Models::AccountEnvVar.where(owner_type: 'Organization', owner_id: id)
4852
end
4953

5054
alias members users

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class Models::User < Model
1212
has_many :user_beta_features
1313
has_many :beta_features, through: :user_beta_features
1414
has_many :beta_migration_requests
15+
has_many :account_env_vars, as: :owner
1516

1617
has_preferences Models::UserPreferences
1718

@@ -113,8 +114,12 @@ def ensure_preferences
113114
end
114115

115116
def custom_keys
116-
return @custom_keys if defined? @custom_keys
117-
@custom_keys = Models::CustomKey.where(owner_type: 'User', owner_id: id)
117+
@custom_keys ||= Models::CustomKey.where(owner_type: 'User', owner_id: id)
118118
end
119+
120+
def account_env_vars
121+
@account_env_vars ||= Models::AccountEnvVar.where(owner_type: 'User', owner_id: id)
122+
end
123+
119124
end
120125
end
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
require 'travis/api/v3/permissions/generic'
2+
3+
module Travis::API::V3
4+
class Permissions::AccountEnvVar < Permissions::Generic
5+
def write?
6+
object.owner_type == 'Organization' ?
7+
authorizer.for_account(object.owner_id, 'account_settings_create') :
8+
true
9+
end
10+
11+
def delete?
12+
object.owner_type == 'Organization' ?
13+
authorizer.for_account(object.owner_id, 'account_settings_delete') :
14+
true
15+
end
16+
17+
private
18+
19+
def organization_permissions
20+
@organization_permissions ||= Permissions::Organization.new(access_control, {id: object.owner_id})
21+
end
22+
end
23+
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module Travis::API::V3
2+
class Queries::AccountEnvVar < Query
3+
4+
def find(params)
5+
Models::AccountEnvVar.where(owner_type: params['owner_type'], owner_id: params['owner_id'])
6+
end
7+
8+
def create(account_env_var)
9+
raise UnprocessableEntity, "'#{params['name']}' environment variable already exists." unless Models::AccountEnvVar.where(name: params['name'], owner_id: params['owner_id'], owner_type: params['owner_type']).count.zero?
10+
account_env_var.save_account_env_var!(account_env_var)
11+
end
12+
13+
def delete(account_env_var)
14+
account_env_var.delete(account_env_var)
15+
end
16+
end
17+
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module Travis::API::V3
2+
class Renderer::AccountEnvVar < ModelRenderer
3+
representation :standard, :id, :owner_id, :owner_type, :name, :value, :public, :created_at, :updated_at
4+
representation :minimal, :id, :owner_id, :owner_type, :name, :value, :public
5+
end
6+
end

lib/travis/api/v3/renderer/owner.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class Renderer::Owner < ModelRenderer
66

77
representation(:minimal, :id, :login, :name, :vcs_type, :ro_mode)
88
representation(:standard, :id, :login, :name, :github_id, :vcs_id, :vcs_type, :avatar_url, :education,
9-
:allow_migration, :allowance, :ro_mode, :custom_keys, :trial_allowed)
9+
:allow_migration, :allowance, :ro_mode, :custom_keys, :trial_allowed, :account_env_vars)
1010
representation(:additional, :repositories, :installation, :trial_allowed)
1111

1212
def initialize(model, **options)

lib/travis/api/v3/routes.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,16 @@ module Routes
353353
delete :delete
354354
end
355355

356+
hidden_resource :account_env_vars do
357+
route '/account_env_vars'
358+
post :create
359+
end
360+
361+
hidden_resource :account_env_var do
362+
route '/account_env_var/{id}'
363+
delete :delete
364+
end
365+
356366
hidden_resource :storage do
357367
route '/storage/{id}'
358368
get :find

0 commit comments

Comments
 (0)