22
33class Travis ::Api ::App
44 class SettingsEndpoint < Endpoint
5+ include ActiveSupport ::Callbacks
6+
7+ define_callbacks :after_save
8+
9+ set_callback :after_save , :after , :save_audit
510 set ( :prefix ) { "/settings/" << name [ /[^:]+$/ ] . underscore }
611
712 class << self
@@ -38,6 +43,7 @@ def define_routes!
3843 # Rails style methods for easy overriding
3944 def index
4045 respond_with ( collection , type : name , version : :v2 )
46+
4147 end
4248
4349 def show
@@ -48,9 +54,13 @@ def update
4854 disallow_migrating! ( repo )
4955
5056 record . update ( JSON . parse ( request . body . read ) [ singular_name ] )
51-
5257 if record . valid?
58+ @changes = { :"env_vars" => { created : "name: #{ record . name } , is_public: #{ record . public } , branch: #{ record . branch || 'all' } " } }
59+
5360 repo_settings . save
61+ run_callbacks :after_save
62+ @changes = { }
63+
5464 respond_with ( record , type : singular_name , version : :v2 )
5565 else
5666 status 422
@@ -62,9 +72,13 @@ def create
6272 disallow_migrating! ( repo )
6373
6474 record = collection . create ( JSON . parse ( request . body . read ) [ singular_name ] )
65-
6675 if record . valid?
76+ @changes = { :"env_vars" => { created : "name: #{ record . name } , is_public: #{ record . public } , branch: #{ record . branch || 'all' } " } }
77+
6778 repo_settings . save
79+ run_callbacks :after_save
80+ @changes = { }
81+
6882 respond_with ( record , type : singular_name , version : :v2 )
6983 else
7084 status 422
@@ -76,7 +90,12 @@ def destroy
7690 disallow_migrating! ( repo )
7791
7892 record = collection . destroy ( params [ :id ] ) || record_not_found
93+ @changes = { :"env_vars" => { destroyed : "name: #{ record . name } , is_public: #{ record . public } , branch: #{ record . branch || 'all' } " } }
94+
7995 repo_settings . save
96+ run_callbacks :after_save
97+ @changes = { }
98+
8099 respond_with ( record , type : singular_name , version : :v2 )
81100 end
82101
@@ -89,7 +108,7 @@ def collection
89108 end
90109
91110 def repo
92- Repository . find ( params [ :repository_id ] )
111+ @repo = Repository . find ( params [ :repository_id ] )
93112 end
94113
95114 # This method can't be called "settings" because it clashes with
@@ -107,5 +126,16 @@ def record
107126 def record_not_found
108127 halt ( 404 , { error : "Could not find a requested setting" } )
109128 end
129+
130+ def changes
131+ @changes
132+ end
133+
134+ private
135+
136+ def save_audit
137+ change_source = access_token . app_id == 2 ? 'admin-v2' : 'travis-api'
138+ Travis ::API ::V3 ::Models ::Audit . create! ( owner : current_user , change_source : change_source , source : @repo , source_changes : { settings : self . changes } )
139+ end
110140 end
111141end
0 commit comments