Skip to content

Commit 3af4955

Browse files
AndriiMyskovitalied
authored andcommitted
Travis Insights integration
1 parent 99fd627 commit 3af4955

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+1714
-1
lines changed

lib/travis/api/v3/insights_client.rb

+258
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
1+
# frozen_string_literal: true
2+
3+
module Travis::API::V3
4+
class InsightsClient
5+
class ConfigurationError < StandardError; end
6+
7+
def initialize(user_id)
8+
@user_id = user_id
9+
end
10+
11+
def user_notifications(filter, page, active, sort_by, sort_direction)
12+
query_string = query_string_from_params(
13+
value: filter,
14+
page: page || '1',
15+
active: active,
16+
order: sort_by,
17+
order_dir: sort_direction
18+
)
19+
response = connection.get("/user_notifications?#{query_string}")
20+
21+
handle_errors_and_respond(response) do |body|
22+
notifications = body['data'].map do |notification|
23+
Travis::API::V3::Models::InsightsNotification.new(notification)
24+
end
25+
26+
Travis::API::V3::Models::InsightsCollection.new(notifications, body.fetch('total_count'))
27+
end
28+
end
29+
30+
def toggle_snooze_user_notifications(notification_ids)
31+
response = connection.put('/user_notifications/toggle_snooze', snooze_ids: notification_ids)
32+
33+
handle_errors_and_respond(response)
34+
end
35+
36+
def user_plugins(filter, page, active, sort_by, sort_direction)
37+
query_string = query_string_from_params(
38+
value: filter,
39+
page: page || '1',
40+
active: active,
41+
order: sort_by,
42+
order_dir: sort_direction
43+
)
44+
response = connection.get("/user_plugins?#{query_string}")
45+
46+
handle_errors_and_respond(response) do |body|
47+
plugins = body['data'].map do |plugin|
48+
Travis::API::V3::Models::InsightsPlugin.new(plugin)
49+
end
50+
51+
Travis::API::V3::Models::InsightsCollection.new(plugins, body.fetch('total_count'))
52+
end
53+
end
54+
55+
def create_plugin(params)
56+
response = connection.post("/user_plugins", user_plugin: params)
57+
handle_errors_and_respond(response) do |body|
58+
Travis::API::V3::Models::InsightsPlugin.new(body['plugin'])
59+
end
60+
end
61+
62+
def toggle_active_plugins(plugin_ids)
63+
response = connection.put('/user_plugins/toggle_active', toggle_ids: plugin_ids)
64+
65+
handle_errors_and_respond(response) do |body|
66+
Travis::API::V3::Models::InsightsCollection.new([], 0)
67+
end
68+
end
69+
70+
def delete_many_plugins(plugin_ids)
71+
response = connection.delete('/user_plugins/delete_many', delete_ids: plugin_ids)
72+
73+
handle_errors_and_respond(response) do |body|
74+
Travis::API::V3::Models::InsightsCollection.new([], 0)
75+
end
76+
end
77+
78+
def generate_key(plugin_name, plugin_type)
79+
response = connection.get('/user_plugins/generate_key', name: plugin_name, plugin_type: plugin_type)
80+
81+
handle_errors_and_respond(response) do |body|
82+
body
83+
end
84+
end
85+
86+
def authenticate_key(params)
87+
response = connection.post('/user_plugins/authenticate_key', params)
88+
89+
handle_errors_and_respond(response) do |body|
90+
body
91+
end
92+
end
93+
94+
def template_plugin_tests(plugin_type)
95+
response = connection.get("/user_plugins/#{plugin_type}/template_plugin_tests")
96+
97+
handle_errors_and_respond(response) do |body|
98+
body
99+
end
100+
end
101+
102+
def get_scan_logs(plugin_id, last_id)
103+
params = last_id ? { last: last_id, poll: true } : {}
104+
response = connection.get("/user_plugins/#{plugin_id}/get_scan_logs", params)
105+
106+
handle_errors_and_respond(response) do |body|
107+
body
108+
end
109+
end
110+
111+
def probes(filter, page, active, sort_by, sort_direction)
112+
query_string = query_string_from_params(
113+
value: filter,
114+
page: page || '1',
115+
active: active,
116+
order: sort_by,
117+
order_dir: sort_direction
118+
)
119+
response = connection.get("/probes?#{query_string}")
120+
121+
handle_errors_and_respond(response) do |body|
122+
probes = body['data'].map do |probe|
123+
Travis::API::V3::Models::InsightsProbe.new(probe)
124+
end
125+
126+
Travis::API::V3::Models::InsightsCollection.new(probes, body.fetch('total_count'))
127+
end
128+
end
129+
130+
def create_probe(params)
131+
response = connection.post("/probes", test_template: params)
132+
handle_errors_and_respond(response) do |body|
133+
Travis::API::V3::Models::InsightsProbe.new(body)
134+
end
135+
end
136+
137+
def update_probe(params)
138+
response = connection.patch("/probes/#{params['probe_id']}", params)
139+
handle_errors_and_respond(response) do |body|
140+
Travis::API::V3::Models::InsightsProbe.new(body)
141+
end
142+
end
143+
144+
def get_probe(params)
145+
response = connection.get("/probes/#{params['probe_id']}/template_test", params)
146+
handle_errors_and_respond(response) do |body|
147+
Travis::API::V3::Models::InsightsProbe.new(body)
148+
end
149+
end
150+
151+
def toggle_active_probes(probe_ids)
152+
response = connection.put('/probes/toggle_active', toggle_ids: probe_ids)
153+
154+
handle_errors_and_respond(response) do |body|
155+
Travis::API::V3::Models::InsightsCollection.new([], 0)
156+
end
157+
end
158+
159+
def delete_many_probes(probe_ids)
160+
response = connection.delete('/probes/delete_many', delete_ids: probe_ids)
161+
162+
handle_errors_and_respond(response) do |body|
163+
Travis::API::V3::Models::InsightsCollection.new([], 0)
164+
end
165+
end
166+
167+
def sandbox_plugins(plugin_type)
168+
response = connection.post('/sandbox/plugins', plugin_type: plugin_type)
169+
170+
handle_errors_and_respond(response) do |body|
171+
body
172+
end
173+
end
174+
175+
def sandbox_plugin_data(plugin_id)
176+
response = connection.post('/sandbox/plugin_data', plugin_id: plugin_id)
177+
178+
handle_errors_and_respond(response) do |body|
179+
body
180+
end
181+
end
182+
183+
def sandbox_run_query(plugin_id, query)
184+
response = connection.post('/sandbox/run_query', plugin_id: plugin_id, query: query)
185+
186+
handle_errors_and_respond(response) do |body|
187+
body
188+
end
189+
end
190+
191+
def public_key
192+
response = connection.get('/api/v1/public_keys/latest.json')
193+
194+
handle_errors_and_respond(response) do |body|
195+
Travis::API::V3::Models::InsightsPublicKey.new(body)
196+
end
197+
end
198+
199+
def search_tags
200+
response = connection.get('/tags')
201+
202+
handle_errors_and_respond(response) do |body|
203+
tags = body.map do |tag|
204+
Travis::API::V3::Models::InsightsTag.new(tag)
205+
end
206+
end
207+
end
208+
209+
private
210+
211+
def handle_errors_and_respond(response)
212+
case response.status
213+
when 200, 201
214+
yield(response.body) if block_given?
215+
when 202
216+
true
217+
when 204
218+
true
219+
when 400
220+
raise Travis::API::V3::ClientError, response.body.fetch('error', '')
221+
when 403
222+
raise Travis::API::V3::InsufficientAccess, response.body['rejection_code']
223+
when 404
224+
raise Travis::API::V3::NotFound, response.body.fetch('error', '')
225+
when 422
226+
raise Travis::API::V3::UnprocessableEntity, response.body.fetch('error', '')
227+
else
228+
raise Travis::API::V3::ServerError, 'Insights API failed'
229+
end
230+
end
231+
232+
def connection(timeout: 20)
233+
@connection ||= Faraday.new(url: insights_url, ssl: { ca_path: '/usr/lib/ssl/certs' }) do |conn|
234+
conn.headers[:Authorization] = "Token token=\"#{insights_auth_token}\""
235+
conn.headers['X-Travis-User-Id'] = @user_id.to_s
236+
conn.headers['Content-Type'] = 'application/json'
237+
conn.request :json
238+
conn.response :json
239+
conn.options[:open_timeout] = timeout
240+
conn.options[:timeout] = timeout
241+
conn.use OpenCensus::Trace::Integrations::FaradayMiddleware if Travis::Api::App::Middleware::OpenCensus.enabled?
242+
conn.adapter :net_http
243+
end
244+
end
245+
246+
def insights_url
247+
Travis.config.new_insights.insights_url || raise(ConfigurationError, 'No Insights API URL configured!')
248+
end
249+
250+
def insights_auth_token
251+
Travis.config.new_insights.insights_auth_token || raise(ConfigurationError, 'No Insights Auth Token configured!')
252+
end
253+
254+
def query_string_from_params(params)
255+
params.delete_if { |_, v| v.nil? || v.empty? }.to_query
256+
end
257+
end
258+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# frozen_string_literal: true
2+
3+
module Travis::API::V3
4+
class Models::InsightsCollection
5+
def initialize(collection, total_count)
6+
@collection = collection
7+
@total_count = total_count
8+
end
9+
10+
def count(*)
11+
@total_count
12+
end
13+
14+
def limit(*)
15+
self
16+
end
17+
18+
def offset(*)
19+
self
20+
end
21+
22+
def map
23+
return @collection.map unless block_given?
24+
25+
@collection.map { |x| yield x }
26+
end
27+
28+
def to_sql
29+
"insights_query:#{Time.now.to_i}"
30+
end
31+
end
32+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module Travis::API::V3
2+
class Models::InsightsNotification
3+
attr_reader :id, :type, :active, :weight, :message, :plugin_name, :plugin_type, :plugin_category, :probe_severity, :description, :description_link
4+
5+
def initialize(attributes = {})
6+
@id = attributes.fetch('id')
7+
@type = attributes.fetch('type')
8+
@active = attributes.fetch('active')
9+
@weight = attributes.fetch('weight')
10+
@message = attributes.fetch('message')
11+
@plugin_name = attributes.fetch('plugin_name')
12+
@plugin_type = attributes.fetch('plugin_type')
13+
@plugin_category = attributes.fetch('plugin_category')
14+
@probe_severity = attributes.fetch('probe_severity')
15+
@description = attributes.fetch('description', '')
16+
@description_link = attributes.fetch('description_link', '')
17+
end
18+
end
19+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module Travis::API::V3
2+
class Models::InsightsPlugin
3+
attr_reader :id, :name, :public_id, :plugin_type, :plugin_category, :last_scan_end, :scan_status, :plugin_status, :active
4+
5+
def initialize(attributes = {})
6+
@id = attributes.fetch('id')
7+
@name = attributes.fetch('name')
8+
@public_id = attributes.fetch('public_id')
9+
@plugin_type = attributes.fetch('plugin_type')
10+
@plugin_category = attributes.fetch('plugin_category')
11+
@last_scan_end = attributes.fetch('last_scan_end')
12+
@scan_status = attributes.fetch('scan_status')
13+
@plugin_status = attributes.fetch('plugin_status')
14+
@active = attributes.fetch('active')
15+
end
16+
end
17+
end
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
module Travis::API::V3
2+
class Models::InsightsProbe
3+
attr_reader :id, :user_id, :user_plugin_id, :test_template_id, :uuid, :uuid_group, :type,
4+
:notification, :description, :description_link, :test, :base_object_locator, :preconditions, :conditionals,
5+
:object_key_locator, :active, :editable, :template_type, :cruncher_type, :status, :labels, :plugin_type,
6+
:plugin_type_name, :plugin_category, :tag_list, :severity
7+
8+
def initialize(attributes = {})
9+
@id = attributes.fetch('id')
10+
@user_id = attributes.fetch('user_id')
11+
@user_plugin_id = attributes.fetch('user_plugin_id')
12+
@test_template_id = attributes.fetch('test_template_id')
13+
@uuid = attributes.fetch('uuid')
14+
@uuid_group = attributes.fetch('uuid_group')
15+
@type = attributes.fetch('type')
16+
@notification = attributes.fetch('notification')
17+
@description = attributes.fetch('description')
18+
@description_link = attributes.fetch('description_link')
19+
@test = attributes.fetch('test')
20+
@base_object_locator = attributes.fetch('base_object_locator')
21+
@preconditions = attributes.fetch('preconditions')
22+
@conditionals = attributes.fetch('conditionals')
23+
@object_key_locator = attributes.fetch('object_key_locator')
24+
@active = attributes.fetch('active')
25+
@editable = attributes.fetch('editable')
26+
@template_type = attributes.fetch('template_type')
27+
@cruncher_type = attributes.fetch('cruncher_type')
28+
@status = attributes.fetch('status', '')
29+
@labels = attributes.fetch('labels')
30+
@plugin_type = attributes.fetch('plugin_type')
31+
@plugin_type_name = attributes.fetch('plugin_type_name', '')
32+
@plugin_category = attributes.fetch('plugin_category')
33+
@tag_list = attributes.fetch('tag_list')
34+
@severity = attributes.fetch('severity')
35+
end
36+
end
37+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module Travis::API::V3
2+
class Models::InsightsPublicKey
3+
attr_reader :key_hash, :key_body, :ordinal_value
4+
5+
def initialize(attributes = {})
6+
@key_hash = attributes.fetch('key_hash')
7+
@key_body = attributes.fetch('key_body')
8+
@ordinal_value = attributes.fetch('ordinal_value')
9+
end
10+
end
11+
end
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module Travis::API::V3
2+
class Models::InsightsTag
3+
attr_reader :name
4+
5+
def initialize(attributes = {})
6+
@name = attributes.fetch('name')
7+
end
8+
end
9+
end

0 commit comments

Comments
 (0)