From 794f548212c6d2887ee519d1bc6709e7d1756909 Mon Sep 17 00:00:00 2001 From: ShahzaibAwan Date: Thu, 10 Nov 2022 06:34:40 +0500 Subject: [PATCH] [BSFY-181] Add env vars changes to changelog for admin --- .../api/app/endpoint/setting_endpoint.rb | 38 ++++++++++++++----- lib/travis/api/v3/models/env_vars.rb | 22 ++++++++--- lib/travis/api/v3/queries/env_var.rb | 7 ++-- lib/travis/api/v3/queries/env_vars.rb | 5 ++- lib/travis/api/v3/queries/user_setting.rb | 2 +- 5 files changed, 54 insertions(+), 20 deletions(-) diff --git a/lib/travis/api/app/endpoint/setting_endpoint.rb b/lib/travis/api/app/endpoint/setting_endpoint.rb index 2746e8c5ae..571b45c8d9 100644 --- a/lib/travis/api/app/endpoint/setting_endpoint.rb +++ b/lib/travis/api/app/endpoint/setting_endpoint.rb @@ -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 @@ -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! @@ -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 @@ -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? @@ -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? @@ -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? @@ -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 @@ -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 diff --git a/lib/travis/api/v3/models/env_vars.rb b/lib/travis/api/v3/models/env_vars.rb index 135aec9893..9881656fef 100644 --- a/lib/travis/api/v3/models/env_vars.rb +++ b/lib/travis/api/v3/models/env_vars.rb @@ -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 @@ -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 @@ -38,7 +41,7 @@ def destroy(id) def repository @repository ||= Models::Repository.find(additional_attributes[:repository_id]) end - + def changes @changes end @@ -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 diff --git a/lib/travis/api/v3/queries/env_var.rb b/lib/travis/api/v3/queries/env_var.rb index e31e394d45..37a035a0de 100644 --- a/lib/travis/api/v3/queries/env_var.rb +++ b/lib/travis/api/v3/queries/env_var.rb @@ -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 diff --git a/lib/travis/api/v3/queries/env_vars.rb b/lib/travis/api/v3/queries/env_vars.rb index ddeb9ef821..f683809b19 100644 --- a/lib/travis/api/v3/queries/env_vars.rb +++ b/lib/travis/api/v3/queries/env_vars.rb @@ -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 diff --git a/lib/travis/api/v3/queries/user_setting.rb b/lib/travis/api/v3/queries/user_setting.rb index 3a1fbe9d1e..b68794b5ac 100644 --- a/lib/travis/api/v3/queries/user_setting.rb +++ b/lib/travis/api/v3/queries/user_setting.rb @@ -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