22
33class Travis ::Api ::App
44 class SettingsEndpoint < Endpoint
5+ include ActiveSupport ::Callbacks
6+ extend ActiveSupport ::Concern
7+
8+ define_callbacks :after_save
9+ set_callback :after_save , :after , :save_audit
10+
511 set ( :prefix ) { "/settings/" << name [ /[^:]+$/ ] . underscore }
612
713 class << self
@@ -19,7 +25,7 @@ def subclass(name)
1925 end
2026
2127 def create_settings_class ( name )
22- klass = Class . new ( self ) do
28+ Class . new ( self ) do
2329 define_method ( :name ) { name }
2430 before { authenticate_by_mode! }
2531 define_routes!
@@ -50,7 +56,15 @@ def update
5056 record . update ( JSON . parse ( request . body . read ) [ singular_name ] )
5157
5258 if record . valid?
59+ @changes = {
60+ env_vars : {
61+ created : "name: #{ record . name } , is_public: #{ record . public } , branch: #{ record . branch || 'all' } "
62+ }
63+ } if is_env_var?
64+
5365 repo_settings . save
66+ run_callbacks :after_save if is_env_var?
67+
5468 respond_with ( record , type : singular_name , version : :v2 )
5569 else
5670 status 422
@@ -64,7 +78,15 @@ def create
6478 record = collection . create ( JSON . parse ( request . body . read ) [ singular_name ] )
6579
6680 if record . valid?
81+ @changes = {
82+ env_vars : {
83+ created : "name: #{ record . name } , is_public: #{ record . public } , branch: #{ record . branch || 'all' } "
84+ }
85+ } if is_env_var?
86+
6787 repo_settings . save
88+ run_callbacks :after_save if is_env_var?
89+
6890 respond_with ( record , type : singular_name , version : :v2 )
6991 else
7092 status 422
@@ -76,7 +98,15 @@ def destroy
7698 disallow_migrating! ( repo )
7799
78100 record = collection . destroy ( params [ :id ] ) || record_not_found
101+ @changes = {
102+ env_vars : {
103+ destroyed : "name: #{ record . name } , is_public: #{ record . public } , branch: #{ record . branch || 'all' } "
104+ }
105+ } if is_env_var?
106+
79107 repo_settings . save
108+ run_callbacks :after_save if is_env_var?
109+
80110 respond_with ( record , type : singular_name , version : :v2 )
81111 end
82112
@@ -89,15 +119,15 @@ def collection
89119 end
90120
91121 def repo
92- Repository . find ( params [ :repository_id ] )
122+ @repo = Repository . find ( params [ :repository_id ] )
93123 end
94124
95125 # This method can't be called "settings" because it clashes with
96126 # Sinatra's method
97127 def repo_settings
98128 @settings ||= begin
99- service ( :find_repo_settings , id : params [ 'repository_id' ] . to_i ) . run
100- end || halt ( 404 , error : "Couldn't find repository" )
129+ service ( :find_repo_settings , id : params [ 'repository_id' ] . to_i ) . run
130+ end || halt ( 404 , error : "Couldn't find repository" )
101131 end
102132
103133 def record
@@ -107,5 +137,28 @@ def record
107137 def record_not_found
108138 halt ( 404 , { error : "Could not find a requested setting" } )
109139 end
140+
141+ def changes
142+ @changes
143+ end
144+
145+ def is_env_var?
146+ singular_name == 'env_var'
147+ end
148+
149+ private
150+
151+ def save_audit
152+ change_source = access_token . app_id == 2 ? 'admin-v2' : 'travis-api'
153+ Travis ::API ::V3 ::Models ::Audit . create! (
154+ owner : current_user ,
155+ change_source : change_source ,
156+ source : @repo ,
157+ source_changes : {
158+ settings : self . changes
159+ }
160+ )
161+ @changes = { }
162+ end
110163 end
111164end
0 commit comments