-
Notifications
You must be signed in to change notification settings - Fork 165
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
5f97c06
86a392e
9c944a2
fe38656
3bd4c7c
88545d9
4592dc9
ad70dd6
5120115
78d4d1e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
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 |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
representation :minimal, *representations[:standard] | ||
|
||
end | ||
end |
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can use ||=instead