Skip to content

Commit

Permalink
[BSFY-181] Add env vars changes to changelog for admin
Browse files Browse the repository at this point in the history
  • Loading branch information
ShahzaibAwan authored and vitalie committed Jun 26, 2023
1 parent af85d25 commit 794f548
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 20 deletions.
38 changes: 29 additions & 9 deletions lib/travis/api/app/endpoint/setting_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ class SettingsEndpoint < Endpoint
include ActiveSupport::Callbacks

define_callbacks :after_save

set_callback :after_save, :after, :save_audit

set(:prefix) { "/settings/" << name[/[^:]+$/].underscore }

class << self
Expand All @@ -24,7 +24,7 @@ def subclass(name)
end

def create_settings_class(name)
klass = Class.new(self) do
Class.new(self) do
define_method(:name) { name }
before { authenticate_by_mode! }
define_routes!
Expand All @@ -43,7 +43,6 @@ def define_routes!
# Rails style methods for easy overriding
def index
respond_with(collection, type: name, version: :v2)

end

def show
Expand All @@ -54,8 +53,13 @@ def update
disallow_migrating!(repo)

record.update(JSON.parse(request.body.read)[singular_name])

if record.valid?
@changes = { :"env_vars" => { created: "name: #{record.name}, is_public: #{record.public}, branch: #{record.branch || 'all'} " } } if is_env_var?
@changes = {
env_vars: {
created: "name: #{record.name}, is_public: #{record.public}, branch: #{record.branch || 'all'} "
}
} if is_env_var?

repo_settings.save
run_callbacks :after_save if is_env_var?
Expand All @@ -71,8 +75,13 @@ def create
disallow_migrating!(repo)

record = collection.create(JSON.parse(request.body.read)[singular_name])

if record.valid?
@changes = { :"env_vars" => { created: "name: #{record.name}, is_public: #{record.public}, branch: #{record.branch || 'all'}" } } if is_env_var?
@changes = {
env_vars: {
created: "name: #{record.name}, is_public: #{record.public}, branch: #{record.branch || 'all'}"
}
} if is_env_var?

repo_settings.save
run_callbacks :after_save if is_env_var?
Expand All @@ -88,7 +97,11 @@ def destroy
disallow_migrating!(repo)

record = collection.destroy(params[:id]) || record_not_found
@changes = { :"env_vars" => { destroyed: "name: #{record.name}, is_public: #{record.public}, branch: #{record.branch || 'all'} " } } if is_env_var?
@changes = {
env_vars: {
destroyed: "name: #{record.name}, is_public: #{record.public}, branch: #{record.branch || 'all'} "
}
} if is_env_var?

repo_settings.save
run_callbacks :after_save if is_env_var?
Expand All @@ -112,8 +125,8 @@ def repo
# Sinatra's method
def repo_settings
@settings ||= begin
service(:find_repo_settings, id: params['repository_id'].to_i).run
end || halt(404, error: "Couldn't find repository")
service(:find_repo_settings, id: params['repository_id'].to_i).run
end || halt(404, error: "Couldn't find repository")
end

def record
Expand All @@ -136,7 +149,14 @@ def is_env_var?

def save_audit
change_source = access_token.app_id == 2 ? 'admin-v2' : 'travis-api'
Travis::API::V3::Models::Audit.create!(owner: current_user, change_source: change_source, source: @repo, source_changes: { settings: self.changes })
Travis::API::V3::Models::Audit.create!(
owner: current_user,
change_source: change_source,
source: @repo,
source_changes: {
settings: self.changes
}
)
@changes = {}
end
end
Expand Down
22 changes: 16 additions & 6 deletions lib/travis/api/v3/models/env_vars.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@

module Travis::API::V3
class Models::EnvVars < Travis::Settings::Collection
include Models::JsonSync, ActiveSupport::Callbacks
include Models::JsonSync
include ActiveSupport::Callbacks
extend ActiveSupport::Concern

model Models::EnvVar
define_callbacks :after_save

define_callbacks :after_save
set_callback :after_save, :after, :save_audit

attr_accessor :user, :change_source

# See Models::JsonSync
def to_h
{ 'env_vars' => map(&:to_h).map(&:stringify_keys) }
end

def create(attributes)
@changes = { :"env_vars" => { created: "#{attributes.except("value")}" } }
@changes = { env_vars: { created: "#{attributes.except("value")}" } }
env_var = super(attributes).tap { sync! }
run_callbacks :after_save
env_var
Expand All @@ -29,7 +32,7 @@ def add(env_var)

def destroy(id)
env_var = find(id)
@changes = { :"env_vars" => { deleted: "#{env_var.attributes.delete("value")}" } }
@changes = { env_vars: { deleted: "#{env_var.attributes.delete("value")}" } }
deleted_env_var = super(id).tap { sync! }
run_callbacks :after_save
deleted_env_var
Expand All @@ -38,7 +41,7 @@ def destroy(id)
def repository
@repository ||= Models::Repository.find(additional_attributes[:repository_id])
end

def changes
@changes
end
Expand All @@ -47,7 +50,14 @@ def changes

def save_audit
if self.change_source
Travis::API::V3::Models::Audit.create!(owner: self.user, change_source: self.change_source, source: self.repository, source_changes: { settings: self.changes })
Travis::API::V3::Models::Audit.create!(
owner: self.user,
change_source: self.change_source,
source: self.repository,
source_changes: {
settings: self.changes
}
)
@changes = {}
end
end
Expand Down
7 changes: 4 additions & 3 deletions lib/travis/api/v3/queries/env_var.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ def find(repository)
repository.env_vars.find(id)
end

def update(env_var, from_admin)
def update(env_var, from_admin = false)
env_vars = env_var.repository.env_vars
env_vars.user = env_var.repository.user_settings.user
env_vars.change_source = 'travis-api' unless from_admin
env_var.update(env_var_params)
env_vars.add(env_var)
env_vars.add(env_var)

env_var
end

def delete(repository, from_admin)
def delete(repository, from_admin = false)
env_vars = repository.env_vars
env_vars.user = repository.user_settings.user
env_vars.change_source = 'travis-api' unless from_admin
Expand Down
5 changes: 4 additions & 1 deletion lib/travis/api/v3/queries/env_vars.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@ def find(repository)
repository.env_vars
end

def create(repository, from_admin)
def create(repository, from_admin = false)
env_vars = repository.env_vars
env_vars.user = repository.user_settings.user
env_vars.change_source = 'travis-api' unless from_admin
env_var = env_vars.create(env_var_params)

unless env_var.valid?
repository.env_vars.destroy(env_var.id)
handle_errors(env_var)
end

repository.save!

env_var
end

Expand Down
2 changes: 1 addition & 1 deletion lib/travis/api/v3/queries/user_setting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def find(repository)
repository.user_settings.read(_name)
end

def update(repository, user, from_admin)
def update(repository, user, from_admin = false)
user_settings = repository.user_settings
user_settings.user = user
user_settings.change_source = 'travis-api' unless from_admin
Expand Down

0 comments on commit 794f548

Please sign in to comment.