2
2
3
3
class Travis ::Api ::App
4
4
class SettingsEndpoint < Endpoint
5
+ include ActiveSupport ::Callbacks
6
+
7
+ define_callbacks :after_save
8
+
9
+ set_callback :after_save , :after , :save_audit
5
10
set ( :prefix ) { "/settings/" << name [ /[^:]+$/ ] . underscore }
6
11
7
12
class << self
@@ -38,6 +43,7 @@ def define_routes!
38
43
# Rails style methods for easy overriding
39
44
def index
40
45
respond_with ( collection , type : name , version : :v2 )
46
+
41
47
end
42
48
43
49
def show
@@ -48,9 +54,13 @@ def update
48
54
disallow_migrating! ( repo )
49
55
50
56
record . update ( JSON . parse ( request . body . read ) [ singular_name ] )
51
-
52
57
if record . valid?
58
+ @changes = { :"env_vars" => { created : "name: #{ record . name } , is_public: #{ record . public } , branch: #{ record . branch || 'all' } " } }
59
+
53
60
repo_settings . save
61
+ run_callbacks :after_save
62
+ @changes = { }
63
+
54
64
respond_with ( record , type : singular_name , version : :v2 )
55
65
else
56
66
status 422
@@ -62,9 +72,13 @@ def create
62
72
disallow_migrating! ( repo )
63
73
64
74
record = collection . create ( JSON . parse ( request . body . read ) [ singular_name ] )
65
-
66
75
if record . valid?
76
+ @changes = { :"env_vars" => { created : "name: #{ record . name } , is_public: #{ record . public } , branch: #{ record . branch || 'all' } " } }
77
+
67
78
repo_settings . save
79
+ run_callbacks :after_save
80
+ @changes = { }
81
+
68
82
respond_with ( record , type : singular_name , version : :v2 )
69
83
else
70
84
status 422
@@ -76,7 +90,12 @@ def destroy
76
90
disallow_migrating! ( repo )
77
91
78
92
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
+
79
95
repo_settings . save
96
+ run_callbacks :after_save
97
+ @changes = { }
98
+
80
99
respond_with ( record , type : singular_name , version : :v2 )
81
100
end
82
101
@@ -89,7 +108,7 @@ def collection
89
108
end
90
109
91
110
def repo
92
- Repository . find ( params [ :repository_id ] )
111
+ @repo = Repository . find ( params [ :repository_id ] )
93
112
end
94
113
95
114
# This method can't be called "settings" because it clashes with
@@ -107,5 +126,16 @@ def record
107
126
def record_not_found
108
127
halt ( 404 , { error : "Could not find a requested setting" } )
109
128
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
110
140
end
111
141
end
0 commit comments