Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TBT-137 Be able to use environment variables across different repos #1350

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions lib/travis/api/v3/models/account_env_var.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Travis::API::V3
class Models::AccountEnvVar < Model
belongs_to :owner, polymorphic: true

def save_account_env_var!(owner_type, owner_id, name, value, public)
self.owner_type = owner_type
self.owner_id = owner_id
self.name = name
self.value = value
self.public = public

if self.valid?
self.save!
end

self
end
end
end
6 changes: 6 additions & 0 deletions lib/travis/api/v3/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,11 @@ def custom_keys
return @custom_keys if defined? @custom_keys
@custom_keys = Models::CustomKey.where(owner_type: 'User', owner_id: id)
end

def account_env_vars
return @account_env_vars if defined? @account_env_vars
@account_env_vars = Models::AccountEnvVar.where(owner_type: 'User', owner_id: id)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can use ||=instead

end

end
end
63 changes: 63 additions & 0 deletions lib/travis/api/v3/queries/account_env_var.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
module Travis::API::V3
class Queries::AccountEnvVar < Query
def create(params, current_user)
# raise UnprocessableEntity, 'Key with this identifier already exists.' unless Travis::API::V3::Models::CustomKey.where(name: params['name'], owner_id: params['owner_id'], owner_type: params['owner_type']).count.zero?

if params['owner_type'] == 'User'
org_ids = User.find(params['owner_id']).organizations.map(&:id)

raise UnprocessableEntity, 'Key with this identifier already exists in one of your organizations.' unless Travis::API::V3::Models::CustomKey.where(name: params['name'], owner_id: org_ids, owner_type: 'Organization').count.zero?
elsif params['owner_type'] == 'Organization'
user_ids = Membership.where(organization_id: params['owner_id']).map(&:user_id)

raise UnprocessableEntity, 'Key with this identifier already exists for your user.' unless Travis::API::V3::Models::CustomKey.where(name: params['name'], owner_id: user_ids, owner_type: 'User').count.zero?
end

key = Travis::API::V3::Models::AccountEnvVar.new.save_account_env_var!(
params['owner_type'],
params['owner_id'],
params['name'],
params['value'],
params['public']
)
handle_errors(key) unless key.valid?

Travis::API::V3::Models::Audit.create!(
owner: current_user,
change_source: 'travis-api',
source: key,
source_changes: {
action: 'create',
fingerprint: key.id
}
)

key
end

def delete(params, current_user)
key = Travis::API::V3::Models::AccountEnvVar.find(params['id'])
Travis::API::V3::Models::Audit.create!(
owner: current_user,
change_source: 'travis-api',
source: key,
source_changes: {
action: 'delete',
name: key.name,
owner_type: key.owner_type,
owner_id: key.owner_id
}
)

key.destroy
end

private

def handle_errors(key)
private_key = key.errors[:private_key]
raise UnprocessableEntity, 'This key is not a private key.' if private_key.include?('invalid_pem')
raise WrongParams if private_key.include?('missing_attr')
end
end
end
7 changes: 7 additions & 0 deletions lib/travis/api/v3/renderer/account_env_var.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Travis::API::V3
class Renderer::AccountEnvVar < ModelRenderer
representation :standard, :id, :owner_id, :owner_type, :name, :value, :public, :created_at, :updated_at
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if it's not public ,the value shouldn't be read/sent, is it handled somewhere?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screenshot 2025-01-21 at 14 41 38 Yes, its hidden in the logs

representation :minimal, *representations[:standard]

end
end
2 changes: 1 addition & 1 deletion lib/travis/api/v3/renderer/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Travis::API::V3
class Renderer::User < Renderer::Owner
representation(:standard, :email, :is_syncing, :synced_at, :recently_signed_up, :secure_user_hash, :ro_mode, :confirmed_at, :custom_keys)
representation(:standard, :email, :is_syncing, :synced_at, :recently_signed_up, :secure_user_hash, :ro_mode, :confirmed_at, :custom_keys, :account_env_vars)
representation(:additional, :emails, :collaborator)

def email
Expand Down
10 changes: 10 additions & 0 deletions lib/travis/api/v3/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,16 @@ module Routes
delete :delete
end

hidden_resource :account_env_vars do
route '/account_env_var'
post :create
end

hidden_resource :account_env_var do
route '/account_env_var/{id}'
delete :delete
end

hidden_resource :storage do
route '/storage/{id}'
get :find
Expand Down
2 changes: 2 additions & 0 deletions lib/travis/api/v3/services.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ module Services
User = Module.new { extend Services }
UserSetting = Module.new { extend Services }
UserSettings = Module.new { extend Services }
AccountEnvVar = Module.new { extend Services }
AccountEnvVars = Module.new { extend Services }

def result_type
@result_type ||= name[/[^:]+$/].underscore.to_sym
Expand Down
10 changes: 10 additions & 0 deletions lib/travis/api/v3/services/account_env_var/delete.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Travis::API::V3
class Services::AccountEnvVar::Delete < Service
def run!
raise LoginRequired unless access_control.full_access_or_logged_in?

query(:account_env_var).delete(params, access_control.user)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check user roles here, might not be authorized to delete (same for create)

deleted
end
end
end
12 changes: 12 additions & 0 deletions lib/travis/api/v3/services/account_env_vars/create.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Travis::API::V3
class Services::AccountEnvVars::Create < Service
params :owner_id, :owner_type, :name, :value, :public
result_type :account_env_var

def run!
raise LoginRequired unless access_control.full_access_or_logged_in?

result query(:account_env_var).create(params, access_control.user)
end
end
end
1 change: 1 addition & 0 deletions lib/travis/model/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class User < Travis::Model
has_many :emails, dependent: :destroy
has_one :owner_group, as: :owner
has_many :custom_keys, as: :owner
has_many :account_env_vars, as: :owner
has_many :broadcasts, as: :recipient

before_create :set_as_recent
Expand Down